Skip to content

Commit a828df5

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 17f7148 commit a828df5

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
@@ -57,6 +57,7 @@ void random_group_element_test(secp256k1_ge_t *ge) {
5757
do {
5858
random_field_element_test(&fe);
5959
if (secp256k1_ge_set_xo_var(ge, &fe, secp256k1_rand32() & 1)) {
60+
secp256k1_fe_normalize(&ge->y);
6061
break;
6162
}
6263
} while(1);
@@ -914,7 +915,7 @@ void ge_equals_ge(const secp256k1_ge_t *a, const secp256k1_ge_t *b) {
914915
return;
915916
}
916917
CHECK(secp256k1_fe_equal_var(&a->x, &b->x));
917-
CHECK(secp256k1_fe_equal_var(&b->y, &b->y));
918+
CHECK(secp256k1_fe_equal_var(&a->y, &b->y));
918919
}
919920

920921
/* This compares jacobian points including their Z, not just their geometric meaning. */
@@ -1305,6 +1306,8 @@ void test_point_times_order(const secp256k1_gej_t *point) {
13051306
/* X * (point + G) + (order-X) * (pointer + G) = 0 */
13061307
secp256k1_scalar_t x;
13071308
secp256k1_scalar_t nx;
1309+
secp256k1_scalar_t zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
1310+
secp256k1_scalar_t one = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 1);
13081311
secp256k1_gej_t res1, res2;
13091312
secp256k1_ge_t res3;
13101313
unsigned char pub[65];
@@ -1322,6 +1325,16 @@ void test_point_times_order(const secp256k1_gej_t *point) {
13221325
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 0) == 0);
13231326
psize = 65;
13241327
CHECK(secp256k1_eckey_pubkey_serialize(&res3, pub, &psize, 1) == 0);
1328+
/* check zero/one edge cases */
1329+
secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &zero, &zero);
1330+
secp256k1_ge_set_gej(&res3, &res1);
1331+
CHECK(secp256k1_ge_is_infinity(&res3));
1332+
secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &one, &zero);
1333+
secp256k1_ge_set_gej(&res3, &res1);
1334+
ge_equals_gej(&res3, point);
1335+
secp256k1_ecmult(&ctx->ecmult_ctx, &res1, point, &zero, &one);
1336+
secp256k1_ge_set_gej(&res3, &res1);
1337+
ge_equals_ge(&res3, &secp256k1_ge_const_g);
13251338
}
13261339

13271340
void run_point_times_order(void) {
@@ -1469,6 +1482,33 @@ void run_ecmult_gen_blind(void) {
14691482
}
14701483
}
14711484

1485+
#ifdef USE_ENDOMORPHISM
1486+
/***** ENDOMORPHISH TESTS *****/
1487+
void test_scalar_split(void) {
1488+
secp256k1_scalar_t full;
1489+
secp256k1_scalar_t s1, slam;
1490+
const unsigned char zero[32] = {0};
1491+
unsigned char tmp[32];
1492+
1493+
random_scalar_order_test(&full);
1494+
secp256k1_scalar_split_lambda_var(&s1, &slam, &full);
1495+
1496+
/* check that both are <= 128 bits in size */
1497+
if (secp256k1_scalar_is_high(&s1))
1498+
secp256k1_scalar_negate(&s1, &s1);
1499+
if (secp256k1_scalar_is_high(&slam))
1500+
secp256k1_scalar_negate(&slam, &slam);
1501+
1502+
secp256k1_scalar_get_b32(tmp, &s1);
1503+
CHECK(memcmp(zero, tmp, 16) == 0);
1504+
secp256k1_scalar_get_b32(tmp, &slam);
1505+
CHECK(memcmp(zero, tmp, 16) == 0);
1506+
}
1507+
1508+
void run_endomorphism_tests(void) {
1509+
test_scalar_split();
1510+
}
1511+
#endif
14721512

14731513
void random_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_scalar_t *key, const secp256k1_scalar_t *msg, int *recid) {
14741514
secp256k1_scalar_t nonce;
@@ -2228,6 +2268,11 @@ int main(int argc, char **argv) {
22282268
run_ecmult_constants();
22292269
run_ecmult_gen_blind();
22302270

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

0 commit comments

Comments
 (0)