Changeset View
Changeset View
Standalone View
Standalone View
contrib/testgen/gen_base58_test_vectors.py
#!/usr/bin/env python | #!/usr/bin/env python3 | ||||
# Copyright (c) 2012-2016 The Bitcoin Core developers | # Copyright (c) 2012-2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# 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 address and private key test vectors. | Generate valid and invalid base58 address and private key test vectors. | ||||
Usage: | Usage: | ||||
gen_base58_test_vectors.py valid 50 > ../../src/test/data/base58_keys_valid.json | gen_base58_test_vectors.py valid 50 > ../../src/test/data/base58_keys_valid.json | ||||
gen_base58_test_vectors.py invalid 50 > ../../src/test/data/base58_keys_invalid.json | gen_base58_test_vectors.py invalid 50 > ../../src/test/data/base58_keys_invalid.json | ||||
''' | ''' | ||||
# 2012 Wladimir J. van der Laan | # 2012 Wladimir J. van der Laan | ||||
# Released under MIT License | # Released under MIT License | ||||
import os | import os | ||||
from itertools import islice | from itertools import islice | ||||
from base58 import b58encode_chk, b58decode_chk, b58chars | from base58 import b58encode_chk, b58decode_chk, b58chars | ||||
Show All 25 Lines | |||||
def is_valid(v): | def is_valid(v): | ||||
'''Check vector v for validity''' | '''Check vector v for validity''' | ||||
result = b58decode_chk(v) | result = b58decode_chk(v) | ||||
if result is None: | if result is None: | ||||
return False | return False | ||||
for template in templates: | for template in templates: | ||||
prefix = str(bytearray(template[0])) | prefix = bytearray(template[0]) | ||||
suffix = str(bytearray(template[2])) | suffix = bytearray(template[2]) | ||||
if result.startswith(prefix) and result.endswith(suffix): | if result.startswith(prefix) and result.endswith(suffix): | ||||
if (len(result) - len(prefix) - len(suffix)) == template[1]: | if (len(result) - len(prefix) - len(suffix)) == template[1]: | ||||
return True | return True | ||||
return False | return False | ||||
def gen_valid_vectors(): | def gen_valid_vectors(): | ||||
'''Generate valid test vectors''' | '''Generate valid test vectors''' | ||||
while True: | while True: | ||||
for template in templates: | for template in templates: | ||||
prefix = str(bytearray(template[0])) | prefix = bytearray(template[0]) | ||||
payload = os.urandom(template[1]) | payload = bytearray(os.urandom(template[1])) | ||||
suffix = str(bytearray(template[2])) | suffix = bytearray(template[2]) | ||||
rv = b58encode_chk(prefix + payload + suffix) | rv = b58encode_chk(prefix + payload + suffix) | ||||
assert is_valid(rv) | assert is_valid(rv) | ||||
metadata = dict([(x, y) for (x, y) in zip( | metadata = {x: y for x, y in zip( | ||||
metadata_keys, template[3]) if y is not None]) | metadata_keys, template[3]) if y is not None} | ||||
yield (rv, b2a_hex(payload), metadata) | hexrepr = b2a_hex(payload) | ||||
if isinstance(hexrepr, bytes): | |||||
hexrepr = hexrepr.decode('utf8') | |||||
yield (rv, hexrepr, metadata) | |||||
def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt_suffix): | def gen_invalid_vector(template, corrupt_prefix, randomize_payload_size, corrupt_suffix): | ||||
'''Generate possibly invalid vector''' | '''Generate possibly invalid vector''' | ||||
if corrupt_prefix: | if corrupt_prefix: | ||||
prefix = os.urandom(1) | prefix = os.urandom(1) | ||||
else: | else: | ||||
prefix = str(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 = os.urandom(max(int(random.expovariate(0.5)), 50)) | ||||
else: | else: | ||||
payload = os.urandom(template[1]) | payload = os.urandom(template[1]) | ||||
if corrupt_suffix: | if corrupt_suffix: | ||||
suffix = os.urandom(len(template[2])) | suffix = os.urandom(len(template[2])) | ||||
else: | else: | ||||
suffix = str(bytearray(template[2])) | suffix = bytearray(template[2]) | ||||
return b58encode_chk(prefix + payload + suffix) | return b58encode_chk(prefix + payload + suffix) | ||||
def randbool(p=0.5): | def randbool(p=0.5): | ||||
'''Return True with P(p)''' | '''Return True with P(p)''' | ||||
return random.random() < p | return random.random() < p | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |