Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix negative offset error. #1271

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

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

#### [0.6.0] - 2023-6-18

* fix: `dibit` hint no longer fails when called with an `m` of zero [#1247](https://github.com/lambdaclass/cairo-rs/pull/1247)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ mod tests {
//Execute the hint
assert_matches!(
run_hint!(vm, ids_data, hint_code),
Err(HintError::Math(MathError::RelocatableSubNegOffset(bx)))
Err(HintError::Math(MathError::RelocatableSubUsizeNegOffset(bx)))
if *bx == (relocatable!(2,5), 26)
);
}
Expand Down
4 changes: 3 additions & 1 deletion vm/src/types/errors/math_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub enum MathError {
#[error("Cant convert felt: {0} to Relocatable")]
Felt252ToRelocatable(Box<Felt252>),
#[error("Operation failed: {} - {}, offsets cant be negative", (*.0).0, (*.0).1)]
RelocatableSubNegOffset(Box<(Relocatable, usize)>),
RelocatableSubFelt252NegOffset(Box<(Relocatable, Felt252)>),
#[error("Operation failed: {} - {}, offsets cant be negative", (*.0).0, (*.0).1)]
RelocatableSubUsizeNegOffset(Box<(Relocatable, usize)>),
#[error("Operation failed: {} + {}, maximum offset value exceeded", (*.0).0, (*.0).1)]
RelocatableAddFelt252OffsetExceeded(Box<(Relocatable, Felt252)>),
#[error("Operation failed: {} + {}, maximum offset value exceeded", (*.0).0, (*.0).1)]
Expand Down
15 changes: 7 additions & 8 deletions vm/src/types/relocatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@
type Output = Result<Relocatable, MathError>;
fn sub(self, other: usize) -> Result<Self, MathError> {
if self.offset < other {
return Err(MathError::RelocatableSubNegOffset(Box::new((self, other))));
return Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
self, other,
))));
}
let new_offset = self.offset - other;
Ok(relocatable!(self.segment_index, new_offset))
Expand All @@ -161,7 +163,7 @@
return Err(MathError::RelocatableSubDiffIndex(Box::new((self, other))));
}
if self.offset < other.offset {
return Err(MathError::RelocatableSubNegOffset(Box::new((
return Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
self,
other.offset,
))));
Expand Down Expand Up @@ -268,10 +270,7 @@
Ok(MaybeRelocatable::from((
rel_a.segment_index,
(rel_a.offset - num_b).to_usize().ok_or_else(|| {
MathError::RelocatableAddFelt252OffsetExceeded(Box::new((
*rel_a,
num_b.clone(),
)))
MathError::RelocatableSubFelt252NegOffset(Box::new((*rel_a, num_b.clone())))

Check warning on line 273 in vm/src/types/relocatable.rs

View check run for this annotation

Codecov / codecov/patch

vm/src/types/relocatable.rs#L273

Added line #L273 was not covered by tests
})?,
)))
}
Expand Down Expand Up @@ -789,7 +788,7 @@

assert_eq!(
reloc + (-3),
Err(MathError::RelocatableSubNegOffset(Box::new((
Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
relocatable!(1, 1),
3
))))
Expand All @@ -814,7 +813,7 @@
assert_eq!(reloc - relocatable!(7, 5), Ok(1));
assert_eq!(
reloc - relocatable!(7, 9),
Err(MathError::RelocatableSubNegOffset(Box::new((
Err(MathError::RelocatableSubUsizeNegOffset(Box::new((
relocatable!(7, 6),
9
))))
Expand Down
Loading