diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 32953a0bfc5..88c3fd09f58 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -6,17 +6,21 @@ use std::hash::RandomState; use ark_ec::short_weierstrass::{Affine, Projective, SWCurveConfig}; use ark_ff::PrimeField; use cairo_native::starknet::{ - ExecutionInfo, ExecutionInfoV2, Secp256k1Point, Secp256r1Point, StarknetSyscallHandler, - SyscallResult, U256, + ExecutionInfo, + ExecutionInfoV2, + Secp256k1Point, + Secp256r1Point, + StarknetSyscallHandler, + SyscallResult, + U256, }; -use cairo_native::starknet_stub::u256_to_biguint; +use cairo_native::starknet_stub::{big4int_to_u256, encode_str_as_felts, u256_to_biguint}; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::state::StorageKey; use starknet_types_core::felt::Felt; use crate::execution::call_info::{CallInfo, OrderedEvent, OrderedL2ToL1Message, Retdata}; use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext}; -use crate::execution::native::utils::encode_str_as_felts; use crate::execution::secp; use crate::execution::syscalls::hint_processor::{SyscallCounter, OUT_OF_GAS_ERROR}; use crate::execution::syscalls::SyscallSelector; @@ -62,7 +66,6 @@ impl<'state> NativeSyscallHandler<'state> { } } - #[allow(dead_code)] fn increment_syscall_count_by(&mut self, selector: &SyscallSelector, n: usize) { let syscall_count = self.syscall_counter.entry(*selector).or_default(); *syscall_count += n @@ -97,7 +100,6 @@ impl<'state> NativeSyscallHandler<'state> { /// Handles all gas-related logics and additional metadata such as `SyscallCounter`. In native, /// we need to explicitly call this method at the beginning of each syscall. - #[allow(dead_code)] fn pre_execute_syscall( &mut self, remaining_gas: &mut u128, @@ -115,8 +117,10 @@ impl<'state> NativeSyscallHandler<'state> { if *remaining_gas < required_gas { // Out of gas failure. - return Err(vec![Felt::from_hex(OUT_OF_GAS_ERROR) - .expect("Failed to parse OUT_OF_GAS_ERROR hex string")]); + return Err(vec![ + Felt::from_hex(OUT_OF_GAS_ERROR) + .expect("Failed to parse OUT_OF_GAS_ERROR hex string"), + ]); } *remaining_gas -= required_gas; @@ -267,55 +271,142 @@ 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> { - todo!("Implement secp256r1_new syscall."); + self.pre_execute_syscall( + remaining_gas, + SyscallSelector::Secp256r1New, + self.context.gas_costs().secp256r1_new_gas_cost, + )?; + + Secp256Point::new(x, y).map(|op| op.map(|p| p.into())) } fn secp256r1_add( &mut self, - _p0: Secp256r1Point, - _p1: Secp256r1Point, - _remaining_gas: &mut u128, + p0: Secp256r1Point, + p1: Secp256r1Point, + remaining_gas: &mut u128, ) -> SyscallResult { - todo!("Implement secp256r1_add syscall."); + self.pre_execute_syscall( + remaining_gas, + SyscallSelector::Secp256r1Add, + self.context.gas_costs().secp256r1_new_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 { - todo!("Implement secp256r1_mul syscall."); + self.pre_execute_syscall( + remaining_gas, + SyscallSelector::Secp256r1Mul, + self.context.gas_costs().secp256r1_new_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> { - todo!("Implement secp256r1_get_point_from_x syscall."); + self.pre_execute_syscall( + remaining_gas, + SyscallSelector::Secp256r1GetPointFromX, + self.context.gas_costs().secp256r1_new_gas_cost, + )?; + + Secp256Point::get_point_from_x(x, y_parity).map(|op| op.map(|p| p.into())) } 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, + SyscallSelector::Secp256r1GetXy, + self.context.gas_costs().secp256r1_get_xy_gas_cost, + )?; + + Ok((p.x, p.y)) } fn sha256_process_block( &mut self, - _prev_state: &mut [u32; 8], - _current_block: &[u32; 16], - _remaining_gas: &mut u128, + prev_state: &mut [u32; 8], + current_block: &[u32; 16], + remaining_gas: &mut u128, ) -> SyscallResult<()> { - todo!("Implement sha256_process_block syscall."); + const SHA256_STATE_SIZE: usize = 8; + self.pre_execute_syscall( + remaining_gas, + SyscallSelector::Sha256ProcessBlock, + self.context.gas_costs().sha256_process_block_gas_cost, + )?; + + let data_as_bytes = sha2::digest::generic_array::GenericArray::from_exact_iter( + current_block.iter().flat_map(|x| x.to_be_bytes()), + ) + .expect( + "u32.to_be_bytes() returns 4 bytes, and data.len() == 16. So data contains 64 bytes.", + ); + let mut state: [u32; SHA256_STATE_SIZE] = *prev_state; + sha2::compress256(&mut state, &[data_as_bytes]); + + prev_state.copy_from_slice(&state); + + Ok(()) + } +} + +impl From> for Secp256k1Point { + fn from(Secp256Point(Affine { x, y, infinity }): Secp256Point) -> Self { + Secp256k1Point { + x: big4int_to_u256(x.into()), + y: big4int_to_u256(y.into()), + is_infinity: infinity, + } + } +} + +impl From> for Secp256r1Point { + fn from(Secp256Point(Affine { x, y, infinity }): Secp256Point) -> Self { + Secp256r1Point { + x: big4int_to_u256(x.into()), + y: big4int_to_u256(y.into()), + is_infinity: infinity, + } + } +} + +impl From for Secp256Point { + 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 for Secp256Point { + 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, + }) } } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs index a3decf37b16..1bf2aeb03b8 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs @@ -27,6 +27,10 @@ fn test_secp256k1(test_contract: FeatureContract, expected_gas: u64) { ); } +#[cfg_attr( + feature = "cairo_native", + test_case(FeatureContract::TestContract(CairoVersion::Native), 339380; "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();