Skip to content

Commit bbca19d

Browse files
fix rebase?
1 parent b862482 commit bbca19d

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

crates/blockifier/src/execution/casm_hash_estimation.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,12 @@ impl EstimateCasmHashResources for CasmV1HashResourceEstimate {
187187
}
188188
}
189189

190-
// TODO(AvivG): Remove allow once used.
191-
#[allow(unused)]
192-
struct CasmV2HashResourceEstimate {}
190+
pub(crate) struct CasmV2HashResourceEstimate {}
191+
192+
impl CasmV2HashResourceEstimate {
193+
// Input for Blake hash function is a sequence of 16 `u32` words.
194+
pub(crate) const U32_WORDS_PER_MESSAGE: usize = 16;
195+
}
193196

194197
impl EstimateCasmHashResources for CasmV2HashResourceEstimate {
195198
fn hash_version(&self) -> HashVersion {

crates/blockifier/src/execution/contract_class.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ pub struct FeltSizeCount {
7373
}
7474

7575
impl FeltSizeCount {
76+
// Constants that define how felts are encoded into u32s for BLAKE hashing.
77+
// Number of `u32` words a large felt expands into.
78+
pub(crate) const U32_WORDS_PER_LARGE_FELT: usize = 8;
79+
// Number of `u32` words a small felt expands into.
80+
pub(crate) const U32_WORDS_PER_SMALL_FELT: usize = 2;
81+
7682
pub(crate) fn n_felts(&self) -> usize {
7783
self.small + self.large
7884
}

crates/blockifier/src/execution/execution_utils.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ use starknet_types_core::felt::Felt;
2525
use crate::blockifier_versioned_constants::VersionedConstants;
2626
use crate::bouncer::vm_resources_to_sierra_gas;
2727
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
28-
use crate::execution::casm_hash_estimation::EstimatedExecutionResources;
28+
use crate::execution::casm_hash_estimation::{
29+
CasmV2HashResourceEstimate,
30+
EstimatedExecutionResources,
31+
};
2932
use crate::execution::contract_class::{FeltSizeCount, RunnableCompiledClass, TrackedResource};
3033
use crate::execution::entry_point::{
3134
execute_constructor_entry_point,
@@ -373,16 +376,6 @@ pub fn poseidon_hash_many_cost(data_length: usize) -> ExecutionResources {
373376
}
374377
}
375378

376-
// Constants that define how felts are encoded into u32s for BLAKE hashing.
377-
mod blake_encoding {
378-
// Number of `u32` words in a single BLAKE input message block.
379-
pub(crate) const U32_WORDS_PER_MESSAGE: usize = 16;
380-
// Number of `u32` words a large felt expands into.
381-
pub(crate) const U32_WORDS_PER_LARGE_FELT: usize = 8;
382-
// Number of `u32` words a small felt expands into.
383-
pub(crate) const U32_WORDS_PER_SMALL_FELT: usize = 2;
384-
}
385-
386379
// Constants used for estimating the cost of BLAKE hashing inside Starknet OS.
387380
// These values are based on empirical measurement by running
388381
// `encode_felt252_data_and_calc_blake_hash` on various combinations of big and small felts.
@@ -408,16 +401,16 @@ mod blake_estimation {
408401
/// Big felts encode to 8 u32s each, small felts encode to 2 u32s each.
409402
fn total_u32s_from_felts(n_big_felts: usize, n_small_felts: usize) -> usize {
410403
let big_u32s = n_big_felts
411-
.checked_mul(blake_encoding::U32_WORDS_PER_LARGE_FELT)
404+
.checked_mul(FeltSizeCount::U32_WORDS_PER_LARGE_FELT)
412405
.expect("Overflow computing big felts u32s");
413406
let small_u32s = n_small_felts
414-
.checked_mul(blake_encoding::U32_WORDS_PER_SMALL_FELT)
407+
.checked_mul(FeltSizeCount::U32_WORDS_PER_SMALL_FELT)
415408
.expect("Overflow computing small felts u32s");
416409
big_u32s.checked_add(small_u32s).expect("Overflow computing total u32s")
417410
}
418411

419412
fn base_steps_for_blake_hash(n_u32s: usize) -> usize {
420-
let rem_u32s = n_u32s % blake_encoding::U32_WORDS_PER_MESSAGE;
413+
let rem_u32s = n_u32s % CasmV2HashResourceEstimate::U32_WORDS_PER_MESSAGE;
421414
if rem_u32s == 0 {
422415
blake_estimation::BASE_STEPS_FULL_MSG
423416
} else {
@@ -461,7 +454,7 @@ fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash(
461454
fn count_blake_opcode(felt_size_groups: &FeltSizeCount) -> usize {
462455
// Count the total number of u32s to be hashed.
463456
let total_u32s = total_u32s_from_felts(felt_size_groups.large, felt_size_groups.small);
464-
total_u32s.div_ceil(blake_encoding::U32_WORDS_PER_MESSAGE)
457+
total_u32s.div_ceil(CasmV2HashResourceEstimate::U32_WORDS_PER_MESSAGE)
465458
}
466459

467460
/// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.

crates/blockifier/src/execution/execution_utils_test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use starknet_types_core::felt::Felt;
66
use crate::execution::contract_class::FeltSizeCount;
77
use crate::execution::execution_utils::blake_estimation::STEPS_EMPTY_INPUT;
88
use crate::execution::execution_utils::{
9-
blake_encoding,
109
count_blake_opcode,
1110
encode_and_blake_hash_resources,
1211
estimate_steps_of_encode_felt252_data_and_calc_blake_hash,
@@ -23,8 +22,8 @@ fn test_u32_constants() {
2322
let big_u32s = encode_felts_to_u32s(vec![big_felt]);
2423

2524
// Blake estimation constants should match the actual encoding.
26-
assert_eq!(small_u32s.len(), blake_encoding::U32_WORDS_PER_SMALL_FELT);
27-
assert_eq!(big_u32s.len(), blake_encoding::U32_WORDS_PER_LARGE_FELT);
25+
assert_eq!(small_u32s.len(), FeltSizeCount::U32_WORDS_PER_SMALL_FELT);
26+
assert_eq!(big_u32s.len(), FeltSizeCount::U32_WORDS_PER_LARGE_FELT);
2827
}
2928

3029
/// Test the edge case of hashing an empty array of felt values.

0 commit comments

Comments
 (0)