Skip to content

Commit

Permalink
migrate orion-numbers code
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelDkhn committed Jun 16, 2024
1 parent 1cc0c12 commit 0cc5102
Show file tree
Hide file tree
Showing 43 changed files with 3,231 additions and 2 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 4 additions & 1 deletion packages/orion-graph/zk-backend/cairo/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ edition = "2023_11"

[scripts]
test = "scarb cairo-test -f test"
nodegen = "python3 nodegen/node/__init__.py"


[cairo]
enable-gas=false
2 changes: 1 addition & 1 deletion packages/orion-ml/.tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 2.6.4
scarb nightly-2024-06-10
1 change: 1 addition & 0 deletions packages/orion-numbers/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scarb nightly-2024-06-10
14 changes: 14 additions & 0 deletions packages/orion-numbers/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "orion_numbers"
version = "0.1.0"
edition = "2023_11"

# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html

[dependencies]

[scripts]
test = "scarb cairo-test -f test"

[cairo]
enable-gas=false
6 changes: 6 additions & 0 deletions packages/orion-numbers/src/f16x16.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod core;
pub mod math;
pub mod trig;
pub mod erf;
pub mod helpers;
pub mod lut;
213 changes: 213 additions & 0 deletions packages/orion-numbers/src/f16x16/core.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
use orion_numbers::f16x16::{math, trig, erf};
use core::traits::TryInto;
use core::option::OptionTrait;
use core::traits::Mul;

pub type f16x16 = i32;

// CONSTANTS
pub const TWO: f16x16 = 131072; // 2 ** 17
pub const ONE: f16x16 = 65536; // 2 ** 16
pub const HALF: f16x16 = 32768; // 2 ** 15
pub const MAX: f16x16 = 2147483647; // 2 ** 31 -1
pub const MIN: f16x16 = -2147483648; // 2 ** 31

#[generate_trait]
pub impl f16x16Impl of FixedTrait {
fn ZERO() -> f16x16 {
0
}

fn HALF() -> f16x16 {
HALF
}

fn ONE() -> f16x16 {
ONE
}

fn MAX() -> f16x16 {
MAX
}

fn MIN() -> f16x16 {
MIN
}

fn new_unscaled(x: i32) -> f16x16 {
x * ONE
}

fn new(x: i32) -> f16x16 {
x
}

fn from_felt(x: felt252) -> f16x16 {
x.try_into().unwrap()
}

fn from_unscaled_felt(x: felt252) -> f16x16 {
return Self::from_felt(x * ONE.into());
}

fn abs(self: f16x16) -> f16x16 {
math::abs(self)
}

fn acos(self: f16x16) -> f16x16 {
trig::acos_fast(self)
}

fn acosh(self: f16x16) -> f16x16 {
trig::acosh(self)
}

fn asin(self: f16x16) -> f16x16 {
trig::asin_fast(self)
}

fn asinh(self: f16x16) -> f16x16 {
trig::asinh(self)
}

fn atan(self: f16x16) -> f16x16 {
trig::atan_fast(self)
}

fn atanh(self: f16x16) -> f16x16 {
trig::atanh(self)
}

fn add(lhs: f16x16, rhs: f16x16) -> f16x16 {
math::add(lhs, rhs)
}

fn ceil(self: f16x16) -> f16x16 {
math::ceil(self)
}

fn cos(self: f16x16) -> f16x16 {
trig::cos_fast(self)
}

fn cosh(self: f16x16) -> f16x16 {
trig::cosh(self)
}

fn div(lhs: f16x16, rhs: f16x16) -> f16x16 {
math::div(lhs, rhs)
}

// Calculates the natural exponent of x: e^x
fn exp(self: f16x16) -> f16x16 {
math::exp(self)
}

// Calculates the binary exponent of x: 2^x
fn exp2(self: f16x16) -> f16x16 {
math::exp2(self)
}

fn floor(self: f16x16) -> f16x16 {
math::floor(self)
}

// Calculates the natural logarithm of x: ln(x)
// self must be greater than zero
fn ln(self: f16x16) -> f16x16 {
math::ln(self)
}

// Calculates the binary logarithm of x: log2(x)
// self must be greather than zero
fn log2(self: f16x16) -> f16x16 {
math::log2(self)
}

// Calculates the base 10 log of x: log10(x)
// self must be greater than zero
fn log10(self: f16x16) -> f16x16 {
math::log10(self)
}

fn mul(lhs: f16x16, rhs: f16x16) -> f16x16 {
math::mul(lhs, rhs)
}

// Calclates the value of x^y and checks for overflow before returning
// self is a fixed point value
// b is a fixed point value
fn pow(self: f16x16, b: f16x16) -> f16x16 {
math::pow(self, b)
}

fn round(self: f16x16) -> f16x16 {
math::round(self)
}

fn sin(self: f16x16) -> f16x16 {
trig::sin_fast(self)
}

fn sinh(self: f16x16) -> f16x16 {
trig::sinh(self)
}

// Calculates the square root of a fixed point value
// x must be positive
fn sqrt(self: f16x16) -> f16x16 {
math::sqrt(self)
}

fn tan(self: f16x16) -> f16x16 {
trig::tan_fast(self)
}

fn tanh(self: f16x16) -> f16x16 {
trig::tanh(self)
}

fn sign(self: f16x16) -> f16x16 {
math::sign(self)
}

fn sub(lhs: f16x16, rhs: f16x16) -> f16x16 {
math::sub(lhs, rhs)
}

fn NaN() -> f16x16 {
-0
}

fn is_nan(self: f16x16) -> bool {
self == -0
}

fn INF() -> f16x16 {
MAX
}

fn POS_INF() -> f16x16 {
MAX
}

fn NEG_INF() -> f16x16 {
MIN
}

fn is_inf(self: f16x16) -> bool {
self == MAX
}

fn is_pos_inf(self: f16x16) -> bool {
self == MAX
}

fn is_neg_inf(self: f16x16) -> bool {
self == MIN
}

fn erf(self: f16x16) -> f16x16 {
erf::erf(self)
}
}
66 changes: 66 additions & 0 deletions packages/orion-numbers/src/f16x16/erf.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use orion_numbers::f16x16::{core::{FixedTrait, f16x16, ONE}, lut};

const ERF_COMPUTATIONAL_ACCURACY: i32 = 100;
const ROUND_CHECK_NUMBER: i32 = 10;
// Values > MAX_ERF_NUMBER return 1
const MAX_ERF_NUMBER: i32 = 229376;
// Values <= ERF_TRUNCATION_NUMBER -> two decimal places, and values > ERF_TRUNCATION_NUMBER -> one
// decimal place
const ERF_TRUNCATION_NUMBER: i32 = 131072;

pub fn erf(x: f16x16) -> f16x16 {
// Lookup
// 1. if x.mag < 3.5 { lookup table }
// 2. else{ return 1}
let mut erf_value: i32 = 0;

if x.abs() < MAX_ERF_NUMBER {
erf_value = lut::erf_lut(x.abs());
} else {
erf_value = ONE;
}

FixedTrait::mul(erf_value, x.sign())
}


// Tests
//
//
// --------------------------------------------------------------------------------------------------------------

#[cfg(test)]
mod tests {
use super::{erf, f16x16};

#[test]
#[available_gas(1000000000)]
fn test_erf() {
// 1.0
let f1 = 65536;
// 0.134
let f2 = 8832;
// 0.520
let f3 = 34078;
// 2.0
let f4 = 131072;
// 3.5
let f5 = 229376;
// 5.164
let f6 = 338428;

let f1_erf = erf(f1);
let f2_erf = erf(f2);
let f3_erf = erf(f3);
let f4_erf = erf(f4);
let f5_erf = erf(f5);
let f6_erf = erf(f6);

assert(f1_erf == 55227, 'f1_erf it works!');
assert(f2_erf == 10285, 'f2_erf it works!');
assert(f3_erf == 35251, 'f3_erf it works!');
assert(f4_erf == 65229, 'f4_erf it works!');
assert(f5_erf == 65536, 'f5_erf it works!');
assert(f6_erf == 65536, 'f6_erf it works!');
}
}
39 changes: 39 additions & 0 deletions packages/orion-numbers/src/f16x16/helpers.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use orion_numbers::f16x16::core::{FixedTrait, f16x16, ONE, HALF};

const DEFAULT_PRECISION: i32 = 7; // 1e-4

// To use `DEFAULT_PRECISION`, final arg is: `Option::None(())`.
// To use `custom_precision` of 430_i32: `Option::Some(430_i32)`.
pub fn assert_precise(
result: f16x16, expected: felt252, msg: felt252, custom_precision: Option<i32>
) {
let precision = match custom_precision {
Option::Some(val) => val,
Option::None => DEFAULT_PRECISION,
};

let diff = (result - FixedTrait::from_felt(expected));

if (diff > precision) {
println!("{}", result);
assert(diff <= precision, msg);
}
}

pub fn assert_relative(
result: f16x16, expected: felt252, msg: felt252, custom_precision: Option<i32>
) {
let precision = match custom_precision {
Option::Some(val) => val,
Option::None => DEFAULT_PRECISION,
};

let diff = result - FixedTrait::from_felt(expected);
let rel_diff = diff / result;

if (rel_diff > precision) {
println!("{}", result);
assert(rel_diff <= precision, msg);
}
}

Loading

0 comments on commit 0cc5102

Please sign in to comment.