Skip to content

Commit 4378cdb

Browse files
Add tests for pre_g tables.
We check that the static table entries are all correct.
1 parent c6aca3d commit 4378cdb

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

src/tests.c

+67
Original file line numberDiff line numberDiff line change
@@ -3384,6 +3384,72 @@ void run_group_decompress(void) {
33843384

33853385
/***** ECMULT TESTS *****/
33863386

3387+
void test_pre_g_table(const secp256k1_ge_storage * pre_g, size_t n) {
3388+
secp256k1_gej g2;
3389+
secp256k1_ge p, q, gg;
3390+
secp256k1_fe dpx, dpy, dqx, dqy;
3391+
size_t i;
3392+
3393+
CHECK(0 < n);
3394+
secp256k1_ge_from_storage(&p, &pre_g[0]);
3395+
secp256k1_gej_set_ge(&g2, &p);
3396+
secp256k1_gej_double_var(&g2, &g2, NULL);
3397+
secp256k1_ge_set_gej_var(&gg, &g2);
3398+
secp256k1_fe_verify(&p.x);
3399+
secp256k1_fe_verify(&p.y);
3400+
CHECK(secp256k1_ge_is_valid_var(&p));
3401+
for (i = 1; i < n; ++i) {
3402+
secp256k1_fe_negate(&dpx, &p.x, 1); secp256k1_fe_add(&dpx, &gg.x); secp256k1_fe_normalize_weak(&dpx);
3403+
secp256k1_fe_negate(&dpy, &p.y, 1); secp256k1_fe_add(&dpy, &gg.y); secp256k1_fe_normalize_weak(&dpy);
3404+
/* Check that p is not equal to gg */
3405+
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dpx) || !secp256k1_fe_normalizes_to_zero_var(&dpy));
3406+
3407+
secp256k1_ge_from_storage(&q, &pre_g[i]);
3408+
secp256k1_fe_verify(&q.x);
3409+
secp256k1_fe_verify(&q.y);
3410+
CHECK(secp256k1_ge_is_valid_var(&q));
3411+
3412+
secp256k1_fe_negate(&dqx, &q.x, 1); secp256k1_fe_add(&dqx, &gg.x); secp256k1_fe_normalize_weak(&dqx);
3413+
dqy = q.y; secp256k1_fe_add(&dqy, &gg.y); secp256k1_fe_normalize_weak(&dqy);
3414+
/* Check that -q is not equal to gg */
3415+
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dqx) || !secp256k1_fe_normalizes_to_zero_var(&dqy));
3416+
3417+
/* Check that -q is not equal to p */
3418+
CHECK(!secp256k1_fe_equal_var(&dpx, &dqx) || !secp256k1_fe_equal_var(&dpy, &dqy));
3419+
3420+
/* Check that p, -q and gg are colinear */
3421+
secp256k1_fe_mul(&dpx, &dpx, &dqy);
3422+
secp256k1_fe_mul(&dpy, &dpy, &dqx);
3423+
CHECK(secp256k1_fe_equal_var(&dpx, &dpy));
3424+
3425+
p = q;
3426+
}
3427+
}
3428+
3429+
void run_ecmult_pre_g(void) {
3430+
secp256k1_ge_storage gs;
3431+
secp256k1_gej gj;
3432+
secp256k1_ge g;
3433+
size_t i;
3434+
3435+
/* Check that the pre_g and pre_g_128 tables are consistent. */
3436+
test_pre_g_table(secp256k1_pre_g, ECMULT_TABLE_SIZE(WINDOW_G));
3437+
test_pre_g_table(secp256k1_pre_g_128, ECMULT_TABLE_SIZE(WINDOW_G));
3438+
3439+
/* Check the first entry from the pre_g table. */
3440+
secp256k1_ge_to_storage(&gs, &secp256k1_ge_const_g);
3441+
CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g[0], sizeof(gs)) == 0);
3442+
3443+
/* Check the first entry from the pre_g_128 table. */
3444+
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
3445+
for (i = 0; i < 128; ++i) {
3446+
secp256k1_gej_double_var(&gj, &gj, NULL);
3447+
}
3448+
secp256k1_ge_set_gej(&g, &gj);
3449+
secp256k1_ge_to_storage(&gs, &g);
3450+
CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g_128[0], sizeof(gs)) == 0);
3451+
}
3452+
33873453
void run_ecmult_chain(void) {
33883454
/* random starting point A (on the curve) */
33893455
secp256k1_gej a = SECP256K1_GEJ_CONST(
@@ -6585,6 +6651,7 @@ int main(int argc, char **argv) {
65856651
run_group_decompress();
65866652

65876653
/* ecmult tests */
6654+
run_ecmult_pre_g();
65886655
run_wnaf();
65896656
run_point_times_order();
65906657
run_ecmult_near_split_bound();

0 commit comments

Comments
 (0)