Skip to content

Commit

Permalink
feat(starknet_os): integrate builtins hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dorimedini-starkware committed Jan 27, 2025
1 parent a50f2ff commit 9ad90d4
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/starknet_os/src/hints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod block_context;
pub mod bls_field;
pub mod builtins;
pub mod enum_definition;
pub mod enum_generation;
pub mod error;
Expand Down
41 changes: 41 additions & 0 deletions crates/starknet_os/src/hints/builtins.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::collections::HashMap;

use cairo_vm::hint_processor::hint_processor_definition::HintReference;
use cairo_vm::serde::deserialize_program::ApTracking;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::vm::vm_core::VirtualMachine;
use starknet_types_core::felt::Felt;

use crate::hints::error::HintResult;

pub const SELECTED_BUILTINS: &str =
"vm_enter_scope({'n_selected_builtins': ids.n_selected_builtins})";
pub fn selected_builtins(
_vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt>,
) -> HintResult {
todo!()
}

pub fn select_builtin(
_vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt>,
) -> HintResult {
todo!()
}

pub fn update_builtin_ptrs(
_vm: &mut VirtualMachine,
_exec_scopes: &mut ExecutionScopes,
_ids_data: &HashMap<String, HintReference>,
_ap_tracking: &ApTracking,
_constants: &HashMap<String, Felt>,
) -> HintResult {
todo!()
}
39 changes: 39 additions & 0 deletions crates/starknet_os/src/hints/enum_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::hints::block_context::{
write_use_kzg_da_to_memory,
};
use crate::hints::bls_field::compute_ids_low;
use crate::hints::builtins::{select_builtin, selected_builtins, update_builtin_ptrs};
use crate::hints::error::{HintExtensionResult, HintResult, OsHintError};
use crate::hints::types::{HintEnum, HintExtensionImplementation, HintImplementation};
use crate::{define_hint_enum, define_hint_extension_enum};
Expand Down Expand Up @@ -146,6 +147,44 @@ define_hint_enum!(
ids.low = (ids.value.d0 + ids.value.d1 * ids.BASE) & ((1 << 128) - 1)"#
}
),
(
SelectedBuiltins,
selected_builtins,
"vm_enter_scope({'n_selected_builtins': ids.n_selected_builtins})"
),
(
SelectBuiltin,
select_builtin,
indoc! {r##"
# A builtin should be selected iff its encoding appears in the selected encodings list
# and the list wasn't exhausted.
# Note that testing inclusion by a single comparison is possible since the lists are sorted.
ids.select_builtin = int(
n_selected_builtins > 0 and memory[ids.selected_encodings] == memory[ids.all_encodings])
if ids.select_builtin:
n_selected_builtins = n_selected_builtins - 1"##
}
),
(
UpdateBuiltinPtrs,
update_builtin_ptrs,
indoc! {r#"
from starkware.starknet.core.os.os_utils import update_builtin_pointers
# Fill the values of all builtin pointers after the current transaction.
ids.return_builtin_ptrs = segments.gen_arg(
update_builtin_pointers(
memory=memory,
n_builtins=ids.n_builtins,
builtins_encoding_addr=ids.builtin_params.builtin_encodings.address_,
n_selected_builtins=ids.n_selected_builtins,
selected_builtins_encoding_addr=ids.selected_encodings,
orig_builtin_ptrs_addr=ids.builtin_ptrs.selectable.address_,
selected_builtin_ptrs_addr=ids.selected_ptrs,
),
)"#
}
)
);

define_hint_extension_enum!(
Expand Down

0 comments on commit 9ad90d4

Please sign in to comment.