gentest.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2021 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test/gentest.py: Cryptocoin key/address generation tests for the MMGen suite
  20. """
  21. import sys,os
  22. pn = os.path.dirname(sys.argv[0])
  23. os.chdir(os.path.join(pn,os.pardir))
  24. sys.path.__setitem__(0,os.path.abspath(os.curdir))
  25. os.environ['MMGEN_TEST_SUITE'] = '1'
  26. # Import these _after_ local path's been added to sys.path
  27. from mmgen.common import *
  28. from test.include.common import getrand
  29. rounds = 100
  30. opts_data = {
  31. 'text': {
  32. 'desc': 'Test key/address generation of the MMGen suite in various ways',
  33. 'usage':'[options] [spec] [rounds | dump file]',
  34. 'options': """
  35. -h, --help Print this help message
  36. -a, --all Test all coins supported by specified external tool
  37. -k, --use-internal-keccak-module Force use of the internal keccak module
  38. --, --longhelp Print help message for long options (common options)
  39. -q, --quiet Produce quieter output
  40. -t, --type=t Specify address type (e.g. 'compressed','segwit','zcash_z','bech32')
  41. -v, --verbose Produce more verbose output
  42. """,
  43. 'notes': """
  44. TEST TYPES:
  45. A/B: {prog} A:B [rounds] (compare key generators A and B)
  46. Speed: {prog} A [rounds] (test speed of key generator A)
  47. Compare: {prog} A <dump file> (compare generator A to wallet dump)
  48. where A and B are one of:
  49. '1' - native Python ECDSA library (slow), or
  50. '2' - bitcoincore.org's libsecp256k1 library (default);
  51. or:
  52. B is name of an external tool (see below) or 'ext'.
  53. If B is 'ext', the external tool will be chosen automatically.
  54. EXAMPLES:
  55. Compare addresses generated by native Python ECDSA library and libsecp256k1,
  56. 100 rounds:
  57. $ {prog} 1:2 100
  58. Compare mmgen-secp256k1 Segwit address generation to pycoin library for all
  59. supported coins, 100 rounds:
  60. $ {prog} --all --type=segwit 2:pycoin 100
  61. Compare mmgen-secp256k1 address generation to keyconv tool for all
  62. supported coins, 100 rounds:
  63. $ {prog} --all --type=compressed 2:keyconv 100
  64. Compare mmgen-secp256k1 XMR address generation to configured external tool,
  65. 10 rounds:
  66. $ {prog} --coin=xmr 2:ext 10
  67. Test speed of mmgen-secp256k1 address generation, 10,000 rounds:
  68. $ {prog} 2 10000
  69. Compare mmgen-secp256k1-generated bech32 addrs to coin daemon wallet dump:
  70. $ {prog} --type=bech32 2 bech32wallet.dump
  71. Supported external tools:
  72. + ethkey (for ETH,ETC)
  73. https://github.com/openethereum/openethereum
  74. (build with 'cargo build -p ethkey-cli --release')
  75. + zcash-mini (for Zcash Z-addresses)
  76. https://github.com/FiloSottile/zcash-mini
  77. + moneropy (for Monero addresses)
  78. https://github.com/bigreddmachine/MoneroPy
  79. + pycoin (for supported coins)
  80. https://github.com/richardkiss/pycoin
  81. + keyconv (for supported coins)
  82. https://github.com/exploitagency/vanitygen-plus
  83. ('keyconv' does not generate Segwit addresses)
  84. """
  85. },
  86. 'code': {
  87. 'notes': lambda s: s.format(
  88. prog='test/gentest.py',
  89. pnm=g.proj_name,
  90. snum=rounds )
  91. }
  92. }
  93. sys.argv = [sys.argv[0]] + ['--skip-cfg-file'] + sys.argv[1:]
  94. cmd_args = opts.init(opts_data,add_opts=['exact_output'])
  95. if not 1 <= len(cmd_args) <= 2:
  96. opts.usage()
  97. from mmgen.protocol import init_proto_from_opts
  98. proto = init_proto_from_opts()
  99. from subprocess import run,PIPE,DEVNULL
  100. def get_cmd_output(cmd,input=None):
  101. return run(cmd,input=input,stdout=PIPE,stderr=DEVNULL).stdout.decode().splitlines()
  102. from collections import namedtuple
  103. gtr = namedtuple('gen_tool_result',['wif','addr','vk'])
  104. class GenTool(object):
  105. def run_tool(self,sec):
  106. vcoin = 'BTC' if proto.coin == 'BCH' else proto.coin
  107. return self.run(sec,vcoin)
  108. class GenToolEthkey(GenTool):
  109. desc = 'ethkey'
  110. def __init__(self):
  111. proto = init_proto('eth')
  112. global addr_type
  113. addr_type = MMGenAddrType(proto,'E')
  114. def run(self,sec,vcoin):
  115. o = get_cmd_output(['ethkey','info',sec])
  116. return gtr(o[0].split()[1],o[-1].split()[1],None)
  117. class GenToolKeyconv(GenTool):
  118. desc = 'keyconv'
  119. def run(self,sec,vcoin):
  120. o = get_cmd_output(['keyconv','-C',vcoin,sec.wif])
  121. return gtr(o[1].split()[1],o[0].split()[1],None)
  122. class GenToolZcash_mini(GenTool):
  123. desc = 'zcash-mini'
  124. def __init__(self):
  125. proto = init_proto('zec')
  126. global addr_type
  127. addr_type = MMGenAddrType(proto,'Z')
  128. def run(self,sec,vcoin):
  129. o = get_cmd_output(['zcash-mini','-key','-simple'],input=(sec.wif+'\n').encode())
  130. return gtr(o[1],o[0],o[-1])
  131. class GenToolPycoin(GenTool):
  132. """
  133. pycoin/networks/all.py pycoin/networks/legacy_networks.py
  134. """
  135. desc = 'pycoin'
  136. def __init__(self):
  137. m = "Unable to import pycoin.networks.registry. Is pycoin installed on your system?"
  138. try:
  139. from pycoin.networks.registry import network_for_netcode
  140. except:
  141. raise ImportError(m)
  142. self.nfnc = network_for_netcode
  143. def run(self,sec,vcoin):
  144. if proto.testnet:
  145. vcoin = ci.external_tests['testnet']['pycoin'][vcoin]
  146. network = self.nfnc(vcoin)
  147. key = network.keys.private(secret_exponent=int(sec,16),is_compressed=addr_type.name != 'legacy')
  148. if key is None:
  149. die(1,f'can’t parse {sec}')
  150. if addr_type.name in ('segwit','bech32'):
  151. hash160_c = key.hash160(is_compressed=True)
  152. if addr_type.name == 'segwit':
  153. p2sh_script = network.contract.for_p2pkh_wit(hash160_c)
  154. addr = network.address.for_p2s(p2sh_script)
  155. else:
  156. addr = network.address.for_p2pkh_wit(hash160_c)
  157. else:
  158. addr = key.address()
  159. return gtr(key.wif(),addr,None)
  160. class GenToolMoneropy(GenTool):
  161. desc = 'moneropy'
  162. def __init__(self):
  163. m = "Unable to import moneropy. Is moneropy installed on your system?"
  164. try:
  165. import moneropy.account
  166. except:
  167. raise ImportError(m)
  168. self.mpa = moneropy.account
  169. proto = init_proto('xmr')
  170. global addr_type
  171. addr_type = MMGenAddrType(proto,'M')
  172. def run(self,sec,vcoin):
  173. sk_t,vk_t,addr_t = self.mpa.account_from_spend_key(sec) # VERY slow!
  174. return gtr(sk_t,addr_t,vk_t)
  175. def get_tool(arg):
  176. if arg not in ext_progs + ['ext']:
  177. die(1,f'{arg!r}: unsupported tool for network {proto.network}')
  178. if opt.all:
  179. if arg == 'ext':
  180. die(1,"'--all' must be combined with a specific external testing tool")
  181. return arg
  182. else:
  183. tool = ci.get_test_support(
  184. proto.coin,
  185. addr_type.name,
  186. proto.network,
  187. verbose = not opt.quiet,
  188. tool = arg if arg in ext_progs else None )
  189. if not tool:
  190. sys.exit(2)
  191. if arg in ext_progs and arg != tool:
  192. sys.exit(3)
  193. return tool
  194. def test_equal(desc,a_val,b_val,in_bytes,sec,wif,a_desc,b_desc):
  195. if a_val != b_val:
  196. fs = """
  197. {i:{w}}: {}
  198. {s:{w}}: {}
  199. {W:{w}}: {}
  200. {a:{w}}: {}
  201. {b:{w}}: {}
  202. """
  203. die(3,
  204. red('\nERROR: {} do not match!').format(desc)
  205. + fs.format(
  206. in_bytes.hex(), sec, wif, a_val, b_val,
  207. i='input', s='sec key', W='WIF key', a=a_desc, b=b_desc,
  208. w=max(len(e) for e in (a_desc,b_desc)) + 1
  209. ).rstrip())
  210. def gentool_test(kg_a,kg_b,ag,rounds):
  211. m = "Comparing address generators '{A}' and '{B}' for {N} {c} ({n}), addrtype {a!r}"
  212. e = ci.get_entry(proto.coin,proto.network)
  213. qmsg(green(m.format(
  214. A = kg_a.desc,
  215. B = kg_b.desc,
  216. N = proto.network,
  217. c = proto.coin,
  218. n = e.name if e else '---',
  219. a = addr_type.name )))
  220. global last_t
  221. last_t = time.time()
  222. def do_compare_test(n,trounds,in_bytes):
  223. global last_t
  224. if opt.verbose or time.time() - last_t >= 0.1:
  225. qmsg_r(f'\rRound {i+1}/{trounds} ')
  226. last_t = time.time()
  227. sec = PrivKey(proto,in_bytes,compressed=addr_type.compressed,pubkey_type=addr_type.pubkey_type)
  228. a_ph = kg_a.to_pubhex(sec)
  229. a_addr = ag.to_addr(a_ph)
  230. a_vk = None
  231. tinfo = (in_bytes,sec,sec.wif,kg_a.desc,kg_b.desc)
  232. if isinstance(kg_b,GenTool):
  233. b = kg_b.run_tool(sec)
  234. test_equal('WIF keys',sec.wif,b.wif,*tinfo)
  235. test_equal('addresses',a_addr,b.addr,*tinfo)
  236. if b.vk:
  237. a_vk = ag.to_viewkey(a_ph)
  238. test_equal('view keys',a_vk,b.vk,*tinfo)
  239. else:
  240. b_addr = ag.to_addr(kg_b.to_pubhex(sec))
  241. test_equal('addresses',a_addr,b_addr,*tinfo)
  242. vmsg(fs.format(b=in_bytes.hex(),k=sec.wif,v=a_vk,a=a_addr))
  243. qmsg_r(f'\rRound {n+1}/{trounds} ')
  244. fs = ( '\ninput: {b}\n%-9s {k}\naddr: {a}\n',
  245. '\ninput: {b}\n%-9s {k}\nviewkey: {v}\naddr: {a}\n')[
  246. 'viewkey' in addr_type.extra_attrs] % (addr_type.wif_label + ':')
  247. # test some important private key edge cases:
  248. edgecase_sks = (
  249. bytes([0x00]*31 + [0x01]), # min
  250. bytes([0xff]*32), # max
  251. bytes([0x0f] + [0xff]*31), # same key as above for zcash-z
  252. bytes([0x00]*31 + [0xff]), # monero will reduce
  253. bytes([0xff]*31 + [0x0f]), # monero will not reduce
  254. )
  255. qmsg(purple('edge cases:'))
  256. for i,in_bytes in enumerate(edgecase_sks):
  257. do_compare_test(i,len(edgecase_sks),in_bytes)
  258. qmsg(green('\rOK ' if opt.verbose else 'OK'))
  259. qmsg(purple('random input:'))
  260. for i in range(rounds):
  261. do_compare_test(i,rounds,getrand(32))
  262. qmsg(green('\rOK ' if opt.verbose else 'OK'))
  263. def speed_test(kg,ag,rounds):
  264. m = "Testing speed of address generator '{}' for coin {}"
  265. qmsg(green(m.format(kg.desc,proto.coin)))
  266. from struct import pack,unpack
  267. seed = getrand(28)
  268. qmsg('Incrementing key with each round')
  269. qmsg('Starting key: {}'.format(
  270. (seed + pack('I',0)).hex()
  271. ))
  272. import time
  273. start = last_t = time.time()
  274. for i in range(rounds):
  275. if time.time() - last_t >= 0.1:
  276. qmsg_r(f'\rRound {i+1}/{rounds} ')
  277. last_t = time.time()
  278. sec = PrivKey(proto,seed+pack('I',i),compressed=addr_type.compressed,pubkey_type=addr_type.pubkey_type)
  279. addr = ag.to_addr(kg.to_pubhex(sec))
  280. vmsg(f'\nkey: {sec.wif}\naddr: {addr}\n')
  281. qmsg(
  282. f'\rRound {i+1}/{rounds} ' +
  283. f'\n{rounds} addresses generated in {time.time()-start:.2f} seconds'
  284. )
  285. def dump_test(kg,ag,fh):
  286. dump = [[*(e.split()[0] for e in line.split('addr='))] for line in fh.readlines() if 'addr=' in line]
  287. if not dump:
  288. die(1,f'File {fh.name!r} appears not to be a wallet dump')
  289. m = 'Comparing output of address generator {!r} against wallet dump {!r}'
  290. qmsg(green(m.format(kg.desc,fh.name)))
  291. for count,(b_wif,b_addr) in enumerate(dump,1):
  292. qmsg_r(f'\rKey {count}/{len(dump)} ')
  293. try:
  294. b_sec = PrivKey(proto,wif=b_wif)
  295. except:
  296. die(2,f'\nInvalid {proto.network} WIF address in dump file: {b_wif}')
  297. a_addr = ag.to_addr(kg.to_pubhex(b_sec))
  298. vmsg(f'\nwif: {b_wif}\naddr: {b_addr}\n')
  299. tinfo = (bytes.fromhex(b_sec),b_sec,b_wif,kg.desc,fh.name)
  300. test_equal('addresses',a_addr,b_addr,*tinfo)
  301. qmsg(green(('\n','')[bool(opt.verbose)] + 'OK'))
  302. def init_tool(tname):
  303. return globals()['GenTool'+capfirst(tname.replace('-','_'))]()
  304. def parse_arg1(arg,arg_id):
  305. m1 = 'First argument must be a numeric generator ID or two colon-separated generator IDs'
  306. m2 = 'Second part of first argument must be a numeric generator ID or one of {}'
  307. def check_gen_num(n):
  308. if not (1 <= int(n) <= len(g.key_generators)):
  309. die(1,f'{n}: invalid generator ID')
  310. return int(n)
  311. if arg_id == 'a':
  312. if is_int(arg):
  313. a_num = check_gen_num(arg)
  314. return (KeyGenerator(proto,addr_type,a_num),a_num)
  315. else:
  316. die(1,m1)
  317. elif arg_id == 'b':
  318. if is_int(arg):
  319. return KeyGenerator(proto,addr_type,check_gen_num(arg))
  320. elif arg in ext_progs + ['ext']:
  321. return init_tool(get_tool(arg))
  322. else:
  323. die(1,m2.format(ext_progs))
  324. def parse_arg2():
  325. m = 'Second argument must be dump filename or integer rounds specification'
  326. if len(cmd_args) == 1:
  327. return None
  328. arg = cmd_args[1]
  329. if is_int(arg) and int(arg) > 0:
  330. return int(arg)
  331. try:
  332. return open(arg)
  333. except:
  334. die(1,m)
  335. # begin execution
  336. from mmgen.protocol import init_proto
  337. from mmgen.altcoin import CoinInfo as ci
  338. from mmgen.obj import MMGenAddrType,PrivKey
  339. from mmgen.addr import KeyGenerator,AddrGenerator
  340. addr_type = MMGenAddrType(
  341. proto = proto,
  342. id_str = opt.type or proto.dfl_mmtype )
  343. ext_progs = list(ci.external_tests[proto.network])
  344. arg1 = cmd_args[0].split(':')
  345. if len(arg1) == 1:
  346. a,a_num = parse_arg1(arg1[0],'a')
  347. b = None
  348. elif len(arg1) == 2:
  349. a,a_num = parse_arg1(arg1[0],'a')
  350. b = parse_arg1(arg1[1],'b')
  351. else:
  352. opts.usage()
  353. if type(a) == type(b):
  354. rdie(1,'Address generators are the same!')
  355. arg2 = parse_arg2()
  356. if not opt.all:
  357. ag = AddrGenerator(proto,addr_type)
  358. if not b and type(arg2) == int:
  359. speed_test(a,ag,arg2)
  360. elif not b and hasattr(arg2,'read'):
  361. dump_test(a,ag,arg2)
  362. elif a and b and type(arg2) == int:
  363. if opt.all:
  364. from mmgen.protocol import CoinProtocol,init_genonly_altcoins
  365. init_genonly_altcoins(testnet=proto.testnet)
  366. for coin in ci.external_tests[proto.network][b.desc]:
  367. if coin.lower() not in CoinProtocol.coins:
  368. # ymsg(f'Coin {coin} not configured')
  369. continue
  370. proto = init_proto(coin)
  371. if addr_type not in proto.mmtypes:
  372. continue
  373. # proto has changed, so reinit kg and ag
  374. a = KeyGenerator(proto,addr_type,a_num)
  375. ag = AddrGenerator(proto,addr_type)
  376. b_chk = ci.get_test_support(proto.coin,addr_type.name,proto.network,tool=b.desc,verbose=not opt.quiet)
  377. if b_chk == b.desc:
  378. gentool_test(a,b,ag,arg2)
  379. else:
  380. gentool_test(a,b,ag,arg2)
  381. else:
  382. opts.usage()