Skip to content

Commit 4392f0f

Browse files
Merge #1533: tests: refactor: tidy up util functions (#1491)
e73f6f8 tests: refactor: drop `secp256k1_` prefix from testrand.h functions (Sebastian Falbesoner) 0ee7453 tests: refactor: add `testutil_` prefix to testutil.h functions (Sebastian Falbesoner) 0c6bc76 tests: refactor: move `random_` helpers from tests.c to testutil.h (Sebastian Falbesoner) 0fef847 tests: refactor: rename `random_field_element_magnitude` -> `random_fe_magnitude` (Sebastian Falbesoner) 59db007 tests: refactor: rename `random_group_element_...` -> `random_ge_...` (Sebastian Falbesoner) Pull request description: This PR is an attempt at tidying up test util functions, as suggested in #1491. The following changes are done: * rename `_group_element...` functions to `_ge...` * rename `_field_element...` functions to `_fe...` * move `random_` helpers from tests.c to testutil.h (the alternative would be testrand.h, but to my understanding, this one is meant to contain the actual RNG implementation rather than helpers using it; happy to move the helpers there if that is preferred though) * prefix testutil.h functions with `testutil_` * prefix testrand.h functions with `testrand_` (this is currently done in a sloppy way by simply dropping the `secp256k1_` prefix, so some functions don't have the full prefix, like e.g. `testrand256`; naming suggestions welcome) ACKs for top commit: sipa: utACK e73f6f8 real-or-random: utACK e73f6f8 Tree-SHA512: c87a35a9f7f23d4bbb87a1ff0d40dd5fbd7d976719ca1027cad187ac44aa2db3ae887ac620639d2287c260e701a5963830b52048692d3e6b38b5eb6cdf17b854
2 parents bedffd5 + e73f6f8 commit 4392f0f

File tree

11 files changed

+397
-400
lines changed

11 files changed

+397
-400
lines changed

src/modules/ecdh/tests_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static void test_ecdh_generator_basepoint(void) {
5656
size_t point_ser_len = sizeof(point_ser);
5757
secp256k1_scalar s;
5858

59-
random_scalar_order(&s);
59+
testutil_random_scalar_order(&s);
6060
secp256k1_scalar_get_b32(s_b32, &s);
6161

6262
CHECK(secp256k1_ec_pubkey_create(CTX, &point[0], s_one) == 1);
@@ -95,7 +95,7 @@ static void test_bad_scalar(void) {
9595
secp256k1_pubkey point;
9696

9797
/* Create random point */
98-
random_scalar_order(&rand);
98+
testutil_random_scalar_order(&rand);
9999
secp256k1_scalar_get_b32(s_rand, &rand);
100100
CHECK(secp256k1_ec_pubkey_create(CTX, &point, s_rand) == 1);
101101

@@ -127,7 +127,7 @@ static void test_result_basepoint(void) {
127127
CHECK(secp256k1_ecdh(CTX, out_base, &point, s_one, NULL, NULL) == 1);
128128

129129
for (i = 0; i < 2 * COUNT; i++) {
130-
random_scalar_order(&rand);
130+
testutil_random_scalar_order(&rand);
131131
secp256k1_scalar_get_b32(s, &rand);
132132
secp256k1_scalar_inverse(&rand, &rand);
133133
secp256k1_scalar_get_b32(s_inv, &rand);

src/modules/ellswift/tests_impl.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ void run_ellswift_tests(void) {
229229
secp256k1_ge g, g2;
230230
secp256k1_pubkey pubkey, pubkey2;
231231
/* Generate random public key and random randomizer. */
232-
random_group_element_test(&g);
232+
testutil_random_ge_test(&g);
233233
secp256k1_pubkey_save(&pubkey, &g);
234-
secp256k1_testrand256(rnd32);
234+
testrand256(rnd32);
235235
/* Convert the public key to ElligatorSwift and back. */
236236
secp256k1_ellswift_encode(CTX, ell64, &pubkey, rnd32);
237237
secp256k1_ellswift_decode(CTX, &pubkey2, ell64);
@@ -249,8 +249,8 @@ void run_ellswift_tests(void) {
249249
unsigned char ell64[64];
250250
int ret;
251251
/* Generate random secret key and random randomizer. */
252-
if (i & 1) secp256k1_testrand256_test(auxrnd32);
253-
random_scalar_order_test(&sec);
252+
if (i & 1) testrand256_test(auxrnd32);
253+
testutil_random_scalar_order_test(&sec);
254254
secp256k1_scalar_get_b32(sec32, &sec);
255255
/* Construct ElligatorSwift-encoded public keys for that key. */
256256
ret = secp256k1_ellswift_create(CTX, ell64, sec32, (i & 1) ? auxrnd32 : NULL);
@@ -271,11 +271,11 @@ void run_ellswift_tests(void) {
271271
secp256k1_pubkey pub;
272272
int ret;
273273
/* Generate random secret key. */
274-
random_scalar_order_test(&sec);
274+
testutil_random_scalar_order_test(&sec);
275275
secp256k1_scalar_get_b32(sec32, &sec);
276276
/* Generate random ElligatorSwift encoding for the remote key and decode it. */
277-
secp256k1_testrand256_test(ell64);
278-
secp256k1_testrand256_test(ell64 + 32);
277+
testrand256_test(ell64);
278+
testrand256_test(ell64 + 32);
279279
secp256k1_ellswift_decode(CTX, &pub, ell64);
280280
secp256k1_pubkey_load(CTX, &dec, &pub);
281281
secp256k1_gej_set_ge(&decj, &dec);
@@ -313,18 +313,18 @@ void run_ellswift_tests(void) {
313313
data = NULL;
314314
} else {
315315
hash_function = secp256k1_ellswift_xdh_hash_function_prefix;
316-
secp256k1_testrand256_test(prefix64);
317-
secp256k1_testrand256_test(prefix64 + 32);
316+
testrand256_test(prefix64);
317+
testrand256_test(prefix64 + 32);
318318
data = prefix64;
319319
}
320320

321321
/* Generate random secret keys and random randomizers. */
322-
secp256k1_testrand256_test(auxrnd32a);
323-
secp256k1_testrand256_test(auxrnd32b);
324-
random_scalar_order_test(&seca);
322+
testrand256_test(auxrnd32a);
323+
testrand256_test(auxrnd32b);
324+
testutil_random_scalar_order_test(&seca);
325325
/* Draw secb uniformly at random to make sure that the secret keys
326326
* differ */
327-
random_scalar_order(&secb);
327+
testutil_random_scalar_order(&secb);
328328
secp256k1_scalar_get_b32(sec32a, &seca);
329329
secp256k1_scalar_get_b32(sec32b, &secb);
330330

@@ -349,42 +349,42 @@ void run_ellswift_tests(void) {
349349
/* Verify that the shared secret doesn't match if other side's public key is incorrect. */
350350
/* For A (using a bad public key for B): */
351351
memcpy(ell64b_bad, ell64b, sizeof(ell64a_bad));
352-
secp256k1_testrand_flip(ell64b_bad, sizeof(ell64b_bad));
352+
testrand_flip(ell64b_bad, sizeof(ell64b_bad));
353353
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
354354
CHECK(ret); /* Mismatching encodings don't get detected by secp256k1_ellswift_xdh. */
355355
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
356356
/* For B (using a bad public key for A): */
357357
memcpy(ell64a_bad, ell64a, sizeof(ell64a_bad));
358-
secp256k1_testrand_flip(ell64a_bad, sizeof(ell64a_bad));
358+
testrand_flip(ell64a_bad, sizeof(ell64a_bad));
359359
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);
360360
CHECK(ret);
361361
CHECK(secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
362362

363363
/* Verify that the shared secret doesn't match if the private key is incorrect. */
364364
/* For A: */
365365
memcpy(sec32a_bad, sec32a, sizeof(sec32a_bad));
366-
secp256k1_testrand_flip(sec32a_bad, sizeof(sec32a_bad));
366+
testrand_flip(sec32a_bad, sizeof(sec32a_bad));
367367
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32a_bad, 0, hash_function, data);
368368
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
369369
/* For B: */
370370
memcpy(sec32b_bad, sec32b, sizeof(sec32b_bad));
371-
secp256k1_testrand_flip(sec32b_bad, sizeof(sec32b_bad));
371+
testrand_flip(sec32b_bad, sizeof(sec32b_bad));
372372
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b, sec32b_bad, 1, hash_function, data);
373373
CHECK(!ret || secp256k1_memcmp_var(share32_bad, share32b, 32) != 0);
374374

375375
if (hash_function != ellswift_xdh_hash_x32) {
376376
/* Verify that the shared secret doesn't match when a different encoding of the same public key is used. */
377377
/* For A (changing B's public key): */
378378
memcpy(auxrnd32b_bad, auxrnd32b, sizeof(auxrnd32b_bad));
379-
secp256k1_testrand_flip(auxrnd32b_bad, sizeof(auxrnd32b_bad));
379+
testrand_flip(auxrnd32b_bad, sizeof(auxrnd32b_bad));
380380
ret = secp256k1_ellswift_create(CTX, ell64b_bad, sec32b, auxrnd32b_bad);
381381
CHECK(ret);
382382
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a, ell64b_bad, sec32a, 0, hash_function, data);
383383
CHECK(ret);
384384
CHECK(secp256k1_memcmp_var(share32_bad, share32a, 32) != 0);
385385
/* For B (changing A's public key): */
386386
memcpy(auxrnd32a_bad, auxrnd32a, sizeof(auxrnd32a_bad));
387-
secp256k1_testrand_flip(auxrnd32a_bad, sizeof(auxrnd32a_bad));
387+
testrand_flip(auxrnd32a_bad, sizeof(auxrnd32a_bad));
388388
ret = secp256k1_ellswift_create(CTX, ell64a_bad, sec32a, auxrnd32a_bad);
389389
CHECK(ret);
390390
ret = secp256k1_ellswift_xdh(CTX, share32_bad, ell64a_bad, ell64b, sec32b, 1, hash_function, data);

src/modules/extrakeys/tests_impl.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ static void test_xonly_pubkey(void) {
2323
int pk_parity;
2424
int i;
2525

26-
secp256k1_testrand256(sk);
26+
testrand256(sk);
2727
memset(ones32, 0xFF, 32);
28-
secp256k1_testrand256(xy_sk);
28+
testrand256(xy_sk);
2929
CHECK(secp256k1_ec_pubkey_create(CTX, &pk, sk) == 1);
3030
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &xonly_pk, &pk_parity, &pk) == 1);
3131

@@ -95,7 +95,7 @@ static void test_xonly_pubkey(void) {
9595
* the curve) then xonly_pubkey_parse should fail as well. */
9696
for (i = 0; i < COUNT; i++) {
9797
unsigned char rand33[33];
98-
secp256k1_testrand256(&rand33[1]);
98+
testrand256(&rand33[1]);
9999
rand33[0] = SECP256K1_TAG_PUBKEY_EVEN;
100100
if (!secp256k1_ec_pubkey_parse(CTX, &pk, rand33, 33)) {
101101
memset(&xonly_pk, 1, sizeof(xonly_pk));
@@ -152,8 +152,8 @@ static void test_xonly_pubkey_tweak(void) {
152152
int i;
153153

154154
memset(overflows, 0xff, sizeof(overflows));
155-
secp256k1_testrand256(tweak);
156-
secp256k1_testrand256(sk);
155+
testrand256(tweak);
156+
testrand256(sk);
157157
CHECK(secp256k1_ec_pubkey_create(CTX, &internal_pk, sk) == 1);
158158
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &internal_xonly_pk, &pk_parity, &internal_pk) == 1);
159159

@@ -190,7 +190,7 @@ static void test_xonly_pubkey_tweak(void) {
190190

191191
/* Invalid pk with a valid tweak */
192192
memset(&internal_xonly_pk, 0, sizeof(internal_xonly_pk));
193-
secp256k1_testrand256(tweak);
193+
testrand256(tweak);
194194
CHECK_ILLEGAL(CTX, secp256k1_xonly_pubkey_tweak_add(CTX, &output_pk, &internal_xonly_pk, tweak));
195195
CHECK(secp256k1_memcmp_var(&output_pk, zeros64, sizeof(output_pk)) == 0);
196196
}
@@ -209,8 +209,8 @@ static void test_xonly_pubkey_tweak_check(void) {
209209
unsigned char tweak[32];
210210

211211
memset(overflows, 0xff, sizeof(overflows));
212-
secp256k1_testrand256(tweak);
213-
secp256k1_testrand256(sk);
212+
testrand256(tweak);
213+
testrand256(sk);
214214
CHECK(secp256k1_ec_pubkey_create(CTX, &internal_pk, sk) == 1);
215215
CHECK(secp256k1_xonly_pubkey_from_pubkey(CTX, &internal_xonly_pk, &pk_parity, &internal_pk) == 1);
216216

@@ -256,7 +256,7 @@ static void test_xonly_pubkey_tweak_recursive(void) {
256256
unsigned char tweak[N_PUBKEYS - 1][32];
257257
int i;
258258

259-
secp256k1_testrand256(sk);
259+
testrand256(sk);
260260
CHECK(secp256k1_ec_pubkey_create(CTX, &pk[0], sk) == 1);
261261
/* Add tweaks */
262262
for (i = 0; i < N_PUBKEYS - 1; i++) {
@@ -292,7 +292,7 @@ static void test_keypair(void) {
292292
memset(overflows, 0xFF, sizeof(overflows));
293293

294294
/* Test keypair_create */
295-
secp256k1_testrand256(sk);
295+
testrand256(sk);
296296
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
297297
CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) != 0);
298298
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
@@ -311,7 +311,7 @@ static void test_keypair(void) {
311311
CHECK(secp256k1_memcmp_var(zeros96, &keypair, sizeof(keypair)) == 0);
312312

313313
/* Test keypair_pub */
314-
secp256k1_testrand256(sk);
314+
testrand256(sk);
315315
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
316316
CHECK(secp256k1_keypair_pub(CTX, &pk, &keypair) == 1);
317317
CHECK_ILLEGAL(CTX, secp256k1_keypair_pub(CTX, NULL, &keypair));
@@ -330,7 +330,7 @@ static void test_keypair(void) {
330330
CHECK(secp256k1_memcmp_var(&pk, &pk_tmp, sizeof(pk)) == 0);
331331

332332
/** Test keypair_xonly_pub **/
333-
secp256k1_testrand256(sk);
333+
testrand256(sk);
334334
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
335335
CHECK(secp256k1_keypair_xonly_pub(CTX, &xonly_pk, &pk_parity, &keypair) == 1);
336336
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_pub(CTX, NULL, &pk_parity, &keypair));
@@ -353,7 +353,7 @@ static void test_keypair(void) {
353353
CHECK(pk_parity == pk_parity_tmp);
354354

355355
/* Test keypair_seckey */
356-
secp256k1_testrand256(sk);
356+
testrand256(sk);
357357
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
358358
CHECK(secp256k1_keypair_sec(CTX, sk_tmp, &keypair) == 1);
359359
CHECK_ILLEGAL(CTX, secp256k1_keypair_sec(CTX, NULL, &keypair));
@@ -381,8 +381,8 @@ static void test_keypair_add(void) {
381381
int i;
382382

383383
CHECK(sizeof(zeros96) == sizeof(keypair));
384-
secp256k1_testrand256(sk);
385-
secp256k1_testrand256(tweak);
384+
testrand256(sk);
385+
testrand256(tweak);
386386
memset(overflows, 0xFF, 32);
387387
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
388388

@@ -407,7 +407,7 @@ static void test_keypair_add(void) {
407407
for (i = 0; i < COUNT; i++) {
408408
secp256k1_scalar scalar_tweak;
409409
secp256k1_keypair keypair_tmp;
410-
secp256k1_testrand256(sk);
410+
testrand256(sk);
411411
CHECK(secp256k1_keypair_create(CTX, &keypair, sk) == 1);
412412
memcpy(&keypair_tmp, &keypair, sizeof(keypair));
413413
/* Because sk may be negated before adding, we need to try with tweak =
@@ -423,7 +423,7 @@ static void test_keypair_add(void) {
423423

424424
/* Invalid keypair with a valid tweak */
425425
memset(&keypair, 0, sizeof(keypair));
426-
secp256k1_testrand256(tweak);
426+
testrand256(tweak);
427427
CHECK_ILLEGAL(CTX, secp256k1_keypair_xonly_tweak_add(CTX, &keypair, tweak));
428428
CHECK(secp256k1_memcmp_var(&keypair, zeros96, sizeof(keypair)) == 0);
429429
/* Only seckey part of keypair invalid */
@@ -446,7 +446,7 @@ static void test_keypair_add(void) {
446446
unsigned char sk32[32];
447447
int pk_parity;
448448

449-
secp256k1_testrand256(tweak);
449+
testrand256(tweak);
450450
CHECK(secp256k1_keypair_xonly_pub(CTX, &internal_pk, NULL, &keypair) == 1);
451451
CHECK(secp256k1_keypair_xonly_tweak_add(CTX, &keypair, tweak) == 1);
452452
CHECK(secp256k1_keypair_xonly_pub(CTX, &output_pk, &pk_parity, &keypair) == 1);

src/modules/recovery/tests_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static int recovery_test_nonce_function(unsigned char *nonce32, const unsigned c
2525
}
2626
/* On the next run, return a valid nonce, but flip a coin as to whether or not to fail signing. */
2727
memset(nonce32, 1, 32);
28-
return secp256k1_testrand_bits(1);
28+
return testrand_bits(1);
2929
}
3030

3131
static void test_ecdsa_recovery_api(void) {
@@ -106,8 +106,8 @@ static void test_ecdsa_recovery_end_to_end(void) {
106106
/* Generate a random key and message. */
107107
{
108108
secp256k1_scalar msg, key;
109-
random_scalar_order_test(&msg);
110-
random_scalar_order_test(&key);
109+
testutil_random_scalar_order_test(&msg);
110+
testutil_random_scalar_order_test(&key);
111111
secp256k1_scalar_get_b32(privkey, &key);
112112
secp256k1_scalar_get_b32(message, &msg);
113113
}
@@ -141,7 +141,7 @@ static void test_ecdsa_recovery_end_to_end(void) {
141141
CHECK(secp256k1_memcmp_var(&pubkey, &recpubkey, sizeof(pubkey)) == 0);
142142
/* Serialize/destroy/parse signature and verify again. */
143143
CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(CTX, sig, &recid, &rsignature[4]) == 1);
144-
sig[secp256k1_testrand_bits(6)] += 1 + secp256k1_testrand_int(255);
144+
sig[testrand_bits(6)] += 1 + testrand_int(255);
145145
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(CTX, &rsignature[4], sig, recid) == 1);
146146
CHECK(secp256k1_ecdsa_recoverable_signature_convert(CTX, &signature[4], &rsignature[4]) == 1);
147147
CHECK(secp256k1_ecdsa_verify(CTX, &signature[4], message, &pubkey) == 0);

src/modules/schnorrsig/tests_exhaustive_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ static void test_exhaustive_schnorrsig_verify(const secp256k1_context *ctx, cons
104104
while (e_count_done < EXHAUSTIVE_TEST_ORDER) {
105105
secp256k1_scalar e;
106106
unsigned char msg32[32];
107-
secp256k1_testrand256(msg32);
107+
testrand256(msg32);
108108
secp256k1_schnorrsig_challenge(&e, sig64, msg32, sizeof(msg32), pk32);
109109
/* Only do work if we hit a challenge we haven't tried before. */
110110
if (!e_done[e]) {
@@ -120,7 +120,7 @@ static void test_exhaustive_schnorrsig_verify(const secp256k1_context *ctx, cons
120120
expect_valid = actual_k != -1 && s != EXHAUSTIVE_TEST_ORDER &&
121121
(s == (actual_k + actual_d * e) % EXHAUSTIVE_TEST_ORDER);
122122
} else {
123-
secp256k1_testrand256(sig64 + 32);
123+
testrand256(sig64 + 32);
124124
expect_valid = 0;
125125
}
126126
valid = secp256k1_schnorrsig_verify(ctx, sig64, msg32, sizeof(msg32), &pubkeys[d - 1]);
@@ -161,7 +161,7 @@ static void test_exhaustive_schnorrsig_sign(const secp256k1_context *ctx, unsign
161161
/* Generate random messages until all challenges have been tried. */
162162
while (e_count_done < EXHAUSTIVE_TEST_ORDER) {
163163
secp256k1_scalar e;
164-
secp256k1_testrand256(msg32);
164+
testrand256(msg32);
165165
secp256k1_schnorrsig_challenge(&e, xonly_pubkey_bytes[k - 1], msg32, sizeof(msg32), xonly_pubkey_bytes[d - 1]);
166166
/* Only do work if we hit a challenge we haven't tried before. */
167167
if (!e_done[e]) {

0 commit comments

Comments
 (0)