Skip to content

Commit 9551bd0

Browse files
blockifier: change input to felt_size_counts: fn encode_blake (#8686)
1 parent 374bdf5 commit 9551bd0

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

crates/blockifier/src/execution/contract_class.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ pub fn estimate_casm_poseidon_hash_computation_resources(
572572
/// Cost to hash a single flat segment of `len` felts.
573573
fn leaf_cost(felt_size_groups: &FeltSizeCount) -> EstimatedExecutionResources {
574574
// All `len` inputs treated as “big” felts; no small-felt optimization here.
575-
encode_and_blake_hash_resources(felt_size_groups.large, felt_size_groups.small)
575+
encode_and_blake_hash_resources(felt_size_groups)
576576
}
577577

578578
/// Cost to hash a multi-segment contract:
@@ -596,7 +596,8 @@ fn node_cost(segs: &[NestedFeltCounts]) -> EstimatedExecutionResources {
596596

597597
// Node‐level hash over (hash1, len1, hash2, len2, …): one segment hash (“big” felt))
598598
// and one segment length (“small” felt) per segment.
599-
resources += &encode_and_blake_hash_resources(segs.len(), segs.len());
599+
resources +=
600+
&encode_and_blake_hash_resources(&FeltSizeCount { large: segs.len(), small: segs.len() });
600601

601602
resources
602603
}

crates/blockifier/src/execution/execution_utils.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::blockifier_versioned_constants::VersionedConstants;
2626
use crate::bouncer::vm_resources_to_sierra_gas;
2727
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
2828
use crate::execution::casm_hash_estimation::EstimatedExecutionResources;
29-
use crate::execution::contract_class::{RunnableCompiledClass, TrackedResource};
29+
use crate::execution::contract_class::{FeltSizeCount, RunnableCompiledClass, TrackedResource};
3030
use crate::execution::entry_point::{
3131
execute_constructor_entry_point,
3232
ConstructorContext,
@@ -473,27 +473,24 @@ fn count_blake_opcode(n_big_felts: usize, n_small_felts: usize) -> usize {
473473
/// - `usize`: number of Blake opcodes used, accounted for separately as those are not reported via
474474
/// `ExecutionResources`.
475475
pub fn encode_and_blake_hash_resources(
476-
// TODO(AvivG): Refactor to use `FeltSizeCount`.
477-
n_big_felts: usize,
478-
n_small_felts: usize,
476+
felt_size_groups: &FeltSizeCount,
479477
) -> EstimatedExecutionResources {
480-
let n_steps = compute_blake_hash_steps(n_big_felts, n_small_felts);
481-
let n_felts = n_big_felts + n_small_felts;
482-
let builtin_instance_counter = match n_felts {
478+
let n_steps = compute_blake_hash_steps(felt_size_groups.large, felt_size_groups.small);
479+
let builtin_instance_counter = match felt_size_groups.n_felts() {
483480
// The empty case does not use builtins at all.
484481
0 => HashMap::new(),
485482
// One `range_check` per input felt to validate its size + Overhead for the non empty case.
486483
_ => HashMap::from([(
487484
BuiltinName::range_check,
488-
n_felts + blake_estimation::BASE_RANGE_CHECK_NON_EMPTY,
485+
felt_size_groups.n_felts() + blake_estimation::BASE_RANGE_CHECK_NON_EMPTY,
489486
)]),
490487
};
491488

492489
let resources = ExecutionResources { n_steps, n_memory_holes: 0, builtin_instance_counter };
493490

494491
EstimatedExecutionResources::V2Hash {
495492
resources,
496-
blake_count: count_blake_opcode(n_big_felts, n_small_felts),
493+
blake_count: count_blake_opcode(felt_size_groups.large, felt_size_groups.small),
497494
}
498495
}
499496

crates/blockifier/src/execution/execution_utils_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
33
use pretty_assertions::assert_eq;
44
use starknet_types_core::felt::Felt;
55

6+
use crate::execution::contract_class::FeltSizeCount;
67
use crate::execution::execution_utils::blake_encoding::{N_U32S_BIG_FELT, N_U32S_SMALL_FELT};
78
use crate::execution::execution_utils::blake_estimation::STEPS_EMPTY_INPUT;
89
use crate::execution::execution_utils::{
@@ -38,7 +39,7 @@ fn test_zero_inputs() {
3839
assert_eq!(opcodes, 0, "Expected zero BLAKE opcodes for zero inputs");
3940

4041
// Should result in base cost only (no opcode cost).
41-
let resources = encode_and_blake_hash_resources(0, 0);
42+
let resources = encode_and_blake_hash_resources(&FeltSizeCount::default());
4243
let expected = ExecutionResources { n_steps: STEPS_EMPTY_INPUT, ..Default::default() };
4344
assert_eq!(resources.resources(), &expected, "Unexpected resources values for zero-input hash");
4445
assert_eq!(resources.blake_count(), 0, "Expected zero BLAKE opcodes for zero inputs");

crates/starknet_os/src/hints/hint_implementation/blake2s/blake2s_test.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22

33
use blake2s::encode_felt252_data_and_calc_blake_hash;
4+
use blockifier::execution::contract_class::FeltSizeCount;
45
use blockifier::execution::execution_utils::encode_and_blake_hash_resources;
56
use cairo_vm::types::builtin_name::BuiltinName;
67
use cairo_vm::types::layout_name::LayoutName;
@@ -19,19 +20,27 @@ use crate::test_utils::cairo_runner::{
1920
};
2021

2122
/// Counts the number of small and big felts in the data.
22-
fn data_to_felt_count(data: &[Felt]) -> (usize, usize) {
23+
// TODO(AvivG): use From<&[Felt]> for FeltSizeCount.
24+
fn data_to_felt_count(data: &[Felt]) -> FeltSizeCount {
2325
// TODO(AvivG): Use `Blake2Felt252::SMALL_THRESHOLD` when exposed.
2426
const SMALL_THRESHOLD: Felt = Felt::from_hex_unchecked("8000000000000000"); // 2^63
2527

26-
data.iter().fold((0, 0), |(small, big), felt| {
27-
if *felt >= SMALL_THRESHOLD { (small, big + 1) } else { (small + 1, big) }
28+
let felt_size_groups = FeltSizeCount::default();
29+
30+
data.iter().fold(felt_size_groups, |mut felt_size_groups, felt| {
31+
if *felt >= SMALL_THRESHOLD {
32+
felt_size_groups.large += 1;
33+
} else {
34+
felt_size_groups.small += 1;
35+
}
36+
felt_size_groups
2837
})
2938
}
3039

3140
/// Return the estimated execution resources for Blake2s hashing.
3241
fn estimated_encode_and_blake_hash_execution_resources(data: &[Felt]) -> ExecutionResources {
33-
let (n_small_felts, n_big_felts) = data_to_felt_count(data);
34-
let estimated = encode_and_blake_hash_resources(n_big_felts, n_small_felts);
42+
let felt_size_groups = data_to_felt_count(data);
43+
let estimated = encode_and_blake_hash_resources(&felt_size_groups);
3544

3645
let mut resources = estimated.resources().clone();
3746
resources.n_steps -= 1;

0 commit comments

Comments
 (0)