Skip to content

Commit

Permalink
Changed sha256 compress signature.
Browse files Browse the repository at this point in the history
commit-id:de150c40
  • Loading branch information
orizi committed May 21, 2024
1 parent bde5f45 commit 7b2cd00
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 36 deletions.
2 changes: 1 addition & 1 deletion corelib/src/sha256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn compute_sha256_u32_array(
let mut ind = 0;

while ind != arr.len() {
let input: Span<u32> = 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;
};
Expand Down
2 changes: 1 addition & 1 deletion corelib/src/starknet/syscalls.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>
state: core::sha256::Sha256StateHandle, input: Box<[u32; 16]>
) -> SyscallResult<core::sha256::Sha256StateHandle> implicits(GasBuiltin, System) nopanic;
6 changes: 1 addition & 5 deletions crates/cairo-lang-runner/src/casm_run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -1295,10 +1295,6 @@ fn sha_256_process_block(
vm: &mut dyn VMWrapper,
) -> Result<SyscallResult, HintError> {
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()),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn starknet_libfunc_cost_base(libfunc: &StarkNetConcreteLibfunc) -> Vec<Cons
| StarkNetConcreteLibfunc::GetExecutionInfoV2(_) => 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,10 +129,3 @@ fn u64_span_ty(
) -> Result<ConcreteTypeId, SpecializationError> {
span_ty(context, context.get_concrete_type(Uint64Type::id(), &[])?, "core::integer::u64")
}

/// User type for `Span<u32>`.
fn u32_span_ty(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
span_ty(context, context.get_concrete_type(Uint32Type::id(), &[])?, "core::integer::u32")
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)?,
])
}

Expand Down Expand Up @@ -237,21 +237,18 @@ impl NoGenericArgsGenericLibfunc for Sha256StateHandleDigestLibfunc {
pub fn sha256_state_handle_unwrapped_type(
context: &dyn SignatureSpecializationContext,
) -> Result<ConcreteTypeId, SpecializationError> {
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<ConcreteTypeId, SpecializationError> {
let args: Vec<GenericArg> = 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)?)
}

0 comments on commit 7b2cd00

Please sign in to comment.