diff --git a/src/math/src/i257.cairo b/src/math/src/i257.cairo index fae4202c..0788e213 100644 --- a/src/math/src/i257.cairo +++ b/src/math/src/i257.cairo @@ -1,3 +1,4 @@ +use core::zeroable::Zeroable; // ====================== INT 257 ====================== // i257 represents a 129-bit integer. @@ -259,10 +260,11 @@ impl i257Zeroable of Zeroable { i257_new(0, false) } fn is_zero(self: i257) -> bool { - self == Zeroable::zero() + assert(!self.is_negative, 'no negative zero'); + self.abs == 0 } fn is_non_zero(self: i257) -> bool { - self != Zeroable::zero() + !self.is_zero() } } diff --git a/src/math/src/tests/i257_test.cairo b/src/math/src/tests/i257_test.cairo index f2a6aa4f..c4537503 100644 --- a/src/math/src/tests/i257_test.cairo +++ b/src/math/src/tests/i257_test.cairo @@ -136,6 +136,19 @@ fn i257_test_mul() { assert!(!result.is_negative, "10 * 0 -> positive"); } +#[test] +fn i257_test_is_zero() { + let a = i257 { abs: 0, is_negative: false }; + assert!(a.is_zero(), "should be true"); +} + +#[test] +#[should_panic(expected: ('no negative zero',))] +fn i257_test_is_zero_panic() { + let a = i257 { abs: 0, is_negative: true }; + let _x = a.is_zero(); +} + #[test] fn i257_test_div_no_rem() { // Test division of positive integers