Skip to content

Commit 2866cb0

Browse files
blockifier: unify from felts and from bytecode to counts
1 parent d4570ea commit 2866cb0

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

crates/blockifier/src/execution/contract_class.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,32 @@ impl FeltSizeCount {
7878
}
7979
}
8080

81-
/// Counts felts in bytecode by size (small < 2^63, large >= 2^63).
81+
// TODO(AvivG): use blake2s::SMALL_THRESHOLD.
82+
const SMALL_THRESHOLD: Felt = Felt::from_hex_unchecked("8000000000000000");
83+
84+
85+
/// Counts elements into “small” vs “large” buckets using the provided predicate.
86+
#[inline]
87+
fn count_felts_by_size<T>(slice: &[T], mut is_small: impl FnMut(&T) -> bool) -> FeltSizeCount {
88+
let mut small = 0;
89+
let mut large = 0;
90+
for it in slice {
91+
if is_small(it) { small += 1 } else { large += 1 }
92+
}
93+
FeltSizeCount { small, large }
94+
}
95+
96+
/// Classifies bytecode words by felt size using `SMALL_THRESHOLD = 2^63`.
8297
impl From<&[BigUintAsHex]> for FeltSizeCount {
8398
fn from(bytecode: &[BigUintAsHex]) -> Self {
84-
// TODO(AvivG): use blake2s::SMALL_THRESHOLD.
85-
const SMALL_THRESHOLD: Felt = Felt::from_hex_unchecked("8000000000000000");
86-
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-
});
99+
count_felts_by_size(bytecode, |x| Felt::from(&x.value) < SMALL_THRESHOLD)
100+
}
101+
}
94102

95-
FeltSizeCount { small, large }
103+
/// Classifies felts by size using `SMALL_THRESHOLD = 2^63`.
104+
impl From<&[Felt]> for FeltSizeCount {
105+
fn from(bytecode: &[Felt]) -> Self {
106+
count_felts_by_size(bytecode, |felt| *felt < SMALL_THRESHOLD)
96107
}
97108
}
98109

0 commit comments

Comments
 (0)