Skip to content

Commit d26e26f

Browse files
committed
Avoid constructing an invalid signature with probability 1:2^256.
1 parent b450c34 commit d26e26f

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/ecdsa_impl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ static int secp256k1_ecdsa_sig_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_
198198
secp256k1_fe_get_b32(b, &r.x);
199199
int overflow = 0;
200200
secp256k1_scalar_set_b32(&sig->r, b, &overflow);
201+
if (secp256k1_scalar_is_zero(&sig->r)) {
202+
/* P.x = order is on the curve, so technically sig->r could end up zero, which would be an invalid signature. */
203+
secp256k1_gej_clear(&rp);
204+
secp256k1_ge_clear(&r);
205+
return 0;
206+
}
201207
if (recid)
202208
*recid = (overflow ? 2 : 0) | (secp256k1_fe_is_odd(&r.y) ? 1 : 0);
203209
secp256k1_scalar_t n;

0 commit comments

Comments
 (0)