-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
181 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
cairo_vm_hints/src/hints/lib/contract_bootloader/builtins.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
use crate::{cairo_types::structs::BuiltinParams, hints::vars}; | ||
use cairo_vm::{ | ||
hint_processor::{ | ||
builtin_hint_processor::{ | ||
builtin_hint_processor_definition::HintProcessorData, | ||
hint_utils::{ | ||
get_integer_from_var_name, get_ptr_from_var_name, insert_value_from_var_name, | ||
}, | ||
}, | ||
hint_processor_utils::felt_to_usize, | ||
}, | ||
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, | ||
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, | ||
Felt252, | ||
}; | ||
use std::{any::Any, collections::HashMap}; | ||
|
||
pub const UPDATE_BUILTIN_PTRS: &str = "from starkware.starknet.core.os.os_utils import update_builtin_pointers\n\n# Fill the values of all builtin pointers after the current transaction.\nids.return_builtin_ptrs = segments.gen_arg(\n update_builtin_pointers(\n memory=memory,\n n_builtins=ids.n_builtins,\n builtins_encoding_addr=ids.builtin_params.builtin_encodings.address_,\n n_selected_builtins=ids.n_selected_builtins,\n selected_builtins_encoding_addr=ids.selected_encodings,\n orig_builtin_ptrs_addr=ids.builtin_ptrs.selectable.address_,\n selected_builtin_ptrs_addr=ids.selected_ptrs,\n ),\n )"; | ||
|
||
pub fn update_builtin_ptrs( | ||
vm: &mut VirtualMachine, | ||
_exec_scopes: &mut ExecutionScopes, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let n_builtins = get_integer_from_var_name( | ||
vars::ids::N_BUILTINS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let builtin_params = get_ptr_from_var_name( | ||
vars::ids::BUILTIN_PARAMS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
let builtins_encoding_addr = | ||
vm.get_relocatable((builtin_params + BuiltinParams::builtin_encodings_offset())?)?; | ||
|
||
let n_selected_builtins = get_integer_from_var_name( | ||
vars::ids::N_SELECTED_BUILTINS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let selected_encodings = get_ptr_from_var_name( | ||
vars::ids::SELECTED_ENCODINGS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let builtin_ptrs = get_ptr_from_var_name( | ||
vars::ids::BUILTIN_PTRS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let orig_builtin_ptrs = builtin_ptrs; | ||
|
||
let selected_ptrs = get_ptr_from_var_name( | ||
vars::ids::SELECTED_PTRS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?; | ||
|
||
let all_builtins = | ||
vm.get_continuous_range(builtins_encoding_addr, felt_to_usize(&n_builtins)?)?; | ||
|
||
let selected_builtins = | ||
vm.get_continuous_range(selected_encodings, felt_to_usize(&n_selected_builtins)?)?; | ||
|
||
let mut returned_builtins: Vec<MaybeRelocatable> = Vec::new(); | ||
let mut selected_builtin_offset: usize = 0; | ||
|
||
for (i, builtin) in all_builtins.iter().enumerate() { | ||
if selected_builtins.contains(builtin) { | ||
returned_builtins.push( | ||
vm.get_maybe(&(selected_ptrs + selected_builtin_offset)?) | ||
.unwrap(), | ||
); | ||
selected_builtin_offset += 1; | ||
} else { | ||
returned_builtins.push(vm.get_maybe(&(orig_builtin_ptrs + i)?).unwrap()); | ||
} | ||
} | ||
|
||
let return_builtin_ptrs_base = vm.add_memory_segment(); | ||
vm.load_data(return_builtin_ptrs_base, &returned_builtins)?; | ||
insert_value_from_var_name( | ||
vars::ids::RETURN_BUILTIN_PTRS, | ||
return_builtin_ptrs_base, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
) | ||
} | ||
|
||
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, | ||
hint_data: &HintProcessorData, | ||
_constants: &HashMap<String, Felt252>, | ||
) -> Result<(), HintError> { | ||
let n_selected_builtins: Box<dyn Any> = Box::new(get_integer_from_var_name( | ||
vars::ids::N_SELECTED_BUILTINS, | ||
vm, | ||
&hint_data.ids_data, | ||
&hint_data.ap_tracking, | ||
)?); | ||
exec_scopes.enter_scope(HashMap::from_iter([( | ||
String::from(vars::scopes::N_SELECTED_BUILTINS), | ||
n_selected_builtins, | ||
)])); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod builtins; | ||
pub mod contract_class; | ||
pub mod dict_manager; | ||
pub mod program; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod lib; | ||
pub mod vars; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
pub mod scopes { | ||
pub(crate) const COMPILED_CLASS: &str = "compiled_class"; | ||
pub(crate) const DICT_MANAGER: &str = "dict_manager"; | ||
pub(crate) const N_SELECTED_BUILTINS: &str = "n_selected_builtins"; | ||
pub(crate) const SYSCALL_HANDLER: &str = "syscall_handler"; | ||
} | ||
|
||
pub mod ids { | ||
pub(crate) const BUILTIN_PARAMS: &str = "builtin_params"; | ||
pub(crate) const BUILTIN_PTRS: &str = "builtin_ptrs"; | ||
pub(crate) const N_BUILTINS: &str = "n_builtins"; | ||
pub(crate) const N_SELECTED_BUILTINS: &str = "n_selected_builtins"; | ||
pub(crate) const RETURN_BUILTIN_PTRS: &str = "return_builtin_ptrs"; | ||
pub(crate) const SELECTED_ENCODINGS: &str = "selected_encodings"; | ||
pub(crate) const SELECTED_PTRS: &str = "selected_ptrs"; | ||
pub(crate) const SYSCALL_PTR: &str = "syscall_ptr"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters