Skip to content

Commit

Permalink
Add tests for ecmult_pre_g tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
roconnor-blockstream committed Jun 26, 2021
1 parent 75029d3 commit de435e5
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3323,6 +3323,51 @@ void run_group_decompress(void) {

/***** ECMULT TESTS *****/

void run_ecmult_pre_g(void) {
secp256k1_gej g2;
secp256k1_ge p, q, gg;
secp256k1_fe dpx, dpy, dqx, dqy;
int i;

/* Set gg to be twice the generator. */
secp256k1_gej_set_ge(&g2, &secp256k1_ge_const_g);
secp256k1_gej_double_var(&g2, &g2, NULL);
secp256k1_ge_set_gej_var(&gg, &g2);

secp256k1_ge_from_storage(&p, &secp256k1_pre_g[0]);
secp256k1_fe_verify(&p.x);
secp256k1_fe_verify(&p.y);
CHECK(secp256k1_ge_is_valid_var(&p));
for (i = 1; i < ECMULT_TABLE_SIZE(WINDOW_G); ++i) {
secp256k1_fe_negate(&dpx, &p.x, 1); secp256k1_fe_add(&dpx, &gg.x); secp256k1_fe_normalize_weak(&dpx);
secp256k1_fe_negate(&dpy, &p.y, 1); secp256k1_fe_add(&dpy, &gg.y); secp256k1_fe_normalize_weak(&dpy);
/* Check that p is not equal to gg */
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dpx) || !secp256k1_fe_normalizes_to_zero_var(&dpy));

secp256k1_ge_from_storage(&q, &secp256k1_pre_g[i]);
secp256k1_fe_verify(&q.x);
secp256k1_fe_verify(&q.y);
CHECK(secp256k1_ge_is_valid_var(&q));

secp256k1_fe_negate(&dqx, &q.x, 1); secp256k1_fe_add(&dqx, &gg.x); secp256k1_fe_normalize_weak(&dqx);
dqy = q.y; secp256k1_fe_add(&dqy, &gg.y); secp256k1_fe_normalize_weak(&dqy);
/* Check that -q is not equal to gg */
CHECK(!secp256k1_fe_normalizes_to_zero_var(&dqx) || !secp256k1_fe_normalizes_to_zero_var(&dqy));

/* Check that -q is not equal to p */
CHECK(!secp256k1_fe_equal_var(&dpx, &dqx) || !secp256k1_fe_equal_var(&dpy, &dqy));

/* Check that p, -q and gg are colinear */
secp256k1_fe_mul(&dqx, &dqx, &dpy);
secp256k1_fe_mul(&dqy, &dqy, &dpx);
secp256k1_fe_negate(&dqy, &dqy, 1);
secp256k1_fe_add(&dqx, &dqy);
CHECK(secp256k1_fe_normalizes_to_zero_var(&dqx));

p = q;
}
}

void run_ecmult_chain(void) {
/* random starting point A (on the curve) */
secp256k1_gej a = SECP256K1_GEJ_CONST(
Expand Down Expand Up @@ -6523,6 +6568,7 @@ int main(int argc, char **argv) {
run_group_decompress();

/* ecmult tests */
run_ecmult_pre_g();
run_wnaf();
run_point_times_order();
run_ecmult_near_split_bound();
Expand Down

0 comments on commit de435e5

Please sign in to comment.