diff --git a/CHANGELOG.md b/CHANGELOG.md index ac26b1176d..aec7c7db9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,8 @@ * Add `CairoPie` methods `run_validity_checks` & `check_pie_compatibility` * Add `Program` method `from_stripped_program` +* bugfix: Don't assume outer deref when fetching integer values from references[#1732](https://github.com/lambdaclass/cairo-vm/pull/1732) + * feat: Implement `extend_additional_data` for `BuiltinRunner`[#1726](https://github.com/lambdaclass/cairo-vm/pull/1726) * BREAKING: Set dynamic params as null by default on air public input [#1716](https://github.com/lambdaclass/cairo-vm/pull/1716) diff --git a/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs b/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs index e56258018e..3251cff0e8 100644 --- a/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs @@ -367,7 +367,7 @@ mod tests { assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::IdentifierNotRelocatable(bx)) - if *bx == ("output".to_string(), (1,0).into()) + if bx.as_ref() == "output" ); } diff --git a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 3120b91d03..19b6a77099 100644 --- a/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -1003,7 +1003,7 @@ mod tests { assert_matches!( run_hint!(vm, ids_data, hint_code), Err(HintError::IdentifierNotInteger(bx)) - if *bx == ("len".to_string(), (1,1).into()) + if bx.as_ref() == "len" ); } diff --git a/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs b/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs index b1be97bb82..1b1ce66a5c 100644 --- a/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs +++ b/vm/src/hint_processor/builtin_hint_processor/find_element_hint.rs @@ -270,7 +270,7 @@ mod tests { let ids_data = ids_data!["array_ptr", "elm_size", "n_elms", "index", "key"]; assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("key".to_string(), (1,4).into()) + Err(HintError::UnknownIdentifier(bx)) if bx.as_ref() == "key" ); } @@ -283,7 +283,7 @@ mod tests { )])); assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("elm_size".to_string(), (1,1).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "elm_size" ); } @@ -321,7 +321,7 @@ mod tests { init_vm_ids_data(HashMap::from([("n_elms".to_string(), relocatable)])); assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("n_elms".to_string(), (1,2).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "n_elms" ); } @@ -364,7 +364,7 @@ mod tests { init_vm_ids_data(HashMap::from([("key".to_string(), relocatable)])); assert_matches!( run_hint!(vm, ids_data, hint_code::FIND_ELEMENT), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("key".to_string(), (1,4).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "key" ); } @@ -403,7 +403,7 @@ mod tests { )])); assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("elm_size".to_string(), (1,1).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "elm_size" ); } @@ -429,7 +429,7 @@ mod tests { )])); assert_matches!( run_hint!(vm, ids_data, hint_code::SEARCH_SORTED_LOWER), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("n_elms".to_string(), (1,2).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "n_elms" ); } diff --git a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs index 07eef72a88..6e0a0ee12f 100644 --- a/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/hint_utils.rs @@ -48,12 +48,10 @@ pub fn get_ptr_from_var_name( match get_ptr_from_reference(vm, reference, ap_tracking) { // Map internal errors into more descriptive variants Ok(val) => Ok(val), - Err(HintError::WrongIdentifierTypeInternal(var_addr)) => Err( - HintError::IdentifierNotRelocatable(Box::new((var_name.to_string(), *var_addr))), - ), - _ => Err(HintError::UnknownIdentifier( - var_name.to_string().into_boxed_str(), + Err(HintError::WrongIdentifierTypeInternal) => Err(HintError::IdentifierNotRelocatable( + Box::::from(var_name), )), + _ => Err(HintError::UnknownIdentifier(Box::::from(var_name))), } } @@ -77,7 +75,7 @@ pub fn get_relocatable_from_var_name( ids_data .get(var_name) .and_then(|x| compute_addr_from_reference(x, vm, ap_tracking)) - .ok_or_else(|| HintError::UnknownIdentifier(var_name.to_string().into_boxed_str())) + .ok_or_else(|| HintError::UnknownIdentifier(Box::::from(var_name))) } //Gets the value of a variable name. @@ -93,12 +91,10 @@ pub fn get_integer_from_var_name( match get_integer_from_reference(vm, reference, ap_tracking) { // Map internal errors into more descriptive variants Ok(val) => Ok(val), - Err(HintError::WrongIdentifierTypeInternal(var_addr)) => Err( - HintError::IdentifierNotInteger(Box::new((var_name.to_string(), *var_addr))), - ), - _ => Err(HintError::UnknownIdentifier( - var_name.to_string().into_boxed_str(), - )), + Err(HintError::WrongIdentifierTypeInternal) => { + Err(HintError::IdentifierNotInteger(Box::::from(var_name))) + } + _ => Err(HintError::UnknownIdentifier(Box::::from(var_name))), } } @@ -111,7 +107,7 @@ pub fn get_maybe_relocatable_from_var_name<'a>( ) -> Result { let reference = get_reference_from_var_name(var_name, ids_data)?; get_maybe_relocatable_from_reference(vm, reference, ap_tracking) - .ok_or_else(|| HintError::UnknownIdentifier(var_name.to_string().into_boxed_str())) + .ok_or_else(|| HintError::UnknownIdentifier(Box::::from(var_name))) } pub fn get_reference_from_var_name<'a>( @@ -120,7 +116,7 @@ pub fn get_reference_from_var_name<'a>( ) -> Result<&'a HintReference, HintError> { ids_data .get(var_name) - .ok_or_else(|| HintError::UnknownIdentifier(var_name.to_string().into_boxed_str())) + .ok_or_else(|| HintError::UnknownIdentifier(Box::::from(var_name))) } pub fn get_constant_from_var_name<'a>( @@ -137,7 +133,6 @@ pub fn get_constant_from_var_name<'a>( #[cfg(test)] mod tests { use super::*; - use crate::stdlib::string::ToString; use crate::{ hint_processor::hint_processor_definition::HintReference, @@ -218,7 +213,7 @@ mod tests { assert_matches!( get_ptr_from_var_name("value", &vm, &ids_data, &ApTracking::new()), - Err(HintError::IdentifierNotRelocatable(bx)) if *bx == ("value".to_string(), (1,0).into()) + Err(HintError::IdentifierNotRelocatable(bx)) if bx.as_ref() == "value" ); } @@ -274,7 +269,7 @@ mod tests { assert_matches!( get_integer_from_var_name("value", &vm, &ids_data, &ApTracking::new()), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("value".to_string(), (1,0).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "value" ); } } diff --git a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs index a0fa71dddb..ad2c5e0767 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -894,7 +894,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("a".to_string(), (1,4).into()) + Err(HintError::UnknownIdentifier(bx)) if bx.as_ref() == "a" ); } @@ -912,7 +912,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("a".to_string(), (1,4).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "a" ); } @@ -1061,14 +1061,14 @@ mod tests { let hint_code = "from starkware.cairo.common.math_utils import assert_integer\nassert_integer(ids.a)\nassert 0 <= ids.a % PRIME < range_check_builtin.bound, f'a = {ids.a} is out of range.'"; let mut vm = vm_with_range_check!(); //Initialize fp - vm.run_context.fp = 4; + vm.run_context.fp = 1; //Insert ids into memory vm.segments = segments![((1, 0), (10, 10))]; let ids_data = ids_data!["a"]; //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("a".to_string(), (1,3).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "a" ); } @@ -1103,7 +1103,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("a".to_string(), (1,3).into()) + Err(HintError::UnknownIdentifier(bx)) if bx.as_ref() == "a" ); } @@ -1156,7 +1156,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code::ASSERT_LE_FELT, &mut exec_scopes, &constants), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("a".to_string(), (1,0).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "a" ); } @@ -1182,7 +1182,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code::ASSERT_LE_FELT, &mut exec_scopes, &constants), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("b".to_string(), (1,1).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "b" ); } @@ -1412,7 +1412,7 @@ mod tests { let ids_data = ids_data!["value"]; assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("value".to_string(), (1,4).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "value" ); } @@ -2234,7 +2234,7 @@ mod tests { ) ]) ), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("value".to_string(), (1,3).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "value" ); } @@ -2404,7 +2404,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("a".to_string(), (1,1).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "a" ); } @@ -2421,7 +2421,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("b".to_string(), (1,2).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "b" ); } @@ -2439,7 +2439,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("b".to_string(), (1,2).into()) + Err(HintError::UnknownIdentifier(bx)) if bx.as_ref() == "b" ); } diff --git a/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs index 2e9b4c55e4..ea0d6e799c 100644 --- a/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/memcpy_hint_utils.rs @@ -96,7 +96,7 @@ mod tests { assert_matches!( get_integer_from_var_name(var_name, &vm, &ids_data, &ApTracking::default()), - Err(HintError::IdentifierNotInteger(bx)) if *bx == (var_name.to_string(), (1,0).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == var_name ); } } diff --git a/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs b/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs index 066884f920..2c5c7e48ca 100644 --- a/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/memset_utils.rs @@ -55,7 +55,6 @@ pub fn memset_step_loop( #[cfg(test)] mod tests { use super::*; - use crate::stdlib::string::ToString; use crate::types::relocatable::Relocatable; use crate::{ any_box, @@ -100,7 +99,7 @@ mod tests { let ids_data = ids_data!["n"]; assert_matches!( run_hint!(vm, ids_data, hint_code), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("n".to_string(), (1,1).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "n" ); } diff --git a/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs b/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs index 23dc562dae..28304f0a16 100644 --- a/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/squash_dict_utils.rs @@ -700,7 +700,7 @@ mod tests { //Execute the hint assert_matches!( run_hint!(vm, ids_data, hint_code, &mut exec_scopes), - Err(HintError::IdentifierNotInteger(bx)) if *bx == ("n_used_accesses".to_string(), (1,0).into()) + Err(HintError::IdentifierNotInteger(bx)) if bx.as_ref() == "n_used_accesses" ); } diff --git a/vm/src/hint_processor/hint_processor_utils.rs b/vm/src/hint_processor/hint_processor_utils.rs index f952a9d4e7..af83fcd57a 100644 --- a/vm/src/hint_processor/hint_processor_utils.rs +++ b/vm/src/hint_processor/hint_processor_utils.rs @@ -15,16 +15,16 @@ use crate::Felt252; use num_traits::ToPrimitive; -///Inserts value into the address of the given ids variable +/// Inserts value into the address of the given ids variable pub fn insert_value_from_reference( value: impl Into, vm: &mut VirtualMachine, hint_reference: &HintReference, ap_tracking: &ApTracking, ) -> Result<(), HintError> { - let var_addr = compute_addr_from_reference(hint_reference, vm, ap_tracking) + let addr = compute_addr_from_reference(hint_reference, vm, ap_tracking) .ok_or(HintError::UnknownIdentifierInternal)?; - vm.insert_value(var_addr, value).map_err(HintError::Memory) + vm.insert_value(addr, value).map_err(HintError::Memory) } ///Returns the Integer value stored in the given ids variable @@ -34,21 +34,10 @@ pub fn get_integer_from_reference( hint_reference: &HintReference, ap_tracking: &ApTracking, ) -> Result { - // Compute the initial value - let mut val = if let OffsetValue::Immediate(f) = &hint_reference.offset1 { - *f - } else { - let var_addr = compute_addr_from_reference(hint_reference, vm, ap_tracking) - .ok_or(HintError::UnknownIdentifierInternal)?; - vm.get_integer(var_addr) - .map_err(|_| HintError::WrongIdentifierTypeInternal(Box::new(var_addr)))? - .into_owned() - }; - // If offset2 is an immediate, we need to add it's value to the initial value - if let OffsetValue::Immediate(f) = &hint_reference.offset2 { - val += f; - } - Ok(val) + get_maybe_relocatable_from_reference(vm, hint_reference, ap_tracking) + .ok_or(HintError::UnknownIdentifierInternal)? + .get_int() + .ok_or(HintError::WrongIdentifierTypeInternal) } ///Returns the Relocatable value stored in the given ids variable @@ -57,14 +46,10 @@ pub fn get_ptr_from_reference( hint_reference: &HintReference, ap_tracking: &ApTracking, ) -> Result { - let var_addr = compute_addr_from_reference(hint_reference, vm, ap_tracking) - .ok_or(HintError::UnknownIdentifierInternal)?; - if hint_reference.outer_dereference { - vm.get_relocatable(var_addr) - .map_err(|_| HintError::WrongIdentifierTypeInternal(Box::new(var_addr))) - } else { - Ok(var_addr) - } + get_maybe_relocatable_from_reference(vm, hint_reference, ap_tracking) + .ok_or(HintError::UnknownIdentifierInternal)? + .get_relocatable() + .ok_or(HintError::WrongIdentifierTypeInternal) } ///Returns the value given by a reference as [MaybeRelocatable] @@ -73,60 +58,51 @@ pub fn get_maybe_relocatable_from_reference( hint_reference: &HintReference, ap_tracking: &ApTracking, ) -> Option { - //First handle case on only immediate - if let OffsetValue::Immediate(num) = &hint_reference.offset1 { - return Some(MaybeRelocatable::from(num)); + let offset1 = get_offset_value( + vm, + &hint_reference.offset1, + &hint_reference.ap_tracking_data, + ap_tracking, + )?; + let offset2 = get_offset_value( + vm, + &hint_reference.offset2, + &hint_reference.ap_tracking_data, + ap_tracking, + )?; + let mut val = offset1.add(&offset2).ok()?; + if hint_reference.inner_dereference && hint_reference.outer_dereference { + val = vm.get_maybe(&val)?; } - //Then calculate address - let var_addr = compute_addr_from_reference(hint_reference, vm, ap_tracking)?; - if hint_reference.outer_dereference { - vm.get_maybe(&var_addr) - } else { - Some(MaybeRelocatable::from(var_addr)) + if hint_reference.inner_dereference || hint_reference.outer_dereference { + val = vm.get_maybe(&val)?; } + Some(val) } -///Computes the memory address of the ids variable indicated by the HintReference as a [Relocatable] +/// Computes the memory address of the ids variable indicated by the HintReference as a [Relocatable] pub fn compute_addr_from_reference( - //Reference data of the ids variable hint_reference: &HintReference, vm: &VirtualMachine, - //ApTracking of the Hint itself - hint_ap_tracking: &ApTracking, + ap_tracking: &ApTracking, ) -> Option { - let mut offset1 = - if let OffsetValue::Reference(_register, _offset, _deref) = &hint_reference.offset1 { - get_offset_value_reference( - vm, - hint_reference, - hint_ap_tracking, - &hint_reference.offset1, - )? - .get_relocatable()? - } else { - return None; - }; - - match &hint_reference.offset2 { - OffsetValue::Reference(_register, _offset, _deref) => { - // Cant add two relocatable values - // So OffSet2 must be Bigint - let value = get_offset_value_reference( - vm, - hint_reference, - hint_ap_tracking, - &hint_reference.offset2, - )?; - - offset1 += value.get_int_ref()?.to_usize()? - } - OffsetValue::Value(value) => offset1 = (offset1 + *value).ok()?, - _ => {} - } + let offset1 = get_offset_value( + vm, + &hint_reference.offset1, + &hint_reference.ap_tracking_data, + ap_tracking, + )?; + let offset2 = get_offset_value( + vm, + &hint_reference.offset2, + &hint_reference.ap_tracking_data, + ap_tracking, + )?; + let mut val = offset1.add(&offset2).ok()?; if hint_reference.inner_dereference { - offset1 = vm.get_relocatable(offset1).ok()? - } - Some(offset1) + val = vm.get_maybe(&val)?; + }; + val.get_relocatable() } fn apply_ap_tracking_correction( @@ -154,33 +130,33 @@ pub fn felt_to_u32(felt: &Felt252) -> Result { .ok_or_else(|| MathError::Felt252ToU32Conversion(Box::new(*felt))) } -fn get_offset_value_reference( +fn get_offset_value( vm: &VirtualMachine, - hint_reference: &HintReference, - hint_ap_tracking: &ApTracking, offset_value: &OffsetValue, + reference_ap_tracking: &Option, + hint_ap_tracking: &ApTracking, ) -> Option { - let (register, offset, deref) = match offset_value { - OffsetValue::Reference(register, offset, deref) => (register, offset, deref), - _ => return None, - }; - - let base_addr = if register == &Register::FP { - vm.get_fp() - } else { - let var_ap_trackig = hint_reference.ap_tracking_data.as_ref()?; - - apply_ap_tracking_correction(vm.get_ap(), var_ap_trackig, hint_ap_tracking)? - }; - - if offset.is_negative() && base_addr.offset < offset.unsigned_abs() as usize { - return None; - } - - if *deref { - vm.get_maybe(&(base_addr + *offset).ok()?) - } else { - Some((base_addr + *offset).ok()?.into()) + match offset_value { + OffsetValue::Immediate(f) => Some(f.into()), + OffsetValue::Value(v) => Some(Felt252::from(*v).into()), + OffsetValue::Reference(register, offset, deref) => { + let addr = (if matches!(register, Register::FP) { + vm.get_fp() + } else { + apply_ap_tracking_correction( + vm.get_ap(), + reference_ap_tracking.as_ref()?, + hint_ap_tracking, + )? + } + *offset) + .ok()?; + + if *deref { + vm.get_maybe(&addr) + } else { + Some(addr.into()) + } + } } } @@ -201,9 +177,10 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_integer_from_reference_with_immediate_value() { + // Reference: cast(2, felt) let mut vm = vm!(); vm.segments = segments![((1, 0), 0)]; - let mut hint_ref = HintReference::new(0, 0, false, true); + let mut hint_ref = HintReference::new(0, 0, false, false); hint_ref.offset1 = OffsetValue::Immediate(Felt252::from(2)); assert_eq!( @@ -213,27 +190,6 @@ mod tests { ); } - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_integer_from_reference_with_immediate_off2() { - let mut vm = vm!(); - vm.segments = segments![((1, 0), 1)]; - let hint_ref = HintReference { - offset1: OffsetValue::Reference(Register::FP, 0, false), - offset2: OffsetValue::Immediate(Felt252::TWO), - outer_dereference: false, - inner_dereference: false, - ap_tracking_data: Default::default(), - cairo_type: None, - }; - - assert_eq!( - get_integer_from_reference(&vm, &hint_ref, &ApTracking::new()) - .expect("Unexpected get integer fail"), - Felt252::THREE - ); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_offset_value_reference_valid() { @@ -243,21 +199,26 @@ mod tests { hint_ref.offset1 = OffsetValue::Reference(Register::FP, 2_i32, false); assert_matches!( - get_offset_value_reference(&vm, &hint_ref, &ApTracking::new(), &hint_ref.offset1), + get_offset_value(&vm, &hint_ref.offset1, &hint_ref.ap_tracking_data, &ApTracking::new()), Some(x) if x == mayberelocatable!(1, 2) ); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_offset_value_reference_invalid() { + fn get_offset_value_invalid() { let mut vm = vm!(); vm.segments = segments![((1, 0), 0)]; let mut hint_ref = HintReference::new(0, 0, false, true); hint_ref.offset1 = OffsetValue::Reference(Register::FP, -2_i32, false); assert_matches!( - get_offset_value_reference(&vm, &hint_ref, &ApTracking::new(), &hint_ref.offset1), + get_offset_value( + &vm, + &hint_ref.offset1, + &hint_ref.ap_tracking_data, + &ApTracking::new() + ), None ); } @@ -413,4 +374,29 @@ mod tests { Felt252::THREE ); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_integer_from_reference_without_outer_defer() { + // Reference: cast([fp + 4] + (-5), felt) + let mut vm = vm!(); + vm.segments = segments![ + ((1, 4), 8), // [fp + 4] + ]; + // [fp + 4] + (-5) = 8 - 5 = 3 + let hint_ref = HintReference { + offset1: OffsetValue::Reference(Register::FP, 4, true), + offset2: OffsetValue::Immediate(Felt252::from(-5)), + outer_dereference: false, + inner_dereference: false, + ap_tracking_data: Default::default(), + cairo_type: None, + }; + + assert_eq!( + get_integer_from_reference(&vm, &hint_ref, &ApTracking::new()) + .expect("Unexpected get integer fail"), + Felt252::THREE + ); + } } diff --git a/vm/src/vm/errors/hint_errors.rs b/vm/src/vm/errors/hint_errors.rs index 0ba9c973c3..14d7a27ff7 100644 --- a/vm/src/vm/errors/hint_errors.rs +++ b/vm/src/vm/errors/hint_errors.rs @@ -32,16 +32,16 @@ pub enum HintError { WrongHintData, #[error("Unknown identifier {0}")] UnknownIdentifier(Box), - #[error("Expected ids.{} at address {} to be an Integer value", (*.0).0, (*.0).1)] - IdentifierNotInteger(Box<(String, Relocatable)>), - #[error("Expected ids.{} at address {} to be a Relocatable value", (*.0).0, (*.0).1)] - IdentifierNotRelocatable(Box<(String, Relocatable)>), + #[error("Expected ids.{} to be an Integer value", (*.0))] + IdentifierNotInteger(Box), + #[error("Expected ids.{} to be a Relocatable value", (*.0))] + IdentifierNotRelocatable(Box), #[error("ids.{} has no member {} or it is of incorrect type", (*.0).0, (*.0).1)] IdentifierHasNoMember(Box<(String, String)>), #[error("Unknown identifier")] UnknownIdentifierInternal, - #[error("Wrong identifier type at address {0}")] - WrongIdentifierTypeInternal(Box), + #[error("Wrong identifier type")] + WrongIdentifierTypeInternal, #[error("Hint Error: {0}")] CustomHint(Box), #[error("Missing constant: {0}")]