Skip to content

Commit 5f2efe6

Browse files
committed
secp256k1_testrand_int(2**N) -> secp256k1_testrand_bits(N)
1 parent 09971a3 commit 5f2efe6

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/modules/schnorrsig/tests_impl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -795,18 +795,18 @@ void test_schnorrsig_sign_verify(void) {
795795
/* Flip a few bits in the signature and in the message and check that
796796
* verify and verify_batch (TODO) fail */
797797
size_t sig_idx = secp256k1_testrand_int(N_SIGS);
798-
size_t byte_idx = secp256k1_testrand_int(32);
798+
size_t byte_idx = secp256k1_testrand_bits(5);
799799
unsigned char xorbyte = secp256k1_testrand_int(254)+1;
800800
sig[sig_idx][byte_idx] ^= xorbyte;
801801
CHECK(!secp256k1_schnorrsig_verify(ctx, sig[sig_idx], msg[sig_idx], sizeof(msg[sig_idx]), &pk));
802802
sig[sig_idx][byte_idx] ^= xorbyte;
803803

804-
byte_idx = secp256k1_testrand_int(32);
804+
byte_idx = secp256k1_testrand_bits(5);
805805
sig[sig_idx][32+byte_idx] ^= xorbyte;
806806
CHECK(!secp256k1_schnorrsig_verify(ctx, sig[sig_idx], msg[sig_idx], sizeof(msg[sig_idx]), &pk));
807807
sig[sig_idx][32+byte_idx] ^= xorbyte;
808808

809-
byte_idx = secp256k1_testrand_int(32);
809+
byte_idx = secp256k1_testrand_bits(5);
810810
msg[sig_idx][byte_idx] ^= xorbyte;
811811
CHECK(!secp256k1_schnorrsig_verify(ctx, sig[sig_idx], msg[sig_idx], sizeof(msg[sig_idx]), &pk));
812812
msg[sig_idx][byte_idx] ^= xorbyte;

src/testrand_impl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static void secp256k1_testrand256_test(unsigned char *b32) {
109109
}
110110

111111
static void secp256k1_testrand_flip(unsigned char *b, size_t len) {
112-
b[secp256k1_testrand_int(len)] ^= (1 << secp256k1_testrand_int(8));
112+
b[secp256k1_testrand_int(len)] ^= (1 << secp256k1_testrand_bits(3));
113113
}
114114

115115
static void secp256k1_testrand_init(const char* hexseed) {

src/tests.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ void signed30_to_uint16(uint16_t* out, const secp256k1_modinv32_signed30* in) {
790790
void mutate_sign_signed30(secp256k1_modinv32_signed30* x) {
791791
int i;
792792
for (i = 0; i < 16; ++i) {
793-
int pos = secp256k1_testrand_int(8);
793+
int pos = secp256k1_testrand_bits(3);
794794
if (x->v[pos] > 0 && x->v[pos + 1] <= 0x3fffffff) {
795795
x->v[pos] -= 0x40000000;
796796
x->v[pos + 1] += 1;
@@ -862,7 +862,7 @@ void mutate_sign_signed62(secp256k1_modinv64_signed62* x) {
862862
static const int64_t M62 = (int64_t)(UINT64_MAX >> 2);
863863
int i;
864864
for (i = 0; i < 8; ++i) {
865-
int pos = secp256k1_testrand_int(4);
865+
int pos = secp256k1_testrand_bits(2);
866866
if (x->v[pos] > 0 && x->v[pos + 1] <= M62) {
867867
x->v[pos] -= (M62 + 1);
868868
x->v[pos + 1] += 1;
@@ -4261,7 +4261,7 @@ void test_secp256k1_pippenger_bucket_window_inv(void) {
42614261
* for a given scratch space.
42624262
*/
42634263
void test_ecmult_multi_pippenger_max_points(void) {
4264-
size_t scratch_size = secp256k1_testrand_int(256);
4264+
size_t scratch_size = secp256k1_testrand_bits(8);
42654265
size_t max_size = secp256k1_pippenger_scratch_size(secp256k1_pippenger_bucket_window_inv(PIPPENGER_MAX_BUCKET_WINDOW-1)+512, 12);
42664266
secp256k1_scratch *scratch;
42674267
size_t n_points_supported;
@@ -6023,14 +6023,14 @@ static void random_ber_signature(unsigned char *sig, size_t *len, int* certainly
60236023
/* We generate two classes of numbers: nlow==1 "low" ones (up to 32 bytes), nlow==0 "high" ones (32 bytes with 129 top bits set, or larger than 32 bytes) */
60246024
nlow[n] = der ? 1 : (secp256k1_testrand_bits(3) != 0);
60256025
/* The length of the number in bytes (the first byte of which will always be nonzero) */
6026-
nlen[n] = nlow[n] ? secp256k1_testrand_int(33) : 32 + secp256k1_testrand_int(200) * secp256k1_testrand_int(8) / 8;
6026+
nlen[n] = nlow[n] ? secp256k1_testrand_int(33) : 32 + secp256k1_testrand_int(200) * secp256k1_testrand_bits(3) / 8;
60276027
CHECK(nlen[n] <= 232);
60286028
/* The top bit of the number. */
60296029
nhbit[n] = (nlow[n] == 0 && nlen[n] == 32) ? 1 : (nlen[n] == 0 ? 0 : secp256k1_testrand_bits(1));
60306030
/* The top byte of the number (after the potential hardcoded 16 0xFF characters for "high" 32 bytes numbers) */
60316031
nhbyte[n] = nlen[n] == 0 ? 0 : (nhbit[n] ? 128 + secp256k1_testrand_bits(7) : 1 + secp256k1_testrand_int(127));
60326032
/* The number of zero bytes in front of the number (which is 0 or 1 in case of DER, otherwise we extend up to 300 bytes) */
6033-
nzlen[n] = der ? ((nlen[n] == 0 || nhbit[n]) ? 1 : 0) : (nlow[n] ? secp256k1_testrand_int(3) : secp256k1_testrand_int(300 - nlen[n]) * secp256k1_testrand_int(8) / 8);
6033+
nzlen[n] = der ? ((nlen[n] == 0 || nhbit[n]) ? 1 : 0) : (nlow[n] ? secp256k1_testrand_int(3) : secp256k1_testrand_int(300 - nlen[n]) * secp256k1_testrand_bits(3) / 8);
60346034
if (nzlen[n] > ((nlen[n] == 0 || nhbit[n]) ? 1 : 0)) {
60356035
*certainly_not_der = 1;
60366036
}
@@ -6039,7 +6039,7 @@ static void random_ber_signature(unsigned char *sig, size_t *len, int* certainly
60396039
nlenlen[n] = nlen[n] + nzlen[n] < 128 ? 0 : (nlen[n] + nzlen[n] < 256 ? 1 : 2);
60406040
if (!der) {
60416041
/* nlenlen[n] max 127 bytes */
6042-
int add = secp256k1_testrand_int(127 - nlenlen[n]) * secp256k1_testrand_int(16) * secp256k1_testrand_int(16) / 256;
6042+
int add = secp256k1_testrand_int(127 - nlenlen[n]) * secp256k1_testrand_bits(4) * secp256k1_testrand_bits(4) / 256;
60436043
nlenlen[n] += add;
60446044
if (add != 0) {
60456045
*certainly_not_der = 1;
@@ -6053,15 +6053,15 @@ static void random_ber_signature(unsigned char *sig, size_t *len, int* certainly
60536053
CHECK(tlen <= 856);
60546054

60556055
/* The length of the garbage inside the tuple. */
6056-
elen = (der || indet) ? 0 : secp256k1_testrand_int(980 - tlen) * secp256k1_testrand_int(8) / 8;
6056+
elen = (der || indet) ? 0 : secp256k1_testrand_int(980 - tlen) * secp256k1_testrand_bits(3) / 8;
60576057
if (elen != 0) {
60586058
*certainly_not_der = 1;
60596059
}
60606060
tlen += elen;
60616061
CHECK(tlen <= 980);
60626062

60636063
/* The length of the garbage after the end of the tuple. */
6064-
glen = der ? 0 : secp256k1_testrand_int(990 - tlen) * secp256k1_testrand_int(8) / 8;
6064+
glen = der ? 0 : secp256k1_testrand_int(990 - tlen) * secp256k1_testrand_bits(3) / 8;
60656065
if (glen != 0) {
60666066
*certainly_not_der = 1;
60676067
}
@@ -6076,7 +6076,7 @@ static void random_ber_signature(unsigned char *sig, size_t *len, int* certainly
60766076
} else {
60776077
int tlenlen = tlen < 128 ? 0 : (tlen < 256 ? 1 : 2);
60786078
if (!der) {
6079-
int add = secp256k1_testrand_int(127 - tlenlen) * secp256k1_testrand_int(16) * secp256k1_testrand_int(16) / 256;
6079+
int add = secp256k1_testrand_int(127 - tlenlen) * secp256k1_testrand_bits(4) * secp256k1_testrand_bits(4) / 256;
60806080
tlenlen += add;
60816081
if (add != 0) {
60826082
*certainly_not_der = 1;

0 commit comments

Comments
 (0)