Skip to content

Commit 2547751

Browse files
blockifier: move logic inside felt_size_count
1 parent b76d138 commit 2547751

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

crates/blockifier/src/execution/contract_class.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ impl FeltSizeCount {
8484
pub(crate) fn n_felts(&self) -> usize {
8585
self.small + self.large
8686
}
87+
88+
/// Returns the total number of `u32` words required to encode all felts
89+
/// according to encode_felts_to_u32s func.
90+
pub(crate) fn encoded_u32_len(&self) -> usize {
91+
self.large * Self::U32_WORDS_PER_LARGE_FELT + self.small * Self::U32_WORDS_PER_SMALL_FELT
92+
}
93+
94+
/// Returns the number of BLAKE opcodes required to hash the felts.
95+
/// Each BLAKE opcode processes one message block of [`U32_WORDS_PER_MESSAGE`] `u32`s
96+
/// (partial messages are padded).
97+
pub(crate) fn blake_opcode_count(&self) -> usize {
98+
self.encoded_u32_len().div_ceil(Self::U32_WORDS_PER_MESSAGE)
99+
}
87100
}
88101

89102
/// Counts felts in bytecode by size (small < 2^63, large >= 2^63).
@@ -104,6 +117,25 @@ impl From<&[BigUintAsHex]> for FeltSizeCount {
104117
}
105118
}
106119

120+
/// Counts felts in bytecode by size (small < 2^63, large >= 2^63).
121+
// TODO(AvivG): Remove this.
122+
impl From<&Vec<Felt>> for FeltSizeCount {
123+
fn from(felts: &Vec<Felt>) -> Self {
124+
// TODO(AvivG): use blake2s::SMALL_THRESHOLD.
125+
const SMALL_THRESHOLD: Felt = Felt::from_hex_unchecked("8000000000000000");
126+
127+
let (small, large) = felts.iter().fold((0, 0), |(small_count, large_count), x| {
128+
if *x < SMALL_THRESHOLD {
129+
(small_count + 1, large_count)
130+
} else {
131+
(small_count, large_count + 1)
132+
}
133+
});
134+
135+
FeltSizeCount { small, large }
136+
}
137+
}
138+
107139
#[derive(Clone, Debug, Eq, PartialEq)]
108140
pub enum NestedFeltCounts {
109141
Leaf(usize, FeltSizeCount), // (leaf length, felt size groups)

crates/blockifier/src/execution/contract_class_test.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ use std::collections::HashSet;
22
use std::sync::Arc;
33

44
use assert_matches::assert_matches;
5+
use blake2s::encode_felts_to_u32s;
56
use blockifier_test_utils::cairo_versions::{CairoVersion, RunnableCairo1};
67
use blockifier_test_utils::contracts::FeatureContract;
78
use cairo_lang_starknet_classes::NestedIntList;
89
use cairo_lang_utils::bigint::BigUintAsHex;
910
use rstest::rstest;
1011
use starknet_api::contract_class::compiled_class_hash::{HashVersion, HashableCompiledClass};
1112
use starknet_api::contract_class::ContractClass;
13+
use starknet_types_core::felt::Felt;
1214

1315
use crate::execution::contract_class::{
1416
CompiledClassV1,
@@ -136,3 +138,16 @@ fn test_create_bytecode_segment_felt_sizes(
136138
let result = NestedFeltCounts::new(&bytecode_segment_lengths, &bytecode);
137139
assert_eq!(result, expected_structure);
138140
}
141+
142+
#[rstest]
143+
#[case::boundary_small_felt(vec![Felt::from((1u64 << 63) - 1)])]
144+
#[case::boundary_at_2_63(vec![Felt::from(1u64 << 63)])]
145+
#[case::very_large_felt(vec![Felt::from_hex("0x800000000000011000000000000000000000000000000000000000000000000").unwrap()])]
146+
#[case::mixed_small_large(vec![Felt::from(42), Felt::from(1u64 << 63), Felt::from(1337)])]
147+
#[case::many_large(vec![Felt::from(1u64 << 63); 100])]
148+
fn test_encode_u32s_lengths(#[case] test_data: Vec<Felt>) {
149+
let actual_u32s = encode_felts_to_u32s(test_data.clone());
150+
let estimated_u32s_len = FeltSizeCount::from(&test_data).encoded_u32_len();
151+
152+
assert_eq!(actual_u32s.len(), estimated_u32s_len);
153+
}

crates/blockifier/src/execution/execution_utils.rs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,6 @@ mod blake_estimation {
394394
pub const STEPS_EMPTY_INPUT: usize = 170;
395395
}
396396

397-
/// Calculates the total number of u32s required to encode the given number of big and small felts.
398-
/// Big felts encode to 8 u32s each, small felts encode to 2 u32s each.
399-
fn total_u32s_from_felts(n_big_felts: usize, n_small_felts: usize) -> usize {
400-
let big_u32s = n_big_felts
401-
.checked_mul(FeltSizeCount::U32_WORDS_PER_LARGE_FELT)
402-
.expect("Overflow computing big felts u32s");
403-
let small_u32s = n_small_felts
404-
.checked_mul(FeltSizeCount::U32_WORDS_PER_SMALL_FELT)
405-
.expect("Overflow computing small felts u32s");
406-
big_u32s.checked_add(small_u32s).expect("Overflow computing total u32s")
407-
}
408-
409397
fn base_steps_for_blake_hash(n_u32s: usize) -> usize {
410398
let rem_u32s = n_u32s % FeltSizeCount::U32_WORDS_PER_MESSAGE;
411399
if rem_u32s == 0 {
@@ -434,7 +422,7 @@ fn felts_steps(n_big_felts: usize, n_small_felts: usize) -> usize {
434422
fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash(
435423
felt_size_groups: &FeltSizeCount,
436424
) -> usize {
437-
let total_u32s = total_u32s_from_felts(felt_size_groups.large, felt_size_groups.small);
425+
let total_u32s = felt_size_groups.encoded_u32_len();
438426
if total_u32s == 0 {
439427
// The empty input case is a special case.
440428
return blake_estimation::STEPS_EMPTY_INPUT;
@@ -446,14 +434,6 @@ fn estimate_steps_of_encode_felt252_data_and_calc_blake_hash(
446434
base_steps.checked_add(felt_steps).expect("Overflow computing total Blake hash steps")
447435
}
448436

449-
/// Returns the number of BLAKE opcodes needed to hash the given felts.
450-
/// Each BLAKE opcode processes 16 u32s (partial messages are padded).
451-
fn count_blake_opcode(felt_size_groups: &FeltSizeCount) -> usize {
452-
// Count the total number of u32s to be hashed.
453-
let total_u32s = total_u32s_from_felts(felt_size_groups.large, felt_size_groups.small);
454-
total_u32s.div_ceil(FeltSizeCount::U32_WORDS_PER_MESSAGE)
455-
}
456-
457437
/// Estimates resource usage for `encode_felt252_data_and_calc_blake_hash` in the Starknet OS.
458438
///
459439
/// # Encoding Details
@@ -482,7 +462,7 @@ pub fn encode_and_blake_hash_resources(
482462

483463
EstimatedExecutionResources::V2Hash {
484464
resources,
485-
blake_count: count_blake_opcode(felt_size_groups),
465+
blake_count: felt_size_groups.blake_opcode_count(),
486466
}
487467
}
488468

crates/blockifier/src/execution/execution_utils_test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use starknet_types_core::felt::Felt;
66
use crate::execution::contract_class::FeltSizeCount;
77
use crate::execution::execution_utils::blake_estimation::STEPS_EMPTY_INPUT;
88
use crate::execution::execution_utils::{
9-
count_blake_opcode,
109
encode_and_blake_hash_resources,
1110
estimate_steps_of_encode_felt252_data_and_calc_blake_hash,
1211
};
@@ -35,7 +34,7 @@ fn test_zero_inputs() {
3534
assert_eq!(steps, STEPS_EMPTY_INPUT, "Unexpected base step cost for zero inputs");
3635

3736
// No opcodes should be emitted.
38-
let opcodes = count_blake_opcode(&FeltSizeCount::default());
37+
let opcodes = FeltSizeCount::default().blake_opcode_count();
3938
assert_eq!(opcodes, 0, "Expected zero BLAKE opcodes for zero inputs");
4039

4140
// Should result in base cost only (no opcode cost).
@@ -47,7 +46,7 @@ fn test_zero_inputs() {
4746

4847
// TODO(AvivG): Add tests for:
4948
// - `estimate_steps_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input).
50-
// - `count_blake_opcode` simple cases (felts input).
49+
// - `blake_opcode_count` simple cases (felts input).
5150
// - `cost_of_encode_felt252_data_and_calc_blake_hash` simple cases (felts input) (including partial
5251
// remainder).
5352
// - `cost_of_encode_felt252_data_and_calc_blake_hash` compare against actual execution resources

0 commit comments

Comments
 (0)