Skip to content

Commit

Permalink
segments hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Nov 24, 2024
1 parent 6af03c4 commit 633976d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 16 deletions.
31 changes: 15 additions & 16 deletions cairo_vm_hints/src/hints/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Felt252>,
vm: &mut VirtualMachine,
exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> 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(),
))
}
50 changes: 50 additions & 0 deletions cairo_vm_hints/src/hints/lib/segments/add_segments.rs
Original file line number Diff line number Diff line change
@@ -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<String, Felt252>,
) -> 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<String, Felt252>,
) -> 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<String, Felt252>,
) -> Result<(), HintError> {
let segment = vm.add_memory_segment();
Ok(vm.insert_value((vm.get_fp() + 9)?, segment)?)
}
32 changes: 32 additions & 0 deletions cairo_vm_hints/src/hints/lib/segments/mod.rs
Original file line number Diff line number Diff line change
@@ -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<String, Felt252>,
) -> 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(),
)),
}
}

0 comments on commit 633976d

Please sign in to comment.