diff --git a/corelib/src/sha256.cairo b/corelib/src/sha256.cairo index e00ff045eb8..f5a82b1cb44 100644 --- a/corelib/src/sha256.cairo +++ b/corelib/src/sha256.cairo @@ -33,7 +33,7 @@ pub fn compute_sha256_u32_array( let mut ind = 0; while ind != arr.len() { - let input: Span = arr.slice(ind, 16); + let input: Box<[u32; 16]> = *arr.slice(ind, 16).try_into().unwrap(); state = starknet::syscalls::sha256_process_block_syscall(state, input).unwrap_syscall(); ind = ind + 16; }; diff --git a/corelib/src/starknet/syscalls.cairo b/corelib/src/starknet/syscalls.cairo index 174af708e10..322833a81e5 100644 --- a/corelib/src/starknet/syscalls.cairo +++ b/corelib/src/starknet/syscalls.cairo @@ -103,5 +103,5 @@ pub extern fn keccak_syscall( /// The system call does not add any padding and the input needs to be a multiple of 512 bits /// (== 16 u32 word). pub extern fn sha256_process_block_syscall( - state: core::sha256::Sha256StateHandle, input: Span + state: core::sha256::Sha256StateHandle, input: Box<[u32; 16]> ) -> SyscallResult implicits(GasBuiltin, System) nopanic; diff --git a/crates/cairo-lang-runner/src/casm_run/mod.rs b/crates/cairo-lang-runner/src/casm_run/mod.rs index 5e9619ade71..9c2e3bef5ff 100644 --- a/crates/cairo-lang-runner/src/casm_run/mod.rs +++ b/crates/cairo-lang-runner/src/casm_run/mod.rs @@ -689,7 +689,7 @@ impl<'a> CairoHintProcessor<'a> { sha_256_process_block( gas_counter, system_buffer.next_fixed_size_arr_pointer(8)?, - system_buffer.next_arr()?, + system_buffer.next_fixed_size_arr_pointer(16)?, exec_scopes, system_buffer, ) @@ -1295,10 +1295,6 @@ fn sha_256_process_block( vm: &mut dyn VMWrapper, ) -> Result { deduct_gas!(gas_counter, SHA256_PROCESS_BLOCK); - if data.len() != 16 { - fail_syscall!(b"Invalid sha256_chunk input size"); - } - let data_as_bytes = sha2::digest::generic_array::GenericArray::from_exact_iter( data.iter().flat_map(|felt| felt.to_bigint().to_u32().unwrap().to_be_bytes()), ) diff --git a/crates/cairo-lang-sierra-gas/src/starknet_libfunc_cost_base.rs b/crates/cairo-lang-sierra-gas/src/starknet_libfunc_cost_base.rs index 36aacff3a24..a36a999efbd 100644 --- a/crates/cairo-lang-sierra-gas/src/starknet_libfunc_cost_base.rs +++ b/crates/cairo-lang-sierra-gas/src/starknet_libfunc_cost_base.rs @@ -45,7 +45,7 @@ pub fn starknet_libfunc_cost_base(libfunc: &StarkNetConcreteLibfunc) -> Vec syscall_cost(0), StarkNetConcreteLibfunc::Deploy(_) => syscall_cost(5), StarkNetConcreteLibfunc::Keccak(_) => syscall_cost(2), - StarkNetConcreteLibfunc::Sha256ProcessBlock(_) => syscall_cost(3), + StarkNetConcreteLibfunc::Sha256ProcessBlock(_) => syscall_cost(2), StarkNetConcreteLibfunc::Sha256StateHandleInit(_) => vec![steps(0)], StarkNetConcreteLibfunc::Sha256StateHandleDigest(_) => vec![steps(0)], StarkNetConcreteLibfunc::LibraryCall(_) => syscall_cost(4), diff --git a/crates/cairo-lang-sierra-to-casm/src/invocations/starknet/mod.rs b/crates/cairo-lang-sierra-to-casm/src/invocations/starknet/mod.rs index dedf7747e08..fcca679d212 100644 --- a/crates/cairo-lang-sierra-to-casm/src/invocations/starknet/mod.rs +++ b/crates/cairo-lang-sierra-to-casm/src/invocations/starknet/mod.rs @@ -68,7 +68,7 @@ pub fn build( } StarkNetConcreteLibfunc::Keccak(_) => build_syscalls(builder, "Keccak", [2], [2]), StarkNetConcreteLibfunc::Sha256ProcessBlock(_) => { - build_syscalls(builder, "Sha256ProcessBlock", [1, 2], [1]) + build_syscalls(builder, "Sha256ProcessBlock", [1, 1], [1]) } StarkNetConcreteLibfunc::Sha256StateHandleInit(_) => build_identity(builder), StarkNetConcreteLibfunc::Sha256StateHandleDigest(_) => build_identity(builder), diff --git a/crates/cairo-lang-sierra/src/extensions/modules/starknet/mod.rs b/crates/cairo-lang-sierra/src/extensions/modules/starknet/mod.rs index 913f0be93b7..bf8b6319d59 100644 --- a/crates/cairo-lang-sierra/src/extensions/modules/starknet/mod.rs +++ b/crates/cairo-lang-sierra/src/extensions/modules/starknet/mod.rs @@ -45,7 +45,7 @@ use self::syscalls::{ use self::testing::TestingLibfunc; use super::array::ArrayType; use super::felt252::Felt252Type; -use super::int::unsigned::{Uint32Type, Uint64Type}; +use super::int::unsigned::Uint64Type; use super::snapshot::snapshot_ty; use super::structure::StructType; use super::try_from_felt252::TryFromFelt252Libfunc; @@ -129,10 +129,3 @@ fn u64_span_ty( ) -> Result { span_ty(context, context.get_concrete_type(Uint64Type::id(), &[])?, "core::integer::u64") } - -/// User type for `Span`. -fn u32_span_ty( - context: &dyn SignatureSpecializationContext, -) -> Result { - span_ty(context, context.get_concrete_type(Uint32Type::id(), &[])?, "core::integer::u32") -} diff --git a/crates/cairo-lang-sierra/src/extensions/modules/starknet/syscalls.rs b/crates/cairo-lang-sierra/src/extensions/modules/starknet/syscalls.rs index e62df05d512..bd52f006f61 100644 --- a/crates/cairo-lang-sierra/src/extensions/modules/starknet/syscalls.rs +++ b/crates/cairo-lang-sierra/src/extensions/modules/starknet/syscalls.rs @@ -1,7 +1,7 @@ -use itertools::chain; +use itertools::{chain, repeat_n}; use super::interoperability::ClassHashType; -use super::{u32_span_ty, u64_span_ty}; +use super::u64_span_ty; use crate::extensions::array::ArrayType; use crate::extensions::boxing::box_ty; use crate::extensions::felt252::Felt252Type; @@ -188,7 +188,7 @@ impl SyscallGenericLibfunc for Sha256ProcessBlockLibfunc { // Previous state of the hash. context.get_concrete_type(Sha256StateHandleType::id(), &[])?, // The current block to process. - u32_span_ty(context)?, + boxed_u32_fixed_array_ty(context, 16)?, ]) } @@ -237,21 +237,18 @@ impl NoGenericArgsGenericLibfunc for Sha256StateHandleDigestLibfunc { pub fn sha256_state_handle_unwrapped_type( context: &dyn SignatureSpecializationContext, ) -> Result { - box_ty( - context, - context.get_concrete_type( - StructType::id(), - &[ - GenericArg::UserType(UserTypeId::from_string("Tuple")), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), - ], - )?, + boxed_u32_fixed_array_ty(context, 8) +} + +/// Returns a fixed type array of the given type and size. +fn boxed_u32_fixed_array_ty( + context: &dyn SignatureSpecializationContext, + size: usize, +) -> Result { + let args: Vec = chain!( + [GenericArg::UserType(UserTypeId::from_string("Tuple"))], + repeat_n(GenericArg::Type(context.get_concrete_type(Uint32Type::id(), &[])?), size) ) + .collect(); + box_ty(context, context.get_concrete_type(StructType::id(), &args)?) }