Skip to content

Commit

Permalink
fixes for offchain processor
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Oct 28, 2024
1 parent 633bd7e commit f9433cd
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 12 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion cairo_vm_hints/src/hints/lib/mmr/left_child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use cairo_vm::Felt252;
use starknet_types_core::felt::Felt;
use std::collections::HashMap;

pub const MMR_LEFT_CHILD: &str = "ids.in_mmr = 1 if ids.left_child<=ids.mmr_len else 0";
pub const MMR_LEFT_CHILD: &str = "ids.in_mmr = 1 if ids.left_child <= ids.mmr_len else 0";

pub fn mmr_left_child(
vm: &mut VirtualMachine,
Expand Down
4 changes: 4 additions & 0 deletions cairo_vm_hints/src/hints/lib/mmr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::collections::HashMap;

mod bit_length;
mod left_child;
mod peak_values;

pub fn run_hint(
vm: &mut VirtualMachine,
Expand All @@ -22,6 +23,9 @@ pub fn run_hint(
left_child::MMR_LEFT_CHILD => {
left_child::mmr_left_child(vm, exec_scope, hint_data, constants)
}
peak_values::HINT_IS_POSITION_IN_MMR_ARRAY => {
peak_values::hint_is_position_in_mmr_array(vm, exec_scope, hint_data, constants)
}
_ => Err(HintError::UnknownHint(
hint_data.code.to_string().into_boxed_str(),
)),
Expand Down
44 changes: 44 additions & 0 deletions cairo_vm_hints/src/hints/lib/mmr/peak_values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::{
get_integer_from_var_name, insert_value_from_var_name,
};
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::relocatable::MaybeRelocatable;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
use starknet_types_core::felt::Felt;
use std::collections::HashMap;

pub const HINT_IS_POSITION_IN_MMR_ARRAY: &str =
"ids.is_position_in_mmr_array= 1 if ids.position > ids.mmr_offset else 0";

pub fn hint_is_position_in_mmr_array(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let position =
get_integer_from_var_name("position", vm, &hint_data.ids_data, &hint_data.ap_tracking)?;
let mmr_offset = get_integer_from_var_name(
"mmr_offset",
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

let is_position_in_mmr_array = if position > mmr_offset {
Felt::ONE
} else {
Felt::ZERO
};
insert_value_from_var_name(
"is_position_in_mmr_array",
MaybeRelocatable::Int(is_position_in_mmr_array),
vm,
&hint_data.ids_data,
&hint_data.ap_tracking,
)?;

Ok(())
}
6 changes: 3 additions & 3 deletions cairo_vm_hints/src/hints/lib/utils/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use std::collections::HashMap;
pub const HINT_ASSERT_INTEGER_DIV32: &str = "from starkware.cairo.common.math_utils import assert_integer\nassert_integer(ids.DIV_32)\nif not (0 < ids.DIV_32 <= PRIME):\n raise ValueError(f'div={hex(ids.DIV_32)} is out of the valid range.')";

pub fn hint_assert_integer_div32(
vm: &mut VirtualMachine,
_vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_hint_data: &HintProcessorData,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let div_32: Felt252 = utils::get_value("DIV_32", vm, hint_data)?;
let div_32: Felt252 = *get_constant_from_var_name("DIV_32", constants)?;
let prime: Felt252 = *get_constant_from_var_name("PRIME", constants)?;

if Felt252::ZERO < div_32 && div_32 <= prime {
Expand Down
5 changes: 3 additions & 2 deletions cairo_vm_hints/src/hints/lib/utils/divmod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_constant_from_var_name;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::vm::{errors::hint_errors::HintError, vm_core::VirtualMachine};
use cairo_vm::Felt252;
Expand All @@ -14,10 +15,10 @@ pub fn hint_value_div32(
vm: &mut VirtualMachine,
_exec_scope: &mut ExecutionScopes,
hint_data: &HintProcessorData,
_constants: &HashMap<String, Felt252>,
constants: &HashMap<String, Felt252>,
) -> Result<(), HintError> {
let value: Felt252 = utils::get_value("value", vm, hint_data)?;
let div_32: Felt252 = utils::get_value("DIV_32", vm, hint_data)?;
let div_32: Felt252 = *get_constant_from_var_name("DIV_32", constants)?;

let (q, r) = value.div_rem(&NonZeroFelt::try_from(div_32).unwrap());
utils::write_value("q", q, vm, hint_data)?;
Expand Down
55 changes: 49 additions & 6 deletions cairo_vm_hints/src/hints/lib/utils/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ pub fn hint_write_2(
) -> Result<(), HintError> {
let word: Felt252 = utils::get_value("word", vm, hint_data)?;
let ap = vm.get_ap();
for (idx, byte) in word.to_bytes_be().into_iter().take(2).enumerate() {
for (idx, byte) in word
.to_bytes_be()
.into_iter()
.rev()
.take(2)
.rev()
.enumerate()
{
println!("2 {}", byte);
vm.insert_value((ap + idx)?, MaybeRelocatable::Int(byte.into()))
.map_err(HintError::Memory)?;
}
Expand All @@ -34,7 +42,14 @@ pub fn hint_write_3(
) -> Result<(), HintError> {
let word: Felt252 = utils::get_value("word", vm, hint_data)?;
let ap = vm.get_ap();
for (idx, byte) in word.to_bytes_be().into_iter().take(3).enumerate() {
for (idx, byte) in word
.to_bytes_be()
.into_iter()
.rev()
.take(3)
.rev()
.enumerate()
{
vm.insert_value((ap + idx)?, MaybeRelocatable::Int(byte.into()))
.map_err(HintError::Memory)?;
}
Expand All @@ -52,7 +67,14 @@ pub fn hint_write_4(
) -> Result<(), HintError> {
let word: Felt252 = utils::get_value("word", vm, hint_data)?;
let ap = vm.get_ap();
for (idx, byte) in word.to_bytes_be().into_iter().take(4).enumerate() {
for (idx, byte) in word
.to_bytes_be()
.into_iter()
.rev()
.take(4)
.rev()
.enumerate()
{
vm.insert_value((ap + idx)?, MaybeRelocatable::Int(byte.into()))
.map_err(HintError::Memory)?;
}
Expand All @@ -70,7 +92,14 @@ pub fn hint_write_5(
) -> Result<(), HintError> {
let word: Felt252 = utils::get_value("word", vm, hint_data)?;
let ap = vm.get_ap();
for (idx, byte) in word.to_bytes_be().into_iter().take(5).enumerate() {
for (idx, byte) in word
.to_bytes_be()
.into_iter()
.rev()
.take(5)
.rev()
.enumerate()
{
vm.insert_value((ap + idx)?, MaybeRelocatable::Int(byte.into()))
.map_err(HintError::Memory)?;
}
Expand All @@ -88,7 +117,14 @@ pub fn hint_write_6(
) -> Result<(), HintError> {
let word: Felt252 = utils::get_value("word", vm, hint_data)?;
let ap = vm.get_ap();
for (idx, byte) in word.to_bytes_be().into_iter().take(6).enumerate() {
for (idx, byte) in word
.to_bytes_be()
.into_iter()
.rev()
.take(6)
.rev()
.enumerate()
{
vm.insert_value((ap + idx)?, MaybeRelocatable::Int(byte.into()))
.map_err(HintError::Memory)?;
}
Expand All @@ -106,7 +142,14 @@ pub fn hint_write_7(
) -> Result<(), HintError> {
let word: Felt252 = utils::get_value("word", vm, hint_data)?;
let ap = vm.get_ap();
for (idx, byte) in word.to_bytes_be().into_iter().take(7).enumerate() {
for (idx, byte) in word
.to_bytes_be()
.into_iter()
.rev()
.take(7)
.rev()
.enumerate()
{
vm.insert_value((ap + idx)?, MaybeRelocatable::Int(byte.into()))
.map_err(HintError::Memory)?;
}
Expand Down

0 comments on commit f9433cd

Please sign in to comment.