Skip to content

Commit

Permalink
feat: add support for Secp256k1 syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
varex83 authored and PearsonWhite committed Nov 19, 2024
1 parent ae1bdb1 commit d053100
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 26 deletions.
101 changes: 75 additions & 26 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::hash::RandomState;
use std::sync::Arc;

use ark_ec::short_weierstrass::{Affine, Projective, SWCurveConfig};
use ark_ff::PrimeField;
use ark_ff::{BigInt, PrimeField};
use cairo_native::starknet::{
BlockInfo,
ExecutionInfo,
Expand All @@ -18,8 +18,8 @@ use cairo_native::starknet::{
TxV2Info,
U256,
};
use cairo_native::starknet_stub::{big4int_to_u256, u256_to_biguint};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use num_bigint::BigUint;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{
calculate_contract_address,
Expand Down Expand Up @@ -281,7 +281,7 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

match syscall_base::get_block_hash_base(self.context, block_number, self.state) {
Ok(value) => Ok(value),
Err(e) => Err(self.handle_error(remaining_gas, e.into())),
Err(e) => Err(self.handle_error(remaining_gas, e)),
}
}

Expand Down Expand Up @@ -650,46 +650,66 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

fn secp256k1_new(
&mut self,
_x: U256,
_y: U256,
_remaining_gas: &mut u128,
x: U256,
y: U256,
remaining_gas: &mut u128,
) -> SyscallResult<Option<Secp256k1Point>> {
todo!("Implement secp256k1_new syscall.");
self.pre_execute_syscall(remaining_gas, self.context.gas_costs().secp256k1_new_gas_cost)?;

Secp256Point::new(x, y)
.map(|option| option.map(|p| p.into()))
.map_err(|e| self.handle_error(remaining_gas, e))
}

fn secp256k1_add(
&mut self,
_p0: Secp256k1Point,
_p1: Secp256k1Point,
_remaining_gas: &mut u128,
p0: Secp256k1Point,
p1: Secp256k1Point,
remaining_gas: &mut u128,
) -> SyscallResult<Secp256k1Point> {
todo!("Implement secp256k1_add syscall.");
self.pre_execute_syscall(remaining_gas, self.context.gas_costs().secp256k1_add_gas_cost)?;

Ok(Secp256Point::add(p0.into(), p1.into()).into())
}

fn secp256k1_mul(
&mut self,
_p: Secp256k1Point,
_m: U256,
_remaining_gas: &mut u128,
p: Secp256k1Point,
m: U256,
remaining_gas: &mut u128,
) -> SyscallResult<Secp256k1Point> {
todo!("Implement secp256k1_mul syscall.");
self.pre_execute_syscall(remaining_gas, self.context.gas_costs().secp256k1_mul_gas_cost)?;

Ok(Secp256Point::mul(p.into(), m).into())
}

fn secp256k1_get_point_from_x(
&mut self,
_x: U256,
_y_parity: bool,
_remaining_gas: &mut u128,
x: U256,
y_parity: bool,
remaining_gas: &mut u128,
) -> SyscallResult<Option<Secp256k1Point>> {
todo!("Implement secp256k1_get_point_from_x syscall.");
self.pre_execute_syscall(
remaining_gas,
self.context.gas_costs().secp256k1_get_point_from_x_gas_cost,
)?;

Secp256Point::get_point_from_x(x, y_parity)
.map(|option| option.map(|p| p.into()))
.map_err(|e| self.handle_error(remaining_gas, e))
}

fn secp256k1_get_xy(
&mut self,
_p: Secp256k1Point,
_remaining_gas: &mut u128,
p: Secp256k1Point,
remaining_gas: &mut u128,
) -> SyscallResult<(U256, U256)> {
todo!("Implement secp256k1_get_xy syscall.");
self.pre_execute_syscall(
remaining_gas,
self.context.gas_costs().secp256k1_get_xy_gas_cost,
)?;

Ok((p.x, p.y))
}

fn secp256r1_new(
Expand Down Expand Up @@ -809,8 +829,8 @@ impl From<Secp256Point<ark_secp256r1::Config>> for Secp256r1Point {
impl From<Secp256k1Point> for Secp256Point<ark_secp256k1::Config> {
fn from(p: Secp256k1Point) -> Self {
Secp256Point(Affine {
x: u256_to_biguint(p.x).into(),
y: u256_to_biguint(p.y).into(),
x: u256_to_big4int(p.x).into(),
y: u256_to_big4int(p.y).into(),
infinity: p.is_infinity,
})
}
Expand All @@ -819,8 +839,8 @@ impl From<Secp256k1Point> for Secp256Point<ark_secp256k1::Config> {
impl From<Secp256r1Point> for Secp256Point<ark_secp256r1::Config> {
fn from(p: Secp256r1Point) -> Self {
Secp256Point(Affine {
x: u256_to_biguint(p.x).into(),
y: u256_to_biguint(p.y).into(),
x: u256_to_big4int(p.x).into(),
y: u256_to_big4int(p.y).into(),
infinity: p.is_infinity,
})
}
Expand Down Expand Up @@ -880,6 +900,35 @@ impl<Curve: SWCurveConfig> fmt::Debug for Secp256Point<Curve> {
}
}

fn u256_to_biguint(u256: U256) -> BigUint {
let lo = BigUint::from(u256.lo);
let hi = BigUint::from(u256.hi);

(hi << 128) + lo
}

fn big4int_to_u256(b_int: BigInt<4>) -> U256 {
let [a, b, c, d] = b_int.0;

let lo = u128::from(a) | (u128::from(b) << 64);
let hi = u128::from(c) | (u128::from(d) << 64);

U256 { lo, hi }
}

fn u256_to_big4int(u256: U256) -> BigInt<4> {
fn to_u64s(bytes: [u8; 16]) -> (u64, u64) {
let lo_bytes: [u8; 8] = bytes[0..8].try_into().expect("Take high bytes");
let lo: u64 = u64::from_le_bytes(lo_bytes);
let hi_bytes: [u8; 8] = bytes[8..16].try_into().expect("Take low bytes");
let hi: u64 = u64::from_le_bytes(hi_bytes);
(lo, hi)
}
let (hi_lo, hi_hi) = to_u64s(u256.hi.to_le_bytes());
let (lo_lo, lo_hi) = to_u64s(u256.lo.to_le_bytes());
BigInt::new([lo_lo, lo_hi, hi_lo, hi_hi])
}

#[cfg(test)]
mod test {
use cairo_native::starknet::U256;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use crate::test_utils::contracts::FeatureContract;
use crate::test_utils::initial_test_state::test_state;
use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE};

#[cfg_attr(
feature = "cairo_native",
test_case(FeatureContract::TestContract(CairoVersion::Native), 17044156; "Native")
)]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 17034156; "VM")]
fn test_secp256k1(test_contract: FeatureContract, expected_gas: u64) {
let chain_info = &ChainInfo::create_for_testing();
Expand Down

0 comments on commit d053100

Please sign in to comment.