Skip to content

Commit a519a77

Browse files
blockifier: move logic inside felt_size_count
1 parent 86ab5e4 commit a519a77

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

crates/blockifier/src/execution/contract_class.rs

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

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

crates/blockifier/src/execution/execution_utils.rs

Lines changed: 1 addition & 21 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 {
@@ -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
};
@@ -37,7 +36,7 @@ fn test_zero_inputs() {
3736
assert_eq!(steps, STEPS_EMPTY_INPUT, "Unexpected base step cost for zero inputs");
3837

3938
// No opcodes should be emitted.
40-
let opcodes = count_blake_opcode(&FeltSizeCount { small: 0, large: 0 });
39+
let opcodes = FeltSizeCount { large: 0, small: 0 }.blake_opcode_count();
4140
assert_eq!(opcodes, 0, "Expected zero BLAKE opcodes for zero inputs");
4241

4342
// Should result in base cost only (no opcode cost).
@@ -49,7 +48,7 @@ fn test_zero_inputs() {
4948

5049
// TODO(AvivG): Add tests for:
5150
// - `estimate_steps_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input).
52-
// - `count_blake_opcode` simple cases (felts input).
51+
// - `blake_opcode_count` simple cases (felts input).
5352
// - `cost_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input) (including partial
5453
// remainder).
5554
// - `cost_of_encode_felt252_data_and_calc_blake_hash` compare against actual execution resources

0 commit comments

Comments
 (0)