diff --git a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs index 6bc7fddb95..a12c20c0d8 100644 --- a/src/hint_processor/builtin_hint_processor/blake2s_utils.rs +++ b/src/hint_processor/builtin_hint_processor/blake2s_utils.rs @@ -9,10 +9,7 @@ use crate::{ }, serde::deserialize_program::ApTracking, types::relocatable::{MaybeRelocatable, Relocatable}, - vm::{ - errors::{hint_errors::HintError, vm_errors::VirtualMachineError}, - vm_core::VirtualMachine, - }, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; use felt::Felt; use num_traits::ToPrimitive; @@ -56,7 +53,7 @@ fn compute_blake2s_func(vm: &mut VirtualMachine, output_rel: Relocatable) -> Res get_maybe_relocatable_array_from_u32(&blake2s_compress(&h, &message, t, 0, f, 0)); let output_ptr = MaybeRelocatable::RelocatableValue(output_rel); vm.load_data(&output_ptr, &new_state) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } @@ -117,7 +114,7 @@ pub fn finalize_blake2s( } let data = get_maybe_relocatable_array_from_u32(&full_padding); vm.load_data(&MaybeRelocatable::RelocatableValue(blake2s_ptr_end), &data) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } @@ -152,7 +149,7 @@ pub fn blake2s_add_uint256( //Insert first batch of data let data = get_maybe_relocatable_array_from_felt(&inner_data); vm.load_data(&MaybeRelocatable::RelocatableValue(data_ptr), &data) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; //Build second batch of data let mut inner_data = Vec::::new(); for i in 0..4 { @@ -164,7 +161,7 @@ pub fn blake2s_add_uint256( &MaybeRelocatable::RelocatableValue(data_ptr).add_usize(4), &data, ) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } @@ -199,7 +196,7 @@ pub fn blake2s_add_uint256_bigend( //Insert first batch of data let data = get_maybe_relocatable_array_from_felt(&inner_data); vm.load_data(&MaybeRelocatable::RelocatableValue(data_ptr), &data) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; //Build second batch of data let mut inner_data = Vec::::new(); for i in 0..4 { @@ -211,13 +208,14 @@ pub fn blake2s_add_uint256_bigend( &MaybeRelocatable::RelocatableValue(data_ptr).add_usize(4), &data, ) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } #[cfg(test)] mod tests { use super::*; + use crate::vm::errors::vm_errors::VirtualMachineError; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use crate::{ any_box, @@ -270,7 +268,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::UnknownMemoryCell( + Err(HintError::Memory(MemoryError::UnknownMemoryCell( x ))) if x == Relocatable::from((2, 0)) ); @@ -337,7 +335,9 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::ExpectedInteger(x))) if x == Relocatable::from((2,0)) + Err(HintError::Memory(MemoryError::ExpectedInteger( + x + ))) if x == Relocatable::from((2, 0)) ); } @@ -400,13 +400,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((2, 0)) && + )) if x == MaybeRelocatable::from((2, 0)) && y == MaybeRelocatable::from((2, 0)) && z == MaybeRelocatable::from(Felt::new(1795745351)) ); diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index c375b55853..db4dd67767 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -454,10 +454,7 @@ mod tests { types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, utils::test_utils::*, vm::{ - errors::{ - exec_scope_errors::ExecScopeError, memory_errors::MemoryError, - vm_errors::VirtualMachineError, - }, + errors::{exec_scope_errors::ExecScopeError, memory_errors::MemoryError}, vm_core::VirtualMachine, vm_memory::memory::Memory, }, @@ -506,13 +503,13 @@ mod tests { //ids and references are not needed for this test assert_matches!( run_hint!(vm, HashMap::new(), hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 6)) && + )) if x == MaybeRelocatable::from((1, 6)) && y == MaybeRelocatable::from((1, 6)) && z == MaybeRelocatable::from((3, 0)) ); @@ -614,13 +611,13 @@ mod tests { let ids_data = ids_data!["continue_copying"]; assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 1)) && + )) if x == MaybeRelocatable::from((1, 1)) && y == MaybeRelocatable::from(Felt::new(5)) && z == MaybeRelocatable::from(Felt::zero()) ); @@ -801,9 +798,7 @@ mod tests { let ids_data = non_continuous_ids_data![("keccak_state", -7), ("high", -3), ("low", -2)]; assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::UnknownMemoryCell( - _ - ))) + Err(HintError::Memory(MemoryError::UnknownMemoryCell(_))) ); } diff --git a/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs b/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs index 8b23a3b4d8..c1dcd3ea9d 100644 --- a/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs +++ b/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs @@ -50,11 +50,11 @@ pub fn keccak_write_args( let low_args: Vec<_> = low_args.into_iter().map(MaybeRelocatable::from).collect(); vm.write_arg(inputs_ptr, &low_args) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; let high_args: Vec<_> = high_args.into_iter().map(MaybeRelocatable::from).collect(); vm.write_arg(inputs_ptr.add(2_i32), &high_args) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } @@ -148,7 +148,7 @@ pub fn block_permutation( &MaybeRelocatable::RelocatableValue(keccak_ptr.sub_usize(keccak_state_size_felts)?), keccak_state_size_felts, ) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; let mut u64_values = maybe_reloc_vec_to_u64_array(&values)? .try_into() @@ -161,7 +161,7 @@ pub fn block_permutation( let bigint_values = u64_array_to_mayberelocatable_vec(&u64_values); vm.write_arg(keccak_ptr, &bigint_values) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } @@ -221,7 +221,7 @@ pub fn cairo_keccak_finalize( let keccak_ptr_end = get_ptr_from_var_name("keccak_ptr_end", vm, ids_data, ap_tracking)?; vm.write_arg(keccak_ptr_end, &padding) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } @@ -300,10 +300,7 @@ mod tests { //Create ids let ids_data = ids_data!["low", "high", "inputs"]; let error = run_hint!(vm, ids_data, hint_code); - assert_matches!( - error, - Err(HintError::Internal(VirtualMachineError::MemoryError(_))) - ); + assert_matches!(error, Err(HintError::Memory(_))); } #[test] diff --git a/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs b/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs index 18c3efbdc3..a8c576c1f6 100644 --- a/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/dict_hint_utils.rs @@ -258,7 +258,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::hint_code; use crate::hint_processor::hint_processor_definition::HintProcessor; use crate::types::exec_scope::ExecutionScopes; - use crate::vm::errors::vm_errors::VirtualMachineError; use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use crate::{ @@ -325,13 +324,13 @@ mod tests { //ids and references are not needed for this test assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 0)) && + )) if x == MaybeRelocatable::from((1, 0)) && y == MaybeRelocatable::from(1) && z == MaybeRelocatable::from((2, 0)) ); diff --git a/src/hint_processor/builtin_hint_processor/dict_manager.rs b/src/hint_processor/builtin_hint_processor/dict_manager.rs index bb70de09c1..5d1b1ff5a9 100644 --- a/src/hint_processor/builtin_hint_processor/dict_manager.rs +++ b/src/hint_processor/builtin_hint_processor/dict_manager.rs @@ -2,12 +2,7 @@ use std::collections::HashMap; use crate::{ types::relocatable::{MaybeRelocatable, Relocatable}, - vm::{ - errors::{ - hint_errors::HintError, memory_errors::MemoryError, vm_errors::VirtualMachineError, - }, - vm_core::VirtualMachine, - }, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; #[derive(PartialEq, Eq, Debug, Clone)] @@ -80,12 +75,6 @@ impl DictManager { return Err(HintError::CantCreateDictionaryOnTakenSegment( base.segment_index, )); - } - - if base.segment_index < 0 { - Err(VirtualMachineError::MemoryError( - MemoryError::AddressInTemporarySegment(base.segment_index), - ))?; }; self.trackers.insert( diff --git a/src/hint_processor/builtin_hint_processor/hint_utils.rs b/src/hint_processor/builtin_hint_processor/hint_utils.rs index 77cc45a890..eb31045b2a 100644 --- a/src/hint_processor/builtin_hint_processor/hint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/hint_utils.rs @@ -25,7 +25,7 @@ pub fn insert_value_from_var_name( ) -> Result<(), HintError> { let var_address = get_relocatable_from_var_name(var_name, vm, ids_data, ap_tracking)?; vm.insert_value(var_address, value) - .map_err(HintError::Internal) + .map_err(HintError::Memory) } //Inserts value into ap @@ -34,7 +34,7 @@ pub fn insert_value_into_ap( value: impl Into, ) -> Result<(), HintError> { vm.insert_value(vm.get_ap(), value) - .map_err(HintError::Internal) + .map_err(HintError::Memory) } //Returns the Relocatable value stored in the given ids variable diff --git a/src/hint_processor/builtin_hint_processor/math_utils.rs b/src/hint_processor/builtin_hint_processor/math_utils.rs index fcc117af4d..d51a88161e 100644 --- a/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -297,7 +297,7 @@ pub fn split_int( if &res > bound { return Err(HintError::SplitIntLimbOutOfRange(res)); } - vm.insert_value(output, res).map_err(HintError::Internal) + vm.insert_value(output, res).map_err(HintError::Memory) } //from starkware.cairo.common.math_utils import is_positive @@ -743,13 +743,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 0)) && + )) if x == MaybeRelocatable::from((1, 0)) && y == MaybeRelocatable::Int(Felt::one()) && z == MaybeRelocatable::Int(Felt::zero()) ); @@ -1321,13 +1321,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 1)) && + )) if x == MaybeRelocatable::from((1, 1)) && y == MaybeRelocatable::from(Felt::new(4)) && z == MaybeRelocatable::from(Felt::one()) ); @@ -1381,13 +1381,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 1)) && + )) if x == MaybeRelocatable::from((1, 1)) && y == MaybeRelocatable::from(Felt::new(7)) && z == MaybeRelocatable::from(Felt::new(9)) ); @@ -1459,13 +1459,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 0)) && + )) if x == MaybeRelocatable::from((1, 0)) && y == MaybeRelocatable::Int(Felt::new(5)) && z == MaybeRelocatable::Int(Felt::new(2)) ); @@ -1569,13 +1569,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 1)) && + )) if x == MaybeRelocatable::from((1, 1)) && y == MaybeRelocatable::Int(Felt::new(10)) && z == MaybeRelocatable::Int(Felt::new(31)) ); @@ -1710,13 +1710,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((2, 0)) && + )) if x == MaybeRelocatable::from((2, 0)) && y == MaybeRelocatable::from(Felt::new(99)) && z == MaybeRelocatable::from(felt_str!("335438970432432812899076431678123043273")) ); @@ -1744,13 +1744,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((2, 1)) && + )) if x == MaybeRelocatable::from((2, 1)) && y == MaybeRelocatable::from(Felt::new(99)) && z == MaybeRelocatable::from(Felt::new(0)) ); diff --git a/src/hint_processor/builtin_hint_processor/memset_utils.rs b/src/hint_processor/builtin_hint_processor/memset_utils.rs index 44b3cf7855..8d13dd38da 100644 --- a/src/hint_processor/builtin_hint_processor/memset_utils.rs +++ b/src/hint_processor/builtin_hint_processor/memset_utils.rs @@ -67,10 +67,7 @@ mod tests { }, types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, utils::test_utils::*, - vm::{ - errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError}, - vm_memory::memory::Memory, - }, + vm::{errors::memory_errors::MemoryError, vm_memory::memory::Memory}, }; use assert_matches::assert_matches; use num_traits::{One, Zero}; @@ -174,13 +171,13 @@ mod tests { let ids_data = ids_data!["continue_loop"]; assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 0)) && + )) if x == MaybeRelocatable::from((1, 0)) && y == MaybeRelocatable::from(Felt::new(5)) && z == MaybeRelocatable::from(Felt::zero()) ); diff --git a/src/hint_processor/builtin_hint_processor/pow_utils.rs b/src/hint_processor/builtin_hint_processor/pow_utils.rs index cf43fa0b2b..cb903b1d3e 100644 --- a/src/hint_processor/builtin_hint_processor/pow_utils.rs +++ b/src/hint_processor/builtin_hint_processor/pow_utils.rs @@ -45,10 +45,8 @@ mod tests { types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, utils::test_utils::*, vm::{ - errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError}, - runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, - vm_memory::memory::Memory, + errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, + vm_core::VirtualMachine, vm_memory::memory::Memory, }, }; use assert_matches::assert_matches; @@ -131,13 +129,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 11)) && + )) if x == MaybeRelocatable::from((1, 11)) && y == MaybeRelocatable::from(Felt::new(3)) && z == MaybeRelocatable::from(Felt::one()) ); diff --git a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index 3582d38098..e76f4e4dae 100644 --- a/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -11,10 +11,7 @@ use crate::{ exec_scope::ExecutionScopes, relocatable::{MaybeRelocatable, Relocatable}, }, - vm::{ - errors::{hint_errors::HintError, vm_errors::VirtualMachineError}, - vm_core::VirtualMachine, - }, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; use felt::Felt; use std::{borrow::Cow, collections::HashMap}; @@ -80,8 +77,7 @@ pub fn nondet_bigint3( .into_iter() .map(|n| MaybeRelocatable::from(Felt::new(n))) .collect(); - vm.write_arg(res_reloc, &arg) - .map_err(VirtualMachineError::MemoryError)?; + vm.write_arg(res_reloc, &arg).map_err(HintError::Memory)?; Ok(()) } diff --git a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs index cb704eb58c..3fb1a80068 100644 --- a/src/hint_processor/builtin_hint_processor/secp/field_utils.rs +++ b/src/hint_processor/builtin_hint_processor/secp/field_utils.rs @@ -176,8 +176,7 @@ mod tests { }, utils::test_utils::*, vm::{ - errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError}, - runners::builtin_runner::RangeCheckBuiltinRunner, + errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, vm_memory::memory::Memory, }, }; @@ -291,13 +290,13 @@ mod tests { .map(|(k, v)| (k.to_string(), v)) .collect() ), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 9)) && + )) if x == MaybeRelocatable::from((1, 9)) && y == MaybeRelocatable::from(Felt::new(55_i32)) && z == MaybeRelocatable::from(Felt::zero()) ); @@ -576,13 +575,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, HashMap::new(), hint_code, &mut exec_scopes), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from(vm.run_context.get_ap()) && + )) if x == MaybeRelocatable::from(vm.run_context.get_ap()) && y == MaybeRelocatable::from(Felt::new(55i32)) && z == MaybeRelocatable::from(Felt::new(1i32)) ); diff --git a/src/hint_processor/builtin_hint_processor/segments.rs b/src/hint_processor/builtin_hint_processor/segments.rs index 42fbd328d8..45a779ca26 100644 --- a/src/hint_processor/builtin_hint_processor/segments.rs +++ b/src/hint_processor/builtin_hint_processor/segments.rs @@ -5,7 +5,6 @@ use crate::hint_processor::{ }; use crate::serde::deserialize_program::ApTracking; use crate::vm::errors::hint_errors::HintError; -use crate::vm::errors::vm_errors::VirtualMachineError; use crate::vm::vm_core::VirtualMachine; use std::collections::HashMap; @@ -22,7 +21,7 @@ pub fn relocate_segment( let dest_ptr = get_ptr_from_var_name("dest_ptr", vm, ids_data, ap_tracking)?; vm.add_relocation_rule(src_ptr, dest_ptr) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(HintError::Memory)?; Ok(()) } diff --git a/src/hint_processor/builtin_hint_processor/set.rs b/src/hint_processor/builtin_hint_processor/set.rs index 5cb9185ed9..a7d249fead 100644 --- a/src/hint_processor/builtin_hint_processor/set.rs +++ b/src/hint_processor/builtin_hint_processor/set.rs @@ -33,7 +33,7 @@ pub fn set_add( } let elm = vm .get_range(&MaybeRelocatable::from(elm_ptr), elm_size) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; if set_ptr > set_end_ptr { return Err(HintError::InvalidSetRange( @@ -47,7 +47,7 @@ pub fn set_add( for i in (0..range_limit).step_by(elm_size) { let set_iter = vm .get_range(&MaybeRelocatable::from(set_ptr + i), elm_size) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; if set_iter == elm { insert_value_from_var_name( diff --git a/src/hint_processor/builtin_hint_processor/sha256_utils.rs b/src/hint_processor/builtin_hint_processor/sha256_utils.rs index fe78128c5b..df8672e035 100644 --- a/src/hint_processor/builtin_hint_processor/sha256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/sha256_utils.rs @@ -74,7 +74,7 @@ pub fn sha256_main( let output_base = get_ptr_from_var_name("output", vm, ids_data, ap_tracking)?; vm.write_arg(output_base, &output) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; Ok(()) } @@ -110,7 +110,7 @@ pub fn sha256_finalize( } vm.write_arg(sha256_ptr_end, &padding) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; Ok(()) } diff --git a/src/hint_processor/builtin_hint_processor/signature.rs b/src/hint_processor/builtin_hint_processor/signature.rs index 2ef5d5a517..b02011b406 100644 --- a/src/hint_processor/builtin_hint_processor/signature.rs +++ b/src/hint_processor/builtin_hint_processor/signature.rs @@ -36,7 +36,7 @@ pub fn verify_ecdsa_signature( } ecdsa_builtin .add_signature(ecdsa_ptr, &(signature_r, signature_s)) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; Ok(()) } diff --git a/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs b/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs index 33d7e6a41c..e24847c735 100644 --- a/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs +++ b/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs @@ -12,7 +12,7 @@ use crate::{ serde::deserialize_program::ApTracking, types::exec_scope::ExecutionScopes, vm::{ - errors::{hint_errors::HintError, vm_errors::VirtualMachineError}, + errors::{hint_errors::HintError, memory_errors::MemoryError}, vm_core::VirtualMachine, }, }; @@ -67,7 +67,7 @@ pub fn squash_dict_inner_first_iteration( exec_scopes.insert_value("current_access_index", first_val.clone()); //Insert current_accesss_index into range_check_ptr vm.insert_value(range_check_ptr, first_val) - .map_err(HintError::Internal) + .map_err(HintError::Memory) } // Implements Hint: ids.should_skip_loop = 0 if current_access_indices else 1 @@ -143,7 +143,7 @@ pub fn squash_dict_inner_continue_loop( //Insert loop_temps.delta_minus1 into memory let should_continue_addr = loop_temps_addr + 3_i32; vm.insert_value(should_continue_addr, should_continue) - .map_err(HintError::Internal) + .map_err(HintError::Memory) } // Implements Hint: assert len(current_access_indices) == 0 @@ -268,7 +268,7 @@ pub fn squash_dict( let key_addr = address + DICT_ACCESS_SIZE * i; let key = vm .get_integer(key_addr) - .map_err(|_| VirtualMachineError::ExpectedInteger(key_addr))?; + .map_err(|_| MemoryError::ExpectedInteger(key_addr))?; access_indices .entry(key.into_owned()) .or_default() diff --git a/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/src/hint_processor/builtin_hint_processor/uint256_utils.rs index 0b27826390..d7ba38457d 100644 --- a/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -128,7 +128,7 @@ pub fn uint256_sqrt( } vm.insert_value(root_addr, Felt::new(root))?; vm.insert_value(root_addr + 1_i32, Felt::zero()) - .map_err(HintError::Internal) + .map_err(HintError::Memory) } /* @@ -234,10 +234,8 @@ mod tests { }, utils::test_utils::*, vm::{ - errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError}, - runners::builtin_runner::RangeCheckBuiltinRunner, - vm_core::VirtualMachine, - vm_memory::memory::Memory, + errors::memory_errors::MemoryError, runners::builtin_runner::RangeCheckBuiltinRunner, + vm_core::VirtualMachine, vm_memory::memory::Memory, }, }; use assert_matches::assert_matches; @@ -285,13 +283,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 12)) && + )) if x == MaybeRelocatable::from((1, 12)) && y == MaybeRelocatable::from(Felt::new(2)) && z == MaybeRelocatable::from(Felt::zero()) ); @@ -356,13 +354,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z ) - ))) if x == MaybeRelocatable::from((1, 10)) && + )) if x == MaybeRelocatable::from((1, 10)) && y == MaybeRelocatable::from(Felt::zero()) && z == MaybeRelocatable::from(felt_str!("7249717543555297151")) ); @@ -422,13 +420,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z, ) - ))) if x == MaybeRelocatable::from((1, 5)) && + )) if x == MaybeRelocatable::from((1, 5)) && y == MaybeRelocatable::from(Felt::one()) && z == MaybeRelocatable::from(felt_str!("48805497317890012913")) ); @@ -492,13 +490,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z, ) - ))) if x == MaybeRelocatable::from((1, 5)) && + )) if x == MaybeRelocatable::from((1, 5)) && y == MaybeRelocatable::from(Felt::new(55)) && z == MaybeRelocatable::from(Felt::one()) ); @@ -548,13 +546,13 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::Internal(VirtualMachineError::MemoryError( + Err(HintError::Memory( MemoryError::InconsistentMemory( x, y, z, ) - ))) if x == MaybeRelocatable::from((1, 10)) && + )) if x == MaybeRelocatable::from((1, 10)) && y == MaybeRelocatable::from(Felt::zero()) && z == MaybeRelocatable::from(Felt::new(10)) ); diff --git a/src/hint_processor/hint_processor_utils.rs b/src/hint_processor/hint_processor_utils.rs index 6312ba54c5..04a13ded80 100644 --- a/src/hint_processor/hint_processor_utils.rs +++ b/src/hint_processor/hint_processor_utils.rs @@ -24,8 +24,7 @@ pub fn insert_value_from_reference( ) -> Result<(), HintError> { let var_addr = compute_addr_from_reference(hint_reference, vm, ap_tracking) .ok_or(HintError::UnknownIdentifierInternal)?; - vm.insert_value(var_addr, value) - .map_err(HintError::Internal) + vm.insert_value(var_addr, value).map_err(HintError::Memory) } ///Returns the Integer value stored in the given ids variable diff --git a/src/vm/context/run_context.rs b/src/vm/context/run_context.rs index ed4f503ed2..804184d944 100644 --- a/src/vm/context/run_context.rs +++ b/src/vm/context/run_context.rs @@ -74,7 +74,7 @@ impl RunContext { }, Op1Addr::Op0 => match op0 { Some(MaybeRelocatable::RelocatableValue(addr)) => *addr, - Some(_) => return Err(VirtualMachineError::MemoryError(AddressNotRelocatable)), + Some(_) => return Err(VirtualMachineError::Memory(AddressNotRelocatable)), None => return Err(VirtualMachineError::UnknownOp0), }, }; @@ -397,7 +397,7 @@ mod tests { let op0 = MaybeRelocatable::from(Felt::new(7)); assert_matches!( run_context.compute_op1_addr(&instruction, Some(&op0)), - Err::(VirtualMachineError::MemoryError( + Err::(VirtualMachineError::Memory( MemoryError::AddressNotRelocatable )) ); diff --git a/src/vm/errors/hint_errors.rs b/src/vm/errors/hint_errors.rs index dea82f97e8..160d2535ce 100644 --- a/src/vm/errors/hint_errors.rs +++ b/src/vm/errors/hint_errors.rs @@ -4,7 +4,9 @@ use thiserror::Error; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; -use super::{exec_scope_errors::ExecScopeError, vm_errors::VirtualMachineError}; +use super::{ + exec_scope_errors::ExecScopeError, memory_errors::MemoryError, vm_errors::VirtualMachineError, +}; #[derive(Debug, Error)] pub enum HintError { @@ -88,6 +90,8 @@ pub enum HintError { NAccessesTooBig(Felt), #[error(transparent)] Internal(#[from] VirtualMachineError), + #[error(transparent)] + Memory(#[from] MemoryError), #[error("Couldn't convert BigInt to usize")] BigintToUsizeFail, #[error("usort() can only be used with input_len<={0}. Got: input_len={1}.")] diff --git a/src/vm/errors/memory_errors.rs b/src/vm/errors/memory_errors.rs index 19d8b27a68..13cc358c41 100644 --- a/src/vm/errors/memory_errors.rs +++ b/src/vm/errors/memory_errors.rs @@ -79,6 +79,15 @@ pub enum MemoryError { InsufficientAllocatedCells(#[from] InsufficientAllocatedCellsError), #[error("Accessed address {0} has higher offset than the maximal offset {1} encountered in the memory segment.")] AccessedAddressOffsetBiggerThanSegmentSize(Relocatable, usize), + #[error("gen_arg: found argument of invalid type.")] + GenArgInvalidType, + // Memory.get() errors + #[error("Expected integer at address {0}")] + ExpectedInteger(Relocatable), + #[error("Expected relocatable at address {0}")] + ExpectedRelocatable(Relocatable), + #[error("Unknown memory cell at address {0}")] + UnknownMemoryCell(Relocatable), } #[derive(Debug, PartialEq, Eq, Error)] diff --git a/src/vm/errors/runner_errors.rs b/src/vm/errors/runner_errors.rs index 00ba382a70..452028da79 100644 --- a/src/vm/errors/runner_errors.rs +++ b/src/vm/errors/runner_errors.rs @@ -15,8 +15,6 @@ pub enum RunnerError { NoProgBase, #[error("Missing main()")] MissingMain, - #[error("Uninitialized base for builtin")] - UninitializedBase, #[error("Base for builtin is not finished")] BaseNotFinished, #[error("Failed to write program output")] @@ -31,18 +29,8 @@ pub enum RunnerError { MemoryValidationError(MemoryError), #[error("Memory loading failed during state initialization: {0}")] MemoryInitializationError(MemoryError), - #[error("Memory addresses must be relocatable")] - NonRelocatableAddress, - #[error("Runner base mustn't be in a TemporarySegment, segment: {0}")] - RunnerInTemporarySegment(isize), #[error("Failed to convert string to FieldElement")] FailedStringConversion, - #[error("Expected integer at address {0:?}")] - ExpectedInteger(MaybeRelocatable), - #[error("Failed to retrieve value from address {0:?}")] - MemoryGet(MaybeRelocatable), - #[error(transparent)] - FailedMemoryGet(MemoryError), #[error("EcOpBuiltin: m should be at most {0}")] EcOpBuiltinScalarLimit(Felt), #[error("Given builtins are not in appropiate order")] @@ -98,9 +86,7 @@ pub enum RunnerError { #[error("{0} is not divisible by {1}")] SafeDivFailUsize(usize, usize), #[error(transparent)] - MemoryError(#[from] MemoryError), - #[error("Negative builtin base")] - NegBuiltinBase, + Memory(#[from] MemoryError), #[error("keccak_builtin: Failed to get first input address")] KeccakNoFirstInput, #[error("keccak_builtin: Failed to convert input cells to u64 values")] diff --git a/src/vm/errors/vm_errors.rs b/src/vm/errors/vm_errors.rs index 470591c15c..9467acbb73 100644 --- a/src/vm/errors/vm_errors.rs +++ b/src/vm/errors/vm_errors.rs @@ -67,17 +67,11 @@ pub enum VirtualMachineError { #[error("Invalid hint encoding at pc: {0}")] InvalidHintEncoding(MaybeRelocatable), #[error(transparent)] - MemoryError(#[from] MemoryError), + Memory(#[from] MemoryError), #[error("Expected range_check builtin to be present")] NoRangeCheckBuiltin, #[error("Expected ecdsa builtin to be present")] NoSignatureBuiltin, - #[error("Failed to retrieve value from address {0}")] - MemoryGet(MaybeRelocatable), - #[error("Expected integer at address {0}")] - ExpectedInteger(Relocatable), - #[error("Expected relocatable at address {0}")] - ExpectedRelocatable(Relocatable), #[error("Value: {0} should be positive")] ValueNotPositive(Felt), #[error("Div out of range: 0 < {0} <= {1}")] @@ -144,8 +138,6 @@ pub enum VirtualMachineError { OutOfBoundsBuiltinSegmentAccess, #[error("Out of bounds access to program segment")] OutOfBoundsProgramSegmentAccess, - #[error("Negative builtin base")] - NegBuiltinBase, #[error("Security Error: Invalid Memory Value: temporary address not relocated: {0}")] InvalidMemoryValueTemporaryAddress(Relocatable), #[error("accessed_addresses is None.")] diff --git a/src/vm/runners/builtin_runner/ec_op.rs b/src/vm/runners/builtin_runner/ec_op.rs index 618e7c3eff..10de79728b 100644 --- a/src/vm/runners/builtin_runner/ec_op.rs +++ b/src/vm/runners/builtin_runner/ec_op.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use crate::math_utils::{ec_add, ec_double, safe_div_usize}; use crate::types::instance_definitions::ec_op_instance_def::{ EcOpInstanceDef, CELLS_PER_EC_OP, INPUT_CELLS_PER_EC_OP, @@ -12,7 +14,6 @@ use felt::Felt; use num_bigint::{BigInt, ToBigInt}; use num_integer::{div_ceil, Integer}; use num_traits::{Num, One, Pow, Zero}; -use std::borrow::Cow; use super::EC_OP_BUILTIN_NAME; @@ -152,18 +153,22 @@ impl EcOpBuiltinRunner { if index != OUTPUT_INDICES.0 && index != OUTPUT_INDICES.1 { return Ok(None); } - let instance = MaybeRelocatable::from((address.segment_index, address.offset - index)); + let instance = Relocatable::from((address.segment_index, address.offset - index)); //All input cells should be filled, and be integer values //If an input cell is not filled, return None - let mut input_cells = Vec::>::with_capacity(self.n_input_cells as usize); + let mut input_cells = Vec::<&Felt>::with_capacity(self.n_input_cells as usize); for i in 0..self.n_input_cells as usize { - match memory.get(&instance.add_usize(i)) { + match memory.get(&(instance + i)) { None => return Ok(None), Some(addr) => { input_cells.push(match addr { - Cow::Borrowed(MaybeRelocatable::Int(num)) => Cow::Borrowed(num), - Cow::Owned(MaybeRelocatable::Int(num)) => Cow::Owned(num), - _ => return Err(RunnerError::ExpectedInteger(instance.add_usize(i))), + // Only relocatable values can be owned + Cow::Borrowed(MaybeRelocatable::Int(ref num)) => num, + _ => { + return Err(RunnerError::Memory(MemoryError::ExpectedInteger( + instance + i, + ))) + } }); } }; @@ -178,29 +183,23 @@ impl EcOpBuiltinRunner { // Assert that if the current address is part of a point, the point is on the curve for pair in &EC_POINT_INDICES[0..2] { if !EcOpBuiltinRunner::point_on_curve( - input_cells[pair.0].as_ref(), - input_cells[pair.1].as_ref(), + input_cells[pair.0], + input_cells[pair.1], &alpha, &beta, ) { return Err(RunnerError::PointNotOnCurve(( - input_cells[pair.0].clone().into_owned(), - input_cells[pair.1].clone().into_owned(), + input_cells[pair.0].clone(), + input_cells[pair.1].clone(), ))); }; } let prime = BigInt::from_str_radix(&felt::PRIME_STR[2..], 16) .map_err(|_| RunnerError::CouldntParsePrime)?; let result = EcOpBuiltinRunner::ec_op_impl( - ( - input_cells[0].as_ref().to_owned(), - input_cells[1].as_ref().to_owned(), - ), - ( - input_cells[2].as_ref().to_owned(), - input_cells[3].as_ref().to_owned(), - ), - input_cells[4].as_ref(), + (input_cells[0].to_owned(), input_cells[1].to_owned()), + (input_cells[2].to_owned(), input_cells[3].to_owned()), + input_cells[4], #[allow(deprecated)] &alpha.to_bigint(), &prime, @@ -896,7 +895,9 @@ mod tests { assert_eq!( builtin.deduce_memory_cell(Relocatable::from((3, 6)), &memory), - Err(RunnerError::ExpectedInteger(MaybeRelocatable::from((3, 3)))) + Err(RunnerError::Memory(MemoryError::ExpectedInteger( + Relocatable::from((3, 3)) + ))) ); } diff --git a/src/vm/runners/builtin_runner/keccak.rs b/src/vm/runners/builtin_runner/keccak.rs index 4d5b909437..65e507c100 100644 --- a/src/vm/runners/builtin_runner/keccak.rs +++ b/src/vm/runners/builtin_runner/keccak.rs @@ -99,10 +99,7 @@ impl KeccakBuiltinRunner { } if let Some((i, bits)) = self.state_rep.iter().enumerate().next() { - let val = memory - .get_integer(first_input_addr + i) - .map_err(|_| RunnerError::ExpectedInteger((first_input_addr + i).into()))?; - + let val = memory.get_integer(first_input_addr + i)?; if val.as_ref() >= &(Felt::one() << *bits) { return Err(RunnerError::IntegerBiggerThanPowerOfTwo( (first_input_addr + i).into(), @@ -596,7 +593,7 @@ mod tests { #[test] fn deduce_memory_cell_expected_integer() { - let memory = memory![((0, 35), 0)]; + let memory = memory![((0, 0), (1, 2))]; let mut builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); @@ -605,7 +602,12 @@ mod tests { let result = builtin.deduce_memory_cell(Relocatable::from((0, 99)), &memory); - assert_eq!(result, Err(RunnerError::ExpectedInteger((0, 0).into()))); + assert_eq!( + result, + Err(RunnerError::Memory(MemoryError::ExpectedInteger( + (0, 0).into() + ))) + ); } #[test] diff --git a/src/vm/runners/builtin_runner/mod.rs b/src/vm/runners/builtin_runner/mod.rs index 232164563e..b69dffdbf3 100644 --- a/src/vm/runners/builtin_runner/mod.rs +++ b/src/vm/runners/builtin_runner/mod.rs @@ -19,7 +19,6 @@ pub use bitwise::BitwiseBuiltinRunner; pub use ec_op::EcOpBuiltinRunner; pub use hash::HashBuiltinRunner; use num_integer::div_floor; -use num_traits::ToPrimitive; pub use output::OutputBuiltinRunner; pub use range_check::RangeCheckBuiltinRunner; pub use signature::SignatureBuiltinRunner; @@ -305,10 +304,7 @@ impl BuiltinRunner { } let cells_per_instance = self.cells_per_instance() as usize; let n_input_cells = self.n_input_cells() as usize; - let builtin_segment_index = self - .base() - .to_usize() - .ok_or(VirtualMachineError::NegBuiltinBase)?; + let builtin_segment_index = self.base(); // If the builtin's segment is empty, there are no security checks to run let builtin_segment = match vm.segments.memory.data.get(builtin_segment_index) { Some(segment) if !segment.is_empty() => segment, @@ -1026,7 +1022,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCellsWithOffsets(BITWISE_BUILTIN_NAME, x) )) if x == vec![0] ); @@ -1054,7 +1050,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCells(BITWISE_BUILTIN_NAME) )) ); @@ -1075,7 +1071,7 @@ mod tests { ]]; assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCellsWithOffsets(HASH_BUILTIN_NAME, x) )) if x == vec![0] ); @@ -1093,7 +1089,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCells(HASH_BUILTIN_NAME) )) ); @@ -1118,7 +1114,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCells(RANGE_CHECK_BUILTIN_NAME) )) ); @@ -1134,7 +1130,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCells(RANGE_CHECK_BUILTIN_NAME) )) ); @@ -1200,7 +1196,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCells(EC_OP_BUILTIN_NAME) )) ); @@ -1222,7 +1218,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCells(EC_OP_BUILTIN_NAME) )) ); @@ -1246,7 +1242,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCellsWithOffsets(EC_OP_BUILTIN_NAME, x) )) if x == vec![0] ); @@ -1277,7 +1273,7 @@ mod tests { assert_matches!( builtin.run_security_checks(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::MissingMemoryCellsWithOffsets(EC_OP_BUILTIN_NAME, x) )) if x == vec![7] ); diff --git a/src/vm/runners/builtin_runner/output.rs b/src/vm/runners/builtin_runner/output.rs index d77a95db4c..820942c4d5 100644 --- a/src/vm/runners/builtin_runner/output.rs +++ b/src/vm/runners/builtin_runner/output.rs @@ -99,9 +99,7 @@ impl OutputBuiltinRunner { )); } let stop_ptr = stop_pointer.offset; - let used = self - .get_used_cells(segments) - .map_err(RunnerError::MemoryError)?; + let used = self.get_used_cells(segments).map_err(RunnerError::Memory)?; if stop_ptr != used { return Err(RunnerError::InvalidStopPointer( OUTPUT_BUILTIN_NAME, diff --git a/src/vm/runners/builtin_runner/signature.rs b/src/vm/runners/builtin_runner/signature.rs index 839f9b0c1a..8019ef63a4 100644 --- a/src/vm/runners/builtin_runner/signature.rs +++ b/src/vm/runners/builtin_runner/signature.rs @@ -595,9 +595,7 @@ mod tests { vm.segments = segments![((0, 0), (0, 0))]; assert_eq!( builtin.final_stack(&vm.segments, (0, 1).into()), - Err(RunnerError::MemoryError( - MemoryError::MissingSegmentUsedSizes - )) + Err(RunnerError::Memory(MemoryError::MissingSegmentUsedSizes)) ) } } diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index 5e300d7153..fc3268ee2a 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -736,9 +736,8 @@ impl CairoRunner { match self.check_used_cells(vm) { Ok(_) => break, Err(e) => match e { - VirtualMachineError::MemoryError( - MemoryError::InsufficientAllocatedCells(_), - ) => {} + VirtualMachineError::Memory(MemoryError::InsufficientAllocatedCells(_)) => { + } e => return Err(e), }, } @@ -1279,7 +1278,7 @@ mod tests { vm.segments.segment_used_sizes = Some(vec![4, 12]); assert_matches!( cairo_runner.check_memory_usage(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) ); @@ -3351,7 +3350,7 @@ mod tests { vm.builtin_runners = vec![]; assert_matches!( cairo_runner.check_diluted_check_usage(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) ); @@ -3807,7 +3806,7 @@ mod tests { assert_matches!( cairo_runner.check_range_check_usage(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) ); @@ -3871,7 +3870,7 @@ mod tests { assert_matches!( cairo_runner.check_used_cells(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) ); @@ -3896,7 +3895,7 @@ mod tests { assert_matches!( cairo_runner.check_used_cells(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) ); @@ -3912,7 +3911,7 @@ mod tests { assert_matches!( cairo_runner.check_used_cells(&vm), - Err(VirtualMachineError::MemoryError( + Err(VirtualMachineError::Memory( MemoryError::InsufficientAllocatedCells(_) )) ); diff --git a/src/vm/trace/mod.rs b/src/vm/trace/mod.rs index 3b288359d8..6f699c403a 100644 --- a/src/vm/trace/mod.rs +++ b/src/vm/trace/mod.rs @@ -1,6 +1,7 @@ use self::trace_entry::TraceEntry; use super::{ - decoding::decoder::decode_instruction, errors::vm_errors::VirtualMachineError, + decoding::decoder::decode_instruction, + errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError}, vm_memory::memory::Memory, }; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; @@ -28,7 +29,7 @@ pub fn get_perm_range_check_limits( .map(|x| match x { Cow::Borrowed(MaybeRelocatable::Int(value)) => Ok(value.clone()), Cow::Owned(MaybeRelocatable::Int(value)) => Ok(value), - _ => Err(VirtualMachineError::ExpectedInteger( + _ => Err(MemoryError::ExpectedInteger( (trace.pc.segment_index, trace.pc.offset + 1).into(), )), }) diff --git a/src/vm/vm_core.rs b/src/vm/vm_core.rs index 522a11c5f9..db58c74ee1 100644 --- a/src/vm/vm_core.rs +++ b/src/vm/vm_core.rs @@ -395,19 +395,19 @@ impl VirtualMachine { self.segments .memory .insert(&operands_addresses.op0_addr, &operands.op0) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; } if deduced_operands.was_op1_deducted() { self.segments .memory .insert(&operands_addresses.op1_addr, &operands.op1) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; } if deduced_operands.was_dest_deducted() { self.segments .memory .insert(&operands_addresses.dst_addr, &operands.dst) - .map_err(VirtualMachineError::MemoryError)?; + .map_err(VirtualMachineError::Memory)?; } Ok(()) @@ -787,12 +787,12 @@ impl VirtualMachine { } ///Gets the integer value corresponding to the Relocatable address - pub fn get_integer(&self, key: Relocatable) -> Result, VirtualMachineError> { + pub fn get_integer(&self, key: Relocatable) -> Result, MemoryError> { self.segments.memory.get_integer(key) } ///Gets the relocatable value corresponding to the Relocatable address - pub fn get_relocatable(&self, key: Relocatable) -> Result { + pub fn get_relocatable(&self, key: Relocatable) -> Result { self.segments.memory.get_relocatable(key) } @@ -818,7 +818,7 @@ impl VirtualMachine { &mut self, key: Relocatable, val: T, - ) -> Result<(), VirtualMachineError> { + ) -> Result<(), MemoryError> { self.segments.memory.insert_value(key, val) } @@ -876,7 +876,7 @@ impl VirtualMachine { &self, addr: Relocatable, size: usize, - ) -> Result>, VirtualMachineError> { + ) -> Result>, MemoryError> { self.segments.memory.get_integer_range(addr, size) } @@ -950,7 +950,7 @@ impl VirtualMachine { self.segments.memory.add_relocation_rule(src_ptr, dst_ptr) } - pub fn gen_arg(&mut self, arg: &dyn Any) -> Result { + pub fn gen_arg(&mut self, arg: &dyn Any) -> Result { self.segments.gen_arg(arg) } diff --git a/src/vm/vm_memory/memory.rs b/src/vm/vm_memory/memory.rs index 497c3a3392..2b47909f45 100644 --- a/src/vm/vm_memory/memory.rs +++ b/src/vm/vm_memory/memory.rs @@ -1,7 +1,7 @@ use crate::{ types::relocatable::{MaybeRelocatable, Relocatable}, utils::from_relocatable_to_indexes, - vm::errors::{memory_errors::MemoryError, vm_errors::VirtualMachineError}, + vm::errors::memory_errors::MemoryError, }; use felt::Felt; use num_traits::ToPrimitive; @@ -188,25 +188,19 @@ impl Memory { //Gets the value from memory address. //If the value is an MaybeRelocatable::Int(Bigint) return &Bigint //else raises Err - pub fn get_integer(&self, key: Relocatable) -> Result, VirtualMachineError> { - match self - .get(&key) - .ok_or_else(|| VirtualMachineError::UnknownMemoryCell(key))? - { + pub fn get_integer(&self, key: Relocatable) -> Result, MemoryError> { + match self.get(&key).ok_or(MemoryError::UnknownMemoryCell(key))? { Cow::Borrowed(MaybeRelocatable::Int(int)) => Ok(Cow::Borrowed(int)), Cow::Owned(MaybeRelocatable::Int(int)) => Ok(Cow::Owned(int)), - _ => Err(VirtualMachineError::ExpectedInteger(key)), + _ => Err(MemoryError::ExpectedInteger(key)), } } - pub fn get_relocatable(&self, key: Relocatable) -> Result { - match self - .get(&key) - .ok_or_else(|| VirtualMachineError::UnknownMemoryCell(key))? - { + pub fn get_relocatable(&self, key: Relocatable) -> Result { + match self.get(&key).ok_or(MemoryError::UnknownMemoryCell(key))? { Cow::Borrowed(MaybeRelocatable::RelocatableValue(rel)) => Ok(*rel), Cow::Owned(MaybeRelocatable::RelocatableValue(rel)) => Ok(rel), - _ => Err(VirtualMachineError::ExpectedRelocatable(key)), + _ => Err(MemoryError::ExpectedRelocatable(key)), } } @@ -214,9 +208,8 @@ impl Memory { &mut self, key: Relocatable, val: T, - ) -> Result<(), VirtualMachineError> { + ) -> Result<(), MemoryError> { self.insert(&key, &val.into()) - .map_err(VirtualMachineError::MemoryError) } pub fn add_validation_rule(&mut self, segment_index: usize, rule: ValidationRule) { @@ -285,7 +278,7 @@ impl Memory { &self, addr: Relocatable, size: usize, - ) -> Result>, VirtualMachineError> { + ) -> Result>, MemoryError> { let mut values = Vec::new(); for i in 0..size { @@ -730,7 +723,7 @@ mod memory_tests { .unwrap(); assert_matches!( segments.memory.get_integer(Relocatable::from((0, 0))), - Err(VirtualMachineError::ExpectedInteger( + Err(MemoryError::ExpectedInteger( e )) if e == Relocatable::from((0, 0)) ); diff --git a/src/vm/vm_memory/memory_segments.rs b/src/vm/vm_memory/memory_segments.rs index 3df938d021..72f9d0e8a3 100644 --- a/src/vm/vm_memory/memory_segments.rs +++ b/src/vm/vm_memory/memory_segments.rs @@ -114,7 +114,7 @@ impl MemorySegmentManager { Ok(relocation_table) } - pub fn gen_arg(&mut self, arg: &dyn Any) -> Result { + pub fn gen_arg(&mut self, arg: &dyn Any) -> Result { if let Some(value) = arg.downcast_ref::() { Ok(value.clone()) } else if let Some(value) = arg.downcast_ref::>() { @@ -126,7 +126,7 @@ impl MemorySegmentManager { self.write_arg(base, value)?; Ok(base.into()) } else { - Err(VirtualMachineError::NotImplemented) + Err(MemoryError::GenArgInvalidType) } } @@ -792,12 +792,12 @@ mod tests { /// Test that the call to .gen_arg() with any other argument returns a not /// implemented error. #[test] - fn gen_arg_not_implemented() { + fn gen_arg_invalid_type() { let mut memory_segment_manager = MemorySegmentManager::new(); assert_matches!( memory_segment_manager.gen_arg(&""), - Err(VirtualMachineError::NotImplemented) + Err(MemoryError::GenArgInvalidType) ); }