Skip to content

Commit

Permalink
feat: Update safe_math and move to libraries (#1803)
Browse files Browse the repository at this point in the history
Fixes #1794 and address the wrong check in `mul`. 

Would prefer to add tests directly, but noir don't support failing tests
in noir yet, so there is really no good reason to do that currently. See
noir-lang/noir#1994
  • Loading branch information
LHerskind authored Aug 25, 2023
1 parent 249765e commit b10656d
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
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.

0 comments on commit b10656d

Please sign in to comment.