From 9eebe2751712c053efa4f87b9eb0cdc080e29c40 Mon Sep 17 00:00:00 2001 From: Bohdan Ohorodnii Date: Wed, 23 Oct 2024 19:21:00 +0300 Subject: [PATCH] feat(blockifier): add support for `sha256_process_block` syscall --- crates/blockifier/cairo_native | 2 +- .../src/execution/native/syscall_handler.rs | 24 +++++++++++++++---- .../syscalls/syscall_tests/sha256.rs | 6 ++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/crates/blockifier/cairo_native b/crates/blockifier/cairo_native index 26e1c67966..ab478323d6 160000 --- a/crates/blockifier/cairo_native +++ b/crates/blockifier/cairo_native @@ -1 +1 @@ -Subproject commit 26e1c67966b93acd8b183823aae6f3e4fdc5c78c +Subproject commit ab478323d6aee5e0424712bbde98de443b8cc72f diff --git a/crates/blockifier/src/execution/native/syscall_handler.rs b/crates/blockifier/src/execution/native/syscall_handler.rs index ac31b39b4b..9e8101bf11 100644 --- a/crates/blockifier/src/execution/native/syscall_handler.rs +++ b/crates/blockifier/src/execution/native/syscall_handler.rs @@ -96,7 +96,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, @@ -312,10 +311,25 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> { 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."); + 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.", + ); + + sha2::compress256(prev_state, &[data_as_bytes]); + + Ok(()) } } diff --git a/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs b/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs index bdeb68c5d6..51e19b9330 100644 --- a/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs +++ b/crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs @@ -10,6 +10,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), 891625; "Native") +)] #[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 881425; "VM")] fn test_sha256(test_contract: FeatureContract, gas_consumed: u64) { let chain_info = &ChainInfo::create_for_testing(); @@ -22,7 +26,7 @@ fn test_sha256(test_contract: FeatureContract, gas_consumed: 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, ..CallExecution::from_retdata(retdata![]) } );