From dc26d08826d962a9d896850ef6d436dcecad71d8 Mon Sep 17 00:00:00 2001 From: notV4l Date: Fri, 9 Aug 2024 20:56:56 +0200 Subject: [PATCH 1/3] update_to_2.7.0 --- .tool-versions | 1 + Scarb.toml | 4 +++- src/f128/math/ops.cairo | 15 +++++++-------- src/f128/types/fixed.cairo | 33 +++++++++++++++++---------------- src/f128/types/vec2.cairo | 2 +- src/f128/types/vec3.cairo | 2 +- src/f128/types/vec4.cairo | 2 +- src/f64/math/ops.cairo | 10 ++++++---- src/f64/types/fixed.cairo | 31 ++++++++++++++++--------------- src/f64/types/vec2.cairo | 2 +- src/f64/types/vec3.cairo | 2 +- src/f64/types/vec4.cairo | 2 +- src/types/vec2.cairo | 2 +- 13 files changed, 57 insertions(+), 51 deletions(-) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..045dc3e --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +scarb 2.7.0 diff --git a/Scarb.toml b/Scarb.toml index 8321422..a8413d9 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.7.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,5 @@ sierra = false # Enable Sierra codegen. casm = false # Enable CASM codegen. [dependencies] +starknet = "2.7.0" +cairo_test = "2.7.0" \ No newline at end of file diff --git a/src/f128/math/ops.cairo b/src/f128/math/ops.cairo index fd39913..9b99bfe 100644 --- a/src/f128/math/ops.cairo +++ b/src/f128/math/ops.cairo @@ -4,6 +4,7 @@ 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 cubit::f128::math::lut; use cubit::f128::types::fixed::{ @@ -48,8 +49,7 @@ 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 a_u256 = WideMul::::wide_mul(a.mag, ONE_u128); let b_u256 = u256 { low: b.mag, high: 0 }; let res_u256 = a_u256 / b_u256; @@ -186,8 +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 res_u256 = WideMul::::wide_mul(a.mag, b.mag); let ONE_u256 = u256 { low: ONE_u128, high: 0 }; let (scaled_u256, _) = u256_safe_div_rem(res_u256, u256_as_non_zero(ONE_u256)); @@ -204,7 +203,7 @@ struct f64 { } fn mul_64(a: f64, b: f64) -> f64 { - let prod_u128 = integer::u64_wide_mul(a.mag, b.mag); + let prod_u128 = WideMul::::wide_mul(a.mag, b.mag); return f64 { mag: (prod_u128 / 4294967296).try_into().unwrap(), sign: a.sign ^ b.sign }; } @@ -289,8 +288,8 @@ fn round(a: Fixed) -> Fixed { // 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 root = Sqrt::::sqrt(a.mag); + let scale_root = Sqrt::::sqrt(ONE_u128); let res_u128 = upcast(root) * ONE_u128 / upcast(scale_root); return FixedTrait::new(res_u128, false); } @@ -310,7 +309,7 @@ fn _split_unsigned(a: Fixed) -> (u128, u128) { mod tests { use cubit::f128::test::helpers::assert_precise; use cubit::f128::types::fixed::{ - ONE, HALF, FixedPartialEq, FixedPartialOrd, FixedAddEq, FixedSub, FixedSubEq, FixedMulEq + ONE, HALF, FixedPartialEq, FixedPartialOrd, FixedAddAssign, FixedSub, FixedSubAssign, FixedMulAssign }; use cubit::f128::math::trig::HALF_PI_u128; diff --git a/src/f128/types/fixed.cairo b/src/f128/types/fixed.cairo index e3f2bd7..69e4c97 100644 --- a/src/f128/types/fixed.cairo +++ b/src/f128/types/fixed.cairo @@ -4,6 +4,7 @@ use core::integer::{U256DivRem, u256_safe_divmod, u256_as_non_zero, u256_from_fe use core::option::OptionTrait; use core::result::{ResultTrait, ResultTraitImpl}; use core::traits::{TryInto, Into}; +use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign}; use starknet::storage_access::StorePacking; @@ -92,16 +93,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 +431,10 @@ impl FixedAdd of Add { } } -impl FixedAddEq of AddEq { +impl FixedAddAssign of 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 +444,10 @@ impl FixedSub of Sub { } } -impl FixedSubEq of SubEq { +impl FixedSubAssign of 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 +457,10 @@ impl FixedMul of Mul { } } -impl FixedMulEq of MulEq { +impl FixedMulAssign of 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 +470,10 @@ impl FixedDiv of Div { } } -impl FixedDivEq of DivEq { +impl FixedDivAssign of 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 +552,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/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..456ba9c 100644 --- a/src/f64/math/ops.cairo +++ b/src/f64/math/ops.cairo @@ -1,7 +1,8 @@ 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::integer::{u64_safe_divmod, u64_as_non_zero}; +use core::num::traits::{WideMul, Sqrt}; use cubit::f64::math::lut; use cubit::f64::types::fixed::{HALF, ONE, Fixed, FixedIntoFelt252, FixedTrait}; @@ -43,7 +44,8 @@ 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 = core::integer::u64_wide_mul(a.mag, ONE); + let a_u128 = WideMul::::wide_mul(a.mag, ONE); let res_u128 = a_u128 / b.mag.into(); // Re-apply sign @@ -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 = WideMul::::wide_mul(a.mag, b.mag); // Re-apply sign return FixedTrait::new((prod_u128 / ONE.into()).try_into().unwrap(), a.sign ^ b.sign); @@ -269,7 +271,7 @@ fn round(a: Fixed) -> Fixed { // 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()); + let root = Sqrt::::sqrt(a.mag.into() * ONE.into()); return FixedTrait::new(root.into(), false); } diff --git a/src/f64/types/fixed.cairo b/src/f64/types/fixed.cairo index 5e4ef81..a56437f 100644 --- a/src/f64/types/fixed.cairo +++ b/src/f64/types/fixed.cairo @@ -4,6 +4,7 @@ use core::integer::{U128DivRem, u128_as_non_zero}; use core::option::OptionTrait; use core::result::{ResultTrait, ResultTraitImpl}; use core::traits::{TryInto, Into}; +use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign}; use starknet::storage_access::StorePacking; @@ -93,11 +94,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 +430,10 @@ impl FixedAdd of Add { } } -impl FixedAddEq of AddEq { +impl FixedAddAssign of 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 +443,10 @@ impl FixedSub of Sub { } } -impl FixedSubEq of SubEq { +impl FixedSubAssign of 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 +456,10 @@ impl FixedMul of Mul { } } -impl FixedMulEq of MulEq { +impl FixedMulAssign of 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 +469,10 @@ impl FixedDiv of Div { } } -impl FixedDivEq of DivEq { +impl FixedDivEq of 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 +549,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 From 075bf5a317452cd59aaf5bd1c044689220361c66 Mon Sep 17 00:00:00 2001 From: notV4l Date: Fri, 9 Aug 2024 21:01:19 +0200 Subject: [PATCH 2/3] update scarb version --- .github/workflows/test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 46f3500..c3430ea 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -18,7 +18,7 @@ jobs: - name: Setup Cairo toolchain uses: software-mansion/setup-scarb@v1 with: - scarb-version: "2.4.0" + scarb-version: "2.7.0" - name: Run tests run: scarb test From 6bce623d5c735c097be47a4045c0e0432bad38e7 Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Thu, 7 Nov 2024 12:29:39 +0100 Subject: [PATCH 3/3] =?UTF-8?q?=E2=AC=86=EF=B8=8F=20Upgrade=20cairo=20to?= =?UTF-8?q?=202.8.4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test.yaml | 2 +- .tool-versions | 2 +- Scarb.toml | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index c3430ea..fc34e6a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -18,7 +18,7 @@ jobs: - name: Setup Cairo toolchain uses: software-mansion/setup-scarb@v1 with: - scarb-version: "2.7.0" + scarb-version: "2.8.4" - name: Run tests run: scarb test diff --git a/.tool-versions b/.tool-versions index 045dc3e..ff54753 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -scarb 2.7.0 +scarb 2.8.4 diff --git a/Scarb.toml b/Scarb.toml index a8413d9..78a6f87 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -1,7 +1,7 @@ [package] name = "cubit" version = "1.3.0" -cairo-version = "^2.7.0" +cairo-version = "^2.8.4" edition = "2023_10" description = "Math library in Cairo using a 64.64 fixed point representation" homepage = "https://github.com/influenceth/cubit" @@ -11,5 +11,5 @@ sierra = false # Enable Sierra codegen. casm = false # Enable CASM codegen. [dependencies] -starknet = "2.7.0" -cairo_test = "2.7.0" \ No newline at end of file +starknet = "2.8.4" +cairo_test = "2.8.4"