Skip to content

Commit baa75da

Browse files
committed
tests: add a couple tests
- Add zero/one sanity check tests for ecmult - Add unit test for secp256k1_scalar_split_lambda_var - Typo fix in `ge_equals_ge`; was comparing b->y to itself, should have been comparing a->y to b->y - Normalize y-coordinate in `random_group_element_test`; this is needed to pass random group elements as the first argument to `ge_equals_ge`, which I will do in a future commit.
1 parent ae4f0c6 commit baa75da

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/tests.c

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void random_group_element_test(secp256k1_ge_t *ge) {
5656
do {
5757
random_field_element_test(&fe);
5858
if (secp256k1_ge_set_xo_var(ge, &fe, secp256k1_rand32() & 1)) {
59+
secp256k1_fe_normalize(&ge->y);
5960
break;
6061
}
6162
} while(1);
@@ -932,7 +933,7 @@ void ge_equals_ge(const secp256k1_ge_t *a, const secp256k1_ge_t *b) {
932933
return;
933934
}
934935
CHECK(secp256k1_fe_equal_var(&a->x, &b->x));
935-
CHECK(secp256k1_fe_equal_var(&b->y, &b->y));
936+
CHECK(secp256k1_fe_equal_var(&a->y, &b->y));
936937
}
937938

938939
/* This compares jacobian points including their Z, not just their geometric meaning. */
@@ -1323,6 +1324,8 @@ void test_point_times_order(const secp256k1_gej_t *point) {
13231324
/* X * (point + G) + (order-X) * (pointer + G) = 0 */
13241325
secp256k1_scalar_t x;
13251326
secp256k1_scalar_t nx;
1327+
secp256k1_scalar_t zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
1328+
secp256k1_scalar_t one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
13261329
secp256k1_gej_t res1, res2;
13271330
secp256k1_ge_t res3;
13281331
unsigned char pub[65];
@@ -1340,6 +1343,16 @@ void test_point_times_order(const secp256k1_gej_t *point) {
13401343
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 0) == 0);
13411344
psize = 65;
13421345
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 1) == 0);
1346+
/* check zero/one edge cases */
1347+
secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &zero, &zero);
1348+
secp256k1_ge_set_gej(&res3, &res1);
1349+
CHECK(secp256k1_ge_is_infinity(&res3));
1350+
secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &one, &zero);
1351+
secp256k1_ge_set_gej(&res3, &res1);
1352+
ge_equals_gej(&res3, point);
1353+
secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &zero, &one);
1354+
secp256k1_ge_set_gej(&res3, &res1);
1355+
ge_equals_ge(&res3, &secp256k1_ge_const_g);
13431356
}
13441357

13451358
void run_point_times_order(void) {
@@ -1487,6 +1500,33 @@ void run_ecmult_gen_blind(void) {
14871500
}
14881501
}
14891502

1503+
#ifdef USE_ENDOMORPHISM
1504+
/***** ENDOMORPHISH TESTS *****/
1505+
void test_scalar_split(void) {
1506+
secp256k1_scalar_t full;
1507+
secp256k1_scalar_t s1, slam;
1508+
const unsigned char zero[32] = {0};
1509+
unsigned char tmp[32];
1510+
1511+
random_scalar_order_test(&full);
1512+
secp256k1_scalar_split_lambda_var(&s1, &slam, &full);
1513+
1514+
/* check that both are <= 128 bits in size */
1515+
if (secp256k1_scalar_is_high(&s1))
1516+
secp256k1_scalar_negate(&s1, &s1);
1517+
if (secp256k1_scalar_is_high(&slam))
1518+
secp256k1_scalar_negate(&slam, &slam);
1519+
1520+
secp256k1_scalar_get_b32(tmp, &s1);
1521+
CHECK(memcmp(zero, tmp, 16) == 0);
1522+
secp256k1_scalar_get_b32(tmp, &slam);
1523+
CHECK(memcmp(zero, tmp, 16) == 0);
1524+
}
1525+
1526+
void run_endomorphism_tests(void) {
1527+
test_scalar_split();
1528+
}
1529+
#endif
14901530

14911531
void random_sign(secp256k1_scalar_t *sigr, secp256k1_scalar_t *sigs, const secp256k1_scalar_t *key, const secp256k1_scalar_t *msg, int *recid) {
14921532
secp256k1_scalar_t nonce;
@@ -2227,6 +2267,11 @@ int main(int argc, char **argv) {
22272267
run_ecmult_constants();
22282268
run_ecmult_gen_blind();
22292269

2270+
/* endomorphism tests */
2271+
#ifdef USE_ENDOMORPHISM
2272+
run_endomorphism_tests();
2273+
#endif
2274+
22302275
/* ecdsa tests */
22312276
run_random_pubkeys();
22322277
run_ecdsa_sign_verify();

0 commit comments

Comments
 (0)