diff --git a/cairo_vm_hints/src/hints/lib/mod.rs b/cairo_vm_hints/src/hints/lib/mod.rs index 51e3b043..5ea535ae 100644 --- a/cairo_vm_hints/src/hints/lib/mod.rs +++ b/cairo_vm_hints/src/hints/lib/mod.rs @@ -7,24 +7,23 @@ use cairo_vm::{ use std::collections::HashMap; pub mod contract_bootloader; +pub mod segments; pub fn run_hint( - _vm: &mut VirtualMachine, - _exec_scope: &mut ExecutionScopes, - _hint_data: &HintProcessorData, - _constants: &HashMap, + vm: &mut VirtualMachine, + exec_scope: &mut ExecutionScopes, + hint_data: &HintProcessorData, + constants: &HashMap, ) -> Result<(), HintError> { - // let hints = []; + let hints = [segments::run_hint]; - // for hint in hints.iter() { - // let res = hint(vm, exec_scope, hint_data, constants); - // if !matches!(res, Err(HintError::UnknownHint(_))) { - // return res; - // } - // } - // Err(HintError::UnknownHint( - // hint_data.code.to_string().into_boxed_str(), - // )) - - todo!() + for hint in hints.iter() { + let res = hint(vm, exec_scope, hint_data, constants); + if !matches!(res, Err(HintError::UnknownHint(_))) { + return res; + } + } + Err(HintError::UnknownHint( + hint_data.code.to_string().into_boxed_str(), + )) } diff --git a/cairo_vm_hints/src/hints/lib/segments/add_segments.rs b/cairo_vm_hints/src/hints/lib/segments/add_segments.rs new file mode 100644 index 00000000..ba52f344 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/segments/add_segments.rs @@ -0,0 +1,50 @@ +use std::collections::HashMap; + +use cairo_vm::{ + hint_processor::builtin_hint_processor::{ + builtin_hint_processor_definition::HintProcessorData, hint_utils::insert_value_into_ap, + }, + types::exec_scope::ExecutionScopes, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, + Felt252, +}; + +pub const SEGMENTS_ADD: &str = "memory[ap] = segments.add()"; +pub const SEGMENTS_ADD_TO_FELT_OR_RELOCATABLE: &str = + "memory[ap] = to_felt_or_relocatable(segments.add())"; + +pub fn segments_add( + vm: &mut VirtualMachine, + _exec_scope: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let segment = vm.add_memory_segment(); + insert_value_into_ap(vm, segment) +} + +pub const SET_FP_PLUS_8_SEGMENTS_ADD: &str = + "memory[fp + 8] = to_felt_or_relocatable(segments.add())"; + +pub fn set_fp_plus_8_segments_add( + vm: &mut VirtualMachine, + _exec_scope: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let segment = vm.add_memory_segment(); + Ok(vm.insert_value((vm.get_fp() + 8)?, segment)?) +} + +pub const SET_FP_PLUS_9_SEGMENTS_ADD: &str = + "memory[fp + 9] = to_felt_or_relocatable(segments.add())"; + +pub fn set_fp_plus_9_segments_add( + vm: &mut VirtualMachine, + _exec_scope: &mut ExecutionScopes, + _hint_data: &HintProcessorData, + _constants: &HashMap, +) -> Result<(), HintError> { + let segment = vm.add_memory_segment(); + Ok(vm.insert_value((vm.get_fp() + 9)?, segment)?) +} diff --git a/cairo_vm_hints/src/hints/lib/segments/mod.rs b/cairo_vm_hints/src/hints/lib/segments/mod.rs new file mode 100644 index 00000000..61f3abe6 --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/segments/mod.rs @@ -0,0 +1,32 @@ +use std::collections::HashMap; + +use cairo_vm::{ + hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData, + types::exec_scope::ExecutionScopes, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, + Felt252, +}; + +mod add_segments; + +pub fn run_hint( + vm: &mut VirtualMachine, + exec_scope: &mut ExecutionScopes, + hint_data: &HintProcessorData, + constants: &HashMap, +) -> Result<(), HintError> { + match hint_data.code.as_str() { + add_segments::SEGMENTS_ADD | add_segments::SEGMENTS_ADD_TO_FELT_OR_RELOCATABLE => { + add_segments::segments_add(vm, exec_scope, hint_data, constants) + } + add_segments::SET_FP_PLUS_8_SEGMENTS_ADD => { + add_segments::set_fp_plus_8_segments_add(vm, exec_scope, hint_data, constants) + } + add_segments::SET_FP_PLUS_9_SEGMENTS_ADD => { + add_segments::set_fp_plus_9_segments_add(vm, exec_scope, hint_data, constants) + } + _ => Err(HintError::UnknownHint( + hint_data.code.to_string().into_boxed_str(), + )), + } +}