Skip to content

Commit a567966

Browse files
blockifier: unify from felts and from bytecode to counts
1 parent 9551bd0 commit a567966

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

crates/blockifier/src/execution/contract_class.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,22 @@ impl FeltSizeCount {
7676
pub fn n_felts(&self) -> usize {
7777
self.small + self.large
7878
}
79-
}
8079

81-
/// Counts felts in bytecode by size (small < 2^63, large >= 2^63).
82-
impl From<&[BigUintAsHex]> for FeltSizeCount {
83-
fn from(bytecode: &[BigUintAsHex]) -> Self {
80+
/// Builds a `FeltSizeCount` by mapping each element to a `Felt` with `to_felt`,
81+
/// then classifying it according to its size using `SMALL_THRESHOLD`.
82+
pub fn from_slice<T, F>(items: &[T], to_felt: F) -> Self
83+
where
84+
F: Fn(&T) -> Felt,
85+
{
8486
// TODO(AvivG): use blake2s::SMALL_THRESHOLD.
8587
const SMALL_THRESHOLD: Felt = Felt::from_hex_unchecked("8000000000000000");
8688

87-
let (small, large) = bytecode.iter().fold((0, 0), |(small_count, large_count), x| {
88-
if Felt::from(&x.value) < SMALL_THRESHOLD {
89-
(small_count + 1, large_count)
90-
} else {
91-
(small_count, large_count + 1)
92-
}
93-
});
89+
let mut small = 0;
90+
let mut large = 0;
9491

92+
for x in items {
93+
if to_felt(x) < SMALL_THRESHOLD { small += 1 } else { large += 1 }
94+
}
9595
FeltSizeCount { small, large }
9696
}
9797
}
@@ -156,7 +156,10 @@ impl NestedFeltCounts {
156156

157157
match bytecode_segment_lengths {
158158
NestedIntList::Leaf(len) => {
159-
let felt_size_groups = FeltSizeCount::from(&bytecode[..*len]);
159+
let felt_size_groups =
160+
FeltSizeCount::from_slice(&bytecode[..*len], |x: &BigUintAsHex| {
161+
Felt::from(&x.value)
162+
});
160163
(NestedFeltCounts::Leaf(*len, felt_size_groups), *len)
161164
}
162165
NestedIntList::Node(segments_vec) => {

crates/starknet_os/src/hints/hint_implementation/blake2s/blake2s_test.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,9 @@ use crate::test_utils::cairo_runner::{
1919
ValueArg,
2020
};
2121

22-
/// Counts the number of small and big felts in the data.
23-
// TODO(AvivG): use From<&[Felt]> for FeltSizeCount.
24-
fn data_to_felt_count(data: &[Felt]) -> FeltSizeCount {
25-
// TODO(AvivG): Use `Blake2Felt252::SMALL_THRESHOLD` when exposed.
26-
const SMALL_THRESHOLD: Felt = Felt::from_hex_unchecked("8000000000000000"); // 2^63
27-
28-
let felt_size_groups = FeltSizeCount::default();
29-
30-
data.iter().fold(felt_size_groups, |mut felt_size_groups, felt| {
31-
if *felt >= SMALL_THRESHOLD {
32-
felt_size_groups.large += 1;
33-
} else {
34-
felt_size_groups.small += 1;
35-
}
36-
felt_size_groups
37-
})
38-
}
39-
4022
/// Return the estimated execution resources for Blake2s hashing.
4123
fn estimated_encode_and_blake_hash_execution_resources(data: &[Felt]) -> ExecutionResources {
42-
let felt_size_groups = data_to_felt_count(data);
24+
let felt_size_groups = FeltSizeCount::from_slice(data, |&felt| felt);
4325
let estimated = encode_and_blake_hash_resources(&felt_size_groups);
4426

4527
let mut resources = estimated.resources().clone();

0 commit comments

Comments
 (0)