Skip to content
This repository has been archived by the owner on Jan 9, 2025. It is now read-only.

Commit

Permalink
fix: add handling of invalid public key (#806)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR: 0.6 day

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [x] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?
The point at infinity is not handled.

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #NA

## What is the new behavior?
An invalid return by `recover_public_key` is handled.
  • Loading branch information
greged93 authored Nov 11, 2023
1 parent 19d628d commit 71c140a
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/kakarot/precompiles/ec_recover.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.cairo_keccak.keccak import finalize_keccak
from starkware.cairo.common.bool import FALSE
from starkware.cairo.common.cairo_secp.ec import EcPoint
from starkware.cairo.common.cairo_secp.bigint import BigInt3
from starkware.cairo.common.cairo_secp.signature import (
recover_public_key,
public_key_point_to_eth_address,
Expand Down Expand Up @@ -59,6 +62,13 @@ namespace PrecompileEcRecover {

// v - 27, see recover_public_key comment
let (public_key_point) = recover_public_key(hash, r, s, v - 27);
let (is_public_key_invalid) = EcRecoverHelpers.ec_point_equal(
public_key_point, EcPoint(BigInt3(0, 0, 0), BigInt3(0, 0, 0))
);

if (is_public_key_invalid != FALSE) {
return (32, input, GAS_COST_EC_RECOVER, 0);
}

let (keccak_ptr: felt*) = alloc();
local keccak_ptr_start: felt* = keccak_ptr;
Expand All @@ -73,3 +83,14 @@ namespace PrecompileEcRecover {
return (32, output, GAS_COST_EC_RECOVER, 0);
}
}

namespace EcRecoverHelpers {
func ec_point_equal(point_0: EcPoint, point_1: EcPoint) -> (is_equal: felt) {
if (point_0.x.d0 == point_1.x.d0 and point_0.y.d0 == point_1.y.d0 and
point_0.x.d1 == point_1.x.d1 and point_0.y.d1 == point_1.y.d1 and
point_0.x.d2 == point_1.x.d2 and point_0.y.d2 == point_1.y.d2) {
return (is_equal=1);
}
return (is_equal=0);
}
}

0 comments on commit 71c140a

Please sign in to comment.