Skip to content

Commit 678b0e5

Browse files
committed
exhaustive tests: remove erroneous comment from ecdsa_sig_sign
Mathematically, we always overflow when using the exhaustive tests (because our scalar order is 13 and our field order is on the order of 2^256), but the `overflow` variable returned when parsing a b32 as a scalar is always set to 0, to prevent infinite (or practically infinite) loops searching for non-overflowing scalars.
1 parent 03ff8c2 commit 678b0e5

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/ecdsa_impl.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,12 @@ static int secp256k1_ecdsa_sig_verify(const secp256k1_ecmult_context *ctx, const
225225
#if defined(EXHAUSTIVE_TEST_ORDER)
226226
{
227227
secp256k1_scalar computed_r;
228-
int overflow = 0;
229228
secp256k1_ge pr_ge;
230229
secp256k1_ge_set_gej(&pr_ge, &pr);
231230
secp256k1_fe_normalize(&pr_ge.x);
232231

233232
secp256k1_fe_get_b32(c, &pr_ge.x);
234-
secp256k1_scalar_set_b32(&computed_r, c, &overflow);
235-
/* we fully expect overflow */
233+
secp256k1_scalar_set_b32(&computed_r, c, NULL);
236234
return secp256k1_scalar_eq(sigr, &computed_r);
237235
}
238236
#else

src/tests_exhaustive.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned cha
7777
* function with an increased `attempt`. So if attempt > 0 this means we
7878
* need to change the nonce to avoid an infinite loop. */
7979
if (attempt > 0) {
80-
(*idata)++;
80+
*idata = (*idata + 1) % EXHAUSTIVE_TEST_ORDER;
8181
}
8282
secp256k1_scalar_set_int(&s, *idata);
8383
secp256k1_scalar_get_b32(nonce32, &s);
@@ -244,6 +244,7 @@ void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *grou
244244
for (i = 1; i < order; i++) { /* message */
245245
for (j = 1; j < order; j++) { /* key */
246246
for (k = 1; k < order; k++) { /* nonce */
247+
const int starting_k = k;
247248
secp256k1_ecdsa_signature sig;
248249
secp256k1_scalar sk, msg, r, s, expected_r;
249250
unsigned char sk32[32], msg32[32];
@@ -262,6 +263,11 @@ void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *grou
262263
CHECK(r == expected_r);
263264
CHECK((k * s) % order == (i + r * j) % order ||
264265
(k * (EXHAUSTIVE_TEST_ORDER - s)) % order == (i + r * j) % order);
266+
267+
/* Overflow means we've tried every possible nonce */
268+
if (k < starting_k) {
269+
break;
270+
}
265271
}
266272
}
267273
}

0 commit comments

Comments
 (0)