Skip to content

Commit 8054e33

Browse files
alonh5kariy
authored andcommitted
Fix negative offset error. (lambdaclass#1271)
1 parent c329316 commit 8054e33

File tree

4 files changed

+13
-10
lines changed

4 files changed

+13
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#### Upcoming Changes
44
* fix: Handle the deserialization of serde_json::Number with scientific notation (e.g.: Number(1e27)) in felt_from_number function [#1188](https://github.com/lambdaclass/cairo-rs/pull/1188)
55

6+
* fix: change error returned when subtracting two `MaybeRelocatable`s to better reflect the cause [#1271](https://github.com/lambdaclass/cairo-rs/pull/1271)
7+
68
#### [0.6.0] - 2023-6-18
79

810
* fix: `dibit` hint no longer fails when called with an `m` of zero [#1247](https://github.com/lambdaclass/cairo-rs/pull/1247)

vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ mod tests {
340340
//Execute the hint
341341
assert_matches!(
342342
run_hint!(vm, ids_data, hint_code),
343-
Err(HintError::Math(MathError::RelocatableSubNegOffset(bx)))
343+
Err(HintError::Math(MathError::RelocatableSubUsizeNegOffset(bx)))
344344
if *bx == (relocatable!(2,5), 26)
345345
);
346346
}

vm/src/types/errors/math_errors.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ pub enum MathError {
3737
#[error("Cant convert felt: {0} to Relocatable")]
3838
Felt252ToRelocatable(Box<Felt252>),
3939
#[error("Operation failed: {} - {}, offsets cant be negative", (*.0).0, (*.0).1)]
40-
RelocatableSubNegOffset(Box<(Relocatable, usize)>),
40+
RelocatableSubFelt252NegOffset(Box<(Relocatable, Felt252)>),
41+
#[error("Operation failed: {} - {}, offsets cant be negative", (*.0).0, (*.0).1)]
42+
RelocatableSubUsizeNegOffset(Box<(Relocatable, usize)>),
4143
#[error("Operation failed: {} + {}, maximum offset value exceeded", (*.0).0, (*.0).1)]
4244
RelocatableAddFelt252OffsetExceeded(Box<(Relocatable, Felt252)>),
4345
#[error("Operation failed: {} + {}, maximum offset value exceeded", (*.0).0, (*.0).1)]

vm/src/types/relocatable.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ impl Sub<usize> for Relocatable {
147147
type Output = Result<Relocatable, MathError>;
148148
fn sub(self, other: usize) -> Result<Self, MathError> {
149149
if self.offset < other {
150-
return Err(MathError::RelocatableSubNegOffset(Box::new((self, other))));
150+
return Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
151+
self, other,
152+
))));
151153
}
152154
let new_offset = self.offset - other;
153155
Ok(relocatable!(self.segment_index, new_offset))
@@ -161,7 +163,7 @@ impl Sub<Relocatable> for Relocatable {
161163
return Err(MathError::RelocatableSubDiffIndex(Box::new((self, other))));
162164
}
163165
if self.offset < other.offset {
164-
return Err(MathError::RelocatableSubNegOffset(Box::new((
166+
return Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
165167
self,
166168
other.offset,
167169
))));
@@ -268,10 +270,7 @@ impl MaybeRelocatable {
268270
Ok(MaybeRelocatable::from((
269271
rel_a.segment_index,
270272
(rel_a.offset - num_b).to_usize().ok_or_else(|| {
271-
MathError::RelocatableAddFelt252OffsetExceeded(Box::new((
272-
*rel_a,
273-
num_b.clone(),
274-
)))
273+
MathError::RelocatableSubFelt252NegOffset(Box::new((*rel_a, num_b.clone())))
275274
})?,
276275
)))
277276
}
@@ -789,7 +788,7 @@ mod tests {
789788

790789
assert_eq!(
791790
reloc + (-3),
792-
Err(MathError::RelocatableSubNegOffset(Box::new((
791+
Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
793792
relocatable!(1, 1),
794793
3
795794
))))
@@ -814,7 +813,7 @@ mod tests {
814813
assert_eq!(reloc - relocatable!(7, 5), Ok(1));
815814
assert_eq!(
816815
reloc - relocatable!(7, 9),
817-
Err(MathError::RelocatableSubNegOffset(Box::new((
816+
Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
818817
relocatable!(7, 6),
819818
9
820819
))))

0 commit comments

Comments
 (0)