diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/Nargo.toml b/yarn-project/noir-contracts/src/contracts/lending_contract/Nargo.toml index 8de71f3fdf10..4efdd16534d9 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/Nargo.toml +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/Nargo.toml @@ -6,3 +6,4 @@ type = "contract" [dependencies] aztec = { path = "../../../../noir-libs/noir-aztec" } +safe_math = { path = "../../../../noir-libs/safe-math" } \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/helpers.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/helpers.nr index e0fe01fc1a17..cee8d55fc1ce 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/helpers.nr +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/helpers.nr @@ -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 diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/interest_math.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/interest_math.nr index aae8b474c6e0..e6bcadd9b56f 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/interest_math.nr +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/interest_math.nr @@ -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 diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr b/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr index d93fabe44241..e2586cb51cd3 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr +++ b/yarn-project/noir-contracts/src/contracts/lending_contract/src/main.nr @@ -1,5 +1,4 @@ mod storage; -mod safe_math; mod interest_math; mod helpers; mod interfaces; @@ -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}; diff --git a/yarn-project/noir-libs/safe-math/Nargo.toml b/yarn-project/noir-libs/safe-math/Nargo.toml new file mode 100644 index 000000000000..02c6607bfb36 --- /dev/null +++ b/yarn-project/noir-libs/safe-math/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "safe_math" +authors = [""] +compiler_version = "0.1" +type = "lib" + +[dependencies] \ No newline at end of file diff --git a/yarn-project/noir-libs/safe-math/src/lib.nr b/yarn-project/noir-libs/safe-math/src/lib.nr new file mode 100644 index 000000000000..f8f829b67e39 --- /dev/null +++ b/yarn-project/noir-libs/safe-math/src/lib.nr @@ -0,0 +1,3 @@ +mod safe_u120; + +use crate::safe_u120::SafeU120; \ No newline at end of file diff --git a/yarn-project/noir-contracts/src/contracts/lending_contract/src/safe_math.nr b/yarn-project/noir-libs/safe-math/src/safe_u120.nr similarity index 82% rename from yarn-project/noir-contracts/src/contracts/lending_contract/src/safe_math.nr rename to yarn-project/noir-libs/safe-math/src/safe_u120.nr index d4b2e6326dd3..1778a15f312a 100644 --- a/yarn-project/noir-contracts/src/contracts/lending_contract/src/safe_math.nr +++ b/yarn-project/noir-libs/safe-math/src/safe_u120.nr @@ -1,4 +1,3 @@ - struct SafeU120 { value: u120, } @@ -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 } @@ -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, @@ -79,4 +77,6 @@ impl SafeU120 { } // todo: implement mul_div with 240 bit intermediate values. -} \ No newline at end of file +} + +// Adding test in here is pretty useless as long as noir don't support failings tests. \ No newline at end of file