mmgen-wallet/test/gentest.py

591 lines
18 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
#
2024-10-18 10:32:06 +00:00
# MMGen Wallet, a terminal-based cryptocurrency wallet
2025-02-16 14:42:27 +00:00
# Copyright (C)2013-2025 The MMGen Project <mmgen@tuta.io>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
2022-11-14 09:54:07 +00:00
test/gentest.py: Cryptocoin key/address generation tests for the MMGen suite
"""
2024-10-18 10:32:12 +00:00
import sys, os, time
try:
from include import test_init
except ImportError:
from test.include import test_init
# Import these _after_ local path's been added to sys.path
2024-10-18 10:32:12 +00:00
from mmgen.cfg import gc, Config
from mmgen.color import green, red, purple
from mmgen.util import msg, ymsg, capfirst, is_int, die
results_file = 'gentest.out.json'
rounds = 100
opts_data = {
'text': {
2019-11-06 17:34:26 +00:00
'desc': 'Test key/address generation of the MMGen suite in various ways',
'usage':'[options] <spec> <rounds | dump file>',
'options': """
-h, --help Print this help message
2024-10-08 12:55:58 +00:00
--, --longhelp Print help message for long (global) options
-a, --all-coins Test all coins supported by specified external tool
-k, --use-internal-keccak-module Force use of the internal keccak module
-q, --quiet Produce quieter output
-s, --save-results Save output of external tool in Compare test to
{rf!r}
2024-10-18 10:32:12 +00:00
-t, --type=t Specify address type (e.g. 'compressed', 'segwit',
'zcash_z', 'bech32')
-v, --verbose Produce more verbose output
""",
'notes': """
TEST TYPES:
Compare: {prog} A:B <rounds> (compare address generators A and B)
Speed: {prog} A <rounds> (test speed of generator A)
Dump: {prog} A <dump file> (compare generator A to wallet dump)
where:
A and B are keygen backend numbers ('1' being the default); or
B is the name of an external tool (see below) or 'ext'.
If B is 'ext', the external tool will be chosen automatically.
For the Compare test, A may be 'all' to test all backends for the current
coin/address type combination.
EXAMPLES:
Compare addresses generated by 'libsecp256k1' and 'python-ecdsa' backends,
with 100 random rounds plus private-key edge cases:
$ {prog} 1:2 100
Compare Segwit addresses from default 'libsecp256k1' backend to 'pycoin'
library for all supported coins, 100 rounds + edge cases:
$ {prog} --all-coins --type=segwit 1:pycoin 100
Compare addresses from 'python-ecdsa' backend to output of 'keyconv' tool
for all supported coins, 100 rounds + edge cases:
$ {prog} --all-coins --type=compressed 2:keyconv 100
Compare bech32 addrs from 'libsecp256k1' backend to Bitcoin Core wallet
dump:
$ {prog} --type=bech32 1 bech32wallet.dump
Compare addresses from Monero 'ed25519ll' backend to output of default
external tool, 10 rounds + edge cases:
$ {prog} --coin=xmr 3:ext 10
Test the speed of default Monero 'nacl' backend, 10,000 rounds:
$ test/gentest.py --coin=xmr 1 10000
Same for Zcash:
$ test/gentest.py --coin=zec --type=zcash_z 1 10000
Test all configured Monero backends against the 'monero-python' library, 3 rounds
+ edge cases:
$ test/gentest.py --coin=xmr all:monero-python 3
Test 'nacl' and 'ed25519ll_djbec' backends against each other, 10,000 rounds
+ edge cases:
$ test/gentest.py --coin=xmr 1:2 10000
SUPPORTED EXTERNAL TOOLS:
+ eth-keys (for ETH, ETC)
https://github.com/ethereum/eth-keys
+ ethkey (eth-keys alternative for MSYS2, or if eth-keys is unavailable)
https://github.com/openethereum/openethereum/releases/tag/v3.1.0
+ zcash-mini (for Zcash-Z addresses and view keys)
https://github.com/FiloSottile/zcash-mini
+ monero-python (for Monero addresses and view keys)
https://github.com/monero-ecosystem/monero-python
+ pycoin (for supported coins)
https://github.com/richardkiss/pycoin
+ keyconv (for supported coins)
https://github.com/exploitagency/vanitygen-plus
('keyconv' does not generate Segwit addresses)
"""
},
'code': {
'options': lambda s: s.format(
2024-10-18 10:32:12 +00:00
rf = results_file,
),
'notes': lambda s: s.format(
2024-10-18 10:32:12 +00:00
prog = 'test/gentest.py',
pnm = gc.proj_name,
snum = rounds)
}
}
2024-10-18 10:32:12 +00:00
def get_cmd_output(cmd, input=None):
return run(cmd, input=input, stdout=PIPE, stderr=DEVNULL, text=True).stdout.splitlines()
saved_results = {}
class GenTool:
2024-10-18 10:32:12 +00:00
def __init__(self, proto, addr_type):
self.proto = proto
self.addr_type = addr_type
self.data = {}
def __del__(self):
if cfg.save_results:
key = f'{self.proto.coin}-{self.proto.network}-{self.addr_type.name}-{self.desc}'.lower()
2024-10-18 10:32:12 +00:00
saved_results[key] = {k.hex():v._asdict() for k, v in self.data.items()}
2024-10-18 10:32:12 +00:00
def run_tool(self, sec, cache_data):
vcoin = 'BTC' if self.proto.coin == 'BCH' else self.proto.coin
key = sec.orig_bytes
if key in self.data:
return self.data[key]
else:
2024-10-18 10:32:12 +00:00
ret = self.run(sec, vcoin)
if cache_data:
2024-10-18 10:32:12 +00:00
self.data[key] = sd(**{'reduced':sec.hex()}, **ret._asdict())
return ret
class GenToolEth_keys(GenTool):
desc = 'eth-keys'
2022-05-06 12:52:40 +00:00
2024-10-18 10:32:12 +00:00
def __init__(self, *args, **kwargs):
self.keys = self.cmdname = None
try:
from eth_keys import keys
self.keys = keys
except ImportError:
self.cmdname = get_ethkey()
self.desc = 'ethkey'
if not (self.keys or self.cmdname):
die(2, 'Neither the ‘eth-keys’ package nor the ‘ethkey’ executable '
'could be found on the system!')
2024-10-18 10:32:12 +00:00
super().__init__(*args, **kwargs)
2022-05-06 12:52:40 +00:00
2024-10-18 10:32:12 +00:00
def run(self, sec, vcoin):
if self.keys:
sk = self.keys.PrivateKey(sec)
return gtr(str(sk)[2:], sk.public_key.to_address()[2:], None)
else:
o = get_cmd_output([self.cmdname, 'info', sec.hex()])
return gtr(o[0].split()[1], o[-1].split()[1], None)
class GenToolKeyconv(GenTool):
desc = 'keyconv'
2024-10-18 10:32:12 +00:00
def run(self, sec, vcoin):
o = get_cmd_output(['keyconv', '-C', vcoin, sec.wif])
return gtr(
o[1].split()[1],
o[0].split()[1],
2024-10-18 10:32:12 +00:00
None)
class GenToolZcash_mini(GenTool):
desc = 'zcash-mini'
2024-10-18 10:32:12 +00:00
def run(self, sec, vcoin):
o = get_cmd_output(['zcash-mini', '-key', '-simple'], input=sec.wif+'\n')
2024-10-18 10:32:12 +00:00
return gtr(o[1], o[0], o[-1])
class GenToolPycoin(GenTool):
"""
pycoin/networks/all.py pycoin/networks/legacy_networks.py
"""
desc = 'pycoin'
2024-10-18 10:32:12 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
2018-05-09 14:28:00 +00:00
from pycoin.networks.registry import network_for_netcode
2024-07-06 14:05:53 +00:00
except ImportError as e:
raise ImportError(
2024-07-06 14:05:53 +00:00
f'{e}\nUnable to import pycoin.networks.registry. Is pycoin installed on your system?') from e
self.nfnc = network_for_netcode
2024-10-18 10:32:12 +00:00
def run(self, sec, vcoin):
if self.proto.testnet:
vcoin = cinfo.external_tests['testnet']['pycoin'][vcoin]
network = self.nfnc(vcoin)
key = network.keys.private(
2024-10-18 10:32:12 +00:00
secret_exponent = int(sec.hex(), 16),
is_compressed = self.addr_type.name != 'legacy')
if key is None:
2024-10-18 10:32:12 +00:00
die(1, f'can’t parse {sec.hex()}')
if self.addr_type.name in ('segwit', 'bech32'):
hash160_c = key.hash160(is_compressed=True)
if self.addr_type.name == 'segwit':
p2sh_script = network.contract.for_p2pkh_wit(hash160_c)
addr = network.address.for_p2s(p2sh_script)
else:
addr = network.address.for_p2pkh_wit(hash160_c)
else:
addr = key.address()
2024-10-18 10:32:12 +00:00
return gtr(key.wif(), addr, None)
class GenToolMonero_python(GenTool):
desc = 'monero-python'
2024-10-18 10:32:12 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
try:
from monero.seed import Seed
2024-07-06 14:05:53 +00:00
except ImportError as e:
raise ImportError(
2024-07-06 14:05:53 +00:00
f'{e}\nUnable to import monero-python. Is monero-python installed on your system?') from e
self.Seed = Seed
2024-10-18 10:32:12 +00:00
def run(self, sec, vcoin):
seed = self.Seed(sec.orig_bytes.hex())
sk = seed.secret_spend_key()
vk = seed.secret_view_key()
addr = seed.public_address()
2024-10-18 10:32:12 +00:00
return gtr(sk, addr, vk)
2024-10-18 10:32:12 +00:00
def find_or_check_tool(proto, addr_type, toolname):
ext_progs = list(cinfo.external_tests[proto.network])
if toolname not in ext_progs + ['ext']:
2024-10-18 10:32:12 +00:00
die(1, f'{toolname!r}: unsupported tool for network {proto.network}')
if cfg.all_coins and toolname == 'ext':
2024-10-18 10:32:12 +00:00
die(1, "'--all-coins' must be combined with a specific external testing tool")
else:
tool = cinfo.get_test_support(
proto.coin,
addr_type.name,
proto.network,
verbose = not cfg.quiet,
2024-10-18 10:32:12 +00:00
toolname = toolname if toolname != 'ext' else None)
if tool and toolname in ext_progs and toolname != tool:
sys.exit(3)
if tool is None:
return None
return tool
2024-10-18 10:32:12 +00:00
def test_equal(desc, a_val, b_val, in_bytes, sec, wif, a_desc, b_desc):
if a_val != b_val:
fs = """
{i:{w}}: {}
{s:{w}}: {}
{W:{w}}: {}
{a:{w}}: {}
{b:{w}}: {}
"""
die(3,
red('\nERROR: {} do not match!').format(desc)
+ fs.format(
in_bytes.hex(), sec, wif, a_val, b_val,
i='input', s='sec key', W='WIF key', a=a_desc, b=b_desc,
2024-10-18 10:32:12 +00:00
w=max(len(e) for e in (a_desc, b_desc)) + 1
).rstrip())
2024-10-18 10:32:12 +00:00
def do_ab_test(proto, scfg, addr_type, gen1, kg2, ag, tool, cache_data):
2024-10-18 10:32:12 +00:00
def do_ab_inner(n, trounds, in_bytes):
2019-11-01 12:31:59 +00:00
global last_t
if cfg.verbose or time.time() - last_t >= 0.1:
qmsg_r(f'\rRound {i+1}/{trounds} ')
last_t = time.time()
2024-10-18 10:32:12 +00:00
sec = PrivKey(proto, in_bytes, compressed=addr_type.compressed, pubkey_type=addr_type.pubkey_type)
data = kg1.gen_data(sec)
addr1 = ag.to_addr(data)
view_pref = 1 if proto.coin == 'BCH' else 0
2024-10-18 10:32:12 +00:00
tinfo = (in_bytes, sec, sec.wif, type(kg1).__name__, type(kg2).__name__ if kg2 else tool.desc)
def do_msg():
if cfg.verbose:
2024-10-18 10:32:12 +00:00
msg(fs.format(b=in_bytes.hex(), r=sec.hex(), k=sec.wif, v=vk2, a=addr1))
if tool:
def run_tool():
2024-10-18 10:32:12 +00:00
o = tool.run_tool(sec, cache_data)
test_equal('WIF keys', sec.wif, o.wif, *tinfo)
test_equal('addresses', addr1.views[view_pref], o.addr, *tinfo)
if o.viewkey:
2024-10-18 10:32:12 +00:00
test_equal('view keys', ag.to_viewkey(data), o.viewkey, *tinfo)
return o.viewkey
vk2 = run_tool()
do_msg()
else:
test_equal('addresses', addr1.views[view_pref], ag.to_addr(kg2.gen_data(sec)), *tinfo)
vk2 = None
do_msg()
qmsg_r(f'\rRound {n+1}/{trounds} ')
2019-11-01 12:31:59 +00:00
def get_randbytes():
if tool and len(tool.data) > len(edgecase_sks):
2024-09-20 09:36:06 +00:00
yield from tuple(tool.data)[len(edgecase_sks):]
else:
2023-10-11 12:58:52 +00:00
for _ in range(scfg.rounds):
yield getrand(32)
2025-03-15 18:24:52 +00:00
kg1 = KeyGenerator(cfg, proto, addr_type.pubkey_type, backend=gen1)
if type(kg1) == type(kg2):
2024-10-18 10:32:12 +00:00
die(4, 'Key generators are the same!')
2024-10-18 10:32:12 +00:00
e = cinfo.get_entry(proto.coin, proto.network)
qmsg(green("Comparing address generators '{A}' and '{B}' for {N} {c} ({n}), addrtype {a!r}".format(
2024-10-18 10:32:12 +00:00
A = type(kg1).__name__.replace('_', '-'),
B = type(kg2).__name__.replace('_', '-') if kg2 else tool.desc,
N = proto.network,
c = proto.coin,
n = e.name if e else '---',
2024-10-18 10:32:12 +00:00
a = addr_type.name)))
global last_t
last_t = time.time()
fs = (
'\ninput: {b}' +
'\nreduced: {r}' +
'\n{:9} {{k}}'.format(addr_type.wif_label+':') +
('\nviewkey: {v}' if 'viewkey' in addr_type.extra_attrs else '') +
2024-10-18 10:32:12 +00:00
'\naddr: {a}\n')
group_order = CoinProtocol.Secp256k1.secp256k1_group_order
2019-11-01 12:31:59 +00:00
# test some important private key edge cases:
edgecase_sks = (
bytes([0x00]*31 + [0x01]), # min
bytes([0xff]*32), # max
bytes([0x0f] + [0xff]*31), # produces same key as above for zcash-z
int.to_bytes(group_order + 1, 32, 'big'), # bitcoin will reduce
int.to_bytes(group_order - 1, 32, 'big'), # bitcoin will not reduce
bytes([0x00]*31 + [0xff]), # monero will reduce
bytes([0xff]*31 + [0x0f]), # monero will not reduce
bytes.fromhex('deadbeef'*8),
)
qmsg(purple('edge cases:'))
2024-10-18 10:32:12 +00:00
for i, privbytes in enumerate(edgecase_sks):
do_ab_inner(i, len(edgecase_sks), privbytes)
qmsg(green('\rOK ' if cfg.verbose else 'OK'))
2019-11-01 12:31:59 +00:00
qmsg(purple('random input:'))
2024-10-18 10:32:12 +00:00
for i, privbytes in enumerate(get_randbytes()):
do_ab_inner(i, scfg.rounds, privbytes)
qmsg(green('\rOK ' if cfg.verbose else 'OK'))
2024-10-18 10:32:12 +00:00
def init_tool(proto, addr_type, toolname):
return globals()['GenTool'+capfirst(toolname.replace('-', '_'))](proto, addr_type)
2024-10-18 10:32:12 +00:00
def ab_test(proto, scfg):
2024-10-18 10:32:12 +00:00
addr_type = MMGenAddrType(proto=proto, id_str=cfg.type or proto.dfl_mmtype)
if scfg.gen2:
assert scfg.gen1 != 'all', "'all' must be used only with external tool"
2025-03-15 18:24:52 +00:00
kg2 = KeyGenerator(cfg, proto, addr_type.pubkey_type, backend=scfg.gen2)
tool = None
else:
2024-10-18 10:32:12 +00:00
toolname = find_or_check_tool(proto, addr_type, scfg.tool)
if toolname is None:
ymsg(f'Warning: skipping tool {scfg.tool!r} for {proto.coin} {addr_type.name}')
return
2024-10-18 10:32:12 +00:00
tool = init_tool(proto, addr_type, toolname)
kg2 = None
2024-10-18 10:32:12 +00:00
ag = AddrGenerator(cfg, proto, addr_type)
if scfg.all_backends: # check all backends against external tool
overhaul public key and address generation code - pubkey generation code has been rewritten and moved from addr.py to keygen.py - address generation code has been rewritten and moved from addr.py to addrgen.py - keygen/addrgen classes now present a consistent API across all pubkey and address types - key/address operations and related data objects now use bytes internally instead of hex strings - pubkey generator backends are now selected using the `--keygen-backend` option - for Monero pubkeys, the new `nacl` backend has replaced `ed25519ll_djbec` as the default - a minimal unit test has been added Examples: # Generate a random Monero keypair using the unoptimized 'ed25519' backend: $ mmgen-tool --coin=xmr --keygen-backend=3 randpair # Generate an LTC Bech32 address list from the default wallet using the # 'python-ecdsa' backend: $ mmgen-addrgen --coin=ltc --type=bech32 --keygen-backend=2 1-10 Testing: # Run the minimal unit test: $ test/unit_tests_py gen # Compare BTC Segwit addresses from default 'libsecp256k1' backend to # 'pycoin' library, with edge cases and 10,000 random rounds: $ test/gentest.py --type=segwit 1:pycoin 10000 # Test all configured Monero backends against 'moneropy', with edge cases # and 10 random rounds: $ test/gentest.py --coin=xmr all:moneropy 10 # Test the 'nacl' and 'ed25519ll_djbec' backends against each other, with # edge cases and 1000 random rounds: $ test/gentest.py --coin=xmr 1:2 1000 # Test the speed of the Monero 'nacl' backend using 10,000 rounds: $ test/gentest.py --coin=xmr 1 10000 # Same for Zcash: $ test/gentest.py --coin=zec --type=zcash_z 1 10000
2022-01-15 14:00:12 +00:00
for n in range(len(get_backends(addr_type.pubkey_type))):
do_ab_test(
proto,
scfg,
addr_type,
gen1 = n+1,
kg2 = kg2,
ag = ag,
tool = tool,
cache_data = scfg.rounds < 1000 and not n)
else: # check specific backend against external tool or another backend
do_ab_test(
proto,
scfg,
addr_type,
gen1 = scfg.gen1,
kg2 = kg2,
ag = ag,
tool = tool,
cache_data = False)
2024-10-18 10:32:12 +00:00
def speed_test(proto, kg, ag, rounds):
qmsg(green('Testing speed of address generator {!r} for coin {}'.format(
type(kg).__name__,
2024-10-18 10:32:12 +00:00
proto.coin)))
2023-10-11 12:58:51 +00:00
from struct import pack
seed = getrand(28)
2019-11-01 12:24:09 +00:00
qmsg('Incrementing key with each round')
2024-10-18 10:32:12 +00:00
qmsg('Starting key: {}'.format((seed + pack('I', 0)).hex()))
start = last_t = time.time()
for i in range(rounds):
if time.time() - last_t >= 0.1:
qmsg_r(f'\rRound {i+1}/{rounds} ')
last_t = time.time()
2024-10-18 10:32:12 +00:00
sec = PrivKey(proto, seed+pack('I', i), compressed=ag.compressed, pubkey_type=ag.pubkey_type)
overhaul public key and address generation code - pubkey generation code has been rewritten and moved from addr.py to keygen.py - address generation code has been rewritten and moved from addr.py to addrgen.py - keygen/addrgen classes now present a consistent API across all pubkey and address types - key/address operations and related data objects now use bytes internally instead of hex strings - pubkey generator backends are now selected using the `--keygen-backend` option - for Monero pubkeys, the new `nacl` backend has replaced `ed25519ll_djbec` as the default - a minimal unit test has been added Examples: # Generate a random Monero keypair using the unoptimized 'ed25519' backend: $ mmgen-tool --coin=xmr --keygen-backend=3 randpair # Generate an LTC Bech32 address list from the default wallet using the # 'python-ecdsa' backend: $ mmgen-addrgen --coin=ltc --type=bech32 --keygen-backend=2 1-10 Testing: # Run the minimal unit test: $ test/unit_tests_py gen # Compare BTC Segwit addresses from default 'libsecp256k1' backend to # 'pycoin' library, with edge cases and 10,000 random rounds: $ test/gentest.py --type=segwit 1:pycoin 10000 # Test all configured Monero backends against 'moneropy', with edge cases # and 10 random rounds: $ test/gentest.py --coin=xmr all:moneropy 10 # Test the 'nacl' and 'ed25519ll_djbec' backends against each other, with # edge cases and 1000 random rounds: $ test/gentest.py --coin=xmr 1:2 1000 # Test the speed of the Monero 'nacl' backend using 10,000 rounds: $ test/gentest.py --coin=xmr 1 10000 # Same for Zcash: $ test/gentest.py --coin=zec --type=zcash_z 1 10000
2022-01-15 14:00:12 +00:00
addr = ag.to_addr(kg.gen_data(sec))
vmsg(f'\nkey: {sec.wif}\naddr: {addr}\n')
2021-10-03 17:40:02 +00:00
qmsg(
f'\rRound {i+1}/{rounds} ' +
f'\n{rounds} addresses generated' +
('' if cfg.test_suite_deterministic else f' in {time.time()-start:.2f} seconds')
2021-10-03 17:40:02 +00:00
)
2024-10-18 10:32:12 +00:00
def dump_test(proto, kg, ag, filename):
with open(filename) as fp:
dump = [[*(e.split()[0] for e in line.split('addr='))] for line in fp.readlines() if 'addr=' in line]
if not dump:
2024-10-18 10:32:12 +00:00
die(1, f'File {filename!r} appears not to be a wallet dump')
qmsg(green(
"A: generator pair '{}:{}'\nB: wallet dump {!r}".format(
type(kg).__name__,
type(ag).__name__,
filename)))
2024-10-18 10:32:12 +00:00
for count, (b_wif, b_addr) in enumerate(dump, 1):
qmsg_r(f'\rKey {count}/{len(dump)} ')
try:
2024-10-18 10:32:12 +00:00
b_sec = PrivKey(proto, wif=b_wif)
except:
2024-10-18 10:32:12 +00:00
die(2, f'\nInvalid {proto.network} WIF address in dump file: {b_wif}')
overhaul public key and address generation code - pubkey generation code has been rewritten and moved from addr.py to keygen.py - address generation code has been rewritten and moved from addr.py to addrgen.py - keygen/addrgen classes now present a consistent API across all pubkey and address types - key/address operations and related data objects now use bytes internally instead of hex strings - pubkey generator backends are now selected using the `--keygen-backend` option - for Monero pubkeys, the new `nacl` backend has replaced `ed25519ll_djbec` as the default - a minimal unit test has been added Examples: # Generate a random Monero keypair using the unoptimized 'ed25519' backend: $ mmgen-tool --coin=xmr --keygen-backend=3 randpair # Generate an LTC Bech32 address list from the default wallet using the # 'python-ecdsa' backend: $ mmgen-addrgen --coin=ltc --type=bech32 --keygen-backend=2 1-10 Testing: # Run the minimal unit test: $ test/unit_tests_py gen # Compare BTC Segwit addresses from default 'libsecp256k1' backend to # 'pycoin' library, with edge cases and 10,000 random rounds: $ test/gentest.py --type=segwit 1:pycoin 10000 # Test all configured Monero backends against 'moneropy', with edge cases # and 10 random rounds: $ test/gentest.py --coin=xmr all:moneropy 10 # Test the 'nacl' and 'ed25519ll_djbec' backends against each other, with # edge cases and 1000 random rounds: $ test/gentest.py --coin=xmr 1:2 1000 # Test the speed of the Monero 'nacl' backend using 10,000 rounds: $ test/gentest.py --coin=xmr 1 10000 # Same for Zcash: $ test/gentest.py --coin=zec --type=zcash_z 1 10000
2022-01-15 14:00:12 +00:00
a_addr = ag.to_addr(kg.gen_data(b_sec))
vmsg(f'\nwif: {b_wif}\naddr: {b_addr}\n')
2024-10-18 10:32:12 +00:00
tinfo = (b_sec, b_sec.hex(), b_wif, type(kg).__name__, filename)
test_equal('addresses', a_addr, b_addr, *tinfo)
2024-10-18 10:32:12 +00:00
qmsg(green(('\n', '')[bool(cfg.verbose)] + 'OK'))
2024-10-18 10:32:12 +00:00
def get_protos(proto, addr_type, toolname):
2019-10-20 14:20:34 +00:00
init_genonly_altcoins(testnet=proto.testnet)
2019-10-20 14:20:34 +00:00
for coin in cinfo.external_tests[proto.network][toolname]:
if coin.lower() not in CoinProtocol.coins:
continue
2024-10-18 10:32:12 +00:00
ret = init_proto(cfg, coin, testnet=proto.testnet)
if addr_type not in ret.mmtypes:
continue
yield ret
def parse_args():
all_backends, gen2, tool = (False, None, None)
match cfg._args:
case (gen1, rounds) if is_int(gen1) and is_int(rounds):
test, dumpfile = ('speed', None)
case (gen1, dumpfile) if is_int(gen1) and os.access(dumpfile, os.R_OK):
test, rounds = ('dump', None)
case (ab, rounds) if (ab := ab.split(':')) and is_int(rounds):
test, dumpfile = ('ab', None)
match ab[0]:
case x if is_int(x):
gen1 = x
case 'all':
all_backends = True
gen1 = None
case _:
die(1, "First part of first argument must be a generator backend number or 'all'")
match ab[1]:
case x if is_int(x):
if cfg.all_coins:
die(1, '--all-coins must be used with external tool only')
gen2 = x
case x:
tool = x
ext_progs = list(cinfo.external_tests[cfg._proto.network]) + ['ext']
if tool not in ext_progs:
die(1, f'Second part of first argument must be a generator backend number or one of {ext_progs}')
case _:
cfg._usage()
2024-10-18 10:32:12 +00:00
return namedtuple('parsed_args',
['test', 'gen1', 'gen2', 'rounds', 'tool', 'all_backends', 'dumpfile'])(
test,
None if gen1 is None else int(gen1),
None if gen2 is None else int(gen2),
None if rounds is None else int(rounds),
tool,
all_backends,
2024-10-18 10:32:12 +00:00
dumpfile)
def main():
scfg = parse_args()
2024-10-18 10:32:12 +00:00
addr_type = MMGenAddrType(proto=proto, id_str=cfg.type or proto.dfl_mmtype)
match scfg.test:
case 'ab':
protos = get_protos(proto, addr_type, scfg.tool) if cfg.all_coins else [proto]
for p in protos:
ab_test(p, scfg)
case 'speed' | 'dump':
kg = KeyGenerator(cfg, proto, addr_type.pubkey_type, backend=scfg.gen1)
ag = AddrGenerator(cfg, proto, addr_type)
match scfg.test:
case 'speed':
speed_test(proto, kg, ag, scfg.rounds)
case 'dump':
dump_test(proto, kg, ag, scfg.dumpfile)
if saved_results:
import json
2024-10-18 10:32:12 +00:00
with open(results_file, 'w') as fp:
fp.write(json.dumps(saved_results, indent=4))
2024-10-18 10:32:12 +00:00
from subprocess import run, PIPE, DEVNULL
from collections import namedtuple
2024-10-18 10:32:12 +00:00
from mmgen.protocol import init_proto, CoinProtocol
from mmgen.altcoin.params import init_genonly_altcoins
from test.altcointest import TestCoinInfo as cinfo
from mmgen.key import PrivKey
from mmgen.addr import MMGenAddrType
2024-10-18 10:32:12 +00:00
from mmgen.addrgen import KeyGenerator, AddrGenerator
overhaul public key and address generation code - pubkey generation code has been rewritten and moved from addr.py to keygen.py - address generation code has been rewritten and moved from addr.py to addrgen.py - keygen/addrgen classes now present a consistent API across all pubkey and address types - key/address operations and related data objects now use bytes internally instead of hex strings - pubkey generator backends are now selected using the `--keygen-backend` option - for Monero pubkeys, the new `nacl` backend has replaced `ed25519ll_djbec` as the default - a minimal unit test has been added Examples: # Generate a random Monero keypair using the unoptimized 'ed25519' backend: $ mmgen-tool --coin=xmr --keygen-backend=3 randpair # Generate an LTC Bech32 address list from the default wallet using the # 'python-ecdsa' backend: $ mmgen-addrgen --coin=ltc --type=bech32 --keygen-backend=2 1-10 Testing: # Run the minimal unit test: $ test/unit_tests_py gen # Compare BTC Segwit addresses from default 'libsecp256k1' backend to # 'pycoin' library, with edge cases and 10,000 random rounds: $ test/gentest.py --type=segwit 1:pycoin 10000 # Test all configured Monero backends against 'moneropy', with edge cases # and 10 random rounds: $ test/gentest.py --coin=xmr all:moneropy 10 # Test the 'nacl' and 'ed25519ll_djbec' backends against each other, with # edge cases and 1000 random rounds: $ test/gentest.py --coin=xmr 1:2 1000 # Test the speed of the Monero 'nacl' backend using 10,000 rounds: $ test/gentest.py --coin=xmr 1 10000 # Same for Zcash: $ test/gentest.py --coin=zec --type=zcash_z 1 10000
2022-01-15 14:00:12 +00:00
from mmgen.keygen import get_backends
from test.include.common import getrand, get_ethkey, set_globals
2024-10-18 10:32:12 +00:00
gtr = namedtuple('gen_tool_result', ['wif', 'addr', 'viewkey'])
sd = namedtuple('saved_data_item', ['reduced', 'wif', 'addr', 'viewkey'])
sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
2023-04-04 16:04:10 +00:00
cfg = Config(opts_data=opts_data)
set_globals(cfg)
qmsg = cfg._util.qmsg
qmsg_r = cfg._util.qmsg_r
vmsg = cfg._util.vmsg
proto = cfg._proto
if proto.coin in ('ETH', 'ETC', 'XMR'):
from mmgen.util2 import load_cryptodome
load_cryptodome()
2023-11-21 15:48:10 +00:00
if __name__ == '__main__':
from mmgen.main import launch
launch(func=main)