Skip to content

Commit 592d498

Browse files
blockifier: move logic inside felt_size_count
1 parent fc4aacc commit 592d498

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 {
@@ -444,14 +432,6 @@ fn compute_blake_hash_steps(felt_size_groups: &FeltSizeCount) -> usize {
444432
base_steps.checked_add(felt_steps).expect("Overflow computing total Blake hash steps")
445433
}
446434

447-
/// Returns the number of BLAKE opcodes needed to hash the given felts.
448-
/// Each BLAKE opcode processes 16 u32s (partial messages are padded).
449-
fn count_blake_opcode(n_big_felts: usize, n_small_felts: usize) -> usize {
450-
// Count the total number of u32s to be hashed.
451-
let total_u32s = total_u32s_from_felts(n_big_felts, n_small_felts);
452-
total_u32s.div_ceil(FeltSizeCount::U32_WORDS_PER_MESSAGE)
453-
}
454-
455435
/// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
456436
///
457437
/// # Encoding Details
@@ -480,7 +460,7 @@ pub fn encode_and_blake_hash_resources(
480460

481461
EstimatedExecutionResources::V2Hash {
482462
resources,
483-
blake_count: count_blake_opcode(felt_size_groups.large, felt_size_groups.small),
463+
blake_count: felt_size_groups.blake_opcode_count(),
484464
}
485465
}
486466

crates/blockifier/src/execution/execution_utils_test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::execution::contract_class::FeltSizeCount;
77
use crate::execution::execution_utils::blake_estimation::STEPS_EMPTY_INPUT;
88
use crate::execution::execution_utils::{
99
compute_blake_hash_steps,
10-
count_blake_opcode,
1110
encode_and_blake_hash_resources,
1211
};
1312

@@ -34,7 +33,7 @@ fn test_zero_inputs() {
3433
assert_eq!(steps, STEPS_EMPTY_INPUT, "Unexpected base step cost for zero inputs");
3534

3635
// No opcodes should be emitted.
37-
let opcodes = count_blake_opcode(0, 0);
36+
let opcodes = FeltSizeCount { large: 0, small: 0 }.blake_opcode_count();
3837
assert_eq!(opcodes, 0, "Expected zero BLAKE opcodes for zero inputs");
3938

4039
// Should result in base cost only (no opcode cost).
@@ -46,7 +45,7 @@ fn test_zero_inputs() {
4645

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

0 commit comments

Comments
 (0)