Skip to content

Commit

Permalink
Merge pull request #203 from gizatechxyz/develop
Browse files Browse the repository at this point in the history
Merge Develop into Main
  • Loading branch information
raphaelDkhn authored Aug 24, 2023
2 parents 3b03305 + 2a73cda commit 75fd5ef
Show file tree
Hide file tree
Showing 250 changed files with 2,197 additions and 2,143 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[![GitHub Workflow Status](https://github.com/gizatechxyz/orion/actions/workflows/test.yaml/badge.svg)](https://github.com/gizatechxyz/orion/actions/workflows/test.yaml)
[![Project license](https://img.shields.io/github/license/gizatechxyz/orion.svg?style=flat-square)](LICENSE)
[![Pull Requests welcome](https://img.shields.io/badge/PRs-welcome-ff69b4.svg?style=flat-square)](https://github.com/gizatechxyz/orion/issues?q=is%3Aissue+is%3Aopen)
[![Join the community](https://dcbadge.vercel.app/api/server/FR3Cd88x6r?style=flat-square)](https://discord.gg/FR3Cd88x6r)
[![Join the community](https://dcbadge.vercel.app/api/server/kvqVYbCpU3?style=flat&compact=true)](https://discord.gg/kvqVYbCpU3)
</div>

# Orion: An Open-source Framework for Validity and ZK ML ✨
Expand Down Expand Up @@ -46,7 +46,7 @@ For a detailed list of changes, please refer to the [CHANGELOG](https://github.c

## 💖 Join the community!

Join the community and help build a safer and transparent AI in our [Discord](https://discord.gg/Kt24CsMb5k)!
Join the community and help build a safer and transparent AI in our [Discord](https://discord.gg/kvqVYbCpU3)!

## ✍️ Authors & contributors

Expand Down
3 changes: 3 additions & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ version = "0.1.0"
description = "ONNX Runtime in Cairo for verifiable ML inference using STARK"
homepage = "https://github.com/gizatechxyz/orion"

[dependencies]
alexandria_data_structures = { git = "https://github.com/keep-starknet-strange/alexandria.git" }

[scripts]
sierra = "cairo-compile . -r"
docgen = "cd docgen && cargo run"
Expand Down
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] - 2023-08-24

## Changed
Refactored Loops to use match to break loops.

## [Unreleased] - 2023-08-16

## Changed
Expand Down
26 changes: 17 additions & 9 deletions src/numbers/signed_integer/i128.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn i128_add(a: i128, b: i128) -> i128 {
if (sum == 0_u128) {
return IntegerTrait::new(sum, false);
}
return IntegerTrait::new(sum, a.sign);
return ensure_non_negative_zero(sum, a.sign);
} else {
// If the integers have different signs,
// the larger absolute value is subtracted from the smaller one.
Expand All @@ -212,7 +212,7 @@ fn i128_add(a: i128, b: i128) -> i128 {
if (difference == 0_u128) {
return IntegerTrait::new(difference, false);
}
return IntegerTrait::new(difference, larger.sign);
return ensure_non_negative_zero(difference, larger.sign);
}
}

Expand All @@ -231,7 +231,7 @@ fn i128_sub(a: i128, b: i128) -> i128 {
}

// The subtraction of `a` to `b` is achieved by negating `b` sign and adding it to `a`.
let neg_b = IntegerTrait::new(b.mag, !b.sign);
let neg_b = ensure_non_negative_zero(b.mag, !b.sign);
return a + neg_b;
}

Expand All @@ -258,7 +258,7 @@ fn i128_mul(a: i128, b: i128) -> i128 {
return IntegerTrait::new(mag, false);
}

return IntegerTrait::new(mag, sign);
return ensure_non_negative_zero(mag, sign);
}

// Divides the first i128 by the second i128.
Expand All @@ -277,7 +277,7 @@ fn i128_div(a: i128, b: i128) -> i128 {

if (sign == false) {
// If the operands are positive, the quotient is simply their absolute value quotient.
return IntegerTrait::new(a.mag / b.mag, sign);
return ensure_non_negative_zero(a.mag / b.mag, sign);
}

// If the operands have different signs, rounding is necessary.
Expand All @@ -287,7 +287,7 @@ fn i128_div(a: i128, b: i128) -> i128 {
if (quotient == 0_u128) {
return IntegerTrait::new(quotient, false);
}
return IntegerTrait::new(quotient, sign);
return ensure_non_negative_zero(quotient, sign);
}

// If the quotient is not an integer, multiply the dividend by 10 to move the decimal point over.
Expand All @@ -300,9 +300,9 @@ fn i128_div(a: i128, b: i128) -> i128 {

// Check the last digit to determine rounding direction.
if (last_digit <= 5_u128) {
return IntegerTrait::new(quotient / 10_u128, sign);
return ensure_non_negative_zero(quotient / 10_u128, sign);
} else {
return IntegerTrait::new((quotient / 10_u128) + 1_u128, sign);
return ensure_non_negative_zero((quotient / 10_u128) + 1_u128, sign);
}
}

Expand Down Expand Up @@ -426,7 +426,7 @@ fn i128_ge(a: i128, b: i128) -> bool {
// * `i128` - The negation of `x`.
fn i128_neg(x: i128) -> i128 {
// The negation of an integer is obtained by flipping its sign.
return IntegerTrait::new(x.mag, !x.sign);
return ensure_non_negative_zero(x.mag, !x.sign);
}

/// Cf: IntegerTrait::abs docstring
Expand All @@ -451,3 +451,11 @@ fn i128_min(a: i128, b: i128) -> i128 {
return b;
}
}

fn ensure_non_negative_zero(mag: u128, sign: bool) -> i128 {
if mag == 0 {
IntegerTrait::<i128>::new(mag, false)
} else {
IntegerTrait::<i128>::new(mag, sign)
}
}
26 changes: 17 additions & 9 deletions src/numbers/signed_integer/i16.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn i16_add(a: i16, b: i16) -> i16 {
if (sum == 0_u16) {
return IntegerTrait::new(sum, false);
}
return IntegerTrait::new(sum, a.sign);
return ensure_non_negative_zero(sum, a.sign);
} else {
// If the integers have different signs,
// the larger absolute value is subtracted from the smaller one.
Expand All @@ -212,7 +212,7 @@ fn i16_add(a: i16, b: i16) -> i16 {
if (difference == 0_u16) {
return IntegerTrait::new(difference, false);
}
return IntegerTrait::new(difference, larger.sign);
return ensure_non_negative_zero(difference, larger.sign);
}
}

Expand All @@ -231,7 +231,7 @@ fn i16_sub(a: i16, b: i16) -> i16 {
}

// The subtraction of `a` to `b` is achieved by negating `b` sign and adding it to `a`.
let neg_b = IntegerTrait::new(b.mag, !b.sign);
let neg_b = ensure_non_negative_zero(b.mag, !b.sign);
return a + neg_b;
}

Expand All @@ -258,7 +258,7 @@ fn i16_mul(a: i16, b: i16) -> i16 {
return IntegerTrait::new(mag, false);
}

return IntegerTrait::new(mag, sign);
return ensure_non_negative_zero(mag, sign);
}

// Divides the first i16 by the second i16.
Expand All @@ -277,7 +277,7 @@ fn i16_div(a: i16, b: i16) -> i16 {

if (sign == false) {
// If the operands are positive, the quotient is simply their absolute value quotient.
return IntegerTrait::new(a.mag / b.mag, sign);
return ensure_non_negative_zero(a.mag / b.mag, sign);
}

// If the operands have different signs, rounding is necessary.
Expand All @@ -287,7 +287,7 @@ fn i16_div(a: i16, b: i16) -> i16 {
if (quotient == 0_u16) {
return IntegerTrait::new(quotient, false);
}
return IntegerTrait::new(quotient, sign);
return ensure_non_negative_zero(quotient, sign);
}

// If the quotient is not an integer, multiply the dividend by 10 to move the decimal point over.
Expand All @@ -300,9 +300,9 @@ fn i16_div(a: i16, b: i16) -> i16 {

// Check the last digit to determine rounding direction.
if (last_digit <= 5_u16) {
return IntegerTrait::new(quotient / 10_u16, sign);
return ensure_non_negative_zero(quotient / 10_u16, sign);
} else {
return IntegerTrait::new((quotient / 10_u16) + 1_u16, sign);
return ensure_non_negative_zero((quotient / 10_u16) + 1_u16, sign);
}
}

Expand Down Expand Up @@ -426,7 +426,7 @@ fn i16_ge(a: i16, b: i16) -> bool {
// * `i16` - The negation of `x`.
fn i16_neg(x: i16) -> i16 {
// The negation of an integer is obtained by flipping its sign.
return IntegerTrait::new(x.mag, !x.sign);
return ensure_non_negative_zero(x.mag, !x.sign);
}

/// Cf: IntegerTrait::abs docstring
Expand All @@ -451,3 +451,11 @@ fn i16_min(a: i16, b: i16) -> i16 {
return b;
}
}

fn ensure_non_negative_zero(mag: u16, sign: bool) -> i16 {
if mag == 0 {
IntegerTrait::<i16>::new(mag, false)
} else {
IntegerTrait::<i16>::new(mag, sign)
}
}
28 changes: 18 additions & 10 deletions src/numbers/signed_integer/i32.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn i32_add(a: i32, b: i32) -> i32 {
if (sum == 0) {
return IntegerTrait::new(sum, false);
}
return IntegerTrait::new(sum, a.sign);
return ensure_non_negative_zero(sum, a.sign);
} else {
// If the integers have different signs,
// the larger absolute value is subtracted from the smaller one.
Expand All @@ -220,7 +220,7 @@ fn i32_add(a: i32, b: i32) -> i32 {
if (difference == 0) {
return IntegerTrait::new(difference, false);
}
return IntegerTrait::new(difference, larger.sign);
return ensure_non_negative_zero(difference, larger.sign);
}
}

Expand All @@ -239,7 +239,7 @@ fn i32_sub(a: i32, b: i32) -> i32 {
}

// The subtraction of `a` to `b` is achieved by negating `b` sign and adding it to `a`.
let neg_b = IntegerTrait::new(b.mag, !b.sign);
let neg_b = ensure_non_negative_zero(b.mag, !b.sign);
return a + neg_b;
}

Expand All @@ -266,7 +266,7 @@ fn i32_mul(a: i32, b: i32) -> i32 {
return IntegerTrait::new(mag, false);
}

return IntegerTrait::new(mag, sign);
return ensure_non_negative_zero(mag, sign);
}

// Divides the first i32 by the second i32.
Expand All @@ -285,7 +285,7 @@ fn i32_div(a: i32, b: i32) -> i32 {

if (sign == false) {
// If the operands are positive, the quotient is simply their absolute value quotient.
return IntegerTrait::new(a.mag / b.mag, sign);
return ensure_non_negative_zero(a.mag / b.mag, sign);
}

// If the operands have different signs, rounding is necessary.
Expand All @@ -295,22 +295,22 @@ fn i32_div(a: i32, b: i32) -> i32 {
if (quotient == 0) {
return IntegerTrait::new(quotient, false);
}
return IntegerTrait::new(quotient, sign);
return ensure_non_negative_zero(quotient, sign);
}

// If the quotient is not an integer, multiply the dividend by 10 to move the decimal point over.
let quotient = (a.mag * 10) / b.mag;
let last_digit = quotient % 10;

if (quotient == 0) {
return IntegerTrait::new(quotient, false);
return ensure_non_negative_zero(quotient, false);
}

// Check the last digit to determine rounding direction.
if (last_digit <= 5) {
return IntegerTrait::new(quotient / 10, sign);
return ensure_non_negative_zero(quotient / 10, sign);
} else {
return IntegerTrait::new((quotient / 10) + 1, sign);
return ensure_non_negative_zero((quotient / 10) + 1, sign);
}
}

Expand Down Expand Up @@ -434,7 +434,7 @@ fn i32_ge(a: i32, b: i32) -> bool {
// * `i32` - The negation of `x`.
fn i32_neg(x: i32) -> i32 {
// The negation of an integer is obtained by flipping its sign.
return IntegerTrait::new(x.mag, !x.sign);
return ensure_non_negative_zero(x.mag, !x.sign);
}

/// Cf: IntegerTrait::abs docstring
Expand Down Expand Up @@ -482,3 +482,11 @@ fn i8_try_from_i32(x: i32) -> Option<i8> {
Option::None(_) => Option::None(())
}
}

fn ensure_non_negative_zero(mag: u32, sign: bool) -> i32 {
if mag == 0 {
IntegerTrait::<i32>::new(mag, false)
} else {
IntegerTrait::<i32>::new(mag, sign)
}
}
26 changes: 17 additions & 9 deletions src/numbers/signed_integer/i64.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ fn i64_add(a: i64, b: i64) -> i64 {
if (sum == 0_u64) {
return IntegerTrait::new(sum, false);
}
return IntegerTrait::new(sum, a.sign);
return ensure_non_negative_zero(sum, a.sign);
} else {
// If the integers have different signs,
// the larger absolute value is subtracted from the smaller one.
Expand All @@ -212,7 +212,7 @@ fn i64_add(a: i64, b: i64) -> i64 {
if (difference == 0_u64) {
return IntegerTrait::new(difference, false);
}
return IntegerTrait::new(difference, larger.sign);
return ensure_non_negative_zero(difference, larger.sign);
}
}

Expand All @@ -231,7 +231,7 @@ fn i64_sub(a: i64, b: i64) -> i64 {
}

// The subtraction of `a` to `b` is achieved by negating `b` sign and adding it to `a`.
let neg_b = IntegerTrait::new(b.mag, !b.sign);
let neg_b = ensure_non_negative_zero(b.mag, !b.sign);
return a + neg_b;
}

Expand All @@ -258,7 +258,7 @@ fn i64_mul(a: i64, b: i64) -> i64 {
return IntegerTrait::new(mag, false);
}

return IntegerTrait::new(mag, sign);
return ensure_non_negative_zero(mag, sign);
}

// Divides the first i64 by the second i64.
Expand All @@ -277,7 +277,7 @@ fn i64_div(a: i64, b: i64) -> i64 {

if (sign == false) {
// If the operands are positive, the quotient is simply their absolute value quotient.
return IntegerTrait::new(a.mag / b.mag, sign);
return ensure_non_negative_zero(a.mag / b.mag, sign);
}

// If the operands have different signs, rounding is necessary.
Expand All @@ -287,7 +287,7 @@ fn i64_div(a: i64, b: i64) -> i64 {
if (quotient == 0_u64) {
return IntegerTrait::new(quotient, false);
}
return IntegerTrait::new(quotient, sign);
return ensure_non_negative_zero(quotient, sign);
}

// If the quotient is not an integer, multiply the dividend by 10 to move the decimal point over.
Expand All @@ -300,9 +300,9 @@ fn i64_div(a: i64, b: i64) -> i64 {

// Check the last digit to determine rounding direction.
if (last_digit <= 5_u64) {
return IntegerTrait::new(quotient / 10_u64, sign);
return ensure_non_negative_zero(quotient / 10_u64, sign);
} else {
return IntegerTrait::new((quotient / 10_u64) + 1_u64, sign);
return ensure_non_negative_zero((quotient / 10_u64) + 1_u64, sign);
}
}

Expand Down Expand Up @@ -426,7 +426,7 @@ fn i64_ge(a: i64, b: i64) -> bool {
// * `i64` - The negation of `x`.
fn i64_neg(x: i64) -> i64 {
// The negation of an integer is obtained by flipping its sign.
return IntegerTrait::new(x.mag, !x.sign);
return ensure_non_negative_zero(x.mag, !x.sign);
}

/// Cf: IntegerTrait::abs docstring
Expand All @@ -451,3 +451,11 @@ fn i64_min(a: i64, b: i64) -> i64 {
return b;
}
}

fn ensure_non_negative_zero(mag: u64, sign: bool) -> i64 {
if mag == 0 {
IntegerTrait::<i64>::new(mag, false)
} else {
IntegerTrait::<i64>::new(mag, sign)
}
}
Loading

0 comments on commit 75fd5ef

Please sign in to comment.