Skip to content

Commit

Permalink
feat: add support for sha256_process_block syscall
Browse files Browse the repository at this point in the history
  • Loading branch information
varex83 committed Oct 23, 2024
1 parent 8052b51 commit a8a4e1b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
24 changes: 20 additions & 4 deletions crates/blockifier/src/execution/native/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,26 @@ impl<'state> StarknetSyscallHandler for &mut NativeSyscallHandler<'state> {

fn sha256_process_block(
&mut self,
_prev_state: &[u32; 8],
_current_block: &[u32; 16],
_remaining_gas: &mut u128,
prev_state: &[u32; 8],
current_block: &[u32; 16],
remaining_gas: &mut u128,
) -> SyscallResult<[u32; 8]> {
todo!("Implement sha256_process_block syscall.");
self.substract_syscall_gas_cost(
remaining_gas,
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 = prev_state.clone();

sha2::compress256(&mut state, &[data_as_bytes]);

Ok(state)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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};

#[test_case(FeatureContract::TestContract(CairoVersion::Native), 891625; "Native")]
#[test_case(FeatureContract::TestContract(CairoVersion::Cairo1), 881625; "VM")]
fn test_sha256(test_contract: FeatureContract, gas_consumed: u64) {
let chain_info = &ChainInfo::create_for_testing();
Expand All @@ -22,7 +23,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![]) }
);
Expand Down

0 comments on commit a8a4e1b

Please sign in to comment.