From 9e3565d31686da5b76a14d74ab9c773778591c60 Mon Sep 17 00:00:00 2001 From: shwang <31915926+Uniblake@users.noreply.github.com> Date: Wed, 21 Feb 2024 11:47:18 +0100 Subject: [PATCH] Fix negative zero for i257 eq gt (#278) ## Pull Request type Please check the type of change your PR introduces: - [x] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## What is the current behavior? Issue Number: N/A ## What is the new behavior? - - - ## Does this introduce a breaking change? - [ ] Yes - [ ] No ## Other information --- src/math/src/i257.cairo | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/math/src/i257.cairo b/src/math/src/i257.cairo index 88644e30..fae4202c 100644 --- a/src/math/src/i257.cairo +++ b/src/math/src/i257.cairo @@ -195,8 +195,12 @@ fn i257_div_rem(lhs: i257, rhs: i257) -> (i257, i257) { } // Implements the PartialEq trait for i257. +// WARNING: If either `lhs` or `rhs` is negative zero, functions will revert. +// Ensure that neither `lhs` nor `rhs` is negative zero before calling. impl i257PartialEq of PartialEq { fn eq(lhs: @i257, rhs: @i257) -> bool { + i257_assert_no_negative_zero(*lhs); + i257_assert_no_negative_zero(*rhs); lhs.is_negative == rhs.is_negative && lhs.abs == rhs.abs } @@ -206,6 +210,8 @@ impl i257PartialEq of PartialEq { } // Implements the PartialOrd trait for i257. +// WARNING: If either `lhs` or `rhs` is negative zero, functions will revert. +// Ensure that neither `lhs` nor `rhs` is negative zero before calling. impl i257PartialOrd of PartialOrd { fn le(lhs: i257, rhs: i257) -> bool { !i257PartialOrd::gt(lhs, rhs) @@ -219,6 +225,8 @@ impl i257PartialOrd of PartialOrd { } fn gt(lhs: i257, rhs: i257) -> bool { + i257_assert_no_negative_zero(lhs); + i257_assert_no_negative_zero(rhs); // Check if `lhs` is negative and `rhs` is positive. if lhs.is_negative & !rhs.is_negative { return false; @@ -262,7 +270,7 @@ impl i257Zeroable of Zeroable { // # Arguments // * `x` - The i257 integer to check. // # Panics -// Panics if `x` is zero and is not negative +// Panics if `x` is zero and is negative fn i257_assert_no_negative_zero(x: i257) { if x.abs == 0 { assert(!x.is_negative, 'negative zero');