Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Nov 27, 2024
1 parent 215d287 commit 9e2d10c
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 33 deletions.
5 changes: 3 additions & 2 deletions cairo_vm_hints/src/hint_processor/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::hints::vars;

use super::CustomHintProcessor;
use crate::hints::lib::contract_bootloader::scopes::CONTRACT_CLASS;
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
Expand All @@ -21,7 +22,7 @@ impl CustomHintProcessor {
) -> Result<(), HintError> {
let contract_class: CasmContractClass = serde_json::from_value(self.private_inputs.clone())
.map_err(|_| HintError::WrongHintData)?;
exec_scopes.insert_value::<CasmContractClass>(CONTRACT_CLASS, contract_class);
exec_scopes.insert_value::<CasmContractClass>(vars::scopes::COMPILED_CLASS, contract_class);
Ok(())
}
}
10 changes: 4 additions & 6 deletions cairo_vm_hints/src/hint_processor/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
pub mod input;
pub mod output;

use crate::hints::lib;
use crate::{
hints::lib::contract_bootloader::scopes::SYSCALL_HANDLER,
syscall_handler::SyscallHandlerWrapper,
};
use crate::hints::{lib, vars};
use crate::syscall_handler::SyscallHandlerWrapper;
use cairo_lang_casm::{
hints::{Hint, StarknetHint},
operand::{BinOpOperand, DerefOrImmediate, Operation, Register, ResOperand},
Expand Down Expand Up @@ -139,7 +136,8 @@ impl HintProcessorLogic for CustomHintProcessor {
if let Hint::Starknet(StarknetHint::SystemCall { system }) = hint {
let syscall_ptr = get_ptr_from_res_operand(vm, system)?;
// TODO: need to be generic here
let syscall_handler = exec_scopes.get::<SyscallHandlerWrapper>(SYSCALL_HANDLER)?;
let syscall_handler =
exec_scopes.get::<SyscallHandlerWrapper>(vars::scopes::SYSCALL_HANDLER)?;

return syscall_handler
.execute_syscall(vm, syscall_ptr)
Expand Down
123 changes: 123 additions & 0 deletions cairo_vm_hints/src/hints/lib/contract_bootloader/builtins.rs
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(())
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::scopes::CONTRACT_CLASS;
use cairo_lang_starknet_classes::casm_contract_class::{CasmContractClass, CasmContractEntryPoint};
use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
Expand All @@ -11,6 +10,8 @@ use cairo_vm::{
};
use std::collections::HashMap;

use crate::hints::vars;

const COMPILED_CLASS_V1: Felt252 =
Felt252::from_hex_unchecked("0x434f4d50494c45445f434c4153535f5631");

Expand All @@ -22,7 +23,7 @@ pub fn load_contract_class(
_hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let contract_class = exec_scopes.get::<CasmContractClass>(CONTRACT_CLASS)?;
let contract_class = exec_scopes.get::<CasmContractClass>(vars::scopes::COMPILED_CLASS)?;
let class_base = vm.add_memory_segment();
write_class(vm, class_base, contract_class)?;
Ok(())
Expand Down
11 changes: 8 additions & 3 deletions cairo_vm_hints/src/hints/lib/contract_bootloader/dict_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::scopes::DICT_MANAGER;
use cairo_vm::{
hint_processor::builtin_hint_processor::{
builtin_hint_processor_definition::HintProcessorData, dict_manager::DictManager,
Expand All @@ -9,6 +8,8 @@ use cairo_vm::{
};
use std::{cell::RefCell, collections::HashMap, rc::Rc};

use crate::hints::vars;

pub const DICT_MANAGER_CREATE: &str = "if '__dict_manager' not in globals():\n from starkware.cairo.common.dict import DictManager\n __dict_manager = DictManager()\n\nmemory[ap] = __dict_manager.new_dict(segments, initial_dict)\ndel initial_dict";

pub fn dict_manager_create(
Expand All @@ -17,10 +18,14 @@ pub fn dict_manager_create(
_hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
if let Err(HintError::VariableNotInScopeError(_)) = exec_scopes.get::<DictManager>(DICT_MANAGER)
if let Err(HintError::VariableNotInScopeError(_)) =
exec_scopes.get::<DictManager>(vars::scopes::DICT_MANAGER)
{
let dict_manager = DictManager::new();
exec_scopes.insert_value(DICT_MANAGER, Rc::new(RefCell::new(dict_manager)));
exec_scopes.insert_value(
vars::scopes::DICT_MANAGER,
Rc::new(RefCell::new(dict_manager)),
);
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions cairo_vm_hints/src/hints/lib/contract_bootloader/mod.rs
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;
Expand Down
7 changes: 3 additions & 4 deletions cairo_vm_hints/src/hints/lib/contract_bootloader/program.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::scopes::CONTRACT_CLASS;
use crate::cairo_types::structs::CompiledClass;
use crate::{cairo_types::structs::CompiledClass, hints::vars};
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
use cairo_vm::{
any_box,
Expand All @@ -23,10 +22,10 @@ pub fn load_program(
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<HintExtension, HintError> {
let cairo_lang_class = exec_scopes.get::<CasmContractClass>(CONTRACT_CLASS)?;
let cairo_lang_class = exec_scopes.get::<CasmContractClass>(vars::scopes::COMPILED_CLASS)?;

let compiled_class_ptr = get_ptr_from_var_name(
CONTRACT_CLASS,
vars::scopes::COMPILED_CLASS,
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
Expand Down
11 changes: 3 additions & 8 deletions cairo_vm_hints/src/hints/lib/contract_bootloader/scopes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::syscall_handler::SyscallHandlerWrapper;
use crate::{hints::vars, syscall_handler::SyscallHandlerWrapper};
use cairo_vm::{
hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData,
types::exec_scope::ExecutionScopes,
Expand All @@ -7,11 +7,6 @@ use cairo_vm::{
};
use std::{any::Any, collections::HashMap};

pub const DICT_MANAGER: &str = "dict_manager";
pub const SYSCALL_HANDLER: &str = "syscall_handler";
pub const CONTRACT_CLASS: &str = "contract_class";
pub const SYSCALL_PTR: &str = "syscall_ptr";

pub const ENTER_SCOPE_SYSCALL_HANDLER: &str =
"vm_enter_scope({'syscall_handler': syscall_handler})";

Expand All @@ -21,10 +16,10 @@ pub fn enter_scope_syscall_handler(
_hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let sys = exec_scopes.get::<SyscallHandlerWrapper>(SYSCALL_HANDLER)?;
let sys = exec_scopes.get::<SyscallHandlerWrapper>(vars::scopes::SYSCALL_HANDLER)?;
let syscall_handler: Box<dyn Any> = Box::new(sys);
exec_scopes.enter_scope(HashMap::from_iter([(
SYSCALL_HANDLER.to_string(),
vars::scopes::SYSCALL_HANDLER.to_string(),
syscall_handler,
)]));
Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::scopes::{SYSCALL_HANDLER, SYSCALL_PTR};
use crate::syscall_handler::SyscallHandlerWrapper;
use crate::{hints::vars, syscall_handler::SyscallHandlerWrapper};
use cairo_vm::{
hint_processor::builtin_hint_processor::{
builtin_hint_processor_definition::HintProcessorData, hint_utils::get_ptr_from_var_name,
Expand All @@ -19,10 +18,13 @@ pub fn syscall_handler_create(
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
if let Err(HintError::VariableNotInScopeError(_)) =
exec_scopes.get::<SyscallHandlerWrapper>(SYSCALL_HANDLER)
exec_scopes.get::<SyscallHandlerWrapper>(vars::scopes::SYSCALL_HANDLER)
{
let syscall_handler = SyscallHandlerWrapper::new();
exec_scopes.insert_value(SYSCALL_HANDLER, Rc::new(RefCell::new(syscall_handler)));
exec_scopes.insert_value(
vars::scopes::SYSCALL_HANDLER,
Rc::new(RefCell::new(syscall_handler)),
);
}

Ok(())
Expand All @@ -37,9 +39,14 @@ pub fn syscall_handler_set_syscall_ptr(
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let syscall_ptr =
get_ptr_from_var_name(SYSCALL_PTR, vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
let syscall_handler = exec_scopes.get::<SyscallHandlerWrapper>(SYSCALL_HANDLER)?;
let syscall_ptr = get_ptr_from_var_name(
vars::ids::SYSCALL_PTR,
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;
let syscall_handler =
exec_scopes.get::<SyscallHandlerWrapper>(vars::scopes::SYSCALL_HANDLER)?;
syscall_handler.set_syscall_ptr(syscall_ptr);

Ok(())
Expand Down
1 change: 1 addition & 0 deletions cairo_vm_hints/src/hints/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod lib;
pub mod vars;
17 changes: 17 additions & 0 deletions cairo_vm_hints/src/hints/vars.rs
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";
}
2 changes: 1 addition & 1 deletion src/contract_bootloader/execute_syscalls.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func execute_call_contract{
}

if (layout == Layout.STARKNET) {
%{ print("Caught Starknet syscall") %}
// %{ print("Caught Starknet syscall") %}
assert 1 = 0;
return ();
Expand Down

0 comments on commit 9e2d10c

Please sign in to comment.