Skip to content

Commit fd6bc34

Browse files
starknet_os: rename generic SHA names to SHA256 (#14205)
1 parent 8f4a522 commit fd6bc34

10 files changed

Lines changed: 31 additions & 65 deletions

File tree

crates/blockifier/src/execution/syscalls/syscall_executor.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,8 @@ use crate::execution::syscalls::vm_syscall_utils::{
5151
ReplaceClassResponse,
5252
SendMessageToL1Request,
5353
SendMessageToL1Response,
54-
Sha256ProcessBlockRequest,
55-
Sha256ProcessBlockResponse,
56-
Sha512ProcessBlockRequest,
57-
Sha512ProcessBlockResponse,
54+
ShaProcessBlockRequest,
55+
ShaProcessBlockResponse,
5856
StorageReadRequest,
5957
StorageReadResponse,
6058
StorageWriteRequest,
@@ -218,11 +216,11 @@ pub trait SyscallExecutor {
218216
) -> Result<MetaTxV0Response, Self::Error>;
219217

220218
fn sha256_process_block(
221-
request: Sha256ProcessBlockRequest,
219+
request: ShaProcessBlockRequest,
222220
vm: &mut VirtualMachine,
223221
syscall_handler: &mut Self,
224222
_remaining_gas: &mut u64,
225-
) -> Result<Sha256ProcessBlockResponse, Self::Error> {
223+
) -> Result<ShaProcessBlockResponse, Self::Error> {
226224
const SHA256_BLOCK_SIZE: usize = 16;
227225

228226
let data = vm
@@ -260,15 +258,15 @@ pub trait SyscallExecutor {
260258
state_as_words.iter().map(|&arg| MaybeRelocatable::from(Felt::from(arg))).collect();
261259
let response = syscall_handler.write_sha256_out_state(&data, vm)?;
262260

263-
Ok(Sha256ProcessBlockResponse { state_ptr: response })
261+
Ok(ShaProcessBlockResponse { state_ptr: response })
264262
}
265263

266264
fn sha512_process_block(
267-
request: Sha512ProcessBlockRequest,
265+
request: ShaProcessBlockRequest,
268266
vm: &mut VirtualMachine,
269267
syscall_handler: &mut Self,
270268
_remaining_gas: &mut u64,
271-
) -> Result<Sha512ProcessBlockResponse, Self::Error> {
269+
) -> Result<ShaProcessBlockResponse, Self::Error> {
272270
const SHA512_BLOCK_SIZE: usize = 16;
273271

274272
let data = vm
@@ -306,7 +304,7 @@ pub trait SyscallExecutor {
306304
state_as_words.iter().map(|&arg| MaybeRelocatable::from(Felt::from(arg))).collect();
307305
let response = syscall_handler.write_sha512_out_state(&data, vm)?;
308306

309-
Ok(Sha512ProcessBlockResponse { state_ptr: response })
307+
Ok(ShaProcessBlockResponse { state_ptr: response })
310308
}
311309

312310
fn replace_class(

crates/blockifier/src/execution/syscalls/vm_syscall_utils.rs

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -505,64 +505,32 @@ impl SyscallResponse for KeccakResponse {
505505
}
506506
}
507507

508-
// Sha256ProcessBlock syscall.
508+
// ShaProcessBlock syscall. Used for both SHA-256 and SHA-512.
509509
#[derive(Debug, Eq, PartialEq)]
510-
pub struct Sha256ProcessBlockRequest {
510+
pub struct ShaProcessBlockRequest {
511511
pub state_ptr: Relocatable,
512512
pub input_start: Relocatable,
513513
}
514514

515-
impl SyscallRequest for Sha256ProcessBlockRequest {
515+
impl SyscallRequest for ShaProcessBlockRequest {
516516
fn read(
517517
vm: &VirtualMachine,
518518
ptr: &mut Relocatable,
519-
) -> SyscallBaseResult<Sha256ProcessBlockRequest> {
519+
) -> SyscallBaseResult<ShaProcessBlockRequest> {
520520
let state_start = vm.get_relocatable(*ptr)?;
521521
*ptr = (*ptr + 1)?;
522522
let input_start = vm.get_relocatable(*ptr)?;
523523
*ptr = (*ptr + 1)?;
524-
Ok(Sha256ProcessBlockRequest { state_ptr: state_start, input_start })
524+
Ok(ShaProcessBlockRequest { state_ptr: state_start, input_start })
525525
}
526526
}
527527

528528
#[derive(Debug, Eq, PartialEq)]
529-
pub struct Sha256ProcessBlockResponse {
529+
pub struct ShaProcessBlockResponse {
530530
pub state_ptr: Relocatable,
531531
}
532532

533-
impl SyscallResponse for Sha256ProcessBlockResponse {
534-
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
535-
write_maybe_relocatable(vm, ptr, self.state_ptr)?;
536-
Ok(())
537-
}
538-
}
539-
540-
// Sha512ProcessBlock syscall.
541-
#[derive(Debug, Eq, PartialEq)]
542-
pub struct Sha512ProcessBlockRequest {
543-
pub state_ptr: Relocatable,
544-
pub input_start: Relocatable,
545-
}
546-
547-
impl SyscallRequest for Sha512ProcessBlockRequest {
548-
fn read(
549-
vm: &VirtualMachine,
550-
ptr: &mut Relocatable,
551-
) -> SyscallBaseResult<Sha512ProcessBlockRequest> {
552-
let state_start = vm.get_relocatable(*ptr)?;
553-
*ptr = (*ptr + 1)?;
554-
let input_start = vm.get_relocatable(*ptr)?;
555-
*ptr = (*ptr + 1)?;
556-
Ok(Sha512ProcessBlockRequest { state_ptr: state_start, input_start })
557-
}
558-
}
559-
560-
#[derive(Debug, Eq, PartialEq)]
561-
pub struct Sha512ProcessBlockResponse {
562-
pub state_ptr: Relocatable,
563-
}
564-
565-
impl SyscallResponse for Sha512ProcessBlockResponse {
533+
impl SyscallResponse for ShaProcessBlockResponse {
566534
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
567535
write_maybe_relocatable(vm, ptr, self.state_ptr)?;
568536
Ok(())

crates/starknet_os/resources/hint_coverage/test_new_syscalls_flow_use_kzg_da_false_n_blocks_1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"StatelessHint": "FillHolesInRc96Segment"
7070
},
7171
{
72-
"StatelessHint": "Sha2Finalize"
72+
"StatelessHint": "Sha256Finalize"
7373
},
7474
{
7575
"StatelessHint": "Sha512Finalize"

crates/starknet_os/resources/hint_coverage/test_new_syscalls_flow_use_kzg_da_true_n_blocks_5.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"StatelessHint": "FillHolesInRc96Segment"
7070
},
7171
{
72-
"StatelessHint": "Sha2Finalize"
72+
"StatelessHint": "Sha256Finalize"
7373
},
7474
{
7575
"StatelessHint": "Sha512Finalize"

crates/starknet_os/src/hints/enum_definition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use crate::hints::hint_implementation::execute_transactions::implementation::{
6666
segments_add,
6767
segments_add_temp_initial_txs_range_check_ptr,
6868
set_component_hashes,
69-
sha2_finalize,
69+
sha256_finalize,
7070
sha512_finalize,
7171
skip_tx,
7272
start_tx,
@@ -273,7 +273,7 @@ use crate::hints::pythonic_hint_strings::patricia::{
273273
};
274274
use crate::hints::pythonic_hint_strings::secp::IS_ON_CURVE;
275275
use crate::hints::pythonic_hint_strings::segment_arena::SEGMENTS_ADD;
276-
use crate::hints::pythonic_hint_strings::sha256::SHA2_FINALIZE;
276+
use crate::hints::pythonic_hint_strings::sha256::SHA256_FINALIZE;
277277
use crate::hints::pythonic_hint_strings::sha512::SHA512_FINALIZE;
278278
use crate::hints::types::{HintContext, HintEnum};
279279
use crate::{
@@ -413,7 +413,7 @@ define_stateless_hint_enum!(
413413
(SegmentsAdd, segments_add, SEGMENTS_ADD),
414414
(LogRemainingTxs, log_remaining_txs),
415415
(FillHolesInRc96Segment, fill_holes_in_rc96_segment),
416-
(Sha2Finalize, sha2_finalize, SHA2_FINALIZE),
416+
(Sha256Finalize, sha256_finalize, SHA256_FINALIZE),
417417
(Sha512Finalize, sha512_finalize, SHA512_FINALIZE),
418418
(EnterScopeDeprecatedSyscallHandler, enter_scope_deprecated_syscall_handler),
419419
(EnterScopeSyscallHandler, enter_scope_syscall_handler),

crates/starknet_os/src/hints/hint_implementation/execute_transactions/implementation.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use starknet_types_core::felt::Felt;
99
use crate::hint_processor::snos_hint_processor::SnosHintProcessor;
1010
use crate::hints::error::{OsHintError, OsHintResult};
1111
use crate::hints::hint_implementation::execute_transactions::utils::{
12-
calculate_padding,
12+
calculate_sha256_padding,
1313
calculate_sha512_padding,
1414
N_MISSING_BLOCKS_BOUND,
1515
SHA256_INPUT_CHUNK_SIZE_BOUND,
@@ -104,12 +104,12 @@ macro_rules! generate_sha_finalize {
104104
}
105105

106106
generate_sha_finalize!(
107-
sha2_finalize,
108-
ShaBatchSize,
107+
sha256_finalize,
108+
Sha256BatchSize,
109109
Sha256InputChunkSize,
110110
SHA256_INPUT_CHUNK_SIZE_BOUND,
111111
Sha256PtrEnd,
112-
calculate_padding
112+
calculate_sha256_padding
113113
);
114114
generate_sha_finalize!(
115115
sha512_finalize,

crates/starknet_os/src/hints/hint_implementation/execute_transactions/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cairo_vm::hint_processor::builtin_hint_processor::blake2s_hash::IV;
1+
use cairo_vm::hint_processor::builtin_hint_processor::blake2s_hash::IV as SHA256_IV;
22
use cairo_vm::types::relocatable::MaybeRelocatable;
33
use sha2::digest::generic_array::GenericArray;
44
use starknet_types_core::felt::Felt;
@@ -51,5 +51,5 @@ macro_rules! generate_calculate_sha_padding {
5151
};
5252
}
5353

54-
generate_calculate_sha_padding!(calculate_padding, u32, IV, compress256);
54+
generate_calculate_sha_padding!(calculate_sha256_padding, u32, SHA256_IV, compress256);
5555
generate_calculate_sha_padding!(calculate_sha512_padding, u64, SHA512_IV, compress512);

crates/starknet_os/src/hints/hint_implementation/execute_transactions/utils_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use cairo_vm::types::relocatable::MaybeRelocatable;
22
use rstest::rstest;
33
use starknet_types_core::felt::Felt;
44

5-
use super::{calculate_padding, calculate_sha512_padding, SHA512_IV};
5+
use super::{calculate_sha256_padding, calculate_sha512_padding, SHA512_IV};
66
use crate::hints::hint_implementation::execute_transactions::utils::N_MISSING_BLOCKS_BOUND;
77

88
#[rstest]
9-
fn test_calculate_padding(
9+
fn test_calculate_sha256_padding(
1010
#[values(3, 1, 0, N_MISSING_BLOCKS_BOUND - 1)] number_of_missing_blocks: u32,
1111
) {
1212
// The expected single padding is independent of the number of missing blocks.
@@ -21,7 +21,7 @@ fn test_calculate_padding(
2121
let expected_padding: Vec<_> =
2222
(0..number_of_missing_blocks).flat_map(|_| expected_single_padding.clone()).collect();
2323
let sha256_input_chunk_size_felts = 16;
24-
let padding = calculate_padding(sha256_input_chunk_size_felts, number_of_missing_blocks);
24+
let padding = calculate_sha256_padding(sha256_input_chunk_size_felts, number_of_missing_blocks);
2525
assert_eq!(padding, expected_padding);
2626
}
2727

crates/starknet_os/src/hints/pythonic_hint_strings/sha256.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use indoc::indoc;
22

3-
pub(crate) const SHA2_FINALIZE: &str = indoc! {r#"# Add dummy pairs of input and output.
3+
pub(crate) const SHA256_FINALIZE: &str = indoc! {r#"# Add dummy pairs of input and output.
44
from starkware.cairo.common.cairo_sha256.sha256_utils import (
55
IV,
66
compute_message_schedule,

crates/starknet_os/src/hints/vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ define_string_enum! {
360360
),
361361
(MerkleHeight, "starkware.starknet.core.os.state.commitment.MERKLE_HEIGHT"),
362362
(NUpdatesSmallPackingBound, "starkware.starknet.core.os.state.output.N_UPDATES_SMALL_PACKING_BOUND"),
363-
(ShaBatchSize, "starkware.cairo.common.cairo_sha256.sha256_utils.BATCH_SIZE"),
363+
(Sha256BatchSize, "starkware.cairo.common.cairo_sha256.sha256_utils.BATCH_SIZE"),
364364
(Sha256InputChunkSize, "starkware.cairo.common.cairo_sha256.sha256_utils.SHA256_INPUT_CHUNK_SIZE_FELTS"),
365365
(Sha512BatchSize, "starkware.cairo.common.cairo_sha512.sha512_utils.BATCH_SIZE"),
366366
(Sha512InputChunkSize, "starkware.cairo.common.cairo_sha512.sha512_utils.SHA512_INPUT_CHUNK_SIZE_FELTS"),

0 commit comments

Comments
 (0)