Skip to content

Commit cc18237

Browse files
committed
Add exhaustive tests for ecmult_const_xonly
1 parent def76d4 commit cc18237

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

src/tests_exhaustive.c

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ static void random_fe(secp256k1_fe *x) {
5959
}
6060
} while(1);
6161
}
62+
63+
static void random_fe_non_zero(secp256k1_fe *nz) {
64+
int tries = 10;
65+
while (--tries >= 0) {
66+
random_fe(nz);
67+
secp256k1_fe_normalize(nz);
68+
if (!secp256k1_fe_is_zero(nz)) {
69+
break;
70+
}
71+
}
72+
/* Infinitesimal probability of spurious failure here */
73+
CHECK(tries >= 0);
74+
}
6275
/** END stolen from tests.c */
6376

6477
static uint32_t num_cores = 1;
@@ -174,10 +187,38 @@ static void test_exhaustive_ecmult(const secp256k1_ge *group, const secp256k1_ge
174187
secp256k1_ecmult(&tmp, &groupj[r_log], &na, &ng);
175188
ge_equals_gej(&group[(i * r_log + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
176189

177-
if (i > 0) {
178-
secp256k1_ecmult_const(&tmp, &group[i], &ng, 256);
179-
ge_equals_gej(&group[(i * j) % EXHAUSTIVE_TEST_ORDER], &tmp);
180-
}
190+
}
191+
}
192+
}
193+
194+
for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
195+
for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
196+
int ret;
197+
secp256k1_gej tmp;
198+
secp256k1_fe xn, xd, tmpf;
199+
secp256k1_scalar na, ng;
200+
201+
if (skip_section(&iter)) continue;
202+
203+
secp256k1_scalar_set_int(&na, i);
204+
secp256k1_scalar_set_int(&ng, j);
205+
206+
/* Test secp256k1_ecmult_const. */
207+
secp256k1_ecmult_const(&tmp, &group[i], &ng, 256);
208+
ge_equals_gej(&group[(i * j) % EXHAUSTIVE_TEST_ORDER], &tmp);
209+
210+
if (j != 0) {
211+
/* Test secp256k1_ecmult_const_xonly with all curve X coordinates, and xd=NULL. */
212+
ret = secp256k1_ecmult_const_xonly(&tmpf, &group[i].x, NULL, &ng, 256, 0);
213+
CHECK(ret);
214+
CHECK(secp256k1_fe_equal_var(&tmpf, &group[(i * j) % EXHAUSTIVE_TEST_ORDER].x));
215+
216+
/* Test secp256k1_ecmult_const_xonly with all curve X coordinates, with random xd. */
217+
random_fe_non_zero(&xd);
218+
secp256k1_fe_mul(&xn, &xd, &group[i].x);
219+
ret = secp256k1_ecmult_const_xonly(&tmpf, &xn, &xd, &ng, 256, 0);
220+
CHECK(ret);
221+
CHECK(secp256k1_fe_equal_var(&tmpf, &group[(i * j) % EXHAUSTIVE_TEST_ORDER].x));
181222
}
182223
}
183224
}

0 commit comments

Comments
 (0)