Skip to content

Commit 2037702

Browse files
blockifier: refactor estimation blake constants (starkware-libs#8107)
1 parent c0221db commit 2037702

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

crates/blockifier/src/execution/execution_utils.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
392404
fn 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

402414
fn 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

413426
fn 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 {
436449
fn 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.");

crates/blockifier/src/execution/execution_utils_test.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ use starknet_types_core::felt::Felt;
55

66
use crate::blockifier_versioned_constants::VersionedConstants;
77
use crate::bouncer::vm_resources_to_sierra_gas;
8-
use crate::execution::execution_utils::blake_cost::{
9-
BASE_STEPS_FULL_MSG,
10-
N_U32S_BIG_FELT,
11-
N_U32S_SMALL_FELT,
12-
};
8+
use crate::execution::execution_utils::blake_encoding::{N_U32S_BIG_FELT, N_U32S_SMALL_FELT};
9+
use crate::execution::execution_utils::blake_estimation::BASE_STEPS_FULL_MSG;
1310
use crate::execution::execution_utils::{
1411
compute_blake_hash_steps,
1512
cost_of_encode_felt252_data_and_calc_blake_hash,
@@ -57,7 +54,8 @@ fn test_zero_inputs() {
5754
// TODO(AvivG): Add tests for:
5855
// - `compute_blake_hash_steps` simple cases (felts input).
5956
// - `count_blake_opcode` simple cases (felts input).
60-
// - `cost_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input).
57+
// - `cost_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input) (including partial
58+
// remainder).
6159
// - `cost_of_encode_felt252_data_and_calc_blake_hash` compare against actual execution resources
6260
// from running a Cairo entry point (computing blake).
6361
// - base steps costs - compare against actual execution resources by running on an empty input.

0 commit comments

Comments
 (0)