Merge pull request #176

9ab9335 Add a reference consistency test to ge_tests. (Pieter Wuille)
60571c6 Rework group tests (Pieter Wuille)
pull/11871/head
Pieter Wuille 10 years ago
commit 4ee4f7ac3f
No known key found for this signature in database
GPG Key ID: 57896D2FF8F0B657

@ -36,12 +36,19 @@ void random_field_element_test(secp256k1_fe_t *fe) {
} }
void random_field_element_magnitude(secp256k1_fe_t *fe) { void random_field_element_magnitude(secp256k1_fe_t *fe) {
int n = secp256k1_rand32() % 9;
secp256k1_fe_normalize(fe); secp256k1_fe_normalize(fe);
int n = secp256k1_rand32() % 4; if (n == 0) {
for (int i = 0; i < n; i++) { return;
secp256k1_fe_negate(fe, fe, 1 + 2*i);
secp256k1_fe_negate(fe, fe, 2 + 2*i);
} }
secp256k1_fe_t zero;
secp256k1_fe_clear(&zero);
secp256k1_fe_negate(&zero, &zero, 0);
secp256k1_fe_mul_int(&zero, n - 1);
secp256k1_fe_add(fe, &zero);
#ifdef VERIFY
CHECK(fe->magnitude == n);
#endif
} }
void random_group_element_test(secp256k1_ge_t *ge) { void random_group_element_test(secp256k1_ge_t *ge) {
@ -771,108 +778,148 @@ void run_sqrt(void) {
/***** GROUP TESTS *****/ /***** GROUP TESTS *****/
int ge_equals_ge(const secp256k1_ge_t *a, const secp256k1_ge_t *b) { void ge_equals_ge(const secp256k1_ge_t *a, const secp256k1_ge_t *b) {
if (a->infinity && b->infinity) CHECK(a->infinity == b->infinity);
return 1; if (a->infinity)
return check_fe_equal(&a->x, &b->x) && check_fe_equal(&a->y, &b->y); return;
CHECK(secp256k1_fe_equal_var(&a->x, &b->x));
CHECK(secp256k1_fe_equal_var(&b->y, &b->y));
} }
void ge_equals_gej(const secp256k1_ge_t *a, const secp256k1_gej_t *b) { void ge_equals_gej(const secp256k1_ge_t *a, const secp256k1_gej_t *b) {
secp256k1_ge_t bb; CHECK(a->infinity == b->infinity);
secp256k1_gej_t bj = *b; if (a->infinity)
secp256k1_ge_set_gej_var(&bb, &bj); return;
CHECK(ge_equals_ge(a, &bb)); /* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
} secp256k1_fe_t z2s;
secp256k1_fe_sqr(&z2s, &b->z);
void gej_equals_gej(const secp256k1_gej_t *a, const secp256k1_gej_t *b) { secp256k1_fe_t u1, u2, s1, s2;
secp256k1_ge_t aa, bb; secp256k1_fe_mul(&u1, &a->x, &z2s);
secp256k1_gej_t aj = *a, bj = *b; u2 = b->x; secp256k1_fe_normalize_weak(&u2);
secp256k1_ge_set_gej_var(&aa, &aj); secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
secp256k1_ge_set_gej_var(&bb, &bj); s2 = b->y; secp256k1_fe_normalize_weak(&s2);
CHECK(ge_equals_ge(&aa, &bb)); CHECK(secp256k1_fe_equal_var(&u1, &u2));
CHECK(secp256k1_fe_equal_var(&s1, &s2));
} }
void test_ge(void) { void test_ge(void) {
char ca[135]; int runs = 4;
char cb[68]; /* Points: (infinity, p1, p1, -p1, -p1, p2, p2, -p2, -p2, p3, p3, -p3, -p3, p4, p4, -p4, -p4).
int rlen; * The second in each pair of identical points uses a random Z coordinate in the Jacobian form.
secp256k1_ge_t a, b, i, n; * All magnitudes are randomized.
random_group_element_test(&a); * All 17*17 combinations of points are added to eachother, using all applicable methods.
random_group_element_test(&b); */
rlen = sizeof(ca); secp256k1_ge_t *ge = malloc(sizeof(secp256k1_ge_t) * (1 + 4 * runs));
secp256k1_ge_get_hex(ca,&rlen,&a); secp256k1_gej_t *gej = malloc(sizeof(secp256k1_gej_t) * (1 + 4 * runs));
CHECK(rlen > 4 && rlen <= (int)sizeof(ca)); secp256k1_gej_set_infinity(&gej[0]);
rlen = sizeof(cb); secp256k1_ge_clear(&ge[0]);
secp256k1_ge_get_hex(cb,&rlen,&b); /* Intentionally undersized buffer. */ secp256k1_ge_set_gej_var(&ge[0], &gej[0]);
n = a; for (int i = 0; i < runs; i++) {
secp256k1_fe_normalize(&a.y); secp256k1_ge_t g;
secp256k1_fe_negate(&n.y, &a.y, 1); random_group_element_test(&g);
secp256k1_ge_set_infinity(&i); ge[1 + 4 * i] = g;
random_field_element_magnitude(&a.x); ge[2 + 4 * i] = g;
random_field_element_magnitude(&a.y); secp256k1_ge_neg(&ge[3 + 4 * i], &g);
random_field_element_magnitude(&b.x); secp256k1_ge_neg(&ge[4 + 4 * i], &g);
random_field_element_magnitude(&b.y); secp256k1_gej_set_ge(&gej[1 + 4 * i], &ge[1 + 4 * i]);
random_field_element_magnitude(&n.x); random_group_element_jacobian_test(&gej[2 + 4 * i], &ge[2 + 4 * i]);
random_field_element_magnitude(&n.y); secp256k1_gej_set_ge(&gej[3 + 4 * i], &ge[3 + 4 * i]);
random_group_element_jacobian_test(&gej[4 + 4 * i], &ge[4 + 4 * i]);
secp256k1_gej_t aj, bj, ij, nj; for (int j = 0; j < 4; j++) {
random_group_element_jacobian_test(&aj, &a); random_field_element_magnitude(&ge[1 + j + 4 * i].x);
random_group_element_jacobian_test(&bj, &b); random_field_element_magnitude(&ge[1 + j + 4 * i].y);
secp256k1_gej_set_infinity(&ij); random_field_element_magnitude(&gej[1 + j + 4 * i].x);
random_group_element_jacobian_test(&nj, &n); random_field_element_magnitude(&gej[1 + j + 4 * i].y);
random_field_element_magnitude(&aj.x); random_field_element_magnitude(&gej[1 + j + 4 * i].z);
random_field_element_magnitude(&aj.y); }
random_field_element_magnitude(&aj.z); }
random_field_element_magnitude(&bj.x);
random_field_element_magnitude(&bj.y); for (int i1 = 0; i1 < 1 + 4 * runs; i1++) {
random_field_element_magnitude(&bj.z); for (int i2 = 0; i2 < 1 + 4 * runs; i2++) {
random_field_element_magnitude(&nj.x); /* Compute reference result using gej + gej (var). */
random_field_element_magnitude(&nj.y); secp256k1_gej_t refj, resj;
random_field_element_magnitude(&nj.z); secp256k1_ge_t ref;
secp256k1_gej_add_var(&refj, &gej[i1], &gej[i2]);
/* gej + gej adds */ secp256k1_ge_set_gej_var(&ref, &refj);
secp256k1_gej_t aaj; secp256k1_gej_add_var(&aaj, &aj, &aj);
secp256k1_gej_t abj; secp256k1_gej_add_var(&abj, &aj, &bj); /* Test gej + ge (var). */
secp256k1_gej_t aij; secp256k1_gej_add_var(&aij, &aj, &ij); secp256k1_gej_add_ge_var(&resj, &gej[i1], &ge[i2]);
secp256k1_gej_t anj; secp256k1_gej_add_var(&anj, &aj, &nj); ge_equals_gej(&ref, &resj);
secp256k1_gej_t iaj; secp256k1_gej_add_var(&iaj, &ij, &aj);
secp256k1_gej_t iij; secp256k1_gej_add_var(&iij, &ij, &ij); /* Test gej + ge (const). */
if (i2 != 0) {
/* gej + ge adds */ /* secp256k1_gej_add_ge does not support its second argument being infinity. */
secp256k1_gej_t aa; secp256k1_gej_add_ge_var(&aa, &aj, &a); secp256k1_gej_add_ge(&resj, &gej[i1], &ge[i2]);
secp256k1_gej_t ab; secp256k1_gej_add_ge_var(&ab, &aj, &b); ge_equals_gej(&ref, &resj);
secp256k1_gej_t ai; secp256k1_gej_add_ge_var(&ai, &aj, &i); }
secp256k1_gej_t an; secp256k1_gej_add_ge_var(&an, &aj, &n);
secp256k1_gej_t ia; secp256k1_gej_add_ge_var(&ia, &ij, &a); /* Test doubling (var). */
secp256k1_gej_t ii; secp256k1_gej_add_ge_var(&ii, &ij, &i); if ((i1 == 0 && i2 == 0) || ((i1 + 3)/4 == (i2 + 3)/4 && ((i1 + 3)%4)/2 == ((i2 + 3)%4)/2)) {
/* Normal doubling. */
/* const gej + ge adds */ secp256k1_gej_double_var(&resj, &gej[i1]);
secp256k1_gej_t aac; secp256k1_gej_add_ge(&aac, &aj, &a); ge_equals_gej(&ref, &resj);
secp256k1_gej_t abc; secp256k1_gej_add_ge(&abc, &aj, &b); secp256k1_gej_double_var(&resj, &gej[i2]);
secp256k1_gej_t anc; secp256k1_gej_add_ge(&anc, &aj, &n); ge_equals_gej(&ref, &resj);
secp256k1_gej_t iac; secp256k1_gej_add_ge(&iac, &ij, &a); }
CHECK(secp256k1_gej_is_infinity(&an)); /* Test adding opposites. */
CHECK(secp256k1_gej_is_infinity(&anj)); if ((i1 == 0 && i2 == 0) || ((i1 + 3)/4 == (i2 + 3)/4 && ((i1 + 3)%4)/2 != ((i2 + 3)%4)/2)) {
CHECK(secp256k1_gej_is_infinity(&anc)); CHECK(secp256k1_ge_is_infinity(&ref));
gej_equals_gej(&aa, &aaj); }
gej_equals_gej(&aa, &aac);
gej_equals_gej(&ab, &abj); /* Test adding infinity. */
gej_equals_gej(&ab, &abc); if (i1 == 0) {
gej_equals_gej(&an, &anj); CHECK(secp256k1_ge_is_infinity(&ge[i1]));
gej_equals_gej(&an, &anc); CHECK(secp256k1_gej_is_infinity(&gej[i1]));
gej_equals_gej(&ia, &iaj); ge_equals_gej(&ref, &gej[i2]);
gej_equals_gej(&ai, &aij); }
gej_equals_gej(&ii, &iij); if (i2 == 0) {
ge_equals_gej(&a, &ai); CHECK(secp256k1_ge_is_infinity(&ge[i2]));
ge_equals_gej(&a, &ai); CHECK(secp256k1_gej_is_infinity(&gej[i2]));
ge_equals_gej(&a, &iaj); ge_equals_gej(&ref, &gej[i1]);
ge_equals_gej(&a, &iaj); }
ge_equals_gej(&a, &iac); }
}
/* Test adding all points together in random order equals infinity. */
{
secp256k1_gej_t *gej_shuffled = malloc((4 * runs + 1) * sizeof(secp256k1_gej_t));
for (int i = 0; i < 4 * runs + 1; i++) {
gej_shuffled[i] = gej[i];
}
for (int i = 0; i < 4 * runs + 1; i++) {
int swap = i + secp256k1_rand32() % (4 * runs + 1 - i);
if (swap != i) {
secp256k1_gej_t t = gej_shuffled[i];
gej_shuffled[i] = gej_shuffled[swap];
gej_shuffled[swap] = t;
}
}
secp256k1_gej_t sum;
secp256k1_gej_set_infinity(&sum);
for (int i = 0; i < 4 * runs + 1; i++) {
secp256k1_gej_add_var(&sum, &sum, &gej_shuffled[i]);
}
CHECK(secp256k1_gej_is_infinity(&sum));
free(gej_shuffled);
}
/* Test batch gej -> ge conversion. */
{
secp256k1_ge_t *ge_set_all = malloc((4 * runs + 1) * sizeof(secp256k1_ge_t));
secp256k1_ge_set_all_gej_var(4 * runs + 1, ge_set_all, gej);
for (int i = 0; i < 4 * runs + 1; i++) {
ge_equals_gej(&ge_set_all[i], &gej[i]);
}
free(ge_set_all);
}
free(ge);
free(gej);
} }
void run_ge(void) { void run_ge(void) {
for (int i = 0; i < 2000*count; i++) { for (int i = 0; i < count * 32; i++) {
test_ge(); test_ge();
} }
} }
@ -1188,7 +1235,7 @@ void test_random_pubkeys(void) {
CHECK(secp256k1_eckey_pubkey_serialize(&elem, in, &size, 0)); CHECK(secp256k1_eckey_pubkey_serialize(&elem, in, &size, 0));
CHECK(size == 65); CHECK(size == 65);
CHECK(secp256k1_eckey_pubkey_parse(&elem2, in, size)); CHECK(secp256k1_eckey_pubkey_parse(&elem2, in, size));
CHECK(ge_equals_ge(&elem,&elem2)); ge_equals_ge(&elem,&elem2);
/* Check that the X9.62 hybrid type is checked. */ /* Check that the X9.62 hybrid type is checked. */
in[0] = (r & 1) ? 6 : 7; in[0] = (r & 1) ? 6 : 7;
res = secp256k1_eckey_pubkey_parse(&elem2, in, size); res = secp256k1_eckey_pubkey_parse(&elem2, in, size);
@ -1197,7 +1244,7 @@ void test_random_pubkeys(void) {
else CHECK(!res); else CHECK(!res);
} }
if (res) { if (res) {
CHECK(ge_equals_ge(&elem,&elem2)); ge_equals_ge(&elem,&elem2);
CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, 0)); CHECK(secp256k1_eckey_pubkey_serialize(&elem, out, &size, 0));
CHECK(memcmp(&in[1], &out[1], 64) == 0); CHECK(memcmp(&in[1], &out[1], 64) == 0);
} }

Loading…
Cancel
Save