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

feat: Update safe_math and move to libraries #1803

Merged
merged 1 commit into from
Aug 25, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ type = "contract"

[dependencies]
aztec = { path = "../../../../noir-libs/noir-aztec" }
safe_math = { path = "../../../../noir-libs/safe-math" }
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
mod safe_math;
mod interest_math;
use crate::safe_math::SafeU120;
use crate::interest_math::compute_multiplier;
use dep::std::hash::pedersen;
use dep::safe_math::SafeU120;


// Utility used to easily get a "id" for a private user that sits in the same
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod safe_math;
use crate::safe_math::SafeU120;
use dep::safe_math::SafeU120;

// Binomial approximation of exponential
// using lower than decired precisions for everything due to u120 limit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod storage;
mod safe_math;
mod interest_math;
mod helpers;
mod interfaces;
Expand All @@ -23,7 +22,7 @@ contract Lending {
};
use dep::aztec::public_call_stack_item::PublicCallStackItem;
use crate::storage::{Storage, Asset};
use crate::safe_math::SafeU120;
use dep::safe_math::SafeU120;
use crate::interest_math::compute_multiplier;
use crate::helpers::{covered_by_collateral, DebtReturn, debt_updates, debt_value, compute_identifier};
use crate::interfaces::{Token, Lending, PriceFeed};
Expand Down
7 changes: 7 additions & 0 deletions yarn-project/noir-libs/safe-math/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "safe_math"
authors = [""]
compiler_version = "0.1"
type = "lib"

[dependencies]
3 changes: 3 additions & 0 deletions yarn-project/noir-libs/safe-math/src/lib.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod safe_u120;

use crate::safe_u120::SafeU120;
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

struct SafeU120 {
value: u120,
}
Expand Down Expand Up @@ -36,8 +35,9 @@ impl SafeU120 {
b: SafeU120,
) -> SafeU120 {
let c: u120 = self.value * b.value;
// checking for overflows. If both self.value and b are not 0, then c must be greater than or equal self.value.
assert((self.value == 0) | (b.value == 0) | (c >= self.value));
if b.value > 0 {
assert(c / b.value == self.value);
}
SafeU120 {
value: c
}
Expand All @@ -53,8 +53,6 @@ impl SafeU120 {
}
}

// todo: Implement a version that avoids shadow-overflows
// (i.e., overflows by self*b where c would not overflow)
fn mul_div(
self: Self,
b: SafeU120,
Expand All @@ -79,4 +77,6 @@ impl SafeU120 {
}

// todo: implement mul_div with 240 bit intermediate values.
}
}

// Adding test in here is pretty useless as long as noir don't support failings tests.