Skip to content

Commit

Permalink
fix: raise for invalid y_parity values
Browse files Browse the repository at this point in the history
  • Loading branch information
obatirou committed Nov 21, 2024
1 parent 2eb0834 commit e85f57a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cairo_zero/kakarot/accounts/library.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ namespace AccountContract {
msg_hash=msg_hash,
r=r,
s=s,
v=y_parity,
y_parity=y_parity,
eth_address=address,
helpers_class=helpers_class,
);
Expand Down
12 changes: 11 additions & 1 deletion cairo_zero/tests/src/kakarot/accounts/test_account_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,17 @@ def test_should_raise_with_wrong_signature(self, cairo_run):
cairo_run(
"test__execute_from_outside",
tx_data=[1],
signature=list(range(5)),
signature=[0, 1, 2, 3, 0],
chain_id=CHAIN_ID,
)

@given(y_parity=integers(min_value=2))
def test_should_raise_with_invalid_y_parity(self, cairo_run, y_parity):
with cairo_error(message="Invalid y_parity"):
cairo_run(
"test__execute_from_outside",
tx_data=[1],
signature=[0, 1, 2, 3, y_parity],
chain_id=CHAIN_ID,
)

Expand Down
15 changes: 13 additions & 2 deletions cairo_zero/utils/signature.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ namespace Signature {
// using the Cairo1 helpers class.
func verify_eth_signature_uint256{
syscall_ptr: felt*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(msg_hash: Uint256, r: Uint256, s: Uint256, v: felt, eth_address: felt, helpers_class: felt) {
}(
msg_hash: Uint256,
r: Uint256,
s: Uint256,
y_parity: felt,
eth_address: felt,
helpers_class: felt,
) {
alloc_locals;
let (msg_hash_bigint: BigInt3) = uint256_to_bigint(msg_hash);
let (r_bigint: BigInt3) = uint256_to_bigint(r);
Expand All @@ -23,9 +30,13 @@ namespace Signature {
validate_signature_entry(s_bigint);
}

with_attr error_message("Invalid y_parity") {
assert (1 - y_parity) * y_parity = 0;
}

with_attr error_message("Invalid signature.") {
let (success, recovered_address) = ICairo1Helpers.library_call_recover_eth_address(
class_hash=helpers_class, msg_hash=msg_hash, r=r, s=s, y_parity=v
class_hash=helpers_class, msg_hash=msg_hash, r=r, s=s, y_parity=y_parity
);
assert success = 1;
assert eth_address = recovered_address;
Expand Down

0 comments on commit e85f57a

Please sign in to comment.