@@ -373,16 +373,6 @@ pub fn poseidon_hash_many_cost(data_length: usize) -> ExecutionResources {
373
373
}
374
374
}
375
375
376
- // Constants that define how felts are encoded into u32s for BLAKE hashing.
377
- mod blake_encoding {
378
- /// Number of u32s in a Blake input message.
379
- pub const N_U32S_MESSAGE : usize = 16 ;
380
-
381
- /// Number of u32s a felt is encoded into.
382
- pub const N_U32S_BIG_FELT : usize = 8 ;
383
- pub const N_U32S_SMALL_FELT : usize = 2 ;
384
- }
385
-
386
376
// Constants used for estimating the cost of BLAKE hashing inside Starknet OS.
387
377
// These values are based on empirical measurement by running
388
378
// `encode_felt252_data_and_calc_blake_hash` on various combinations of big and small felts.
@@ -404,20 +394,8 @@ mod blake_estimation {
404
394
pub const STEPS_EMPTY_INPUT : usize = 170 ;
405
395
}
406
396
407
- /// Calculates the total number of u32s required to encode the given number of big and small felts.
408
- /// Big felts encode to 8 u32s each, small felts encode to 2 u32s each.
409
- fn total_u32s_from_felts ( n_big_felts : usize , n_small_felts : usize ) -> usize {
410
- let big_u32s = n_big_felts
411
- . checked_mul ( blake_encoding:: N_U32S_BIG_FELT )
412
- . expect ( "Overflow computing big felts u32s" ) ;
413
- let small_u32s = n_small_felts
414
- . checked_mul ( blake_encoding:: N_U32S_SMALL_FELT )
415
- . expect ( "Overflow computing small felts u32s" ) ;
416
- big_u32s. checked_add ( small_u32s) . expect ( "Overflow computing total u32s" )
417
- }
418
-
419
397
fn base_steps_for_blake_hash ( n_u32s : usize ) -> usize {
420
- let rem_u32s = n_u32s % blake_encoding :: N_U32S_MESSAGE ;
398
+ let rem_u32s = n_u32s % FeltSizeCount :: U32_WORDS_PER_MESSAGE ;
421
399
if rem_u32s == 0 {
422
400
blake_estimation:: BASE_STEPS_FULL_MSG
423
401
} else {
@@ -441,27 +419,19 @@ fn felts_steps(n_big_felts: usize, n_small_felts: usize) -> usize {
441
419
/// Estimates the number of VM steps needed to hash the given felts with Blake in Starknet OS.
442
420
/// Each small felt unpacks into 2 u32s, and each big felt into 8 u32s.
443
421
/// Adds a base cost depending on whether the total fits exactly into full 16-u32 messages.
444
- fn compute_blake_hash_steps ( n_big_felts : usize , n_small_felts : usize ) -> usize {
445
- let total_u32s = total_u32s_from_felts ( n_big_felts , n_small_felts ) ;
422
+ fn compute_blake_hash_steps ( felt_size_groups : & FeltSizeCount ) -> usize {
423
+ let total_u32s = felt_size_groups . encoded_u32_len ( ) ;
446
424
if total_u32s == 0 {
447
425
// The empty input case is a special case.
448
426
return blake_estimation:: STEPS_EMPTY_INPUT ;
449
427
}
450
428
451
429
let base_steps = base_steps_for_blake_hash ( total_u32s) ;
452
- let felt_steps = felts_steps ( n_big_felts , n_small_felts ) ;
430
+ let felt_steps = felts_steps ( felt_size_groups . large , felt_size_groups . small ) ;
453
431
454
432
base_steps. checked_add ( felt_steps) . expect ( "Overflow computing total Blake hash steps" )
455
433
}
456
434
457
- /// Returns the number of BLAKE opcodes needed to hash the given felts.
458
- /// Each BLAKE opcode processes 16 u32s (partial messages are padded).
459
- fn count_blake_opcode ( n_big_felts : usize , n_small_felts : usize ) -> usize {
460
- // Count the total number of u32s to be hashed.
461
- let total_u32s = total_u32s_from_felts ( n_big_felts, n_small_felts) ;
462
- total_u32s. div_ceil ( blake_encoding:: N_U32S_MESSAGE )
463
- }
464
-
465
435
/// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
466
436
///
467
437
/// # Encoding Details
@@ -475,7 +445,7 @@ fn count_blake_opcode(n_big_felts: usize, n_small_felts: usize) -> usize {
475
445
pub fn encode_and_blake_hash_resources (
476
446
felt_size_groups : & FeltSizeCount ,
477
447
) -> EstimatedExecutionResources {
478
- let n_steps = compute_blake_hash_steps ( felt_size_groups. large , felt_size_groups . small ) ;
448
+ let n_steps = compute_blake_hash_steps ( felt_size_groups) ;
479
449
let builtin_instance_counter = match felt_size_groups. n_felts ( ) {
480
450
// The empty case does not use builtins at all.
481
451
0 => HashMap :: new ( ) ,
@@ -490,7 +460,7 @@ pub fn encode_and_blake_hash_resources(
490
460
491
461
EstimatedExecutionResources :: V2Hash {
492
462
resources,
493
- blake_count : count_blake_opcode ( felt_size_groups. large , felt_size_groups . small ) ,
463
+ blake_count : felt_size_groups. blake_opcode_count ( ) ,
494
464
}
495
465
}
496
466
0 commit comments