Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit f8978b1

Browse files
authored
Merge pull request #777 from cag/switch-to-coincurve
Switched to coincurve
2 parents 85efc86 + 9a9c246 commit f8978b1

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

ethereum/utils.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414

1515
try:
16-
import secp256k1
16+
import coincurve
1717
except ImportError:
1818
import warnings
19-
warnings.warn('could not import secp256k1', ImportWarning)
20-
secp256k1 = None
19+
warnings.warn('could not import coincurve', ImportWarning)
20+
coincurve = None
2121

2222
big_endian_to_int = lambda x: big_endian_int.deserialize(str_to_bytes(x).lstrip(b'\x00'))
2323
int_to_big_endian = lambda x: big_endian_int.serialize(x)
@@ -85,22 +85,15 @@ def bytes_to_int(value):
8585

8686

8787
def ecrecover_to_pub(rawhash, v, r, s):
88-
if secp256k1 and hasattr(secp256k1, "PublicKey"):
89-
# Legendre symbol check; the secp256k1 library does not seem to do this
90-
pk = secp256k1.PublicKey(flags=secp256k1.ALL_FLAGS)
91-
xc = r * r * r + 7
92-
assert pow(xc, (SECP256K1P - 1) // 2, SECP256K1P) == 1
88+
if coincurve and hasattr(coincurve, "PublicKey"):
9389
try:
94-
pk.public_key = pk.ecdsa_recover(
90+
pk = coincurve.PublicKey.from_signature_and_message(
91+
zpad(utils.bytearray_to_bytestr(int_to_32bytearray(r)), 32) + zpad(utils.bytearray_to_bytestr(int_to_32bytearray(s)), 32) +
92+
utils.ascii_chr(v - 27),
9593
rawhash,
96-
pk.ecdsa_recoverable_deserialize(
97-
zpad(bytearray_to_bytestr(int_to_32bytearray(r)), 32) +
98-
zpad(bytearray_to_bytestr(int_to_32bytearray(s)), 32),
99-
v - 27
100-
),
101-
raw=True
94+
hasher=None,
10295
)
103-
pub = pk.serialize(compressed=False)[1:]
96+
pub = pk.format(compressed=False)[1:]
10497
except:
10598
pub = b"\x00" * 64
10699
else:
@@ -111,12 +104,9 @@ def ecrecover_to_pub(rawhash, v, r, s):
111104

112105

113106
def ecsign(rawhash, key):
114-
if secp256k1 and hasattr(secp256k1, 'PrivateKey'):
115-
pk = secp256k1.PrivateKey(key, raw=True)
116-
signature = pk.ecdsa_recoverable_serialize(
117-
pk.ecdsa_sign_recoverable(rawhash, raw=True)
118-
)
119-
signature = signature[0] + bytearray_to_bytestr([signature[1]])
107+
if coincurve and hasattr(coincurve, 'PrivateKey'):
108+
pk = coincurve.PrivateKey(priv)
109+
signature = pk.sign_recoverable(msghash, hasher=None)
120110
v = safe_ord(signature[64]) + 27
121111
r = big_endian_to_int(signature[0:32])
122112
s = big_endian_to_int(signature[32:64])

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ scrypt
66
py_ecc
77
rlp>=0.4.7
88
https://github.com/ethereum/ethash/tarball/master
9+
coincurve>=5.0.1

0 commit comments

Comments
 (0)