@@ -368,54 +368,67 @@ pub fn poseidon_hash_many_cost(data_length: usize) -> ExecutionResources {
368368 }
369369}
370370
371- mod blake_cost {
372- // U-32 counts
371+ // Constants that define how felts are encoded into u32s for BLAKE hashing.
372+ mod blake_encoding {
373+ /// Number of u32s in a Blake input message.
373374 pub const N_U32S_MESSAGE : usize = 16 ;
375+
376+ /// Number of u32s a felt is encoded into.
374377 pub const N_U32S_BIG_FELT : usize = 8 ;
375378 pub const N_U32S_SMALL_FELT : usize = 2 ;
379+ }
376380
377- // Steps counts
381+ // Constants used for estimating the cost of BLAKE hashing inside Starknet OS.
382+ // These values are based on empirical measurement by running
383+ // `encode_felt252_data_and_calc_blake_hash` on various combinations of big and small felts.
384+ mod blake_estimation {
385+ // Per-felt step cost (measured).
378386 pub const STEPS_BIG_FELT : usize = 45 ;
379387 pub const STEPS_SMALL_FELT : usize = 15 ;
380388
381- // One-time segment setup cost (full vs partial)
389+ // One-time overhead.
390+ // Overhead when input fills a full Blake message (16 u32s).
382391 pub const BASE_STEPS_FULL_MSG : usize = 217 ;
392+ // Overhead when input results in a partial message (remainder < 16 u32s).
383393 pub const BASE_STEPS_PARTIAL_MSG : usize = 195 ;
394+ // Extra steps per 2-u32 remainder in partial messages.
384395 pub const STEPS_PER_2_U32_REMINDER : usize = 3 ;
385396
386- // TODO(AvivG): This is a placeholder, add the actual gas cost for the BLAKE opcode
397+ // BLAKE opcode gas cost in Stwo.
398+ // TODO(AvivG): Replace with actual cost when known.
387399 pub const BLAKE_OPCODE_GAS : usize = 0 ;
388400}
389401
390402/// Calculates the total number of u32s required to encode the given number of big and small felts.
391403/// Big felts encode to 8 u32s each, small felts encode to 2 u32s each.
392404fn total_u32s_from_felts ( n_big_felts : usize , n_small_felts : usize ) -> usize {
393405 let big_u32s = n_big_felts
394- . checked_mul ( blake_cost :: N_U32S_BIG_FELT )
406+ . checked_mul ( blake_encoding :: N_U32S_BIG_FELT )
395407 . expect ( "Overflow computing big felts u32s" ) ;
396408 let small_u32s = n_small_felts
397- . checked_mul ( blake_cost :: N_U32S_SMALL_FELT )
409+ . checked_mul ( blake_encoding :: N_U32S_SMALL_FELT )
398410 . expect ( "Overflow computing small felts u32s" ) ;
399411 big_u32s. checked_add ( small_u32s) . expect ( "Overflow computing total u32s" )
400412}
401413
402414fn base_steps_for_blake_hash ( n_u32s : usize ) -> usize {
403- let rem_u32s = n_u32s % blake_cost :: N_U32S_MESSAGE ;
415+ let rem_u32s = n_u32s % blake_encoding :: N_U32S_MESSAGE ;
404416 if rem_u32s == 0 {
405- blake_cost :: BASE_STEPS_FULL_MSG
417+ blake_estimation :: BASE_STEPS_FULL_MSG
406418 } else {
407419 // This computation is based on running blake2s with different inputs.
408420 // Note: all inputs expand to an even number of u32s --> `rem_u32s` is always even.
409- blake_cost:: BASE_STEPS_PARTIAL_MSG + ( rem_u32s / 2 ) * blake_cost:: STEPS_PER_2_U32_REMINDER
421+ blake_estimation:: BASE_STEPS_PARTIAL_MSG
422+ + ( rem_u32s / 2 ) * blake_estimation:: STEPS_PER_2_U32_REMINDER
410423 }
411424}
412425
413426fn felts_steps ( n_big_felts : usize , n_small_felts : usize ) -> usize {
414427 let big_steps = n_big_felts
415- . checked_mul ( blake_cost :: STEPS_BIG_FELT )
428+ . checked_mul ( blake_estimation :: STEPS_BIG_FELT )
416429 . expect ( "Overflow computing big felt steps" ) ;
417430 let small_steps = n_small_felts
418- . checked_mul ( blake_cost :: STEPS_SMALL_FELT )
431+ . checked_mul ( blake_estimation :: STEPS_SMALL_FELT )
419432 . expect ( "Overflow computing small felt steps" ) ;
420433 big_steps. checked_add ( small_steps) . expect ( "Overflow computing total felt steps" )
421434}
@@ -436,7 +449,7 @@ fn compute_blake_hash_steps(n_big_felts: usize, n_small_felts: usize) -> usize {
436449fn count_blake_opcode ( n_big_felts : usize , n_small_felts : usize ) -> usize {
437450 // Count the total number of u32s to be hashed.
438451 let total_u32s = total_u32s_from_felts ( n_big_felts, n_small_felts) ;
439- total_u32s. div_ceil ( blake_cost :: N_U32S_MESSAGE )
452+ total_u32s. div_ceil ( blake_encoding :: N_U32S_MESSAGE )
440453}
441454
442455/// Estimates the VM resources for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
@@ -464,7 +477,7 @@ where
464477
465478 let blake_op_count = count_blake_opcode ( n_big_felts, n_small_felts) ;
466479 let blake_op_gas = blake_op_count
467- . checked_mul ( blake_cost :: BLAKE_OPCODE_GAS )
480+ . checked_mul ( blake_estimation :: BLAKE_OPCODE_GAS )
468481 . map ( u64_from_usize)
469482 . map ( GasAmount )
470483 . expect ( "Overflow computing Blake opcode gas." ) ;
0 commit comments