From 4020b0b63bcd8d94cdb696a8f199a3b18b7feef4 Mon Sep 17 00:00:00 2001 From: Bohdan Ohorodnii Date: Mon, 11 Nov 2024 14:17:03 +0200 Subject: [PATCH] feat: add support for `Secp256k1` syscall --- .../src/execution/native/syscall_handler.rs | 58 +++++++++++++------ .../execution/syscalls/syscall_tests/secp.rs | 4 ++ 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 892cdb9d75..ce335efa8e 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -631,46 +631,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> { - 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 { - 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 { - 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> { - 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( diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs index 23e518ae1a..af01610ddf 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs @@ -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();