From f9433cda84385a92342ed8aa3f7364e977e1f2cc Mon Sep 17 00:00:00 2001 From: Bartosz Nowak Date: Mon, 28 Oct 2024 08:33:59 +0100 Subject: [PATCH] fixes for offchain processor --- .../mod.rs} | 0 .../src/hints/lib/mmr/left_child.rs | 2 +- cairo_vm_hints/src/hints/lib/mmr/mod.rs | 4 ++ .../src/hints/lib/mmr/peak_values.rs | 44 +++++++++++++++ cairo_vm_hints/src/hints/lib/utils/assert.rs | 6 +- cairo_vm_hints/src/hints/lib/utils/divmod.rs | 5 +- cairo_vm_hints/src/hints/lib/utils/write.rs | 55 +++++++++++++++++-- 7 files changed, 104 insertions(+), 12 deletions(-) rename cairo_vm_hints/src/{hint_processor.rs => hint_processor/mod.rs} (100%) create mode 100644 cairo_vm_hints/src/hints/lib/mmr/peak_values.rs diff --git a/cairo_vm_hints/src/hint_processor.rs b/cairo_vm_hints/src/hint_processor/mod.rs similarity index 100% rename from cairo_vm_hints/src/hint_processor.rs rename to cairo_vm_hints/src/hint_processor/mod.rs diff --git a/cairo_vm_hints/src/hints/lib/mmr/left_child.rs b/cairo_vm_hints/src/hints/lib/mmr/left_child.rs index 673058c..833f9d2 100644 --- a/cairo_vm_hints/src/hints/lib/mmr/left_child.rs +++ b/cairo_vm_hints/src/hints/lib/mmr/left_child.rs @@ -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, diff --git a/cairo_vm_hints/src/hints/lib/mmr/mod.rs b/cairo_vm_hints/src/hints/lib/mmr/mod.rs index 142a591..5a82dc2 100644 --- a/cairo_vm_hints/src/hints/lib/mmr/mod.rs +++ b/cairo_vm_hints/src/hints/lib/mmr/mod.rs @@ -8,6 +8,7 @@ use std::collections::HashMap; mod bit_length; mod left_child; +mod peak_values; pub fn run_hint( vm: &mut VirtualMachine, @@ -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(), )), diff --git a/cairo_vm_hints/src/hints/lib/mmr/peak_values.rs b/cairo_vm_hints/src/hints/lib/mmr/peak_values.rs new file mode 100644 index 0000000..424b19f --- /dev/null +++ b/cairo_vm_hints/src/hints/lib/mmr/peak_values.rs @@ -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, +) -> 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(()) +} diff --git a/cairo_vm_hints/src/hints/lib/utils/assert.rs b/cairo_vm_hints/src/hints/lib/utils/assert.rs index b0ee599..c76a8fc 100644 --- a/cairo_vm_hints/src/hints/lib/utils/assert.rs +++ b/cairo_vm_hints/src/hints/lib/utils/assert.rs @@ -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, ) -> 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 { diff --git a/cairo_vm_hints/src/hints/lib/utils/divmod.rs b/cairo_vm_hints/src/hints/lib/utils/divmod.rs index ebda9d7..2836154 100644 --- a/cairo_vm_hints/src/hints/lib/utils/divmod.rs +++ b/cairo_vm_hints/src/hints/lib/utils/divmod.rs @@ -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; @@ -14,10 +15,10 @@ pub fn hint_value_div32( vm: &mut VirtualMachine, _exec_scope: &mut ExecutionScopes, hint_data: &HintProcessorData, - _constants: &HashMap, + constants: &HashMap, ) -> 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)?; diff --git a/cairo_vm_hints/src/hints/lib/utils/write.rs b/cairo_vm_hints/src/hints/lib/utils/write.rs index 2d4ab38..1308095 100644 --- a/cairo_vm_hints/src/hints/lib/utils/write.rs +++ b/cairo_vm_hints/src/hints/lib/utils/write.rs @@ -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)?; } @@ -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)?; } @@ -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)?; } @@ -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)?; } @@ -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)?; } @@ -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)?; }