From 5b02ceb87cf56f80daaa2c14e403a46f615a1b17 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 7 Dec 2023 16:48:01 +0000 Subject: [PATCH] minor cleanups --- mmgen/proto/secp256k1/keygen.py | 10 +++++----- mmgen/util.py | 9 +++------ test/test-release.d/cfg.sh | 3 ++- test/tooltest2.py | 2 +- test/unit_tests.py | 4 ++-- test/unit_tests_d/ut_bip39.py | 26 +++++++++++++++++--------- 6 files changed, 30 insertions(+), 24 deletions(-) diff --git a/mmgen/proto/secp256k1/keygen.py b/mmgen/proto/secp256k1/keygen.py index ff1c4dc6..c48bb034 100755 --- a/mmgen/proto/secp256k1/keygen.py +++ b/mmgen/proto/secp256k1/keygen.py @@ -62,14 +62,14 @@ class backend: 0x02 depending on whether they're greater or less than the midpoint of the curve. """ def privnum2pubkey(numpriv,compressed=False): - pko = self.ecdsa.SigningKey.from_secret_exponent(numpriv,curve=self.ecdsa.SECP256k1) - # pubkey = x (32 bytes) + y (32 bytes) (unsigned big-endian) - pubkey = pko.get_verifying_key().to_string() + pk = self.ecdsa.SigningKey.from_secret_exponent(numpriv,curve=self.ecdsa.SECP256k1) + # vk_bytes = x (32 bytes) + y (32 bytes) (unsigned big-endian) + vk_bytes = pk.verifying_key.to_string() if compressed: # discard Y coord, replace with appropriate version byte # even y: <0, odd y: >0 -- https://bitcointalk.org/index.php?topic=129652.0 - return (b'\x02',b'\x03')[pubkey[-1] & 1] + pubkey[:32] + return (b'\x03' if vk_bytes[-1] & 1 else b'\x02') + vk_bytes[:32] else: - return b'\x04' + pubkey + return b'\x04' + vk_bytes return PubKey( s = privnum2pubkey( int.from_bytes(privkey,'big'), compressed=privkey.compressed ), diff --git a/mmgen/util.py b/mmgen/util.py index ce41e14b..e2bb9a19 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -27,6 +27,7 @@ from .cfg import gv,gc ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz' +digits = '0123456789' hexdigits = '0123456789abcdefABCDEF' hexdigits_uc = '0123456789ABCDEF' hexdigits_lc = '0123456789abcdef' @@ -347,12 +348,8 @@ def secs_to_hms(secs): def secs_to_ms(secs): return '{:02d}:{:02d}'.format(secs//60, secs % 60) -def is_int(s): - try: - int(str(s)) - return True - except: - return False +def is_int(s): # actually is_nonnegative_int() + return set(str(s)) <= set(digits) def check_int_between(val,imin,imax,desc): if not imin <= int(val) <= imax: diff --git a/test/test-release.d/cfg.sh b/test/test-release.d/cfg.sh index 38b6594f..63d3d077 100755 --- a/test/test-release.d/cfg.sh +++ b/test/test-release.d/cfg.sh @@ -265,7 +265,8 @@ init_tests() { a $gentest_py --coin=ltc --type=bech32 1 $REFDIR/litecoin/ltcwallet-bech32.dump a $gentest_py --coin=ltc --type=compressed --testnet=1 1 $REFDIR/litecoin/ltcwallet-testnet.dump - # libsecp256k1 vs python-ecdsa: - - $gentest_py 1:2 $rounds100x + - $gentest_py --type=legacy 1:2 $rounds100x + - $gentest_py --type=compressed 1:2 $rounds100x - $gentest_py --type=segwit 1:2 $rounds100x - $gentest_py --type=bech32 1:2 $rounds100x - $gentest_py --testnet=1 1:2 $rounds100x diff --git a/test/tooltest2.py b/test/tooltest2.py index e23c9d86..34c3074f 100755 --- a/test/tooltest2.py +++ b/test/tooltest2.py @@ -114,7 +114,7 @@ sample_text_hexdump = ( kafile_opts = ['-p1','-Ptest/ref/keyaddrfile_password'] from test.unit_tests_d.ut_baseconv import unit_test as ut_baseconv -from test.unit_tests_d.ut_bip39 import unit_test as ut_bip39 +from test.unit_tests_d.ut_bip39 import unit_tests as ut_bip39 from test.unit_tests_d.ut_xmrseed import unit_test as ut_xmrseed btc_wif1 = '5HwzecKMWD82ppJK3qMKpC7ohXXAwcyAN5VgdJ9PLFaAzpBG4sX' diff --git a/test/unit_tests.py b/test/unit_tests.py index eff9641a..47c1e159 100755 --- a/test/unit_tests.py +++ b/test/unit_tests.py @@ -34,7 +34,7 @@ if not os.getenv('MMGEN_DEVTOOLS'): init_dev() from mmgen.cfg import Config,gc -from mmgen.color import green,gray +from mmgen.color import green,gray,brown from mmgen.util import msg,gmsg,ymsg,Msg,async_run from test.include.common import set_globals,end_msg @@ -147,7 +147,7 @@ def run_test(test,subtest=None): def run_subtest(t,subtest): subtest_disp = subtest.replace('_','-') - msg(f'Running unit subtest {test}.{subtest_disp}') + msg(brown(f'Running unit subtest {test}.{subtest_disp}')) if getattr(t,'silence_output',False): t._silence() diff --git a/test/unit_tests_d/ut_bip39.py b/test/unit_tests_d/ut_bip39.py index 983f9b65..a38db359 100755 --- a/test/unit_tests_d/ut_bip39.py +++ b/test/unit_tests_d/ut_bip39.py @@ -5,10 +5,12 @@ test/unit_tests_d/ut_bip39: BIP39 unit test for the MMGen suite """ from mmgen.util import msg,msg_r +from mmgen.color import blue,orange,purple +from mmgen.bip39 import bip39 -from ..include.common import cfg,qmsg,vmsg +from ..include.common import cfg,qmsg,vmsg,vmsg_r -class unit_test: +class unit_tests: vectors = ( ( "00000000000000000000000000000000", @@ -85,12 +87,10 @@ class unit_test: ) ) - def run_test(self,name,ut): + def conversion(self,name,ut): - msg_r('Testing BIP39 conversion routines...') - qmsg('') - - from mmgen.bip39 import bip39 + vmsg('') + qmsg(blue('Testing BIP39 conversion routines')) b = bip39() b.check_wordlist(cfg) @@ -111,8 +111,15 @@ class unit_test: res = b.tohex( v[1].split() ) assert res == chk, f'mismatch:\nres: {res}\nchk: {chk}' + qmsg('OK') vmsg('') - qmsg('Checking error handling:') + + return True + + def errors(self,name,ut): + + vmsg('') + qmsg(blue('Testing error handling')) good_mn = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong".split() bad_len_mn = "zoo zoo zoo".split() @@ -121,6 +128,7 @@ class unit_test: bad_seed = 'deadbeef' good_seed = 'deadbeef' * 4 + b = bip39() th = b.tohex fh = b.fromhex bad_data = ( @@ -137,7 +145,7 @@ class unit_test: ut.process_bad_data(bad_data) + qmsg('OK') vmsg('') - msg('OK') return True