int main(void) { int i; secp256k1_gej groupj[EXHAUSTIVE_TEST_ORDER]; secp256k1_ge group[EXHAUSTIVE_TEST_ORDER]; /* Build context */ secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); /* TODO set z = 1, then do num_tests runs with random z values */ /* Generate the entire group */ secp256k1_gej_set_infinity(&groupj[0]); secp256k1_ge_set_gej(&group[0], &groupj[0]); for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* Set a different random z-value for each Jacobian point */ secp256k1_fe z; random_fe(&z); secp256k1_gej_add_ge(&groupj[i], &groupj[i - 1], &secp256k1_ge_const_g); secp256k1_ge_set_gej(&group[i], &groupj[i]); secp256k1_gej_rescale(&groupj[i], &z); /* Verify against ecmult_gen */ { secp256k1_scalar scalar_i; secp256k1_gej generatedj; secp256k1_ge generated; secp256k1_scalar_set_int(&scalar_i, i); secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &generatedj, &scalar_i); secp256k1_ge_set_gej(&generated, &generatedj); CHECK(group[i].infinity == 0); CHECK(generated.infinity == 0); CHECK(secp256k1_fe_equal_var(&generated.x, &group[i].x)); CHECK(secp256k1_fe_equal_var(&generated.y, &group[i].y)); } } /* Run the tests */ #ifdef USE_ENDOMORPHISM test_exhaustive_endomorphism(group, EXHAUSTIVE_TEST_ORDER); #endif test_exhaustive_addition(group, groupj, EXHAUSTIVE_TEST_ORDER); test_exhaustive_ecmult(ctx, group, groupj, EXHAUSTIVE_TEST_ORDER); test_exhaustive_ecmult_multi(ctx, group, EXHAUSTIVE_TEST_ORDER); test_exhaustive_sign(ctx, group, EXHAUSTIVE_TEST_ORDER); test_exhaustive_verify(ctx, group, EXHAUSTIVE_TEST_ORDER); #ifdef ENABLE_MODULE_RECOVERY test_exhaustive_recovery_sign(ctx, group, EXHAUSTIVE_TEST_ORDER); test_exhaustive_recovery_verify(ctx, group, EXHAUSTIVE_TEST_ORDER); #endif secp256k1_context_destroy(ctx); return 0; }
int secp256k1_ec_pubkey_combine(const secp256k1_context* ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey * const *pubnonces, size_t n) { size_t i; secp256k1_gej Qj; secp256k1_ge Q; ARG_CHECK(pubnonce != NULL); memset(pubnonce, 0, sizeof(*pubnonce)); ARG_CHECK(n >= 1); ARG_CHECK(pubnonces != NULL); secp256k1_gej_set_infinity(&Qj); for (i = 0; i < n; i++) { secp256k1_pubkey_load(ctx, &Q, pubnonces[i]); secp256k1_gej_add_ge(&Qj, &Qj, &Q); } if (secp256k1_gej_is_infinity(&Qj)) return 0; secp256k1_ge_set_gej(&Q, &Qj); secp256k1_pubkey_save(pubnonce, &Q); return 1; }
void test_ge(void) { char ca[135]; char cb[68]; int rlen; secp256k1_ge_t a, b, i, n; random_group_element_test(&a); random_group_element_test(&b); rlen = sizeof(ca); secp256k1_ge_get_hex(ca,&rlen,&a); CHECK(rlen > 4 && rlen <= (int)sizeof(ca)); rlen = sizeof(cb); secp256k1_ge_get_hex(cb,&rlen,&b); /* Intentionally undersized buffer. */ n = a; secp256k1_fe_normalize(&a.y); secp256k1_fe_negate(&n.y, &a.y, 1); secp256k1_ge_set_infinity(&i); random_field_element_magnitude(&a.x); random_field_element_magnitude(&a.y); random_field_element_magnitude(&b.x); random_field_element_magnitude(&b.y); random_field_element_magnitude(&n.x); random_field_element_magnitude(&n.y); secp256k1_gej_t aj, bj, ij, nj; random_group_element_jacobian_test(&aj, &a); random_group_element_jacobian_test(&bj, &b); secp256k1_gej_set_infinity(&ij); random_group_element_jacobian_test(&nj, &n); random_field_element_magnitude(&aj.x); random_field_element_magnitude(&aj.y); random_field_element_magnitude(&aj.z); random_field_element_magnitude(&bj.x); random_field_element_magnitude(&bj.y); random_field_element_magnitude(&bj.z); random_field_element_magnitude(&nj.x); random_field_element_magnitude(&nj.y); random_field_element_magnitude(&nj.z); /* gej + gej adds */ secp256k1_gej_t aaj; secp256k1_gej_add_var(&aaj, &aj, &aj); secp256k1_gej_t abj; secp256k1_gej_add_var(&abj, &aj, &bj); secp256k1_gej_t aij; secp256k1_gej_add_var(&aij, &aj, &ij); secp256k1_gej_t anj; secp256k1_gej_add_var(&anj, &aj, &nj); secp256k1_gej_t iaj; secp256k1_gej_add_var(&iaj, &ij, &aj); secp256k1_gej_t iij; secp256k1_gej_add_var(&iij, &ij, &ij); /* gej + ge adds */ secp256k1_gej_t aa; secp256k1_gej_add_ge_var(&aa, &aj, &a); secp256k1_gej_t ab; secp256k1_gej_add_ge_var(&ab, &aj, &b); 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); secp256k1_gej_t ii; secp256k1_gej_add_ge_var(&ii, &ij, &i); /* const gej + ge adds */ secp256k1_gej_t aac; secp256k1_gej_add_ge(&aac, &aj, &a); secp256k1_gej_t abc; secp256k1_gej_add_ge(&abc, &aj, &b); secp256k1_gej_t anc; secp256k1_gej_add_ge(&anc, &aj, &n); secp256k1_gej_t iac; secp256k1_gej_add_ge(&iac, &ij, &a); CHECK(secp256k1_gej_is_infinity(&an)); CHECK(secp256k1_gej_is_infinity(&anj)); CHECK(secp256k1_gej_is_infinity(&anc)); gej_equals_gej(&aa, &aaj); gej_equals_gej(&aa, &aac); gej_equals_gej(&ab, &abj); gej_equals_gej(&ab, &abc); gej_equals_gej(&an, &anj); gej_equals_gej(&an, &anc); gej_equals_gej(&ia, &iaj); gej_equals_gej(&ai, &aij); gej_equals_gej(&ii, &iij); ge_equals_gej(&a, &ai); ge_equals_gej(&a, &ai); ge_equals_gej(&a, &iaj); ge_equals_gej(&a, &iaj); ge_equals_gej(&a, &iac); }
void test_ge(void) { secp256k1_ge_t a, b, i, n; random_group_element_test(&a); random_group_element_test(&b); n = a; secp256k1_fe_normalize(&a.y); secp256k1_fe_negate(&n.y, &a.y, 1); secp256k1_ge_set_infinity(&i); random_field_element_magnitude(&a.x); random_field_element_magnitude(&a.y); random_field_element_magnitude(&b.x); random_field_element_magnitude(&b.y); random_field_element_magnitude(&n.x); random_field_element_magnitude(&n.y); secp256k1_gej_t aj, bj, ij, nj; random_group_element_jacobian_test(&aj, &a); random_group_element_jacobian_test(&bj, &b); secp256k1_gej_set_infinity(&ij); random_group_element_jacobian_test(&nj, &n); random_field_element_magnitude(&aj.x); random_field_element_magnitude(&aj.y); random_field_element_magnitude(&aj.z); random_field_element_magnitude(&bj.x); random_field_element_magnitude(&bj.y); random_field_element_magnitude(&bj.z); random_field_element_magnitude(&nj.x); random_field_element_magnitude(&nj.y); random_field_element_magnitude(&nj.z); /* gej + gej adds */ secp256k1_gej_t aaj; secp256k1_gej_add_var(&aaj, &aj, &aj); secp256k1_gej_t abj; secp256k1_gej_add_var(&abj, &aj, &bj); secp256k1_gej_t aij; secp256k1_gej_add_var(&aij, &aj, &ij); secp256k1_gej_t anj; secp256k1_gej_add_var(&anj, &aj, &nj); secp256k1_gej_t iaj; secp256k1_gej_add_var(&iaj, &ij, &aj); secp256k1_gej_t iij; secp256k1_gej_add_var(&iij, &ij, &ij); /* gej + ge adds */ secp256k1_gej_t aa; secp256k1_gej_add_ge_var(&aa, &aj, &a); secp256k1_gej_t ab; secp256k1_gej_add_ge_var(&ab, &aj, &b); 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); secp256k1_gej_t ii; secp256k1_gej_add_ge_var(&ii, &ij, &i); /* const gej + ge adds */ secp256k1_gej_t aac; secp256k1_gej_add_ge(&aac, &aj, &a); secp256k1_gej_t abc; secp256k1_gej_add_ge(&abc, &aj, &b); secp256k1_gej_t anc; secp256k1_gej_add_ge(&anc, &aj, &n); secp256k1_gej_t iac; secp256k1_gej_add_ge(&iac, &ij, &a); CHECK(secp256k1_gej_is_infinity(&an)); CHECK(secp256k1_gej_is_infinity(&anj)); CHECK(secp256k1_gej_is_infinity(&anc)); gej_equals_gej(&aa, &aaj); gej_equals_gej(&aa, &aac); gej_equals_gej(&ab, &abj); gej_equals_gej(&ab, &abc); gej_equals_gej(&an, &anj); gej_equals_gej(&an, &anc); gej_equals_gej(&ia, &iaj); gej_equals_gej(&ai, &aij); gej_equals_gej(&ii, &iij); ge_equals_gej(&a, &ai); ge_equals_gej(&a, &ai); ge_equals_gej(&a, &iaj); ge_equals_gej(&a, &iaj); ge_equals_gej(&a, &iac); }