Skip to content

Commit 6cbb062

Browse files
blockifier: move logic inside felt_size_count
1 parent a81bc8c commit 6cbb062

File tree

3 files changed

+17
-25
lines changed

3 files changed

+17
-25
lines changed

crates/blockifier/src/execution/contract_class.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ impl FeltSizeCount {
8484
pub fn n_felts(&self) -> usize {
8585
self.small + self.large
8686
}
87+
88+
/// Returns the total number of `u32` words required to encode all felts
89+
/// according to the small/large felt encoding scheme.
90+
pub(crate) fn encoded_u32_len(&self) -> usize {
91+
self.large * Self::U32_WORDS_PER_LARGE_FELT + self.small * Self::U32_WORDS_PER_SMALL_FELT
92+
}
93+
94+
/// Returns the number of BLAKE opcodes required to hash the felts.
95+
/// Each BLAKE opcode processes one message block of [`U32_WORDS_PER_MESSAGE`] `u32`s
96+
/// (partial messages are padded).
97+
pub(crate) fn blake_opcode_count(&self) -> usize {
98+
self.encoded_u32_len().div_ceil(Self::U32_WORDS_PER_MESSAGE)
99+
}
87100
}
88101

89102
/// Counts felts in bytecode by size (small < 2^63, large >= 2^63).

crates/blockifier/src/execution/execution_utils.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,6 @@ mod blake_estimation {
394394
pub const STEPS_EMPTY_INPUT: usize = 170;
395395
}
396396

397-
/// Calculates the total number of u32s required to encode the given number of big and small felts.
398-
/// Big felts encode to 8 u32s each, small felts encode to 2 u32s each.
399-
fn total_u32s_from_felts(n_big_felts: usize, n_small_felts: usize) -> usize {
400-
let big_u32s = n_big_felts
401-
.checked_mul(FeltSizeCount::U32_WORDS_PER_LARGE_FELT)
402-
.expect("Overflow computing big felts u32s");
403-
let small_u32s = n_small_felts
404-
.checked_mul(FeltSizeCount::U32_WORDS_PER_SMALL_FELT)
405-
.expect("Overflow computing small felts u32s");
406-
big_u32s.checked_add(small_u32s).expect("Overflow computing total u32s")
407-
}
408-
409397
fn base_steps_for_blake_hash(n_u32s: usize) -> usize {
410398
let rem_u32s = n_u32s % FeltSizeCount::U32_WORDS_PER_MESSAGE;
411399
if rem_u32s == 0 {
@@ -434,7 +422,7 @@ fn felts_steps(n_big_felts: usize, n_small_felts: usize) -> usize {
434422
fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash(
435423
felt_size_groups: &FeltSizeCount,
436424
) -> usize {
437-
let total_u32s = total_u32s_from_felts(felt_size_groups.large, felt_size_groups.small);
425+
let total_u32s = felt_size_groups.encoded_u32_len();
438426
if total_u32s == 0 {
439427
// The empty input case is a special case.
440428
return blake_estimation::STEPS_EMPTY_INPUT;
@@ -446,14 +434,6 @@ fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash(
446434
base_steps.checked_add(felt_steps).expect("Overflow computing total Blake hash steps")
447435
}
448436

449-
/// Returns the number of BLAKE opcodes needed to hash the given felts.
450-
/// Each BLAKE opcode processes 16 u32s (partial messages are padded).
451-
fn count_blake_opcode(felt_size_groups: &FeltSizeCount) -> usize {
452-
// Count the total number of u32s to be hashed.
453-
let total_u32s = total_u32s_from_felts(felt_size_groups.large, felt_size_groups.small);
454-
total_u32s.div_ceil(FeltSizeCount::U32_WORDS_PER_MESSAGE)
455-
}
456-
457437
/// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
458438
///
459439
/// # Encoding Details
@@ -482,7 +462,7 @@ pub fn encode_and_blake_hash_resources(
482462

483463
EstimatedExecutionResources::V2Hash {
484464
resources,
485-
blake_count: count_blake_opcode(felt_size_groups),
465+
blake_count: felt_size_groups.blake_opcode_count(),
486466
}
487467
}
488468

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-
count_blake_opcode,
109
encode_and_blake_hash_resources,
1110
estimate_steps_of_encode_felt252_data_and_calc_blake_hash,
1211
};
@@ -35,7 +34,7 @@ fn test_zero_inputs() {
3534
assert_eq!(steps, STEPS_EMPTY_INPUT, "Unexpected base step cost for zero inputs");
3635

3736
// No opcodes should be emitted.
38-
let opcodes = count_blake_opcode(&FeltSizeCount::default());
37+
let opcodes = FeltSizeCount::default().blake_opcode_count();
3938
assert_eq!(opcodes, 0, "Expected zero BLAKE opcodes for zero inputs");
4039

4140
// Should result in base cost only (no opcode cost).
@@ -47,7 +46,7 @@ fn test_zero_inputs() {
4746

4847
// TODO(AvivG): Add tests for:
4948
// - `estimate_steps_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input).
50-
// - `count_blake_opcode` simple cases (felts input).
49+
// - `blake_opcode_count` simple cases (felts input).
5150
// - `cost_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input) (including partial
5251
// remainder).
5352
// - `cost_of_encode_felt252_data_and_calc_blake_hash` compare against actual execution resources

0 commit comments

Comments
 (0)