contrib: make gen_key_io_test_vectors deterministic

Also, remove instructions which are redundant with the README
pull/21279/head
MarcoFalke 4 years ago
parent ce33194ea0
commit fafb4796d3
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548

@ -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

@ -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
@ -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])
@ -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))
@ -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])
@ -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:
@ -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
@ -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:

Loading…
Cancel
Save