addr generation with secp256k1mod

This commit is contained in:
philemon 2016-08-22 14:20:46 +03:00
commit 5676d7a3f9
Signed by untrusted user who does not match committer: mmgen
GPG key ID: 62DBE9E5212F05BE
20 changed files with 430 additions and 431 deletions

View file

@ -29,14 +29,14 @@ from binascii import hexlify
# Import these _after_ local path's been added to sys.path
from mmgen.common import *
from mmgen.bitcoin import hextowif,privnum2addr
from mmgen.bitcoin import hex2wif,privnum2addr
start_mscolor()
rounds = 100
opts_data = {
'desc': "Test addresses generated by {} against output of 'keyconv'".format(g.proj_name),
'usage':'[options] [rounds]',
'desc': "Test address generation using various methods",
'usage':'[options] a:b [rounds]',
'options': """
-h, --help Print this help message
-s, --system Test scripts and modules installed on system rather than
@ -44,51 +44,64 @@ opts_data = {
-v, --verbose Produce more verbose output
""",
'notes': """
{pnm} can generate addresses from secret keys using one of three methods,
as specified by the user:
'keyconv' is the address generation utility from the well-known vanitygen
package. If it's installed on your system, {pnm} will use it by default to
generate Bitcoin addresses. Otherwise, it falls back on its own internal
routines, which use the Python ecdsa library.
1) with the native Python ecdsa library (very slow)
2) with the 'keyconv' utility from the 'vanitygen' package (the default)
3) using bitcoincore.org's secp256k1 library (very fast, experimental)
rounds is {} by default.
""".format(rounds,pnm=g.proj_name)
This test suite compares the output of these different methods against each
other over set of randomly generated secret keys ({snum} by default).
EXAMPLE:
gentest.py 2:3 1000
(compare output of 'keyconv' with secp256k1 library, 1000 rounds)
""".format(pnm=g.proj_name,snum=rounds)
}
cmd_args = opts.init(opts_data,add_opts=['exact_output'])
if len(cmd_args) == 1:
if not 1 <= len(cmd_args) <= 2: opts.usage()
if len(cmd_args) == 2:
try:
rounds = int(cmd_args[0])
rounds = int(cmd_args[1])
assert rounds > 0
except:
die(1,"'rounds' must be a positive integer")
elif len(cmd_args) > 1:
opts.usage(opts_data)
try:
a,b = cmd_args[0].split(':')
a,b = int(a),int(b)
for i in a,b: assert 1 <= i <= len(g.key_generators)
assert a != b
except:
die(1,"%s: incorrect 'a:b' specifier" % cmd_args[0])
if opt.system: sys.path.pop(0)
from mmgen.addr import test_for_keyconv
if not test_for_keyconv(silent=True):
die(1,"To run this test, you must install 'keyconv' from the vanitygen package.")
m = "Comparing {}'s internally generated addresses against output of 'keyconv'"
msg(green(m.format(g.proj_name)))
from subprocess import check_output
m = "Comparing address generators '{}' and '{}'"
msg(green(m.format(g.key_generators[a-1],g.key_generators[b-1])))
from mmgen.addr import get_privhex2addr_f
gen_a = get_privhex2addr_f(selector=a)
gen_b = get_privhex2addr_f(selector=b)
compressed = False
for i in range(1,rounds+1):
msg_r('\rRound %s/%s ' % (i,rounds))
sec = hexlify(os.urandom(32))
wif = hextowif(sec)
a = privnum2addr(int(sec,16))
vmsg('\nkey: %s\naddr: %s\n' % (wif,a))
b = check_output(['keyconv', wif]).split()[1]
if a != b:
wif = hex2wif(sec,compressed=compressed)
a_addr = gen_a(sec,compressed)
b_addr = gen_b(sec,compressed)
vmsg('\nkey: %s\naddr: %s\n' % (wif,a_addr))
if a_addr != b_addr:
msg_r(red('\nERROR: Addresses do not match!'))
die(3,"""
sec key: {}
WIF key: {}
{pnm}: {}
keyconv: {}
""".format(sec,wif,a,b,pnm=g.proj_name).rstrip())
""".format(sec,wif,a_addr,b_addr,pnm=g.proj_name).rstrip())
if a != 2 and b != 2:
compressed = not compressed
msg(green(('\n','')[bool(opt.verbose)] + 'OK'))