contrib: make gen_key_io_test_vectors deterministic

Also, remove instructions which are redundant with the README
This commit is contained in:
MarcoFalke 2021-02-23 16:41:43 +01:00
parent ce33194ea0
commit fafb4796d3
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
2 changed files with 12 additions and 12 deletions

View file

@ -2,7 +2,7 @@
Utilities to generate test vectors for the data-driven Bitcoin tests. 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 valid 70 > ../../src/test/data/key_io_valid.json
./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json ./gen_key_io_test_vectors.py invalid 70 > ../../src/test/data/key_io_invalid.json

View file

@ -4,10 +4,6 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php. # file COPYING or http://www.opensource.org/licenses/mit-license.php.
''' '''
Generate valid and invalid base58/bech32(m) address and private key test vectors. 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 from itertools import islice
@ -131,7 +127,7 @@ def is_valid_bech32(v):
def gen_valid_base58_vector(template): def gen_valid_base58_vector(template):
'''Generate valid base58 vector''' '''Generate valid base58 vector'''
prefix = bytearray(template[0]) prefix = bytearray(template[0])
payload = bytearray(os.urandom(template[1])) payload = rand_bytes(size=template[1])
suffix = bytearray(template[2]) suffix = bytearray(template[2])
dst_prefix = bytearray(template[4]) dst_prefix = bytearray(template[4])
dst_suffix = bytearray(template[5]) dst_suffix = bytearray(template[5])
@ -143,7 +139,7 @@ def gen_valid_bech32_vector(template):
'''Generate valid bech32 vector''' '''Generate valid bech32 vector'''
hrp = template[0] hrp = template[0]
witver = template[1] witver = template[1]
witprog = bytearray(os.urandom(template[2])) witprog = rand_bytes(size=template[2])
encoding = template[4] encoding = template[4]
dst_prefix = bytearray(template[5]) dst_prefix = bytearray(template[5])
rv = bech32_encode(encoding, hrp, [witver] + convertbits(witprog, 8, 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) corrupt_suffix = randbool(0.2)
if corrupt_prefix: if corrupt_prefix:
prefix = os.urandom(1) prefix = rand_bytes(size=1)
else: else:
prefix = bytearray(template[0]) prefix = bytearray(template[0])
if randomize_payload_size: 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: else:
payload = os.urandom(template[1]) payload = rand_bytes(size=template[1])
if corrupt_suffix: if corrupt_suffix:
suffix = os.urandom(len(template[2])) suffix = rand_bytes(size=len(template[2]))
else: else:
suffix = bytearray(template[2]) suffix = bytearray(template[2])
@ -204,7 +200,7 @@ def gen_invalid_bech32_vector(template):
to_upper = randbool(0.1) to_upper = randbool(0.1)
hrp = template[0] hrp = template[0]
witver = template[1] witver = template[1]
witprog = bytearray(os.urandom(template[2])) witprog = rand_bytes(size=template[2])
encoding = template[3] encoding = template[3]
if no_data: if no_data:
@ -234,6 +230,9 @@ def randbool(p = 0.5):
'''Return True with P(p)''' '''Return True with P(p)'''
return random.random() < p return random.random() < p
def rand_bytes(*, size):
return bytearray(random.getrandbits(8) for _ in range(size))
def gen_invalid_vectors(): def gen_invalid_vectors():
'''Generate invalid test vectors''' '''Generate invalid test vectors'''
# start with some manual edge-cases # start with some manual edge-cases
@ -250,6 +249,7 @@ def gen_invalid_vectors():
if __name__ == '__main__': if __name__ == '__main__':
import json import json
iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors} iters = {'valid':gen_valid_vectors, 'invalid':gen_invalid_vectors}
random.seed(42)
try: try:
uiter = iters[sys.argv[1]] uiter = iters[sys.argv[1]]
except IndexError: except IndexError: