Skip to content

Commit

Permalink
feat(blockifier): add Secp256r1 cairo native syscalls
Browse files Browse the repository at this point in the history
  • Loading branch information
PearsonWhite authored and rodrigo-pino committed Nov 18, 2024
1 parent 5de4d73 commit 2b9e215
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
98 changes: 78 additions & 20 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cairo_native::starknet::{
TxV2Info,
U256,
};
use cairo_native::starknet_stub::u256_to_biguint;
use cairo_native::starknet_stub::{big4int_to_u256, u256_to_biguint};
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
use starknet_api::contract_class::EntryPointType;
use starknet_api::core::{
Expand Down Expand Up @@ -667,46 +667,65 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

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

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

fn secp256r1_add(
&mut self,
_p0: Secp256r1Point,
_p1: Secp256r1Point,
_remaining_gas: &mut u128,
p0: Secp256r1Point,
p1: Secp256r1Point,
remaining_gas: &mut u128,
) -> SyscallResult<Secp256r1Point> {
todo!("Implement secp256r1_add syscall.");
self.pre_execute_syscall(remaining_gas, self.context.gas_costs().secp256r1_add_gas_cost)?;
Ok(Secp256Point::add(p0.into(), p1.into()).into())
}

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

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

fn secp256r1_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<Secp256r1Point>> {
todo!("Implement secp256r1_get_point_from_x syscall.");
self.pre_execute_syscall(
remaining_gas,
self.context.gas_costs().secp256r1_get_point_from_x_gas_cost,
)?;

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

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

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

fn sha256_process_block(
Expand Down Expand Up @@ -740,6 +759,45 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {
/// secp256k1 and secp256r1 curves through the generic `Curve` parameter.
#[derive(PartialEq, Clone, Copy)]
struct Secp256Point<Curve: SWCurveConfig>(Affine<Curve>);
impl From<Secp256Point<ark_secp256k1::Config>> for Secp256k1Point {
fn from(Secp256Point(Affine { x, y, infinity }): Secp256Point<ark_secp256k1::Config>) -> Self {
Secp256k1Point {
x: big4int_to_u256(x.into()),
y: big4int_to_u256(y.into()),
is_infinity: infinity,
}
}
}

impl From<Secp256Point<ark_secp256r1::Config>> for Secp256r1Point {
fn from(Secp256Point(Affine { x, y, infinity }): Secp256Point<ark_secp256r1::Config>) -> Self {
Secp256r1Point {
x: big4int_to_u256(x.into()),
y: big4int_to_u256(y.into()),
is_infinity: infinity,
}
}
}

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(),
infinity: p.is_infinity,
})
}
}

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(),
infinity: p.is_infinity,
})
}
}

// todo(xrvdg) remove dead_code annotation after adding syscalls
#[allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ fn test_secp256k1(test_contract: FeatureContract, expected_gas: u64) {
);
}

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

0 comments on commit 2b9e215

Please sign in to comment.