From 6040a7c4cefb2a4e5c7dc2f5f94efc134d094350 Mon Sep 17 00:00:00 2001 From: Bohdan Ohorodnii Date: Mon, 14 Oct 2024 13:33:51 +0300 Subject: [PATCH] feat: add support for `keccak` syscall --- .../src/execution/native/syscall_handler.rs | 47 +++++++++++++++++-- .../syscalls/syscall_tests/keccak.rs | 6 ++- .../execution/syscalls/syscall_tests/secp.rs | 2 +- 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index 311d9457970..8003d2dd01e 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -25,6 +25,7 @@ use crate::execution::secp; use crate::execution::syscalls::hint_processor::{ SyscallCounter, SyscallExecutionError, + INVALID_INPUT_LENGTH_ERROR, OUT_OF_GAS_ERROR, }; use crate::execution::syscalls::SyscallSelector; @@ -105,7 +106,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, @@ -227,8 +227,49 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { todo!("Implement send_message_to_l1 syscall."); } - fn keccak(&mut self, _input: &[u64], _remaining_gas: &mut u128) -> SyscallResult { - todo!("Implement keccak syscall."); + fn keccak(&mut self, input: &[u64], remaining_gas: &mut u128) -> SyscallResult { + self.pre_execute_syscall( + remaining_gas, + SyscallSelector::Keccak, + self.context.gas_costs().keccak_gas_cost, + )?; + + const KECCAK_FULL_RATE_IN_WORDS: usize = 17; + + let input_length = input.len(); + let (n_rounds, remainder) = num_integer::div_rem(input_length, KECCAK_FULL_RATE_IN_WORDS); + + if remainder != 0 { + // In VM this error is wrapped into `SyscallExecutionError::SyscallError` + return Err(vec![Felt::from_hex(INVALID_INPUT_LENGTH_ERROR).unwrap()]); + } + + // TODO(Ori, 1/2/2024): Write an indicative expect message explaining why the conversion + // works. + let n_rounds_as_u128 = u128::try_from(n_rounds).expect("Failed to convert usize to u128."); + let gas_cost = + n_rounds_as_u128 * u128::from(self.context.gas_costs().keccak_round_cost_gas_cost); + + if gas_cost > *remaining_gas { + // In VM this error is wrapped into `SyscallExecutionError::SyscallError` + return Err(vec![Felt::from_hex(OUT_OF_GAS_ERROR).unwrap()]); + } + *remaining_gas -= gas_cost; + + self.increment_syscall_count_by(&SyscallSelector::Keccak, n_rounds); + + let mut state = [0u64; 25]; + for chunk in input.chunks(KECCAK_FULL_RATE_IN_WORDS) { + for (i, val) in chunk.iter().enumerate() { + state[i] ^= val; + } + keccak::f1600(&mut state) + } + + Ok(U256 { + hi: u128::from(state[2]) | (u128::from(state[3]) << 64), + lo: u128::from(state[0]) | (u128::from(state[1]) << 64), + }) } fn secp256k1_new( diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs index 3327f40c40e..18cef7001af 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/keccak.rs @@ -11,6 +11,10 @@ use crate::test_utils::initial_test_state::test_state; use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE}; #[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 254910; "VM")] +#[cfg_attr( + feature = "cairo_native", + test_case(FeatureContract::TestContract(CairoVersion::Native), 265110; "Native") +)] fn test_keccak(test_contract: FeatureContract, expected_gas: u64) { let chain_info = &ChainInfo::create_for_testing(); let mut state = test_state(chain_info, BALANCE, &[(test_contract, 1)]); @@ -22,7 +26,7 @@ fn test_keccak(test_contract: FeatureContract, expected_gas: u64) { ..trivial_external_entry_point_new(test_contract) }; - assert_eq!( + pretty_assertions::assert_eq!( entry_point_call.execute_directly(&mut state).unwrap().execution, CallExecution { gas_consumed: expected_gas, ..CallExecution::from_retdata(retdata![]) } ); diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs index f28bea9adc8..249b3f8874f 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/secp.rs @@ -11,7 +11,7 @@ use crate::test_utils::{trivial_external_entry_point_new, CairoVersion, BALANCE} #[cfg_attr( feature = "cairo_native", - test_case(FeatureContract::TestContract(CairoVersion::Native), 674500; "Native") + test_case(FeatureContract::TestContract(CairoVersion::Native), 17046546; "Native") )] #[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 17034156; "VM")] fn test_secp256k1(test_contract: FeatureContract, expected_gas: u64) {