Skip to content

Commit

Permalink
contrib: make gen_key_io_test_vectors deterministic
Browse files Browse the repository at this point in the history
Also, remove instructions which are redundant with the README
  • Loading branch information
MarcoFalke committed Apr 6, 2022
1 parent ce33194 commit fafb479
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion contrib/testgen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
22 changes: 11 additions & 11 deletions contrib/testgen/gen_key_io_test_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
'''
Generate valid and invalid base58/bech32(m) address and private key test vectors.
Usage:
./gen_key_io_test_vectors.py valid 70 > ../../src/test/data/key_io_valid.json
./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json
'''

from itertools import islice
Expand Down Expand Up @@ -131,7 +127,7 @@ def is_valid_bech32(v):
def gen_valid_base58_vector(template):
'''Generate valid base58 vector'''
prefix = bytearray(template[0])
payload = bytearray(os.urandom(template[1]))
payload = rand_bytes(size=template[1])
suffix = bytearray(template[2])
dst_prefix = bytearray(template[4])
dst_suffix = bytearray(template[5])
Expand All @@ -143,7 +139,7 @@ def gen_valid_bech32_vector(template):
'''Generate valid bech32 vector'''
hrp = template[0]
witver = template[1]
witprog = bytearray(os.urandom(template[2]))
witprog = rand_bytes(size=template[2])
encoding = template[4]
dst_prefix = bytearray(template[5])
rv = bech32_encode(encoding, hrp, [witver] + convertbits(witprog, 8, 5))
Expand Down Expand Up @@ -173,17 +169,17 @@ def gen_invalid_base58_vector(template):
corrupt_suffix = randbool(0.2)

if corrupt_prefix:
prefix = os.urandom(1)
prefix = rand_bytes(size=1)
else:
prefix = bytearray(template[0])

if randomize_payload_size:
payload = os.urandom(max(int(random.expovariate(0.5)), 50))
payload = rand_bytes(size=max(int(random.expovariate(0.5)), 50))
else:
payload = os.urandom(template[1])
payload = rand_bytes(size=template[1])

if corrupt_suffix:
suffix = os.urandom(len(template[2]))
suffix = rand_bytes(size=len(template[2]))
else:
suffix = bytearray(template[2])

Expand All @@ -204,7 +200,7 @@ def gen_invalid_bech32_vector(template):
to_upper = randbool(0.1)
hrp = template[0]
witver = template[1]
witprog = bytearray(os.urandom(template[2]))
witprog = rand_bytes(size=template[2])
encoding = template[3]

if no_data:
Expand Down Expand Up @@ -234,6 +230,9 @@ def randbool(p = 0.5):
'''Return True with P(p)'''
return random.random() < p

def rand_bytes(*, size):
return bytearray(random.getrandbits(8) for _ in range(size))

def gen_invalid_vectors():
'''Generate invalid test vectors'''
# start with some manual edge-cases
Expand All @@ -250,6 +249,7 @@ def gen_invalid_vectors():
if __name__ == '__main__':
import json
iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors}
random.seed(42)
try:
uiter = iters[sys.argv[1]]
except IndexError:
Expand Down

0 comments on commit fafb479

Please sign in to comment.