Skip to content

Commit e1a2bd8

Browse files
Add tests for ecmult_pre_g tables.
1 parent 6c1dfdc commit e1a2bd8

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
@@ -3323,6 +3323,72 @@ void run_group_decompress(void) {
33233323

33243324
/***** ECMULT TESTS *****/
33253325

3326+
void test_pre_g_table(const secp256k1_ge_storage * pre_g, size_t n) {
3327+
secp256k1_gej g2;
3328+
secp256k1_ge p, q, gg;
3329+
secp256k1_fe dpx, dpy, dqx, dqy;
3330+
size_t i;
3331+
3332+
CHECK(0 < n);
3333+
secp256k1_ge_from_storage(&p, &pre_g[0]);
3334+
secp256k1_gej_set_ge(&g2, &p);
3335+
secp256k1_gej_double_var(&g2, &g2, NULL);
3336+
secp256k1_ge_set_gej_var(&gg, &g2);
3337+
secp256k1_fe_verify(&p.x);
3338+
secp256k1_fe_verify(&p.y);
3339+
CHECK(secp256k1_ge_is_valid_var(&p));
3340+
for (i = 1; i < n; ++i) {
3341+
secp256k1_fe_negate(&dpx, &p.x, 1); secp256k1_fe_add(&dpx, &gg.x); secp256k1_fe_normalize_weak(&dpx);
3342+
secp256k1_fe_negate(&dpy, &p.y, 1); secp256k1_fe_add(&dpy, &gg.y); secp256k1_fe_normalize_weak(&dpy);
3343+
/* Check that p is not equal to gg */
3344+
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dpx) || !secp256k1_fe_normalizes_to_zero_var(&dpy));
3345+
3346+
secp256k1_ge_from_storage(&q, &pre_g[i]);
3347+
secp256k1_fe_verify(&q.x);
3348+
secp256k1_fe_verify(&q.y);
3349+
CHECK(secp256k1_ge_is_valid_var(&q));
3350+
3351+
secp256k1_fe_negate(&dqx, &q.x, 1); secp256k1_fe_add(&dqx, &gg.x); secp256k1_fe_normalize_weak(&dqx);
3352+
dqy = q.y; secp256k1_fe_add(&dqy, &gg.y); secp256k1_fe_normalize_weak(&dqy);
3353+
/* Check that -q is not equal to gg */
3354+
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dqx) || !secp256k1_fe_normalizes_to_zero_var(&dqy));
3355+
3356+
/* Check that -q is not equal to p */
3357+
CHECK(!secp256k1_fe_equal_var(&dpx, &dqx) || !secp256k1_fe_equal_var(&dpy, &dqy));
3358+
3359+
/* Check that p, -q and gg are colinear */
3360+
secp256k1_fe_mul(&dpx, &dpx, &dqy);
3361+
secp256k1_fe_mul(&dpy, &dpy, &dqx);
3362+
CHECK(secp256k1_fe_equal_var(&dpx, &dpy));
3363+
3364+
p = q;
3365+
}
3366+
}
3367+
3368+
void run_ecmult_pre_g(void) {
3369+
secp256k1_ge_storage gs;
3370+
secp256k1_gej gj;
3371+
secp256k1_ge g;
3372+
size_t i;
3373+
3374+
/* Check that the pre_g and pre_g_128 tables are consistent. */
3375+
test_pre_g_table(secp256k1_pre_g, ECMULT_TABLE_SIZE(WINDOW_G));
3376+
test_pre_g_table(secp256k1_pre_g_128, ECMULT_TABLE_SIZE(WINDOW_G));
3377+
3378+
/* Check the first entry from the pre_g table. */
3379+
secp256k1_ge_to_storage(&gs, &secp256k1_ge_const_g);
3380+
CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g[0], sizeof(gs)) == 0);
3381+
3382+
/* Check the first entry from the pre_g_128 table. */
3383+
secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
3384+
for (i = 0; i < 128; ++i) {
3385+
secp256k1_gej_double_var(&gj, &gj, NULL);
3386+
}
3387+
secp256k1_ge_set_gej(&g, &gj);
3388+
secp256k1_ge_to_storage(&gs, &g);
3389+
CHECK(secp256k1_memcmp_var(&gs, &secp256k1_pre_g_128[0], sizeof(gs)) == 0);
3390+
}
3391+
33263392
void run_ecmult_chain(void) {
33273393
/* random starting point A (on the curve) */
33283394
secp256k1_gej a = SECP256K1_GEJ_CONST(
@@ -6523,6 +6589,7 @@ int main(int argc, char **argv) {
65236589
run_group_decompress();
65246590

65256591
/* ecmult tests */
6592+
run_ecmult_pre_g();
65266593
run_wnaf();
65276594
run_point_times_order();
65286595
run_ecmult_near_split_bound();

0 commit comments

Comments
 (0)