Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secp256k1 Cost annihilation #291

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cairo/programs/os.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ func apply_transactions{
pedersen_ptr: HashBuiltin*,
bitwise_ptr: BitwiseBuiltin*,
range_check_ptr,
range_check96_ptr: felt*,
add_mod_ptr: ModBuiltin*,
mul_mod_ptr: ModBuiltin*,
poseidon_ptr: PoseidonBuiltin*,
keccak_ptr: KeccakBuiltin*,
header: model.BlockHeader*,
block_hashes: Uint256*,
Expand Down
23 changes: 17 additions & 6 deletions cairo/src/precompiles/ec_recover.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from starkware.cairo.common.alloc import alloc
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin, KeccakBuiltin
from starkware.cairo.common.cairo_builtins import (
HashBuiltin,
BitwiseBuiltin,
KeccakBuiltin,
PoseidonBuiltin,
ModBuiltin,
)
from starkware.cairo.common.builtin_keccak.keccak import keccak_uint256s_bigend
from starkware.cairo.common.bool import FALSE
from starkware.cairo.common.math_cmp import RC_BOUND
Expand All @@ -14,7 +20,7 @@ from src.errors import Errors
from src.utils.uint256 import uint256_eq
from src.utils.utils import Helpers
from src.utils.array import slice
from src.utils.signature import Signature
from src.utils.signature import Signature, uint256_to_uint384
from src.utils.maths import unsigned_div_rem

// @title EcRecover Precompile related functions.
Expand All @@ -38,8 +44,12 @@ namespace PrecompileEcRecover {
func run{
pedersen_ptr: HashBuiltin*,
range_check_ptr,
range_check96_ptr: felt*,
add_mod_ptr: ModBuiltin*,
mul_mod_ptr: ModBuiltin*,
bitwise_ptr: BitwiseBuiltin*,
keccak_ptr: KeccakBuiltin*,
poseidon_ptr: PoseidonBuiltin*,
}(_address: felt, input_len: felt, input: felt*) -> (
output_len: felt, output: felt*, gas_used: felt, reverted: felt
) {
Expand All @@ -56,7 +66,8 @@ namespace PrecompileEcRecover {
return (0, output, GAS_COST_EC_RECOVER, 0);
}

let msg_hash_bigint = Helpers.bytes32_to_bigint(input_padded);
let msg_hash_uint256 = Helpers.bytes32_to_uint256(input_padded);
let (msg_hash_uint384) = uint256_to_uint384(msg_hash_uint256);
let r = Helpers.bytes_to_uint256(32, input_padded + 32 * 2);
let s = Helpers.bytes_to_uint256(32, input_padded + 32 * 3);

Expand All @@ -77,10 +88,10 @@ namespace PrecompileEcRecover {
return (0, output, GAS_COST_EC_RECOVER, 0);
}

let (r_bigint) = uint256_to_bigint(r);
let (s_bigint) = uint256_to_bigint(s);
let (r_uint384) = uint256_to_uint384(r);
let (s_uint384) = uint256_to_uint384(s);
let (success, recovered_address) = Signature.try_recover_eth_address(
msg_hash_bigint, r_bigint, s_bigint, v - 27
msg_hash_uint384, r_uint384, s_uint384, v - 27
);

if (success == 0) {
Expand Down
Loading
Loading