Skip to content

Commit 0b986ac

Browse files
blockifier: encode_and_blake into trait
1 parent 17d92ae commit 0b986ac

File tree

4 files changed

+57
-47
lines changed

4 files changed

+57
-47
lines changed

crates/blockifier/src/execution/casm_hash_estimation.rs

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::execution::contract_class::{
1414
FeltSizeCount,
1515
NestedFeltCounts,
1616
};
17-
use crate::execution::execution_utils::{encode_and_blake_hash_resources, poseidon_hash_many_cost};
17+
use crate::execution::execution_utils::poseidon_hash_many_cost;
1818
use crate::utils::u64_from_usize;
1919

2020
#[cfg(test)]
@@ -149,7 +149,7 @@ impl From<(ExecutionResources, HashVersion)> for EstimatedExecutionResources {
149149
/// This provides resource estimates rather than exact values.
150150
// TODO(AvivG): Remove allow once used.
151151
#[allow(unused)]
152-
trait EstimateCasmHashResources {
152+
pub trait EstimateCasmHashResources {
153153
/// Specifies the hash function variant that the estimate is for.
154154
fn hash_version(&self) -> HashVersion;
155155

@@ -192,17 +192,44 @@ impl EstimateCasmHashResources for CasmV1HashResourceEstimate {
192192

193193
// TODO(AvivG): Remove allow once used.
194194
#[allow(unused)]
195-
struct CasmV2HashResourceEstimate {}
195+
pub struct CasmV2HashResourceEstimate {}
196196

197197
impl EstimateCasmHashResources for CasmV2HashResourceEstimate {
198198
fn hash_version(&self) -> HashVersion {
199199
HashVersion::V2
200200
}
201201

202+
/// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
203+
///
204+
/// # Encoding Details
205+
/// - Small felts → 2 `u32`s each; Big felts → 8 `u32`s each.
206+
/// - Each felt requires one `range_check` operation.
207+
///
208+
/// # Returns:
209+
/// - `ExecutionResources`: VM resource usage (e.g., n_steps, range checks).
210+
/// - `usize`: number of Blake opcodes used, accounted for separately as those are not reported
211+
/// via `ExecutionResources`.
202212
fn estimated_resources_of_hash_function(
203213
felt_size_groups: &FeltSizeCount,
204214
) -> EstimatedExecutionResources {
205-
encode_and_blake_hash_resources(felt_size_groups)
215+
let n_steps = estimate_steps_of_encode_felt252_data_and_calc_blake_hash(felt_size_groups);
216+
let builtin_instance_counter = match felt_size_groups.n_felts() {
217+
// The empty case does not use builtins at all.
218+
0 => HashMap::new(),
219+
// One `range_check` per input felt to validate its size + Overhead for the non empty
220+
// case.
221+
_ => HashMap::from([(
222+
BuiltinName::range_check,
223+
felt_size_groups.n_felts() + blake_estimation::BASE_RANGE_CHECK_NON_EMPTY,
224+
)]),
225+
};
226+
227+
let resources = ExecutionResources { n_steps, n_memory_holes: 0, builtin_instance_counter };
228+
229+
EstimatedExecutionResources::V2Hash {
230+
resources,
231+
blake_count: felt_size_groups.blake_opcode_count(),
232+
}
206233
}
207234
}
208235

@@ -267,38 +294,6 @@ fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash(
267294
base_steps.checked_add(felt_steps).expect("Overflow computing total Blake hash steps")
268295
}
269296

270-
/// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
271-
///
272-
/// # Encoding Details
273-
/// - Small felts → 2 `u32`s each; Big felts → 8 `u32`s each.
274-
/// - Each felt requires one `range_check` operation.
275-
///
276-
/// # Returns:
277-
/// - `ExecutionResources`: VM resource usage (e.g., n_steps, range checks).
278-
/// - `usize`: number of Blake opcodes used, accounted for separately as those are not reported via
279-
/// `ExecutionResources`.
280-
pub fn encode_and_blake_hash_resources(
281-
felt_size_groups: &FeltSizeCount,
282-
) -> EstimatedExecutionResources {
283-
let n_steps = estimate_steps_of_encode_felt252_data_and_calc_blake_hash(felt_size_groups);
284-
let builtin_instance_counter = match felt_size_groups.n_felts() {
285-
// The empty case does not use builtins at all.
286-
0 => HashMap::new(),
287-
// One `range_check` per input felt to validate its size + Overhead for the non empty case.
288-
_ => HashMap::from([(
289-
BuiltinName::range_check,
290-
felt_size_groups.n_felts() + blake_estimation::BASE_RANGE_CHECK_NON_EMPTY,
291-
)]),
292-
};
293-
294-
let resources = ExecutionResources { n_steps, n_memory_holes: 0, builtin_instance_counter };
295-
296-
EstimatedExecutionResources::V2Hash {
297-
resources,
298-
blake_count: felt_size_groups.blake_opcode_count(),
299-
}
300-
}
301-
302297
/// Converts the execution resources and blake opcode count to L2 gas.
303298
///
304299
/// Used for both Stwo ("proving_gas") and Stone ("sierra_gas") estimations, which differ in

crates/blockifier/src/execution/casm_hash_estimation_test.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use starknet_types_core::felt::Felt;
99

1010
use crate::execution::casm_hash_estimation::blake_estimation::STEPS_EMPTY_INPUT;
1111
use crate::execution::casm_hash_estimation::{
12-
encode_and_blake_hash_resources,
12+
CasmV2HashResourceEstimate,
13+
EstimateCasmHashResources,
1314
estimate_steps_of_encode_felt252_data_and_calc_blake_hash,
1415
EstimatedExecutionResources,
1516
};
@@ -133,7 +134,11 @@ fn test_zero_inputs() {
133134
assert_eq!(opcodes, 0, "Expected zero BLAKE opcodes for zero inputs");
134135

135136
// Should result in base cost only (no opcode cost).
136-
let resources = encode_and_blake_hash_resources(&FeltSizeCount { large: 0, small: 0 });
137+
let resources =
138+
CasmV2HashResourceEstimate::estimated_resources_of_hash_function(&FeltSizeCount {
139+
large: 0,
140+
small: 0,
141+
});
137142
let expected = ExecutionResources { n_steps: STEPS_EMPTY_INPUT, ..Default::default() };
138143
assert_eq!(resources.resources(), &expected, "Unexpected resources values for zero-input hash");
139144
assert_eq!(resources.blake_count(), 0, "Expected zero BLAKE opcodes for zero inputs");

crates/blockifier/src/execution/contract_class.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ use crate::bouncer::vm_resources_to_sierra_gas;
4444
use crate::execution::call_info::BuiltinCounterMap;
4545
use crate::execution::casm_hash_estimation::{
4646
blake_execution_resources_estimation_to_gas,
47-
encode_and_blake_hash_resources,
47+
CasmV2HashResourceEstimate,
48+
EstimateCasmHashResources,
4849
};
4950
use crate::execution::entry_point::{EntryPointExecutionContext, EntryPointTypeAndSelector};
5051
use crate::execution::errors::PreExecutionError;
@@ -592,10 +593,11 @@ fn leaf_cost(
592593
blake_opcode_gas: usize,
593594
) -> GasAmount {
594595
// All `len` inputs treated as “big” felts; no small-felt optimization here.
595-
// TODO(AvivG): Call `encode_and_blake_hash_resources` directly, and perform the resource→gas
596-
// conversion only after `estimate_casm_blake_hash_computation_resources` executes.
596+
// TODO(AvivG): Call `estimated_resources_of_hash_function` directly, and perform the
597+
// resource→gas conversion only after `estimate_casm_blake_hash_computation_resources`
598+
// executes.
597599
blake_execution_resources_estimation_to_gas(
598-
encode_and_blake_hash_resources(felt_size_groups),
600+
CasmV2HashResourceEstimate::estimated_resources_of_hash_function(felt_size_groups),
599601
versioned_constants,
600602
blake_opcode_gas,
601603
)
@@ -630,10 +632,14 @@ fn node_cost(
630632

631633
// Node‐level hash over (hash1, len1, hash2, len2, …): one segment hash (“big” felt))
632634
// and one segment length (“small” felt) per segment.
633-
// TODO(AvivG): Call `encode_and_blake_hash_resources` directly, and perform the resource→gas
634-
// conversion only after `estimate_casm_blake_hash_computation_resources` executes.
635+
// TODO(AvivG): Call `estimated_resources_of_hash_function` directly, and perform the
636+
// resource→gas conversion only after `estimate_casm_blake_hash_computation_resources`
637+
// executes.
635638
let node_hash_cost = blake_execution_resources_estimation_to_gas(
636-
encode_and_blake_hash_resources(&FeltSizeCount { large: segs.len(), small: segs.len() }),
639+
CasmV2HashResourceEstimate::estimated_resources_of_hash_function(&FeltSizeCount {
640+
large: segs.len(),
641+
small: segs.len(),
642+
}),
637643
versioned_constants,
638644
blake_opcode_gas,
639645
);

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

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

33
use blake2s::encode_felt252_data_and_calc_blake_hash;
4-
use blockifier::execution::casm_hash_estimation::encode_and_blake_hash_resources;
4+
use blockifier::execution::casm_hash_estimation::{
5+
CasmV2HashResourceEstimate,
6+
EstimateCasmHashResources,
7+
};
58
use blockifier::execution::contract_class::FeltSizeCount;
69
use cairo_vm::types::builtin_name::BuiltinName;
710
use cairo_vm::types::layout_name::LayoutName;
@@ -39,7 +42,8 @@ fn data_to_felt_count(data: &[Felt]) -> FeltSizeCount {
3942
/// Return the estimated execution resources for Blake2s hashing.
4043
fn estimated_encode_and_blake_hash_execution_resources(data: &[Felt]) -> ExecutionResources {
4144
let felt_size_groups = data_to_felt_count(data);
42-
let estimated = encode_and_blake_hash_resources(&felt_size_groups);
45+
let estimated =
46+
CasmV2HashResourceEstimate::estimated_resources_of_hash_function(&felt_size_groups);
4347

4448
let mut resources = estimated.resources().clone();
4549
resources.n_steps -= 1;

0 commit comments

Comments
 (0)