diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 46f3500..20df85a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -9,7 +9,8 @@ on: - main jobs: - main: + test-w-cairo-2-8-4: + name: Test with Cairo 2.8.4 runs-on: ubuntu-latest steps: - name: Checkout @@ -18,7 +19,23 @@ jobs: - name: Setup Cairo toolchain uses: software-mansion/setup-scarb@v1 with: - scarb-version: "2.4.0" + scarb-version: "2.8.4" - name: Run tests run: scarb test + + test-w-cairo-2-9-2: + name: Test with Cairo 2.9.2 + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup Cairo toolchain + uses: software-mansion/setup-scarb@v1 + with: + scarb-version: "2.9.2" + + - name: Run tests + run: scarb test + \ No newline at end of file diff --git a/Scarb.toml b/Scarb.toml index 8321422..4075b74 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,7 +1,7 @@ [package] name = "cubit" version = "1.3.0" -cairo-version = ">=2.4.0" +cairo-version = ">=2.8.0" edition = "2023_10" description = "Math library in Cairo using a 64.64 fixed point representation" homepage = "https://github.com/influenceth/cubit" @@ -11,3 +11,7 @@ sierra = false # Enable Sierra codegen. casm = false # Enable CASM codegen. [dependencies] +starknet = ">=2.8.0" + +[dev-dependencies] +cairo_test = "2.8.4" \ No newline at end of file diff --git a/src/f128/math/ops.cairo b/src/f128/math/ops.cairo index fd39913..c57f665 100644 --- a/src/f128/math/ops.cairo +++ b/src/f128/math/ops.cairo @@ -3,12 +3,13 @@ use core::option::OptionTrait; use core::result::{ResultTrait, ResultTraitImpl}; use core::traits::{Into, TryInto}; use core::integer; -use core::integer::{u256_safe_div_rem, u256_as_non_zero, upcast}; +use core::num::traits::{WideMul, Sqrt}; +use core::integer::{u256_safe_div_rem, u256_as_non_zero}; use cubit::f128::math::lut; use cubit::f128::types::fixed::{ - HALF_u128, MAX_u128, ONE_u128, Fixed, FixedInto, FixedTrait, FixedAdd, FixedDiv, FixedMul, - FixedNeg + HALF_u128, MAX_u128, ONE_u128, ONE_u256, SQRT_ONE_u128, Fixed, FixedInto, FixedTrait, FixedAdd, + FixedDiv, FixedMul, FixedNeg }; // PUBLIC @@ -48,9 +49,8 @@ fn ceil(a: Fixed) -> Fixed { } fn div(a: Fixed, b: Fixed) -> Fixed { - let (a_high, a_low) = integer::u128_wide_mul(a.mag, ONE_u128); - let a_u256 = u256 { low: a_low, high: a_high }; - let b_u256 = u256 { low: b.mag, high: 0 }; + let a_u256 = a.mag.wide_mul(ONE_u128); + let b_u256 = b.mag.into(); let res_u256 = a_u256 / b_u256; assert(res_u256.high == 0, 'result overflow'); @@ -91,7 +91,7 @@ fn exp2(a: Fixed) -> Fixed { res_u = res_u * (r1 + FixedTrait::ONE()); } - if (a.sign == true) { + if (a.sign) { return FixedTrait::ONE() / res_u; } else { return res_u; @@ -147,7 +147,7 @@ fn ln(a: Fixed) -> Fixed { // Calculates the binary logarithm of x: log2(x) // self must be greather than zero fn log2(a: Fixed) -> Fixed { - assert(a.sign == false, 'must be positive'); + assert(!a.sign, 'must be positive'); if (a.mag == ONE_u128) { return FixedTrait::ZERO(); @@ -186,9 +186,7 @@ fn lt(a: Fixed, b: Fixed) -> bool { } fn mul(a: Fixed, b: Fixed) -> Fixed { - let (high, low) = integer::u128_wide_mul(a.mag, b.mag); - let res_u256 = u256 { low: low, high: high }; - let ONE_u256 = u256 { low: ONE_u128, high: 0 }; + let res_u256 = a.mag.wide_mul(b.mag); let (scaled_u256, _) = u256_safe_div_rem(res_u256, u256_as_non_zero(ONE_u256)); assert(scaled_u256.high == 0, 'result overflow'); @@ -204,8 +202,8 @@ struct f64 { } fn mul_64(a: f64, b: f64) -> f64 { - let prod_u128 = integer::u64_wide_mul(a.mag, b.mag); - return f64 { mag: (prod_u128 / 4294967296).try_into().unwrap(), sign: a.sign ^ b.sign }; + let prod_u128 = a.mag.wide_mul(b.mag); + return f64 { mag: (prod_u128 / SQRT_ONE_u128).try_into().unwrap(), sign: a.sign ^ b.sign }; } fn ne(a: @Fixed, b: @Fixed) -> bool { @@ -242,7 +240,7 @@ fn pow_int(a: Fixed, b: u128, sign: bool) -> Fixed { let mut x = a; let mut n = b; - if sign == true { + if sign { x = FixedTrait::ONE() / x; } @@ -288,10 +286,8 @@ fn round(a: Fixed) -> Fixed { // Calculates the square root of a fixed point value // x must be positive fn sqrt(a: Fixed) -> Fixed { - assert(a.sign == false, 'must be positive'); - let root = integer::u128_sqrt(a.mag); - let scale_root = integer::u128_sqrt(ONE_u128); - let res_u128 = upcast(root) * ONE_u128 / upcast(scale_root); + assert(!a.sign, 'must be positive'); + let res_u128 = a.mag.sqrt().into() * ONE_u128 / SQRT_ONE_u128; return FixedTrait::new(res_u128, false); } @@ -304,19 +300,18 @@ fn _split_unsigned(a: Fixed) -> (u128, u128) { return integer::u128_safe_divmod(a.mag, integer::u128_as_non_zero(ONE_u128)); } -// Tests -------------------------------------------------------------------------------------------------------------- +// Tests +// -------------------------------------------------------------------------------------------------------------- #[cfg(test)] mod tests { use cubit::f128::test::helpers::assert_precise; - use cubit::f128::types::fixed::{ - ONE, HALF, FixedPartialEq, FixedPartialOrd, FixedAddEq, FixedSub, FixedSubEq, FixedMulEq - }; + use cubit::f128::types::fixed::{ONE, HALF}; use cubit::f128::math::trig::HALF_PI_u128; use cubit::f128::math::trig::PI_u128; - use super::{FixedTrait, ONE_u128, lut, exp2_int, integer}; + use super::{FixedTrait, ONE_u128, lut, exp2_int}; #[test] fn test_into() { @@ -495,7 +490,7 @@ mod tests { let a = FixedTrait::from_unscaled_felt(42); let b = FixedTrait::from_unscaled_felt(42); let c = a == b; - assert(c == true, 'invalid result'); + assert(c, 'invalid result'); } #[test] @@ -503,7 +498,7 @@ mod tests { let a = FixedTrait::from_unscaled_felt(42); let b = FixedTrait::from_unscaled_felt(42); let c = a != b; - assert(c == false, 'invalid result'); + assert(!c, 'invalid result'); } #[test] @@ -577,12 +572,12 @@ mod tests { let c = FixedTrait::from_unscaled_felt(-1); assert(a <= a, 'a <= a'); - assert(a <= b == false, 'a <= b'); - assert(a <= c == false, 'a <= c'); + assert(!(a <= b), 'a <= b'); + assert(!(a <= c), 'a <= c'); assert(b <= a, 'b <= a'); assert(b <= b, 'b <= b'); - assert(b <= c == false, 'b <= c'); + assert(!(b <= c), 'b <= c'); assert(c <= a, 'c <= a'); assert(c <= b, 'c <= b'); @@ -595,17 +590,17 @@ mod tests { let b = FixedTrait::from_unscaled_felt(0); let c = FixedTrait::from_unscaled_felt(-1); - assert(a < a == false, 'a < a'); - assert(a < b == false, 'a < b'); - assert(a < c == false, 'a < c'); + assert(!(a < a), 'a < a'); + assert(!(a < b), 'a < b'); + assert(!(a < c), 'a < c'); assert(b < a, 'b < a'); - assert(b < b == false, 'b < b'); - assert(b < c == false, 'b < c'); + assert(!(b < b), 'b < b'); + assert(!(b < c), 'b < c'); assert(c < a, 'c < a'); assert(c < b, 'c < b'); - assert(c < c == false, 'c < c'); + assert(!(c < c), 'c < c'); } #[test] @@ -618,12 +613,12 @@ mod tests { assert(a >= b, 'a >= b'); assert(a >= c, 'a >= c'); - assert(b >= a == false, 'b >= a'); + assert(!(b >= a), 'b >= a'); assert(b >= b, 'b >= b'); assert(b >= c, 'b >= c'); - assert(c >= a == false, 'c >= a'); - assert(c >= b == false, 'c >= b'); + assert(!(c >= a), 'c >= a'); + assert(!(c >= b), 'c >= b'); assert(c >= c, 'c >= c'); } @@ -633,17 +628,17 @@ mod tests { let b = FixedTrait::from_unscaled_felt(0); let c = FixedTrait::from_unscaled_felt(-1); - assert(a > a == false, 'a > a'); + assert(!(a > a), 'a > a'); assert(a > b, 'a > b'); assert(a > c, 'a > c'); - assert(b > a == false, 'b > a'); - assert(b > b == false, 'b > b'); + assert(!(b > a), 'b > a'); + assert(!(b > b), 'b > b'); assert(b > c, 'b > c'); - assert(c > a == false, 'c > a'); - assert(c > b == false, 'c > b'); - assert(c > c == false, 'c > c'); + assert(!(c > a), 'c > a'); + assert(!(c > b), 'c > b'); + assert(!(c > c), 'c > c'); } #[test] diff --git a/src/f128/types/fixed.cairo b/src/f128/types/fixed.cairo index e3f2bd7..fa6b25d 100644 --- a/src/f128/types/fixed.cairo +++ b/src/f128/types/fixed.cairo @@ -16,6 +16,8 @@ use cubit::f64::{Fixed as Fixed64, FixedTrait as FixedTrait64, ONE as ONE_u64}; const PRIME: felt252 = 3618502788666131213697322783095070105623107215331596699973092056135872020480; const ONE: felt252 = 18446744073709551616; // 2 ** 64 const ONE_u128: u128 = 18446744073709551616_u128; // 2 ** 64 +const ONE_u256: u256 = 18446744073709551616_u256; // 2 ** 64 +const SQRT_ONE_u128: u128 = 4294967296; // 2 ** 32 const HALF: felt252 = 9223372036854775808; // 2 ** 63 const HALF_u128: u128 = 9223372036854775808_u128; // 2 ** 63 const MAX_u128: u128 = 340282366920938463463374607431768211455_u128; // 2 ** 128 - 1 @@ -92,16 +94,16 @@ impl FixedImpl of FixedTrait { } fn new_unscaled(mag: u128, sign: bool) -> Fixed { - return FixedTrait::new(mag * ONE_u128, sign); + return Self::new(mag * ONE_u128, sign); } fn from_felt(val: felt252) -> Fixed { let mag = core::integer::u128_try_from_felt252(utils::felt_abs(val)).unwrap(); - return FixedTrait::new(mag, utils::felt_sign(val)); + return Self::new(mag, utils::felt_sign(val)); } fn from_unscaled_felt(val: felt252) -> Fixed { - return FixedTrait::from_felt(val * ONE); + return Self::from_felt(val * ONE); } fn abs(self: Fixed) -> Fixed { @@ -430,10 +432,10 @@ impl FixedAdd of Add { } } -impl FixedAddEq of AddEq { +impl FixedAddAssign of core::ops::AddAssign { #[inline(always)] - fn add_eq(ref self: Fixed, other: Fixed) { - self = Add::add(self, other); + fn add_assign(ref self: Fixed, rhs: Fixed) { + self = Add::add(self, rhs); } } @@ -443,10 +445,10 @@ impl FixedSub of Sub { } } -impl FixedSubEq of SubEq { +impl FixedSubAssign of core::ops::SubAssign { #[inline(always)] - fn sub_eq(ref self: Fixed, other: Fixed) { - self = Sub::sub(self, other); + fn sub_assign(ref self: Fixed, rhs: Fixed) { + self = Sub::sub(self, rhs); } } @@ -456,10 +458,10 @@ impl FixedMul of Mul { } } -impl FixedMulEq of MulEq { +impl FixedMulAssign of core::ops::MulAssign { #[inline(always)] - fn mul_eq(ref self: Fixed, other: Fixed) { - self = Mul::mul(self, other); + fn mul_assign(ref self: Fixed, rhs: Fixed) { + self = Mul::mul(self, rhs); } } @@ -469,10 +471,10 @@ impl FixedDiv of Div { } } -impl FixedDivEq of DivEq { +impl FixedDivAssign of core::ops::DivAssign { #[inline(always)] - fn div_eq(ref self: Fixed, other: Fixed) { - self = Div::div(self, other); + fn div_assign(ref self: Fixed, rhs: Fixed) { + self = Div::div(self, rhs); } } @@ -551,7 +553,7 @@ impl FixedOne of core::num::traits::One { } #[inline(always)] fn is_one(self: @Fixed) -> bool { - *self == FixedOne::one() + *self == Self::one() } #[inline(always)] fn is_non_one(self: @Fixed) -> bool { @@ -560,7 +562,8 @@ impl FixedOne of core::num::traits::One { } -// Tests -------------------------------------------------------------------------------------------------------------- +// Tests +// -------------------------------------------------------------------------------------------------------------- #[cfg(test)] mod tests { @@ -812,12 +815,12 @@ mod tests { let a = FixedTrait::from_unscaled_felt(42); let b = FixedTrait::from_unscaled_felt(42); let c = ops::eq(@a, @b); - assert(c == true, 'invalid result'); + assert(c, 'invalid result'); let a = FixedTrait::from_unscaled_felt(42); let b = FixedTrait::from_unscaled_felt(-42); let c = ops::eq(@a, @b); - assert(c == false, 'invalid result'); + assert(!c, 'invalid result'); } #[test] @@ -826,12 +829,12 @@ mod tests { let a = FixedTrait::from_unscaled_felt(42); let b = FixedTrait::from_unscaled_felt(42); let c = ops::ne(@a, @b); - assert(c == false, 'invalid result'); + assert(!c, 'invalid result'); let a = FixedTrait::from_unscaled_felt(42); let b = FixedTrait::from_unscaled_felt(-42); let c = ops::ne(@a, @b); - assert(c == true, 'invalid result'); + assert(c, 'invalid result'); } #[test] @@ -945,12 +948,12 @@ mod tests { let c = FixedTrait::from_unscaled_felt(-1); assert(ops::le(a, a), 'a <= a'); - assert(ops::le(a, b) == false, 'a <= b'); - assert(ops::le(a, c) == false, 'a <= c'); + assert(!ops::le(a, b), 'a <= b'); + assert(!ops::le(a, c), 'a <= c'); assert(ops::le(b, a), 'b <= a'); assert(ops::le(b, b), 'b <= b'); - assert(ops::le(b, c) == false, 'b <= c'); + assert(!ops::le(b, c), 'b <= c'); assert(ops::le(c, a), 'c <= a'); assert(ops::le(c, b), 'c <= b'); @@ -964,17 +967,17 @@ mod tests { let b = FixedTrait::from_unscaled_felt(0); let c = FixedTrait::from_unscaled_felt(-1); - assert(ops::lt(a, a) == false, 'a < a'); - assert(ops::lt(a, b) == false, 'a < b'); - assert(ops::lt(a, c) == false, 'a < c'); + assert(!ops::lt(a, a), 'a < a'); + assert(!ops::lt(a, b), 'a < b'); + assert(!ops::lt(a, c), 'a < c'); assert(ops::lt(b, a), 'b < a'); - assert(ops::lt(b, b) == false, 'b < b'); - assert(ops::lt(b, c) == false, 'b < c'); + assert(!ops::lt(b, b), 'b < b'); + assert(!ops::lt(b, c), 'b < c'); assert(ops::lt(c, a), 'c < a'); assert(ops::lt(c, b), 'c < b'); - assert(ops::lt(c, c) == false, 'c < c'); + assert(!ops::lt(c, c), 'c < c'); } #[test] @@ -988,12 +991,12 @@ mod tests { assert(ops::ge(a, b), 'a >= b'); assert(ops::ge(a, c), 'a >= c'); - assert(ops::ge(b, a) == false, 'b >= a'); + assert(!ops::ge(b, a), 'b >= a'); assert(ops::ge(b, b), 'b >= b'); assert(ops::ge(b, c), 'b >= c'); - assert(ops::ge(c, a) == false, 'c >= a'); - assert(ops::ge(c, b) == false, 'c >= b'); + assert(!ops::ge(c, a), 'c >= a'); + assert(!ops::ge(c, b), 'c >= b'); assert(ops::ge(c, c), 'c >= c'); } @@ -1004,17 +1007,17 @@ mod tests { let b = FixedTrait::from_unscaled_felt(0); let c = FixedTrait::from_unscaled_felt(-1); - assert(ops::gt(a, a) == false, 'a > a'); + assert(!ops::gt(a, a), 'a > a'); assert(ops::gt(a, b), 'a > b'); assert(ops::gt(a, c), 'a > c'); - assert(ops::gt(b, a) == false, 'b > a'); - assert(ops::gt(b, b) == false, 'b > b'); + assert(!ops::gt(b, a), 'b > a'); + assert(!ops::gt(b, b), 'b > b'); assert(ops::gt(b, c), 'b > c'); - assert(ops::gt(c, a) == false, 'c > a'); - assert(ops::gt(c, b) == false, 'c > b'); - assert(ops::gt(c, c) == false, 'c > c'); + assert(!ops::gt(c, a), 'c > a'); + assert(!ops::gt(c, b), 'c > b'); + assert(!ops::gt(c, c), 'c > c'); } #[test] diff --git a/src/f128/types/vec2.cairo b/src/f128/types/vec2.cairo index c6892c2..7cc71f4 100644 --- a/src/f128/types/vec2.cairo +++ b/src/f128/types/vec2.cairo @@ -2,7 +2,7 @@ use core::debug::PrintTrait; use cubit::f128::types::fixed::{Fixed, FixedTrait, FixedPrint}; -#[derive(Copy, Drop, Serde, Store)] +#[derive(Copy, Drop, Serde, starknet::Store)] struct Vec2 { x: Fixed, y: Fixed diff --git a/src/f128/types/vec3.cairo b/src/f128/types/vec3.cairo index 8b1e646..3753872 100644 --- a/src/f128/types/vec3.cairo +++ b/src/f128/types/vec3.cairo @@ -2,7 +2,7 @@ use core::debug::PrintTrait; use cubit::f128::types::fixed::{Fixed, FixedTrait, FixedPrint}; -#[derive(Copy, Drop, Serde, Store)] +#[derive(Copy, Drop, Serde, starknet::Store)] struct Vec3 { x: Fixed, y: Fixed, diff --git a/src/f128/types/vec4.cairo b/src/f128/types/vec4.cairo index 2986a1e..8386c3e 100644 --- a/src/f128/types/vec4.cairo +++ b/src/f128/types/vec4.cairo @@ -2,7 +2,7 @@ use core::debug::PrintTrait; use cubit::f128::types::fixed::{Fixed, FixedTrait, FixedPrint}; -#[derive(Copy, Drop, Serde, Store)] +#[derive(Copy, Drop, Serde, starknet::Store)] struct Vec4 { x: Fixed, y: Fixed, diff --git a/src/f64/math/ops.cairo b/src/f64/math/ops.cairo index 48dce5d..e27d380 100644 --- a/src/f64/math/ops.cairo +++ b/src/f64/math/ops.cairo @@ -1,10 +1,12 @@ use core::option::OptionTrait; use core::result::{ResultTrait, ResultTraitImpl}; use core::traits::{Into, TryInto}; -use core::integer::{u64_safe_divmod, u64_as_non_zero, u64_wide_mul}; +use core::num::traits::{WideMul, Sqrt}; +use core::integer::{u64_safe_divmod, u64_as_non_zero}; use cubit::f64::math::lut; use cubit::f64::types::fixed::{HALF, ONE, Fixed, FixedIntoFelt252, FixedTrait}; +use cubit::f128::types::fixed::SQRT_ONE_u128; // PUBLIC @@ -43,7 +45,7 @@ fn ceil(a: Fixed) -> Fixed { } fn div(a: Fixed, b: Fixed) -> Fixed { - let a_u128 = core::integer::u64_wide_mul(a.mag, ONE); + let a_u128 = a.mag.wide_mul(ONE); let res_u128 = a_u128 / b.mag.into(); // Re-apply sign @@ -82,7 +84,7 @@ fn exp2(a: Fixed) -> Fixed { res_u = res_u * (r1 + FixedTrait::ONE()); } - if (a.sign == true) { + if (a.sign) { return FixedTrait::ONE() / res_u; } else { return res_u; @@ -138,7 +140,7 @@ fn ln(a: Fixed) -> Fixed { // Calculates the binary logarithm of x: log2(x) // self must be greather than zero fn log2(a: Fixed) -> Fixed { - assert(a.sign == false, 'must be positive'); + assert(!a.sign, 'must be positive'); if (a.mag == ONE) { return FixedTrait::ZERO(); @@ -182,7 +184,7 @@ fn lt(a: Fixed, b: Fixed) -> bool { } fn mul(a: Fixed, b: Fixed) -> Fixed { - let prod_u128 = core::integer::u64_wide_mul(a.mag, b.mag); + let prod_u128 = a.mag.wide_mul(b.mag); // Re-apply sign return FixedTrait::new((prod_u128 / ONE.into()).try_into().unwrap(), a.sign ^ b.sign); @@ -222,7 +224,7 @@ fn pow_int(a: Fixed, b: u64, sign: bool) -> Fixed { let mut x = a; let mut n = b; - if sign == true { + if sign { x = FixedTrait::ONE() / x; } @@ -268,16 +270,16 @@ fn round(a: Fixed) -> Fixed { // Calculates the square root of a Fixed point value // x must be positive fn sqrt(a: Fixed) -> Fixed { - assert(a.sign == false, 'must be positive'); - let root = core::integer::u128_sqrt(a.mag.into() * ONE.into()); - return FixedTrait::new(root.into(), false); + assert(!a.sign, 'must be positive'); + return FixedTrait::new((a.mag.into() * SQRT_ONE_u128).sqrt(), false); } fn sub(a: Fixed, b: Fixed) -> Fixed { return add(a, -b); } -// Tests -------------------------------------------------------------------------------------------------------------- +// Tests +// -------------------------------------------------------------------------------------------------------------- #[cfg(test)] mod tests { @@ -457,7 +459,7 @@ mod tests { let a = FixedTrait::new_unscaled(42, false); let b = FixedTrait::new_unscaled(42, false); let c = eq(@a, @b); - assert(c == true, 'invalid result'); + assert(c, 'invalid result'); } #[test] @@ -465,7 +467,7 @@ mod tests { let a = FixedTrait::new_unscaled(42, false); let b = FixedTrait::new_unscaled(42, false); let c = ne(@a, @b); - assert(c == false, 'invalid result'); + assert(!c, 'invalid result'); } #[test] @@ -539,12 +541,12 @@ mod tests { let c = FixedTrait::new_unscaled(1, true); assert(a <= a, 'a <= a'); - assert(a <= b == false, 'a <= b'); - assert(a <= c == false, 'a <= c'); + assert(!(a <= b), 'a <= b'); + assert(!(a <= c), 'a <= c'); assert(b <= a, 'b <= a'); assert(b <= b, 'b <= b'); - assert(b <= c == false, 'b <= c'); + assert(!(b <= c), 'b <= c'); assert(c <= a, 'c <= a'); assert(c <= b, 'c <= b'); @@ -557,17 +559,17 @@ mod tests { let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::new_unscaled(1, true); - assert(a < a == false, 'a < a'); - assert(a < b == false, 'a < b'); - assert(a < c == false, 'a < c'); + assert(!(a < a), 'a < a'); + assert(!(a < b), 'a < b'); + assert(!(a < c), 'a < c'); assert(b < a, 'b < a'); - assert(b < b == false, 'b < b'); - assert(b < c == false, 'b < c'); + assert(!(b < b), 'b < b'); + assert(!(b < c), 'b < c'); assert(c < a, 'c < a'); assert(c < b, 'c < b'); - assert(c < c == false, 'c < c'); + assert(!(c < c), 'c < c'); } #[test] @@ -580,12 +582,12 @@ mod tests { assert(a >= b, 'a >= b'); assert(a >= c, 'a >= c'); - assert(b >= a == false, 'b >= a'); + assert(!(b >= a), 'b >= a'); assert(b >= b, 'b >= b'); assert(b >= c, 'b >= c'); - assert(c >= a == false, 'c >= a'); - assert(c >= b == false, 'c >= b'); + assert(!(c >= a), 'c >= a'); + assert(!(c >= b), 'c >= b'); assert(c >= c, 'c >= c'); } @@ -595,17 +597,17 @@ mod tests { let b = FixedTrait::new_unscaled(0, false); let c = FixedTrait::new_unscaled(1, true); - assert(a > a == false, 'a > a'); + assert(!(a > a), 'a > a'); assert(a > b, 'a > b'); assert(a > c, 'a > c'); - assert(b > a == false, 'b > a'); - assert(b > b == false, 'b > b'); + assert(!(b > a), 'b > a'); + assert(!(b > b), 'b > b'); assert(b > c, 'b > c'); - assert(c > a == false, 'c > a'); - assert(c > b == false, 'c > b'); - assert(c > c == false, 'c > c'); + assert(!(c > a), 'c > a'); + assert(!(c > b), 'c > b'); + assert(!(c > c), 'c > c'); } #[test] @@ -628,7 +630,7 @@ mod tests { let a = FixedTrait::new(HALF_PI / 2, false); assert(a.tan().mag == ONE, 'invalid quarter pi'); } -// #[test] + // #[test] // #[available_gas(1000000)] // fn test_cosh() { // let a = FixedTrait::new_unscaled(2, false); @@ -637,7 +639,7 @@ mod tests { // ); // 3.762195691016423 // } -// #[test] + // #[test] // #[available_gas(1000000)] // fn test_sinh() { // let a = FixedTrait::new_unscaled(2, false); @@ -646,7 +648,7 @@ mod tests { // ); // 3.6268604077773023 // } -// #[test] + // #[test] // #[available_gas(1000000)] // fn test_tanh() { // let a = FixedTrait::new_unscaled(2, false); @@ -655,21 +657,21 @@ mod tests { // ); // 0.9640275800745076 // } -// #[test] + // #[test] // #[available_gas(1000000)] // fn test_acosh() { // let a = FixedTrait::new(69400261067392811864, false); // 3.762195691016423 // assert_precise(a.acosh(), 2 * ONE, 'invalid two', Option::None(())); // } -// #[test] + // #[test] // #[available_gas(1000000)] // fn test_asinh() { // let a = FixedTrait::new(66903765733337761105, false); // 3.6268604077773023 // assert_precise(a.asinh(), 2 * ONE, 'invalid two', Option::None(())); // } -// #[test] + // #[test] // #[available_gas(1000000)] // fn test_atanh() { // let a = FixedTrait::new(16602069666338597000, false); // 0.9 diff --git a/src/f64/types/fixed.cairo b/src/f64/types/fixed.cairo index 5e4ef81..b732581 100644 --- a/src/f64/types/fixed.cairo +++ b/src/f64/types/fixed.cairo @@ -93,11 +93,11 @@ impl FixedImpl of FixedTrait { fn from_felt(val: felt252) -> Fixed { let mag = core::integer::u64_try_from_felt252(utils::felt_abs(val)).unwrap(); - return FixedTrait::new(mag, utils::felt_sign(val)); + return Self::new(mag, utils::felt_sign(val)); } fn from_unscaled_felt(val: felt252) -> Fixed { - return FixedTrait::from_felt(val * ONE.into()); + return Self::from_felt(val * ONE.into()); } fn abs(self: Fixed) -> Fixed { @@ -429,10 +429,10 @@ impl FixedAdd of Add { } } -impl FixedAddEq of AddEq { +impl FixedAddAssign of core::ops::AddAssign { #[inline(always)] - fn add_eq(ref self: Fixed, other: Fixed) { - self = Add::add(self, other); + fn add_assign(ref self: Fixed, rhs: Fixed) { + self = Add::add(self, rhs); } } @@ -442,10 +442,10 @@ impl FixedSub of Sub { } } -impl FixedSubEq of SubEq { +impl FixedSubAssign of core::ops::SubAssign { #[inline(always)] - fn sub_eq(ref self: Fixed, other: Fixed) { - self = Sub::sub(self, other); + fn sub_assign(ref self: Fixed, rhs: Fixed) { + self = Sub::sub(self, rhs); } } @@ -455,10 +455,10 @@ impl FixedMul of Mul { } } -impl FixedMulEq of MulEq { +impl FixedMulAssign of core::ops::MulAssign { #[inline(always)] - fn mul_eq(ref self: Fixed, other: Fixed) { - self = Mul::mul(self, other); + fn mul_assign(ref self: Fixed, rhs: Fixed) { + self = Mul::mul(self, rhs); } } @@ -468,10 +468,10 @@ impl FixedDiv of Div { } } -impl FixedDivEq of DivEq { +impl FixedDivAssign of core::ops::DivAssign { #[inline(always)] - fn div_eq(ref self: Fixed, other: Fixed) { - self = Div::div(self, other); + fn div_assign(ref self: Fixed, rhs: Fixed) { + self = Div::div(self, rhs); } } @@ -548,7 +548,7 @@ impl FixedOne of core::num::traits::One { } #[inline(always)] fn is_one(self: @Fixed) -> bool { - *self == FixedOne::one() + *self == Self::one() } #[inline(always)] fn is_non_one(self: @Fixed) -> bool { diff --git a/src/f64/types/vec2.cairo b/src/f64/types/vec2.cairo index 02fbfd2..0b2ec87 100644 --- a/src/f64/types/vec2.cairo +++ b/src/f64/types/vec2.cairo @@ -2,7 +2,7 @@ use core::debug::PrintTrait; use cubit::f64::types::fixed::{Fixed, FixedTrait, FixedPrint}; -#[derive(Copy, Drop, Serde, Store)] +#[derive(Copy, Drop, Serde, starknet::Store)] struct Vec2 { x: Fixed, y: Fixed diff --git a/src/f64/types/vec3.cairo b/src/f64/types/vec3.cairo index ff4652e..291675c 100644 --- a/src/f64/types/vec3.cairo +++ b/src/f64/types/vec3.cairo @@ -2,7 +2,7 @@ use core::debug::PrintTrait; use cubit::f64::types::fixed::{Fixed, FixedTrait, FixedPrint}; -#[derive(Copy, Drop, Serde, Store)] +#[derive(Copy, Drop, Serde, starknet::Store)] struct Vec3 { x: Fixed, y: Fixed, diff --git a/src/f64/types/vec4.cairo b/src/f64/types/vec4.cairo index f618027..f8c3ffe 100644 --- a/src/f64/types/vec4.cairo +++ b/src/f64/types/vec4.cairo @@ -2,7 +2,7 @@ use core::debug::PrintTrait; use cubit::f64::types::fixed::{Fixed, FixedTrait, FixedPrint}; -#[derive(Copy, Drop, Serde, Store)] +#[derive(Copy, Drop, Serde, starknet::Store)] struct Vec4 { x: Fixed, y: Fixed, diff --git a/src/types/vec2.cairo b/src/types/vec2.cairo index 31eb72a..0031d54 100644 --- a/src/types/vec2.cairo +++ b/src/types/vec2.cairo @@ -2,7 +2,7 @@ use debug::PrintTrait; use cubit::types::fixed::{Fixed, FixedTrait, FixedPrint}; -#[derive(Copy, Drop, Serde, Store)] +#[derive(Copy, Drop, Serde, starknet::Store)] struct Vec2 { x: Fixed, y: Fixed diff --git a/src/utils.cairo b/src/utils.cairo index 56dd4d3..f6cc3f8 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -14,7 +14,7 @@ fn felt_sign(a: felt252) -> bool { fn felt_abs(a: felt252) -> felt252 { let a_sign = felt_sign(a); - if (a_sign == true) { + if (a_sign) { return a * -1; } else { return a * 1; @@ -29,11 +29,11 @@ mod tests { fn test_sign() { let min = -1809251394333065606848661391547535052811553607665798349986546028067936010240; let max = 1809251394333065606848661391547535052811553607665798349986546028067936010240; - assert(felt_sign(min) == true, 'invalid result'); - assert(felt_sign(-1) == true, 'invalid result'); - assert(felt_sign(0) == false, 'invalid result'); - assert(felt_sign(1) == false, 'invalid result'); - assert(felt_sign(max) == false, 'invalid result'); + assert(felt_sign(min), 'invalid result'); + assert(felt_sign(-1), 'invalid result'); + assert(!felt_sign(0), 'invalid result'); + assert(!felt_sign(1), 'invalid result'); + assert(!felt_sign(max), 'invalid result'); } #[test]