Skip to content

Commit cd473e0

Browse files
committed
Avoid calling secp256k1_*_is_zero when secp256k1_*_set_b32 fails.
Most of the codebase correctly used short-cutting to avoid calling _is_zero on possibly incompletely initialized elements, but a few places were missed.
1 parent 6c36de7 commit cd473e0

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

src/ecmult_gen_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
187187
do {
188188
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
189189
retry = !secp256k1_fe_set_b32(&s, nonce32);
190-
retry |= secp256k1_fe_is_zero(&s);
190+
retry = retry || secp256k1_fe_is_zero(&s);
191191
} while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > Fp. */
192192
/* Randomize the projection to defend against multiplier sidechannels. */
193193
secp256k1_gej_rescale(&ctx->initial, &s);
@@ -196,7 +196,7 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
196196
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
197197
secp256k1_scalar_set_b32(&b, nonce32, &retry);
198198
/* A blinding value of 0 works, but would undermine the projection hardening. */
199-
retry |= secp256k1_scalar_is_zero(&b);
199+
retry = retry || secp256k1_scalar_is_zero(&b);
200200
} while (retry); /* This branch true is cryptographically unreachable. Requires sha256_hmac output > order. */
201201
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
202202
memset(nonce32, 0, 32);

src/modules/recovery/main_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context* ctx, secp256k1_ecd
147147
break;
148148
}
149149
secp256k1_scalar_set_b32(&non, nonce32, &overflow);
150-
if (!secp256k1_scalar_is_zero(&non) && !overflow) {
150+
if (!overflow && !secp256k1_scalar_is_zero(&non)) {
151151
if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, &recid)) {
152152
break;
153153
}

src/secp256k1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ int secp256k1_ec_pubkey_create(const secp256k1_context* ctx, secp256k1_pubkey *p
507507
ARG_CHECK(seckey != NULL);
508508

509509
secp256k1_scalar_set_b32(&sec, seckey, &overflow);
510-
ret = (!overflow) & (!secp256k1_scalar_is_zero(&sec));
510+
ret = !overflow && !secp256k1_scalar_is_zero(&sec);
511511
if (ret) {
512512
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pj, &sec);
513513
secp256k1_ge_set_gej(&p, &pj);

0 commit comments

Comments
 (0)