From ee21d2eae3d41b2edba1754bfdb8e62a74f88e63 Mon Sep 17 00:00:00 2001 From: FrancoGiachetta Date: Fri, 20 Dec 2024 17:24:28 -0300 Subject: [PATCH] fix issue --- .../builtin_hint_processor/math_utils.rs | 70 +++++++++++++++---- .../hint_processor_definition.rs | 8 ++- vm/src/hint_processor/hint_processor_utils.rs | 7 +- vm/src/serde/deserialize_utils.rs | 19 +++-- 4 files changed, 82 insertions(+), 22 deletions(-) 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 52c4256a6e..eb65aed496 100644 --- a/vm/src/hint_processor/builtin_hint_processor/math_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/math_utils.rs @@ -2053,8 +2053,14 @@ mod tests { //Create ids let ids_data = HashMap::from([ ("value".to_string(), HintReference::new_simple(-4)), - ("low".to_string(), HintReference::new(-3, 0, true, true, true)), - ("high".to_string(), HintReference::new(-3, 1, true, true, true)), + ( + "low".to_string(), + HintReference::new(-3, 0, true, true, true), + ), + ( + "high".to_string(), + HintReference::new(-3, 1, true, true, true), + ), ]); //Execute the hint assert_matches!( @@ -2131,8 +2137,14 @@ mod tests { //Create ids_data & hint_data let ids_data = HashMap::from([ ("value".to_string(), HintReference::new_simple(-4)), - ("low".to_string(), HintReference::new(-3, 0, true, true, true)), - ("high".to_string(), HintReference::new(-3, 1, true, true, true)), + ( + "low".to_string(), + HintReference::new(-3, 0, true, true, true), + ), + ( + "high".to_string(), + HintReference::new(-3, 1, true, true, true), + ), ]); //Execute the hint @@ -2175,8 +2187,14 @@ mod tests { //Create ids_data & hint_data let ids_data = HashMap::from([ ("value".to_string(), HintReference::new_simple(-4)), - ("low".to_string(), HintReference::new(-3, 0, true, true, true)), - ("high".to_string(), HintReference::new(-3, 1, true, true, true)), + ( + "low".to_string(), + HintReference::new(-3, 0, true, true, true), + ), + ( + "high".to_string(), + HintReference::new(-3, 1, true, true, true), + ), ]); //Execute the hint assert_matches!( @@ -2213,8 +2231,14 @@ mod tests { //Create ids_data & hint_data let ids_data = HashMap::from([ ("value".to_string(), HintReference::new_simple(-4)), - ("low".to_string(), HintReference::new(-3, 0, true, true, true)), - ("high".to_string(), HintReference::new(-3, 1, true, true, true)), + ( + "low".to_string(), + HintReference::new(-3, 0, true, true, true), + ), + ( + "high".to_string(), + HintReference::new(-3, 1, true, true, true), + ), ]); //Execute the hint assert_matches!( @@ -2251,8 +2275,14 @@ mod tests { //Create ids let ids_data = HashMap::from([ ("value".to_string(), HintReference::new_simple(-4)), - ("low".to_string(), HintReference::new(-3, 0, true, true, true)), - ("high".to_string(), HintReference::new(-3, 1, true, true, true)), + ( + "low".to_string(), + HintReference::new(-3, 0, true, true, true), + ), + ( + "high".to_string(), + HintReference::new(-3, 1, true, true, true), + ), ]); //Execute the hint assert_matches!( @@ -2277,8 +2307,14 @@ mod tests { //Create ids let ids_data = HashMap::from([ ("value".to_string(), HintReference::new_simple(-4)), - ("low".to_string(), HintReference::new(-3, 0, true, true, true)), - ("high".to_string(), HintReference::new(-3, 1, true, true, true)), + ( + "low".to_string(), + HintReference::new(-3, 0, true, true, true), + ), + ( + "high".to_string(), + HintReference::new(-3, 1, true, true, true), + ), ]); //Execute the hint assert_matches!( @@ -2315,8 +2351,14 @@ mod tests { //Create ids let ids_data = HashMap::from([ ("value".to_string(), HintReference::new_simple(-4)), - ("low".to_string(), HintReference::new(-3, 0, true, true, true)), - ("high".to_string(), HintReference::new(-3, 1, true, true, true)), + ( + "low".to_string(), + HintReference::new(-3, 0, true, true, true), + ), + ( + "high".to_string(), + HintReference::new(-3, 1, true, true, true), + ), ]); //Execute the hint assert_matches!( diff --git a/vm/src/hint_processor/hint_processor_definition.rs b/vm/src/hint_processor/hint_processor_definition.rs index b6420c1b51..d4c871903a 100644 --- a/vm/src/hint_processor/hint_processor_definition.rs +++ b/vm/src/hint_processor/hint_processor_definition.rs @@ -121,7 +121,13 @@ impl HintReference { } } - pub fn new(offset1: i32, offset2: i32, inner_dereference: bool, dereference: bool, is_positive: bool) -> Self { + pub fn new( + offset1: i32, + offset2: i32, + inner_dereference: bool, + dereference: bool, + is_positive: bool, + ) -> Self { HintReference { offset1: OffsetValue::Reference(Register::FP, offset1, inner_dereference, is_positive), offset2: OffsetValue::Value(offset2), diff --git a/vm/src/hint_processor/hint_processor_utils.rs b/vm/src/hint_processor/hint_processor_utils.rs index e6793a5462..3d442fbf7f 100644 --- a/vm/src/hint_processor/hint_processor_utils.rs +++ b/vm/src/hint_processor/hint_processor_utils.rs @@ -70,7 +70,12 @@ pub fn get_maybe_relocatable_from_reference( &hint_reference.ap_tracking_data, ap_tracking, )?; - let mut val = offset1.add(&offset2).ok()?; + let mut val = match hint_reference.offset2 { + OffsetValue::Reference(_, _, _, true) + | OffsetValue::Immediate(_) + | OffsetValue::Value(_) => offset1.add(&offset2).ok()?, + OffsetValue::Reference(_, _, _, false) => offset1.sub(&offset2).ok()?, + }; if hint_reference.inner_dereference && hint_reference.outer_dereference { val = vm.get_maybe(&val)?; } diff --git a/vm/src/serde/deserialize_utils.rs b/vm/src/serde/deserialize_utils.rs index ce2cc47205..b69ee10f2e 100644 --- a/vm/src/serde/deserialize_utils.rs +++ b/vm/src/serde/deserialize_utils.rs @@ -129,12 +129,12 @@ fn inner_dereference(input: &str) -> IResult<&str, OffsetValue> { let (input, sign) = opt(alt((tag(" + "), tag(" - "))))(input)?; let is_positive = match sign { - Some(s) => match s { + Some(s) => match s.trim() { "+" => true, "-" => false, - _ => unreachable!() + a => unreachable!("{}", a), }, - None => true + None => true, }; map_res( @@ -188,13 +188,17 @@ pub(crate) fn parse_value(input: &str) -> IResult<&str, ValueAddress> { let offset1 = match fst_offset { OffsetValue::Immediate(imm) => OffsetValue::Immediate(imm), OffsetValue::Value(val) => OffsetValue::Immediate(Felt252::from(val)), - OffsetValue::Reference(reg, val, refe, is_positive) => OffsetValue::Reference(reg, val, refe, is_positive), + OffsetValue::Reference(reg, val, refe, is_positive) => { + OffsetValue::Reference(reg, val, refe, is_positive) + } }; let offset2 = match snd_offset { OffsetValue::Immediate(imm) => OffsetValue::Immediate(imm), OffsetValue::Value(val) => OffsetValue::Immediate(Felt252::from(val)), - OffsetValue::Reference(reg, val, refe, is_positive) => OffsetValue::Reference(reg, val, refe, is_positive), + OffsetValue::Reference(reg, val, refe, is_positive) => { + OffsetValue::Reference(reg, val, refe, is_positive) + } }; (offset1, offset2) @@ -379,7 +383,10 @@ mod tests { assert_eq!( parsed, - Ok((" + 2", OffsetValue::Reference(Register::FP, -1_i32, true, true))) + Ok(( + " + 2", + OffsetValue::Reference(Register::FP, -1_i32, true, true) + )) ); }