Skip to content

Commit d4ea96b

Browse files
blockifier: encode_and_blake into trait
1 parent 25ac177 commit d4ea96b

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)]
@@ -148,7 +148,7 @@ impl From<(ExecutionResources, HashVersion)> for EstimatedExecutionResources {
148148
/// This provides resource estimates rather than exact values.
149149
// TODO(AvivG): Remove allow once used.
150150
#[allow(unused)]
151-
trait EstimateCasmHashResources {
151+
pub trait EstimateCasmHashResources {
152152
/// Specifies the hash function variant that the estimate is for.
153153
fn hash_version(&self) -> HashVersion;
154154

@@ -191,17 +191,44 @@ impl EstimateCasmHashResources for CasmV1HashResourceEstimate {
191191

192192
// TODO(AvivG): Remove allow once used.
193193
#[allow(unused)]
194-
struct CasmV2HashResourceEstimate {}
194+
pub struct CasmV2HashResourceEstimate {}
195195

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

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

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

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