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

Cairo 2.8.4 #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb 2.8.4
4 changes: 3 additions & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "cubit"
version = "1.3.0"
cairo-version = ">=2.4.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"
Expand All @@ -11,3 +11,5 @@ sierra = false # Enable Sierra codegen.
casm = false # Enable CASM codegen.

[dependencies]
starknet = "2.8.4"
cairo_test = "2.8.4"
15 changes: 7 additions & 8 deletions src/f128/math/ops.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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::<u128,u128>::wide_mul(a.mag, ONE_u128);
let b_u256 = u256 { low: b.mag, high: 0 };
let res_u256 = a_u256 / b_u256;

Expand Down Expand Up @@ -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::<u128,u128>::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));

Expand All @@ -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::<u64,u64>::wide_mul(a.mag, b.mag);
return f64 { mag: (prod_u128 / 4294967296).try_into().unwrap(), sign: a.sign ^ b.sign };
}

Expand Down Expand Up @@ -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::<u128>::sqrt(a.mag);
let scale_root = Sqrt::<u128>::sqrt(ONE_u128);
let res_u128 = upcast(root) * ONE_u128 / upcast(scale_root);
return FixedTrait::new(res_u128, false);
}
Expand All @@ -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;
Expand Down
33 changes: 17 additions & 16 deletions src/f128/types/fixed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -430,10 +431,10 @@ impl FixedAdd of Add<Fixed> {
}
}

impl FixedAddEq of AddEq<Fixed> {
impl FixedAddAssign of AddAssign<Fixed, Fixed> {
#[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);
}
}

Expand All @@ -443,10 +444,10 @@ impl FixedSub of Sub<Fixed> {
}
}

impl FixedSubEq of SubEq<Fixed> {
impl FixedSubAssign of SubAssign<Fixed, Fixed> {
#[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);
}
}

Expand All @@ -456,10 +457,10 @@ impl FixedMul of Mul<Fixed> {
}
}

impl FixedMulEq of MulEq<Fixed> {
impl FixedMulAssign of MulAssign<Fixed, Fixed> {
#[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);
}
}

Expand All @@ -469,10 +470,10 @@ impl FixedDiv of Div<Fixed> {
}
}

impl FixedDivEq of DivEq<Fixed> {
impl FixedDivAssign of DivAssign<Fixed, Fixed> {
#[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);
}
}

Expand Down Expand Up @@ -551,7 +552,7 @@ impl FixedOne of core::num::traits::One<Fixed> {
}
#[inline(always)]
fn is_one(self: @Fixed) -> bool {
*self == FixedOne::one()
*self == Self::one()
}
#[inline(always)]
fn is_non_one(self: @Fixed) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/f128/types/vec2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/f128/types/vec3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/f128/types/vec4.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 6 additions & 4 deletions src/f64/math/ops.cairo
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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::<u64, u64>::wide_mul(a.mag, ONE);
let res_u128 = a_u128 / b.mag.into();

// Re-apply sign
Expand Down Expand Up @@ -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::<u64,u64>::wide_mul(a.mag, b.mag);

// Re-apply sign
return FixedTrait::new((prod_u128 / ONE.into()).try_into().unwrap(), a.sign ^ b.sign);
Expand Down Expand Up @@ -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::<u128>::sqrt(a.mag.into() * ONE.into());
return FixedTrait::new(root.into(), false);
}

Expand Down
31 changes: 16 additions & 15 deletions src/f64/types/fixed.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -429,10 +430,10 @@ impl FixedAdd of Add<Fixed> {
}
}

impl FixedAddEq of AddEq<Fixed> {
impl FixedAddAssign of AddAssign<Fixed,Fixed> {
#[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);
}
}

Expand All @@ -442,10 +443,10 @@ impl FixedSub of Sub<Fixed> {
}
}

impl FixedSubEq of SubEq<Fixed> {
impl FixedSubAssign of SubAssign<Fixed,Fixed> {
#[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);
}
}

Expand All @@ -455,10 +456,10 @@ impl FixedMul of Mul<Fixed> {
}
}

impl FixedMulEq of MulEq<Fixed> {
impl FixedMulAssign of MulAssign<Fixed, Fixed> {
#[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);
}
}

Expand All @@ -468,10 +469,10 @@ impl FixedDiv of Div<Fixed> {
}
}

impl FixedDivEq of DivEq<Fixed> {
impl FixedDivEq of DivAssign<Fixed, Fixed> {
#[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);
}
}

Expand Down Expand Up @@ -548,7 +549,7 @@ impl FixedOne of core::num::traits::One<Fixed> {
}
#[inline(always)]
fn is_one(self: @Fixed) -> bool {
*self == FixedOne::one()
*self == Self::one()
}
#[inline(always)]
fn is_non_one(self: @Fixed) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/f64/types/vec2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/f64/types/vec3.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/f64/types/vec4.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/types/vec2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down