From 9e2d10cde68ea459b99e4334c7c8776df992f6b6 Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Wed, 27 Nov 2024 13:16:53 +0100 Subject: [PATCH] reorg --- cairo_vm_hints/src/hint_processor/input.rs | 5 +- cairo_vm_hints/src/hint_processor/mod.rs | 10 +- .../hints/lib/contract_bootloader/builtins.rs | 123 ++++++++++++++++++ .../lib/contract_bootloader/contract_class.rs | 5 +- .../lib/contract_bootloader/dict_manager.rs | 11 +- .../src/hints/lib/contract_bootloader/mod.rs | 1 + .../hints/lib/contract_bootloader/program.rs | 7 +- .../hints/lib/contract_bootloader/scopes.rs | 11 +- .../contract_bootloader/syscall_handler.rs | 21 ++- cairo_vm_hints/src/hints/mod.rs | 1 + cairo_vm_hints/src/hints/vars.rs | 17 +++ .../execute_syscalls.cairo | 2 +- 12 files changed, 181 insertions(+), 33 deletions(-) create mode 100644 cairo_vm_hints/src/hints/lib/contract_bootloader/builtins.rs create mode 100644 cairo_vm_hints/src/hints/vars.rs diff --git a/cairo_vm_hints/src/hint_processor/input.rs b/cairo_vm_hints/src/hint_processor/input.rs index e875a977..f4f5d40a 100644 --- a/cairo_vm_hints/src/hint_processor/input.rs +++ b/cairo_vm_hints/src/hint_processor/input.rs @@ -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, @@ -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::(CONTRACT_CLASS, contract_class); + exec_scopes.insert_value::(vars::scopes::COMPILED_CLASS, contract_class); Ok(()) } } diff --git a/cairo_vm_hints/src/hint_processor/mod.rs b/cairo_vm_hints/src/hint_processor/mod.rs index 37b11bf4..9407766f 100644 --- a/cairo_vm_hints/src/hint_processor/mod.rs +++ b/cairo_vm_hints/src/hint_processor/mod.rs @@ -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}, @@ -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::(SYSCALL_HANDLER)?; + let syscall_handler = + exec_scopes.get::(vars::scopes::SYSCALL_HANDLER)?; return syscall_handler .execute_syscall(vm, syscall_ptr) diff --git a/cairo_vm_hints/src/hints/lib/contract_bootloader/builtins.rs b/cairo_vm_hints/src/hints/lib/contract_bootloader/builtins.rs new file mode 100644 index 00000000..51f01616 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/contract_bootloader/builtins.rs @@ -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, +) -> 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 = 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, +) -> Result<(), HintError> { + let n_selected_builtins: Box = 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(()) +} diff --git a/cairo_vm_hints/src/hints/lib/contract_bootloader/contract_class.rs b/cairo_vm_hints/src/hints/lib/contract_bootloader/contract_class.rs index e21dbf41..ff338fdd 100644 --- a/cairo_vm_hints/src/hints/lib/contract_bootloader/contract_class.rs +++ b/cairo_vm_hints/src/hints/lib/contract_bootloader/contract_class.rs @@ -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, @@ -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"); @@ -22,7 +23,7 @@ pub fn load_contract_class( _hint_data: &HintProcessorData, _constants: &HashMap, ) -> Result<(), HintError> { - let contract_class = exec_scopes.get::(CONTRACT_CLASS)?; + let contract_class = exec_scopes.get::(vars::scopes::COMPILED_CLASS)?; let class_base = vm.add_memory_segment(); write_class(vm, class_base, contract_class)?; Ok(()) diff --git a/cairo_vm_hints/src/hints/lib/contract_bootloader/dict_manager.rs b/cairo_vm_hints/src/hints/lib/contract_bootloader/dict_manager.rs index 51c08860..93b204d7 100644 --- a/cairo_vm_hints/src/hints/lib/contract_bootloader/dict_manager.rs +++ b/cairo_vm_hints/src/hints/lib/contract_bootloader/dict_manager.rs @@ -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, @@ -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( @@ -17,10 +18,14 @@ pub fn dict_manager_create( _hint_data: &HintProcessorData, _constants: &HashMap, ) -> Result<(), HintError> { - if let Err(HintError::VariableNotInScopeError(_)) = exec_scopes.get::(DICT_MANAGER) + if let Err(HintError::VariableNotInScopeError(_)) = + exec_scopes.get::(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(()) diff --git a/cairo_vm_hints/src/hints/lib/contract_bootloader/mod.rs b/cairo_vm_hints/src/hints/lib/contract_bootloader/mod.rs index b1b8eaa1..d1412b53 100644 --- a/cairo_vm_hints/src/hints/lib/contract_bootloader/mod.rs +++ b/cairo_vm_hints/src/hints/lib/contract_bootloader/mod.rs @@ -1,3 +1,4 @@ +pub mod builtins; pub mod contract_class; pub mod dict_manager; pub mod program; diff --git a/cairo_vm_hints/src/hints/lib/contract_bootloader/program.rs b/cairo_vm_hints/src/hints/lib/contract_bootloader/program.rs index c9501de5..ebc4d05e 100644 --- a/cairo_vm_hints/src/hints/lib/contract_bootloader/program.rs +++ b/cairo_vm_hints/src/hints/lib/contract_bootloader/program.rs @@ -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, @@ -23,10 +22,10 @@ pub fn load_program( hint_data: &HintProcessorData, _constants: &HashMap, ) -> Result { - let cairo_lang_class = exec_scopes.get::(CONTRACT_CLASS)?; + let cairo_lang_class = exec_scopes.get::(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, diff --git a/cairo_vm_hints/src/hints/lib/contract_bootloader/scopes.rs b/cairo_vm_hints/src/hints/lib/contract_bootloader/scopes.rs index 3c7dca4d..3e7c7ac0 100644 --- a/cairo_vm_hints/src/hints/lib/contract_bootloader/scopes.rs +++ b/cairo_vm_hints/src/hints/lib/contract_bootloader/scopes.rs @@ -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, @@ -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})"; @@ -21,10 +16,10 @@ pub fn enter_scope_syscall_handler( _hint_data: &HintProcessorData, _constants: &HashMap, ) -> Result<(), HintError> { - let sys = exec_scopes.get::(SYSCALL_HANDLER)?; + let sys = exec_scopes.get::(vars::scopes::SYSCALL_HANDLER)?; let syscall_handler: Box = Box::new(sys); exec_scopes.enter_scope(HashMap::from_iter([( - SYSCALL_HANDLER.to_string(), + vars::scopes::SYSCALL_HANDLER.to_string(), syscall_handler, )])); Ok(()) diff --git a/cairo_vm_hints/src/hints/lib/contract_bootloader/syscall_handler.rs b/cairo_vm_hints/src/hints/lib/contract_bootloader/syscall_handler.rs index f4e045bd..b269f046 100644 --- a/cairo_vm_hints/src/hints/lib/contract_bootloader/syscall_handler.rs +++ b/cairo_vm_hints/src/hints/lib/contract_bootloader/syscall_handler.rs @@ -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, @@ -19,10 +18,13 @@ pub fn syscall_handler_create( _constants: &HashMap, ) -> Result<(), HintError> { if let Err(HintError::VariableNotInScopeError(_)) = - exec_scopes.get::(SYSCALL_HANDLER) + exec_scopes.get::(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(()) @@ -37,9 +39,14 @@ pub fn syscall_handler_set_syscall_ptr( hint_data: &HintProcessorData, _constants: &HashMap, ) -> 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::(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::(vars::scopes::SYSCALL_HANDLER)?; syscall_handler.set_syscall_ptr(syscall_ptr); Ok(()) diff --git a/cairo_vm_hints/src/hints/mod.rs b/cairo_vm_hints/src/hints/mod.rs index 965f28e9..c7a7e1c3 100644 --- a/cairo_vm_hints/src/hints/mod.rs +++ b/cairo_vm_hints/src/hints/mod.rs @@ -1 +1,2 @@ pub mod lib; +pub mod vars; diff --git a/cairo_vm_hints/src/hints/vars.rs b/cairo_vm_hints/src/hints/vars.rs new file mode 100644 index 00000000..7f73834a --- /dev/null +++ b/cairo_vm_hints/src/hints/vars.rs @@ -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"; +} diff --git a/src/contract_bootloader/execute_syscalls.cairo b/src/contract_bootloader/execute_syscalls.cairo index 65974a35..79d5c0a5 100644 --- a/src/contract_bootloader/execute_syscalls.cairo +++ b/src/contract_bootloader/execute_syscalls.cairo @@ -114,7 +114,7 @@ func execute_call_contract{ } if (layout == Layout.STARKNET) { - %{ print("Caught Starknet syscall") %} + // %{ print("Caught Starknet syscall") %} assert 1 = 0; return ();