Skip to content

Commit 71c9104

Browse files
blockifier: encode_and_blake into trait
1 parent 6bfccae commit 71c9104

File tree

4 files changed

+56
-48
lines changed

4 files changed

+56
-48
lines changed

crates/blockifier/src/execution/casm_hash_estimation.rs

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -190,19 +190,44 @@ impl EstimateCasmHashResources for CasmV1HashResourceEstimate {
190190
}
191191
}
192192

193-
// TODO(AvivG): Remove allow once used.
194-
#[allow(unused)]
195-
struct CasmV2HashResourceEstimate {}
193+
pub struct CasmV2HashResourceEstimate {}
196194

197195
impl EstimateCasmHashResources for CasmV2HashResourceEstimate {
198196
fn hash_version(&self) -> HashVersion {
199197
HashVersion::V2
200198
}
201199

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

@@ -267,38 +292,6 @@ fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash(
267292
base_steps.checked_add(felt_steps).expect("Overflow computing total Blake hash steps")
268293
}
269294

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-
302295
/// Converts the execution resources and blake opcode count to L2 gas.
303296
///
304297
/// 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)