From ac2403b106d6b56653c30e499c66a2790f4eb005 Mon Sep 17 00:00:00 2001 From: Alan Lu Date: Fri, 28 Apr 2017 13:57:14 -0500 Subject: [PATCH 1/3] Switched to coincurve --- ethereum/utils.py | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/ethereum/utils.py b/ethereum/utils.py index c54e4dd52..36b55d4fd 100644 --- a/ethereum/utils.py +++ b/ethereum/utils.py @@ -13,11 +13,11 @@ try: - import secp256k1 + import coincurve except ImportError: - import warnings - warnings.warn('could not import secp256k1', ImportWarning) - secp256k1 = None + import warning + warning.warn('could not import coincurve', ImportWarning) + coincurve = None big_endian_to_int = lambda x: big_endian_int.deserialize(str_to_bytes(x).lstrip(b'\x00')) int_to_big_endian = lambda x: big_endian_int.serialize(x) @@ -85,22 +85,15 @@ def bytes_to_int(value): def ecrecover_to_pub(rawhash, v, r, s): - if secp256k1 and hasattr(secp256k1, "PublicKey"): - # Legendre symbol check; the secp256k1 library does not seem to do this - pk = secp256k1.PublicKey(flags=secp256k1.ALL_FLAGS) - xc = r * r * r + 7 - assert pow(xc, (SECP256K1P - 1) // 2, SECP256K1P) == 1 + if coincurve and hasattr(coincurve, "PublicKey"): try: - pk.public_key = pk.ecdsa_recover( + pk = coincurve.PublicKey.from_signature_and_message( + zpad(utils.bytearray_to_bytestr(int_to_32bytearray(r)), 32) + zpad(utils.bytearray_to_bytestr(int_to_32bytearray(s)), 32) + + utils.ascii_chr(v - 27), rawhash, - pk.ecdsa_recoverable_deserialize( - zpad(bytearray_to_bytestr(int_to_32bytearray(r)), 32) + - zpad(bytearray_to_bytestr(int_to_32bytearray(s)), 32), - v - 27 - ), - raw=True + hasher=None, ) - pub = pk.serialize(compressed=False)[1:] + pub = pk.format(compressed=False)[1:] except: pub = b"\x00" * 64 else: @@ -111,12 +104,9 @@ def ecrecover_to_pub(rawhash, v, r, s): def ecsign(rawhash, key): - if secp256k1 and hasattr(secp256k1, 'PrivateKey'): - pk = secp256k1.PrivateKey(key, raw=True) - signature = pk.ecdsa_recoverable_serialize( - pk.ecdsa_sign_recoverable(rawhash, raw=True) - ) - signature = signature[0] + bytearray_to_bytestr([signature[1]]) + if coincurve and hasattr(coincurve, 'PrivateKey'): + pk = coincurve.PrivateKey(priv) + signature = pk.sign_recoverable(msghash, hasher=None) v = safe_ord(signature[64]) + 27 r = big_endian_to_int(signature[0:32]) s = big_endian_to_int(signature[32:64]) From e490fad48d1c16f9e920b51f3d4eef6ce1a1e8b5 Mon Sep 17 00:00:00 2001 From: Alan Lu Date: Tue, 15 Aug 2017 11:53:19 -0500 Subject: [PATCH 2/3] Update requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements.txt b/requirements.txt index 5c22c1798..707691293 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ scrypt py_ecc rlp>=0.4.7 https://github.com/ethereum/ethash/tarball/master +coincurve>=5.0.1 From 9a9c2462b0065e407c1a641f6f2e604485c0a50c Mon Sep 17 00:00:00 2001 From: Alan Lu Date: Tue, 15 Aug 2017 11:57:49 -0500 Subject: [PATCH 3/3] Correct import for warnings --- ethereum/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ethereum/utils.py b/ethereum/utils.py index 36b55d4fd..d21998241 100644 --- a/ethereum/utils.py +++ b/ethereum/utils.py @@ -15,8 +15,8 @@ try: import coincurve except ImportError: - import warning - warning.warn('could not import coincurve', ImportWarning) + import warnings + warnings.warn('could not import coincurve', ImportWarning) coincurve = None big_endian_to_int = lambda x: big_endian_int.deserialize(str_to_bytes(x).lstrip(b'\x00'))