Skip to content

Commit abcfe6b

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

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/tests.c

+69
Original file line numberDiff line numberDiff line change
@@ -3416,6 +3416,74 @@ void run_group_decompress(void) {
34163416

34173417
/***** ECMULT TESTS *****/
34183418

3419+
void test_pre_g_table(const secp256k1_ge_storage * pre_g, size_t n) {
3420+
secp256k1_gej g2;
3421+
secp256k1_ge p, q, gg;
3422+
secp256k1_fe dpx, dpy, dqx, dqy;
3423+
size_t i;
3424+
3425+
CHECK(0 < n);
3426+
3427+
secp256k1_ge_from_storage(&p, &pre_g[0]);
3428+
secp256k1_fe_verify(&p.x);
3429+
secp256k1_fe_verify(&p.y);
3430+
CHECK(secp256k1_ge_is_valid_var(&p));
3431+
3432+
secp256k1_gej_set_ge(&g2, &p);
3433+
secp256k1_gej_double_var(&g2, &g2, NULL);
3434+
secp256k1_ge_set_gej_var(&gg, &g2);
3435+
for (i = 1; i < n; ++i) {
3436+
secp256k1_fe_negate(&dpx, &p.x, 1); secp256k1_fe_add(&dpx, &gg.x); secp256k1_fe_normalize_weak(&dpx);
3437+
secp256k1_fe_negate(&dpy, &p.y, 1); secp256k1_fe_add(&dpy, &gg.y); secp256k1_fe_normalize_weak(&dpy);
3438+
/* Check that p is not equal to gg */
3439+
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dpx) || !secp256k1_fe_normalizes_to_zero_var(&dpy));
3440+
3441+
secp256k1_ge_from_storage(&q, &pre_g[i]);
3442+
secp256k1_fe_verify(&q.x);
3443+
secp256k1_fe_verify(&q.y);
3444+
CHECK(secp256k1_ge_is_valid_var(&q));
3445+
3446+
secp256k1_fe_negate(&dqx, &q.x, 1); secp256k1_fe_add(&dqx, &gg.x); secp256k1_fe_normalize_weak(&dqx);
3447+
dqy = q.y; secp256k1_fe_add(&dqy, &gg.y); secp256k1_fe_normalize_weak(&dqy);
3448+
/* Check that -q is not equal to gg */
3449+
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dqx) || !secp256k1_fe_normalizes_to_zero_var(&dqy));
3450+
3451+
/* Check that -q is not equal to p */
3452+
CHECK(!secp256k1_fe_equal_var(&dpx, &dqx) || !secp256k1_fe_equal_var(&dpy, &dqy));
3453+
3454+
/* Check that p, -q and gg are colinear */
3455+
secp256k1_fe_mul(&dpx, &dpx, &dqy);
3456+
secp256k1_fe_mul(&dpy, &dpy, &dqx);
3457+
CHECK(secp256k1_fe_equal_var(&dpx, &dpy));
3458+
3459+
p = q;
3460+
}
3461+
}
3462+
3463+
void run_ecmult_pre_g(void) {
3464+
secp256k1_ge_storage gs;
3465+
secp256k1_gej gj;
3466+
secp256k1_ge g;
3467+
size_t i;
3468+
3469+
/* Check that the pre_g and pre_g_128 tables are consistent. */
3470+
test_pre_g_table(secp256k1_pre_g, ECMULT_TABLE_SIZE(WINDOW_G));
3471+
test_pre_g_table(secp256k1_pre_g_128, ECMULT_TABLE_SIZE(WINDOW_G));
3472+
3473+
/* Check the first entry from the pre_g table. */
3474+
secp256k1_ge_to_storage(&gs, &secp256k1_ge_const_g);
3475+
CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g[0], sizeof(gs)) == 0);
3476+
3477+
/* Check the first entry from the pre_g_128 table. */
3478+
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
3479+
for (i = 0; i < 128; ++i) {
3480+
secp256k1_gej_double_var(&gj, &gj, NULL);
3481+
}
3482+
secp256k1_ge_set_gej(&g, &gj);
3483+
secp256k1_ge_to_storage(&gs, &g);
3484+
CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g_128[0], sizeof(gs)) == 0);
3485+
}
3486+
34193487
void run_ecmult_chain(void) {
34203488
/* random starting point A (on the curve) */
34213489
secp256k1_gej a = SECP256K1_GEJ_CONST(
@@ -6618,6 +6686,7 @@ int main(int argc, char **argv) {
66186686
run_group_decompress();
66196687

66206688
/* ecmult tests */
6689+
run_ecmult_pre_g();
66216690
run_wnaf();
66226691
run_point_times_order();
66236692
run_ecmult_near_split_bound();

0 commit comments

Comments
 (0)