Skip to content

Commit 54aa418

Browse files
committed
Code review feedback
1 parent 275d66a commit 54aa418

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/ethereum/crypto/elliptic_curve.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import coincurve
77
from Crypto.Util.asn1 import DerSequence
8+
from cryptography.exceptions import InvalidSignature
89
from cryptography.hazmat.backends import default_backend
910
from cryptography.hazmat.primitives import hashes
1011
from cryptography.hazmat.primitives.asymmetric import ec
@@ -97,22 +98,24 @@ def secp256r1_verify(
9798
) -> None:
9899
"""
99100
Verifies a P-256 signature.
101+
100102
Parameters
101103
----------
102104
r :
103105
the `r` component of the signature
104106
s :
105107
the `s` component of the signature
106-
x:
108+
x :
107109
the `x` coordinate of the public key
108-
y:
110+
y :
109111
the `y` coordinate of the public key
110112
msg_hash :
111113
Hash of the message being recovered.
112-
Returns
113-
-------
114-
result : `ethereum.base_types.Bytes`
115-
return 1 if the signature is valid, empty bytes otherwise
114+
115+
Raises
116+
------
117+
118+
Raises an `InvalidSignatureError` if the signature is not valid.
116119
"""
117120
# Convert U256 to regular integers for DerSequence
118121
r_int = int(r)
@@ -124,9 +127,11 @@ def secp256r1_verify(
124127

125128
pubnum = ec.EllipticCurvePublicNumbers(x_int, y_int, ec.SECP256R1())
126129
pubkey = pubnum.public_key(default_backend())
127-
pubkey.verify(sig, msg_hash, ec.ECDSA(Prehashed(hashes.SHA256())))
128130

129-
return
131+
try:
132+
pubkey.verify(sig, msg_hash, ec.ECDSA(Prehashed(hashes.SHA256())))
133+
except InvalidSignature as e:
134+
raise InvalidSignatureError from e
130135

131136

132137
def is_on_curve_secp256r1(x: U256, y: U256) -> bool:

src/ethereum/osaka/vm/precompiled_contracts/p256verify.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
secp256r1_verify,
1818
)
1919
from ethereum.crypto.hash import Hash32
20+
from ethereum.exceptions import InvalidSignatureError
2021
from ethereum.utils.byte import left_pad_zero_bytes
2122

2223
from ...vm import Evm
@@ -75,11 +76,9 @@ def p256verify(evm: Evm) -> None:
7576
if public_key_x == U256(0) and public_key_y == U256(0):
7677
return
7778

78-
success_return_value = left_pad_zero_bytes(b"\x01", 32)
79-
8079
try:
8180
secp256r1_verify(r, s, public_key_x, public_key_y, message_hash)
82-
except Exception:
81+
except InvalidSignatureError:
8382
return
8483

85-
evm.output = success_return_value
84+
evm.output = left_pad_zero_bytes(b"\x01", 32)

0 commit comments

Comments
 (0)