@@ -18,15 +18,11 @@ use cairo_vm::vm::vm_core::VirtualMachine;
18
18
use num_bigint:: BigUint ;
19
19
use starknet_api:: core:: ClassHash ;
20
20
use starknet_api:: deprecated_contract_class:: Program as DeprecatedProgram ;
21
- use starknet_api:: execution_resources:: GasAmount ;
22
21
use starknet_api:: transaction:: fields:: Calldata ;
23
22
use starknet_types_core:: felt:: Felt ;
24
23
25
- use crate :: blockifier_versioned_constants:: VersionedConstants ;
26
- use crate :: bouncer:: vm_resources_to_sierra_gas;
27
24
use crate :: execution:: call_info:: { CallExecution , CallInfo , Retdata } ;
28
- use crate :: execution:: casm_hash_estimation:: EstimatedExecutionResources ;
29
- use crate :: execution:: contract_class:: { FeltSizeCount , RunnableCompiledClass , TrackedResource } ;
25
+ use crate :: execution:: contract_class:: { RunnableCompiledClass , TrackedResource } ;
30
26
use crate :: execution:: entry_point:: {
31
27
execute_constructor_entry_point,
32
28
ConstructorContext ,
@@ -52,10 +48,6 @@ use crate::execution::{deprecated_entry_point_execution, entry_point_execution};
52
48
use crate :: state:: errors:: StateError ;
53
49
use crate :: state:: state_api:: State ;
54
50
55
- #[ cfg( test) ]
56
- #[ path = "execution_utils_test.rs" ]
57
- pub mod test;
58
-
59
51
pub type Args = Vec < CairoArg > ;
60
52
61
53
pub const SEGMENT_ARENA_BUILTIN_SIZE : usize = 3 ;
@@ -372,125 +364,3 @@ pub fn poseidon_hash_many_cost(data_length: usize) -> ExecutionResources {
372
364
builtin_instance_counter : HashMap :: from ( [ ( BuiltinName :: poseidon, data_length / 2 + 1 ) ] ) ,
373
365
}
374
366
}
375
-
376
- // Constants used for estimating the cost of BLAKE hashing inside Starknet OS.
377
- // These values are based on empirical measurement by running
378
- // `encode_felt252_data_and_calc_blake_hash` on various combinations of big and small felts.
379
- mod blake_estimation {
380
- // Per-felt step cost (measured).
381
- pub const STEPS_BIG_FELT : usize = 45 ;
382
- pub const STEPS_SMALL_FELT : usize = 15 ;
383
-
384
- // One-time overhead.
385
- // Overhead when input fills a full Blake message (16 u32s).
386
- pub const BASE_STEPS_FULL_MSG : usize = 217 ;
387
- // Overhead when input results in a partial message (remainder < 16 u32s).
388
- pub const BASE_STEPS_PARTIAL_MSG : usize = 195 ;
389
- // Extra steps per 2-u32 remainder in partial messages.
390
- pub const STEPS_PER_2_U32_REMINDER : usize = 3 ;
391
- // Overhead when input for `encode_felt252_data_and_calc_blake_hash` is non-empty.
392
- pub const BASE_RANGE_CHECK_NON_EMPTY : usize = 3 ;
393
- // Empty input steps.
394
- pub const STEPS_EMPTY_INPUT : usize = 170 ;
395
- }
396
-
397
- fn base_steps_for_blake_hash ( n_u32s : usize ) -> usize {
398
- let rem_u32s = n_u32s % FeltSizeCount :: U32_WORDS_PER_MESSAGE ;
399
- if rem_u32s == 0 {
400
- blake_estimation:: BASE_STEPS_FULL_MSG
401
- } else {
402
- // This computation is based on running blake2s with different inputs.
403
- // Note: all inputs expand to an even number of u32s --> `rem_u32s` is always even.
404
- blake_estimation:: BASE_STEPS_PARTIAL_MSG
405
- + ( rem_u32s / 2 ) * blake_estimation:: STEPS_PER_2_U32_REMINDER
406
- }
407
- }
408
-
409
- fn felts_steps ( n_big_felts : usize , n_small_felts : usize ) -> usize {
410
- let big_steps = n_big_felts
411
- . checked_mul ( blake_estimation:: STEPS_BIG_FELT )
412
- . expect ( "Overflow computing big felt steps" ) ;
413
- let small_steps = n_small_felts
414
- . checked_mul ( blake_estimation:: STEPS_SMALL_FELT )
415
- . expect ( "Overflow computing small felt steps" ) ;
416
- big_steps. checked_add ( small_steps) . expect ( "Overflow computing total felt steps" )
417
- }
418
-
419
- /// Estimates the number of VM steps needed to hash the given felts with Blake in Starknet OS.
420
- /// Each small felt unpacks into 2 u32s, and each big felt into 8 u32s.
421
- /// Adds a base cost depending on whether the total fits exactly into full 16-u32 messages.
422
- fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash (
423
- felt_size_groups : & FeltSizeCount ,
424
- ) -> usize {
425
- let total_u32s = felt_size_groups. encoded_u32_len ( ) ;
426
- if total_u32s == 0 {
427
- // The empty input case is a special case.
428
- return blake_estimation:: STEPS_EMPTY_INPUT ;
429
- }
430
-
431
- let base_steps = base_steps_for_blake_hash ( total_u32s) ;
432
- let felt_steps = felts_steps ( felt_size_groups. large , felt_size_groups. small ) ;
433
-
434
- base_steps. checked_add ( felt_steps) . expect ( "Overflow computing total Blake hash steps" )
435
- }
436
-
437
- /// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
438
- ///
439
- /// # Encoding Details
440
- /// - Small felts → 2 `u32`s each; Big felts → 8 `u32`s each.
441
- /// - Each felt requires one `range_check` operation.
442
- ///
443
- /// # Returns:
444
- /// - `ExecutionResources`: VM resource usage (e.g., n_steps, range checks).
445
- /// - `usize`: number of Blake opcodes used, accounted for separately as those are not reported via
446
- /// `ExecutionResources`.
447
- pub fn encode_and_blake_hash_resources (
448
- felt_size_groups : & FeltSizeCount ,
449
- ) -> EstimatedExecutionResources {
450
- let n_steps = estimate_steps_of_encode_felt252_data_and_calc_blake_hash ( felt_size_groups) ;
451
- let builtin_instance_counter = match felt_size_groups. n_felts ( ) {
452
- // The empty case does not use builtins at all.
453
- 0 => HashMap :: new ( ) ,
454
- // One `range_check` per input felt to validate its size + Overhead for the non empty case.
455
- _ => HashMap :: from ( [ (
456
- BuiltinName :: range_check,
457
- felt_size_groups. n_felts ( ) + blake_estimation:: BASE_RANGE_CHECK_NON_EMPTY ,
458
- ) ] ) ,
459
- } ;
460
-
461
- let resources = ExecutionResources { n_steps, n_memory_holes : 0 , builtin_instance_counter } ;
462
-
463
- EstimatedExecutionResources :: V2Hash {
464
- resources,
465
- blake_count : felt_size_groups. blake_opcode_count ( ) ,
466
- }
467
- }
468
-
469
- /// Converts the execution resources and blake opcode count to L2 gas.
470
- ///
471
- /// Used for both Stwo ("proving_gas") and Stone ("sierra_gas") estimations, which differ in
472
- /// builtin costs. This unified logic is valid because only the `range_check` builtin is used,
473
- /// and its cost is identical across provers (see `bouncer.get_tx_weights`).
474
- // TODO(AvivG): Move inside blake estimation struct.
475
- pub fn blake_execution_resources_estimation_to_gas (
476
- resources : EstimatedExecutionResources ,
477
- versioned_constants : & VersionedConstants ,
478
- blake_opcode_gas : usize ,
479
- ) -> GasAmount {
480
- // TODO(AvivG): Remove this once gas computation is separated from resource estimation.
481
- assert ! (
482
- resources
483
- . resources( )
484
- . builtin_instance_counter
485
- . keys( )
486
- . all( |& k| k == BuiltinName :: range_check) ,
487
- "Expected either empty builtins or only `range_check` builtin, got: {:?}. This breaks the \
488
- assumption that builtin costs are identical between provers.",
489
- resources. resources( ) . builtin_instance_counter. keys( ) . collect:: <Vec <_>>( )
490
- ) ;
491
-
492
- resources. to_sierra_gas (
493
- |resources| vm_resources_to_sierra_gas ( resources, versioned_constants) ,
494
- Some ( blake_opcode_gas) ,
495
- )
496
- }
0 commit comments