Skip to content

Commit 63150ab

Browse files
Merge #827: Rename testrand functions to have test in name
a45c1fa Rename testrand functions to have test in name (Pieter Wuille) Pull request description: Suggested here: #808 (comment) ACKs for top commit: real-or-random: ACK a45c1fa diff looks good elichai: utACK a45c1fa Tree-SHA512: a15c29b88877e0f1a099acab90cbfa1e70420527e07348a69c8a5b539319a3131b771b86852e772a669a1eb3475d508d0f7e10f37eec363dc6640d4eaf967536
2 parents c5257ae + a45c1fa commit 63150ab

File tree

8 files changed

+140
-140
lines changed

8 files changed

+140
-140
lines changed

src/modules/extrakeys/tests_impl.h

+17-17
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ void test_xonly_pubkey(void) {
3535
secp256k1_context *sign = api_test_context(SECP256K1_CONTEXT_SIGN, &ecount);
3636
secp256k1_context *verify = api_test_context(SECP256K1_CONTEXT_VERIFY, &ecount);
3737

38-
secp256k1_rand256(sk);
38+
secp256k1_testrand256(sk);
3939
memset(ones32, 0xFF, 32);
40-
secp256k1_rand256(xy_sk);
40+
secp256k1_testrand256(xy_sk);
4141
CHECK(secp256k1_ec_pubkey_create(sign, &pk, sk) == 1);
4242
CHECK(secp256k1_xonly_pubkey_from_pubkey(none, &xonly_pk, &pk_parity, &pk) == 1);
4343

@@ -120,7 +120,7 @@ void test_xonly_pubkey(void) {
120120
* the curve) then xonly_pubkey_parse should fail as well. */
121121
for (i = 0; i < count; i++) {
122122
unsigned char rand33[33];
123-
secp256k1_rand256(&rand33[1]);
123+
secp256k1_testrand256(&rand33[1]);
124124
rand33[0] = SECP256K1_TAG_PUBKEY_EVEN;
125125
if (!secp256k1_ec_pubkey_parse(ctx, &pk, rand33, 33)) {
126126
memset(&xonly_pk, 1, sizeof(xonly_pk));
@@ -154,8 +154,8 @@ void test_xonly_pubkey_tweak(void) {
154154
secp256k1_context *verify = api_test_context(SECP256K1_CONTEXT_VERIFY, &ecount);
155155

156156
memset(overflows, 0xff, sizeof(overflows));
157-
secp256k1_rand256(tweak);
158-
secp256k1_rand256(sk);
157+
secp256k1_testrand256(tweak);
158+
secp256k1_testrand256(sk);
159159
CHECK(secp256k1_ec_pubkey_create(ctx, &internal_pk, sk) == 1);
160160
CHECK(secp256k1_xonly_pubkey_from_pubkey(none, &internal_xonly_pk, &pk_parity, &internal_pk) == 1);
161161

@@ -198,7 +198,7 @@ void test_xonly_pubkey_tweak(void) {
198198

199199
/* Invalid pk with a valid tweak */
200200
memset(&internal_xonly_pk, 0, sizeof(internal_xonly_pk));
201-
secp256k1_rand256(tweak);
201+
secp256k1_testrand256(tweak);
202202
ecount = 0;
203203
CHECK(secp256k1_xonly_pubkey_tweak_add(verify, &output_pk, &internal_xonly_pk, tweak) == 0);
204204
CHECK(ecount == 1);
@@ -228,8 +228,8 @@ void test_xonly_pubkey_tweak_check(void) {
228228
secp256k1_context *verify = api_test_context(SECP256K1_CONTEXT_VERIFY, &ecount);
229229

230230
memset(overflows, 0xff, sizeof(overflows));
231-
secp256k1_rand256(tweak);
232-
secp256k1_rand256(sk);
231+
secp256k1_testrand256(tweak);
232+
secp256k1_testrand256(sk);
233233
CHECK(secp256k1_ec_pubkey_create(ctx, &internal_pk, sk) == 1);
234234
CHECK(secp256k1_xonly_pubkey_from_pubkey(none, &internal_xonly_pk, &pk_parity, &internal_pk) == 1);
235235

@@ -287,7 +287,7 @@ void test_xonly_pubkey_tweak_recursive(void) {
287287
unsigned char tweak[N_PUBKEYS - 1][32];
288288
int i;
289289

290-
secp256k1_rand256(sk);
290+
secp256k1_testrand256(sk);
291291
CHECK(secp256k1_ec_pubkey_create(ctx, &pk[0], sk) == 1);
292292
/* Add tweaks */
293293
for (i = 0; i < N_PUBKEYS - 1; i++) {
@@ -327,7 +327,7 @@ void test_keypair(void) {
327327

328328
/* Test keypair_create */
329329
ecount = 0;
330-
secp256k1_rand256(sk);
330+
secp256k1_testrand256(sk);
331331
CHECK(secp256k1_keypair_create(none, &keypair, sk) == 0);
332332
CHECK(memcmp(zeros96, &keypair, sizeof(keypair)) == 0);
333333
CHECK(ecount == 1);
@@ -349,7 +349,7 @@ void test_keypair(void) {
349349

350350
/* Test keypair_pub */
351351
ecount = 0;
352-
secp256k1_rand256(sk);
352+
secp256k1_testrand256(sk);
353353
CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1);
354354
CHECK(secp256k1_keypair_pub(none, &pk, &keypair) == 1);
355355
CHECK(secp256k1_keypair_pub(none, NULL, &keypair) == 0);
@@ -371,7 +371,7 @@ void test_keypair(void) {
371371

372372
/** Test keypair_xonly_pub **/
373373
ecount = 0;
374-
secp256k1_rand256(sk);
374+
secp256k1_testrand256(sk);
375375
CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1);
376376
CHECK(secp256k1_keypair_xonly_pub(none, &xonly_pk, &pk_parity, &keypair) == 1);
377377
CHECK(secp256k1_keypair_xonly_pub(none, NULL, &pk_parity, &keypair) == 0);
@@ -414,8 +414,8 @@ void test_keypair_add(void) {
414414
secp256k1_context *verify = api_test_context(SECP256K1_CONTEXT_VERIFY, &ecount);
415415

416416
CHECK(sizeof(zeros96) == sizeof(keypair));
417-
secp256k1_rand256(sk);
418-
secp256k1_rand256(tweak);
417+
secp256k1_testrand256(sk);
418+
secp256k1_testrand256(tweak);
419419
memset(overflows, 0xFF, 32);
420420
CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1);
421421

@@ -444,7 +444,7 @@ void test_keypair_add(void) {
444444
for (i = 0; i < count; i++) {
445445
secp256k1_scalar scalar_tweak;
446446
secp256k1_keypair keypair_tmp;
447-
secp256k1_rand256(sk);
447+
secp256k1_testrand256(sk);
448448
CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1);
449449
memcpy(&keypair_tmp, &keypair, sizeof(keypair));
450450
/* Because sk may be negated before adding, we need to try with tweak =
@@ -460,7 +460,7 @@ void test_keypair_add(void) {
460460

461461
/* Invalid keypair with a valid tweak */
462462
memset(&keypair, 0, sizeof(keypair));
463-
secp256k1_rand256(tweak);
463+
secp256k1_testrand256(tweak);
464464
ecount = 0;
465465
CHECK(secp256k1_keypair_xonly_tweak_add(verify, &keypair, tweak) == 0);
466466
CHECK(ecount == 1);
@@ -486,7 +486,7 @@ void test_keypair_add(void) {
486486
unsigned char pk32[32];
487487
int pk_parity;
488488

489-
secp256k1_rand256(tweak);
489+
secp256k1_testrand256(tweak);
490490
CHECK(secp256k1_keypair_xonly_pub(ctx, &internal_pk, NULL, &keypair) == 1);
491491
CHECK(secp256k1_keypair_xonly_tweak_add(ctx, &keypair, tweak) == 1);
492492
CHECK(secp256k1_keypair_xonly_pub(ctx, &output_pk, &pk_parity, &keypair) == 1);

src/modules/recovery/tests_impl.h

+2-2
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_rand_bits(1);
28+
return secp256k1_testrand_bits(1);
2929
}
3030

3131
void test_ecdsa_recovery_api(void) {
@@ -196,7 +196,7 @@ void test_ecdsa_recovery_end_to_end(void) {
196196
CHECK(memcmp(&pubkey, &recpubkey, sizeof(pubkey)) == 0);
197197
/* Serialize/destroy/parse signature and verify again. */
198198
CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &rsignature[4]) == 1);
199-
sig[secp256k1_rand_bits(6)] += 1 + secp256k1_rand_int(255);
199+
sig[secp256k1_testrand_bits(6)] += 1 + secp256k1_testrand_int(255);
200200
CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(ctx, &rsignature[4], sig, recid) == 1);
201201
CHECK(secp256k1_ecdsa_recoverable_signature_convert(ctx, &signature[4], &rsignature[4]) == 1);
202202
CHECK(secp256k1_ecdsa_verify(ctx, &signature[4], message, &pubkey) == 0);

src/modules/schnorrsig/tests_exhaustive_impl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static void test_exhaustive_schnorrsig_verify(const secp256k1_context *ctx, cons
100100
while (e_count_done < EXHAUSTIVE_TEST_ORDER) {
101101
secp256k1_scalar e;
102102
unsigned char msg32[32];
103-
secp256k1_rand256(msg32);
103+
secp256k1_testrand256(msg32);
104104
secp256k1_schnorrsig_challenge(&e, sig64, msg32, pk32);
105105
/* Only do work if we hit a challenge we haven't tried before. */
106106
if (!e_done[e]) {
@@ -116,7 +116,7 @@ static void test_exhaustive_schnorrsig_verify(const secp256k1_context *ctx, cons
116116
expect_valid = actual_k != -1 && s != EXHAUSTIVE_TEST_ORDER &&
117117
(s_s == (actual_k + actual_d * e) % EXHAUSTIVE_TEST_ORDER);
118118
} else {
119-
secp256k1_rand256(sig64 + 32);
119+
secp256k1_testrand256(sig64 + 32);
120120
expect_valid = 0;
121121
}
122122
valid = secp256k1_schnorrsig_verify(ctx, sig64, msg32, &pubkeys[d - 1]);
@@ -153,7 +153,7 @@ static void test_exhaustive_schnorrsig_sign(const secp256k1_context *ctx, unsign
153153
/* Generate random messages until all challenges have been tried. */
154154
while (e_count_done < EXHAUSTIVE_TEST_ORDER) {
155155
secp256k1_scalar e;
156-
secp256k1_rand256(msg32);
156+
secp256k1_testrand256(msg32);
157157
secp256k1_schnorrsig_challenge(&e, xonly_pubkey_bytes[k - 1], msg32, xonly_pubkey_bytes[d - 1]);
158158
/* Only do work if we hit a challenge we haven't tried before. */
159159
if (!e_done[e]) {

src/modules/schnorrsig/tests_impl.h

+19-19
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, size_t n_bytes) {
1616
unsigned char nonces[2][32];
1717
CHECK(nonce_function_bip340(nonces[0], args[0], args[1], args[2], args[3], args[4]) == 1);
18-
secp256k1_rand_flip(args[n_flip], n_bytes);
18+
secp256k1_testrand_flip(args[n_flip], n_bytes);
1919
CHECK(nonce_function_bip340(nonces[1], args[0], args[1], args[2], args[3], args[4]) == 1);
2020
CHECK(memcmp(nonces[0], nonces[1], 32) != 0);
2121
}
@@ -59,10 +59,10 @@ void run_nonce_function_bip340_tests(void) {
5959
secp256k1_nonce_function_bip340_sha256_tagged_aux(&sha_optimized);
6060
test_sha256_eq(&sha, &sha_optimized);
6161

62-
secp256k1_rand256(msg);
63-
secp256k1_rand256(key);
64-
secp256k1_rand256(pk);
65-
secp256k1_rand256(aux_rand);
62+
secp256k1_testrand256(msg);
63+
secp256k1_testrand256(key);
64+
secp256k1_testrand256(pk);
65+
secp256k1_testrand256(aux_rand);
6666

6767
/* Check that a bitflip in an argument results in different nonces. */
6868
args[0] = msg;
@@ -124,10 +124,10 @@ void test_schnorrsig_api(void) {
124124
secp256k1_context_set_illegal_callback(vrfy, counting_illegal_callback_fn, &ecount);
125125
secp256k1_context_set_illegal_callback(both, counting_illegal_callback_fn, &ecount);
126126

127-
secp256k1_rand256(sk1);
128-
secp256k1_rand256(sk2);
129-
secp256k1_rand256(sk3);
130-
secp256k1_rand256(msg);
127+
secp256k1_testrand256(sk1);
128+
secp256k1_testrand256(sk2);
129+
secp256k1_testrand256(sk3);
130+
secp256k1_testrand256(msg);
131131
CHECK(secp256k1_keypair_create(ctx, &keypairs[0], sk1) == 1);
132132
CHECK(secp256k1_keypair_create(ctx, &keypairs[1], sk2) == 1);
133133
CHECK(secp256k1_keypair_create(ctx, &keypairs[2], sk3) == 1);
@@ -675,7 +675,7 @@ void test_schnorrsig_sign(void) {
675675
unsigned char sig[64];
676676
unsigned char zeros64[64] = { 0 };
677677

678-
secp256k1_rand256(sk);
678+
secp256k1_testrand256(sk);
679679
CHECK(secp256k1_keypair_create(ctx, &keypair, sk));
680680
CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, NULL) == 1);
681681

@@ -703,32 +703,32 @@ void test_schnorrsig_sign_verify(void) {
703703
secp256k1_xonly_pubkey pk;
704704
secp256k1_scalar s;
705705

706-
secp256k1_rand256(sk);
706+
secp256k1_testrand256(sk);
707707
CHECK(secp256k1_keypair_create(ctx, &keypair, sk));
708708
CHECK(secp256k1_keypair_xonly_pub(ctx, &pk, NULL, &keypair));
709709

710710
for (i = 0; i < N_SIGS; i++) {
711-
secp256k1_rand256(msg[i]);
711+
secp256k1_testrand256(msg[i]);
712712
CHECK(secp256k1_schnorrsig_sign(ctx, sig[i], msg[i], &keypair, NULL, NULL));
713713
CHECK(secp256k1_schnorrsig_verify(ctx, sig[i], msg[i], &pk));
714714
}
715715

716716
{
717717
/* Flip a few bits in the signature and in the message and check that
718718
* verify and verify_batch (TODO) fail */
719-
size_t sig_idx = secp256k1_rand_int(N_SIGS);
720-
size_t byte_idx = secp256k1_rand_int(32);
721-
unsigned char xorbyte = secp256k1_rand_int(254)+1;
719+
size_t sig_idx = secp256k1_testrand_int(N_SIGS);
720+
size_t byte_idx = secp256k1_testrand_int(32);
721+
unsigned char xorbyte = secp256k1_testrand_int(254)+1;
722722
sig[sig_idx][byte_idx] ^= xorbyte;
723723
CHECK(!secp256k1_schnorrsig_verify(ctx, sig[sig_idx], msg[sig_idx], &pk));
724724
sig[sig_idx][byte_idx] ^= xorbyte;
725725

726-
byte_idx = secp256k1_rand_int(32);
726+
byte_idx = secp256k1_testrand_int(32);
727727
sig[sig_idx][32+byte_idx] ^= xorbyte;
728728
CHECK(!secp256k1_schnorrsig_verify(ctx, sig[sig_idx], msg[sig_idx], &pk));
729729
sig[sig_idx][32+byte_idx] ^= xorbyte;
730730

731-
byte_idx = secp256k1_rand_int(32);
731+
byte_idx = secp256k1_testrand_int(32);
732732
msg[sig_idx][byte_idx] ^= xorbyte;
733733
CHECK(!secp256k1_schnorrsig_verify(ctx, sig[sig_idx], msg[sig_idx], &pk));
734734
msg[sig_idx][byte_idx] ^= xorbyte;
@@ -766,7 +766,7 @@ void test_schnorrsig_taproot(void) {
766766
unsigned char sig[64];
767767

768768
/* Create output key */
769-
secp256k1_rand256(sk);
769+
secp256k1_testrand256(sk);
770770
CHECK(secp256k1_keypair_create(ctx, &keypair, sk) == 1);
771771
CHECK(secp256k1_keypair_xonly_pub(ctx, &internal_pk, NULL, &keypair) == 1);
772772
/* In actual taproot the tweak would be hash of internal_pk */
@@ -776,7 +776,7 @@ void test_schnorrsig_taproot(void) {
776776
CHECK(secp256k1_xonly_pubkey_serialize(ctx, output_pk_bytes, &output_pk) == 1);
777777

778778
/* Key spend */
779-
secp256k1_rand256(msg);
779+
secp256k1_testrand256(msg);
780780
CHECK(secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, NULL) == 1);
781781
/* Verify key spend */
782782
CHECK(secp256k1_xonly_pubkey_parse(ctx, &output_pk, output_pk_bytes) == 1);

src/testrand.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,34 @@
1414
/* A non-cryptographic RNG used only for test infrastructure. */
1515

1616
/** Seed the pseudorandom number generator for testing. */
17-
SECP256K1_INLINE static void secp256k1_rand_seed(const unsigned char *seed16);
17+
SECP256K1_INLINE static void secp256k1_testrand_seed(const unsigned char *seed16);
1818

1919
/** Generate a pseudorandom number in the range [0..2**32-1]. */
20-
static uint32_t secp256k1_rand32(void);
20+
static uint32_t secp256k1_testrand32(void);
2121

2222
/** Generate a pseudorandom number in the range [0..2**bits-1]. Bits must be 1 or
2323
* more. */
24-
static uint32_t secp256k1_rand_bits(int bits);
24+
static uint32_t secp256k1_testrand_bits(int bits);
2525

2626
/** Generate a pseudorandom number in the range [0..range-1]. */
27-
static uint32_t secp256k1_rand_int(uint32_t range);
27+
static uint32_t secp256k1_testrand_int(uint32_t range);
2828

2929
/** Generate a pseudorandom 32-byte array. */
30-
static void secp256k1_rand256(unsigned char *b32);
30+
static void secp256k1_testrand256(unsigned char *b32);
3131

3232
/** Generate a pseudorandom 32-byte array with long sequences of zero and one bits. */
33-
static void secp256k1_rand256_test(unsigned char *b32);
33+
static void secp256k1_testrand256_test(unsigned char *b32);
3434

3535
/** Generate pseudorandom bytes with long sequences of zero and one bits. */
36-
static void secp256k1_rand_bytes_test(unsigned char *bytes, size_t len);
36+
static void secp256k1_testrand_bytes_test(unsigned char *bytes, size_t len);
3737

3838
/** Flip a single random bit in a byte array */
39-
static void secp256k1_rand_flip(unsigned char *b, size_t len);
39+
static void secp256k1_testrand_flip(unsigned char *b, size_t len);
4040

4141
/** Initialize the test RNG using (hex encoded) array up to 16 bytes, or randomly if hexseed is NULL. */
42-
static void secp256k1_rand_init(const char* hexseed);
42+
static void secp256k1_testrand_init(const char* hexseed);
4343

4444
/** Print final test information. */
45-
static void secp256k1_rand_finish(void);
45+
static void secp256k1_testrand_finish(void);
4646

4747
#endif /* SECP256K1_TESTRAND_H */

0 commit comments

Comments
 (0)