Skip to content

Commit

Permalink
chore: use builtins iter to define os_order_iter
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jul 2, 2024
1 parent 4733c8e commit 63717fb
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions crates/gateway/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,13 @@ pub fn is_subsequence<T: Eq>(subsequence: &[T], sequence: &[T]) -> bool {
offset == subsequence.len()
}

// TODO(Arni): Remove the traitIntoEnumIteratorExt once EnumIter is implemented in starknet API.
impl IntoOsOrderEnumIteratorExt for Builtin {
fn os_order_iter() -> impl Iterator<Item = Builtin> {
// The OS expects this order for the builtins.
// TODO(Arni): Remove the IntoIterExt once IntoIterEnum is implemented in starknet API.
trait IntoIterExt {
fn iter() -> impl Iterator<Item = Builtin>;
}

impl IntoIterExt for Builtin {
fn iter() -> impl Iterator<Item = Builtin> {
vec![
Builtin::Pedersen,
Builtin::RangeCheck,
Expand All @@ -196,6 +199,28 @@ impl IntoOsOrderEnumIteratorExt for Builtin {
}
}

// The OS expects this order for the builtins.
fn builtin_order(builtin: &Builtin) -> usize {
match builtin {
Builtin::Pedersen => 0,
Builtin::RangeCheck => 1,
Builtin::Ecdsa => 2,
Builtin::Bitwise => 3,
Builtin::EcOp => 4,
Builtin::Poseidon => 5,
Builtin::SegmentArena => 6,
Builtin::Keccak => 7,
}
}

impl IntoOsOrderEnumIteratorExt for Builtin {
fn os_order_iter() -> impl Iterator<Item = Builtin> {
let mut builtins_vector = Self::iter().collect::<Vec<Builtin>>();
builtins_vector.sort_by_key(builtin_order);
builtins_vector.into_iter()
}
}

pub trait IntoOsOrderEnumIteratorExt {
/// Returns an iterator over all the builtins in the order the Starknet OS expects.
fn os_order_iter() -> impl Iterator<Item = Builtin>;
Expand Down

0 comments on commit 63717fb

Please sign in to comment.