@@ -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 ,
@@ -372,123 +368,3 @@ pub fn poseidon_hash_many_cost(data_length: usize) -> ExecutionResources {
372
368
builtin_instance_counter : HashMap :: from ( [ ( BuiltinName :: poseidon, data_length / 2 + 1 ) ] ) ,
373
369
}
374
370
}
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 compute_blake_hash_steps ( felt_size_groups : & FeltSizeCount ) -> usize {
423
- let total_u32s = felt_size_groups. encoded_u32_len ( ) ;
424
- if total_u32s == 0 {
425
- // The empty input case is a special case.
426
- return blake_estimation:: STEPS_EMPTY_INPUT ;
427
- }
428
-
429
- let base_steps = base_steps_for_blake_hash ( total_u32s) ;
430
- let felt_steps = felts_steps ( felt_size_groups. large , felt_size_groups. small ) ;
431
-
432
- base_steps. checked_add ( felt_steps) . expect ( "Overflow computing total Blake hash steps" )
433
- }
434
-
435
- /// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
436
- ///
437
- /// # Encoding Details
438
- /// - Small felts → 2 `u32`s each; Big felts → 8 `u32`s each.
439
- /// - Each felt requires one `range_check` operation.
440
- ///
441
- /// # Returns:
442
- /// - `ExecutionResources`: VM resource usage (e.g., n_steps, range checks).
443
- /// - `usize`: number of Blake opcodes used, accounted for separately as those are not reported via
444
- /// `ExecutionResources`.
445
- pub fn encode_and_blake_hash_resources (
446
- felt_size_groups : & FeltSizeCount ,
447
- ) -> EstimatedExecutionResources {
448
- let n_steps = compute_blake_hash_steps ( felt_size_groups) ;
449
- let builtin_instance_counter = match felt_size_groups. n_felts ( ) {
450
- // The empty case does not use builtins at all.
451
- 0 => HashMap :: new ( ) ,
452
- // One `range_check` per input felt to validate its size + Overhead for the non empty case.
453
- _ => HashMap :: from ( [ (
454
- BuiltinName :: range_check,
455
- felt_size_groups. n_felts ( ) + blake_estimation:: BASE_RANGE_CHECK_NON_EMPTY ,
456
- ) ] ) ,
457
- } ;
458
-
459
- let resources = ExecutionResources { n_steps, n_memory_holes : 0 , builtin_instance_counter } ;
460
-
461
- EstimatedExecutionResources :: V2Hash {
462
- resources,
463
- blake_count : felt_size_groups. blake_opcode_count ( ) ,
464
- }
465
- }
466
-
467
- /// Converts the execution resources and blake opcode count to L2 gas.
468
- ///
469
- /// Used for both Stwo ("proving_gas") and Stone ("sierra_gas") estimations, which differ in
470
- /// builtin costs. This unified logic is valid because only the `range_check` builtin is used,
471
- /// and its cost is identical across provers (see `bouncer.get_tx_weights`).
472
- // TODO(AvivG): Move inside blake estimation struct.
473
- pub fn blake_execution_resources_estimation_to_gas (
474
- resources : EstimatedExecutionResources ,
475
- versioned_constants : & VersionedConstants ,
476
- blake_opcode_gas : usize ,
477
- ) -> GasAmount {
478
- // TODO(AvivG): Remove this once gas computation is separated from resource estimation.
479
- assert ! (
480
- resources
481
- . resources( )
482
- . builtin_instance_counter
483
- . keys( )
484
- . all( |& k| k == BuiltinName :: range_check) ,
485
- "Expected either empty builtins or only `range_check` builtin, got: {:?}. This breaks the \
486
- assumption that builtin costs are identical between provers.",
487
- resources. resources( ) . builtin_instance_counter. keys( ) . collect:: <Vec <_>>( )
488
- ) ;
489
-
490
- resources. to_sierra_gas (
491
- |resources| vm_resources_to_sierra_gas ( resources, versioned_constants) ,
492
- Some ( blake_opcode_gas) ,
493
- )
494
- }
0 commit comments