Skip to content

Commit fafb479

Browse files
author
MarcoFalke
committed
contrib: make gen_key_io_test_vectors deterministic
Also, remove instructions which are redundant with the README
1 parent ce33194 commit fafb479

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

contrib/testgen/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Utilities to generate test vectors for the data-driven Bitcoin tests.
44

5-
Usage:
5+
To use inside a scripted-diff (or just execute directly):
66

77
./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
88
./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json

contrib/testgen/gen_key_io_test_vectors.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
'''
66
Generate valid and invalid base58/bech32(m) address and private key test vectors.
7-
8-
Usage:
9-
./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
10-
./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
117
'''
128

139
from itertools import islice
@@ -131,7 +127,7 @@ def is_valid_bech32(v):
131127
def gen_valid_base58_vector(template):
132128
'''Generate valid base58 vector'''
133129
prefix = bytearray(template[0])
134-
payload = bytearray(os.urandom(template[1]))
130+
payload = rand_bytes(size=template[1])
135131
suffix = bytearray(template[2])
136132
dst_prefix = bytearray(template[4])
137133
dst_suffix = bytearray(template[5])
@@ -143,7 +139,7 @@ def gen_valid_bech32_vector(template):
143139
'''Generate valid bech32 vector'''
144140
hrp = template[0]
145141
witver = template[1]
146-
witprog = bytearray(os.urandom(template[2]))
142+
witprog = rand_bytes(size=template[2])
147143
encoding = template[4]
148144
dst_prefix = bytearray(template[5])
149145
rv = bech32_encode(encoding, hrp, [witver] + convertbits(witprog, 8, 5))
@@ -173,17 +169,17 @@ def gen_invalid_base58_vector(template):
173169
corrupt_suffix = randbool(0.2)
174170

175171
if corrupt_prefix:
176-
prefix = os.urandom(1)
172+
prefix = rand_bytes(size=1)
177173
else:
178174
prefix = bytearray(template[0])
179175

180176
if randomize_payload_size:
181-
payload = os.urandom(max(int(random.expovariate(0.5)), 50))
177+
payload = rand_bytes(size=max(int(random.expovariate(0.5)), 50))
182178
else:
183-
payload = os.urandom(template[1])
179+
payload = rand_bytes(size=template[1])
184180

185181
if corrupt_suffix:
186-
suffix = os.urandom(len(template[2]))
182+
suffix = rand_bytes(size=len(template[2]))
187183
else:
188184
suffix = bytearray(template[2])
189185

@@ -204,7 +200,7 @@ def gen_invalid_bech32_vector(template):
204200
to_upper = randbool(0.1)
205201
hrp = template[0]
206202
witver = template[1]
207-
witprog = bytearray(os.urandom(template[2]))
203+
witprog = rand_bytes(size=template[2])
208204
encoding = template[3]
209205

210206
if no_data:
@@ -234,6 +230,9 @@ def randbool(p = 0.5):
234230
'''Return True with P(p)'''
235231
return random.random() < p
236232

233+
def rand_bytes(*, size):
234+
return bytearray(random.getrandbits(8) for _ in range(size))
235+
237236
def gen_invalid_vectors():
238237
'''Generate invalid test vectors'''
239238
# start with some manual edge-cases
@@ -250,6 +249,7 @@ def gen_invalid_vectors():
250249
if __name__ == '__main__':
251250
import json
252251
iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors}
252+
random.seed(42)
253253
try:
254254
uiter = iters[sys.argv[1]]
255255
except IndexError:

0 commit comments

Comments
 (0)