diff --git a/README.md b/README.md index 804b394..e36522e 100644 --- a/README.md +++ b/README.md @@ -9,13 +9,13 @@ Put this into your Nargo.toml. If you are using Noir: ```toml -nodash = { git = "https://github.com/olehmisar/nodash/", tag = "noir-v0.34.1" } +nodash = { git = "https://github.com/olehmisar/nodash/", tag = "v0.35.0" } ``` If you are using Aztec: ```toml -nodash = { git = "https://github.com/olehmisar/nodash/", tag = "aztec-v0.54.0" } +nodash = { git = "https://github.com/olehmisar/nodash/", tag = "aztec-v0.55.0" } ``` ## Docs @@ -44,6 +44,14 @@ assert(clamp(4 as u64, 1 as u64, 3 as u64) == 3 as u64); assert(clamp(2 as u64, 1 as u64, 3 as u64) == 2 as u64); ``` +### `div_ceil` + +```rs +use nodash::div_ceil; + +assert(div_ceil(10 as u64, 3) == 4); +``` + ### `solidity::encode_with_selector` Equivalent to `abi.encodeWithSelector` in Solidity. diff --git a/src/lib.nr b/src/lib.nr index c0ede4b..84608a1 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -3,7 +3,7 @@ mod solidity; mod string; pub use string::to_hex_string_bytes; -pub use math::{clamp, sqrt::sqrt}; +pub use math::{clamp, div_ceil, sqrt::sqrt}; pub fn array_concat(a: [T; L1], b: [T; L2]) -> [T; L1 + L2] { let mut result = [a[0]; L1 + L2]; diff --git a/src/math.nr b/src/math.nr index 184ff51..57f6e0f 100644 --- a/src/math.nr +++ b/src/math.nr @@ -1,4 +1,8 @@ mod sqrt; +mod numeric; + +use std::{ops::{Add, Div, Rem}, cmp::Eq}; +use numeric::Numeric; pub fn clamp(x: T, min: T, max: T) -> T where T: Ord { if (x < min) { @@ -10,8 +14,17 @@ pub fn clamp(x: T, min: T, max: T) -> T where T: Ord { } } -mod clamp_tests { - use crate::math::clamp; +pub fn div_ceil(a: T, b: T) -> T where T: Add + Div + Rem + Eq + Numeric { + let q = a / b; + if (a % b == T::zero()) { + q + } else { + q + T::one() + } +} + +mod math_tests { + use crate::math::{clamp, div_ceil}; #[test] fn test_clamp() { @@ -22,4 +35,16 @@ mod clamp_tests { // if in range, return value assert(clamp(2 as u64, 1 as u64, 3 as u64) == 2 as u64); } + + #[test] + fn test_div_ceil() { + assert(div_ceil(1 as u64, 2) == 1); + assert(div_ceil(2 as u64, 2) == 1); + assert(div_ceil(3 as u64, 2) == 2); + assert(div_ceil(4 as u64, 2) == 2); + assert(div_ceil(5 as u64, 2) == 3); + assert(div_ceil(1337 as u64, 19) == 71); + assert(div_ceil(1337 as u64, 7) == 191); + assert(div_ceil(10 as u64, 3) == 4); + } } diff --git a/src/math/numeric.nr b/src/math/numeric.nr new file mode 100644 index 0000000..0374d91 --- /dev/null +++ b/src/math/numeric.nr @@ -0,0 +1,50 @@ +// TODO: remove this trait if it is implemented in Noir std lib. +trait Numeric { + fn zero() -> Self; + fn one() -> Self; +} + +impl Numeric for u8 { + fn zero() -> Self { + 0 + } + fn one() -> Self { + 1 + } +} + +impl Numeric for u16 { + fn zero() -> Self { + 0 + } + fn one() -> Self { + 1 + } +} + +impl Numeric for u32 { + fn zero() -> Self { + 0 + } + fn one() -> Self { + 1 + } +} + +impl Numeric for u64 { + fn zero() -> Self { + 0 + } + fn one() -> Self { + 1 + } +} + +impl Numeric for U128 { + fn zero() -> Self { + U128::from_integer(0) + } + fn one() -> Self { + U128::from_integer(1) + } +} diff --git a/src/math/sqrt.nr b/src/math/sqrt.nr index df91b7c..9433ffa 100644 --- a/src/math/sqrt.nr +++ b/src/math/sqrt.nr @@ -1,4 +1,5 @@ use std::{ops::{Mul, Div, Add}, cmp::Ord}; +use crate::math::numeric::Numeric; // TODO: check if this is correct pub fn sqrt(value: T) -> T where T: Numeric + Mul + Ord + Add + Div { @@ -39,48 +40,6 @@ unconstrained fn sqrt_unconstrained(value: T) -> T where T: Numeric + Ord + A } } -// TODO: remove this trait if it is implemented in Noir std lib. -trait Numeric { - fn zero() -> Self; - fn one() -> Self; -} - -impl Numeric for u8 { - fn zero() -> Self { - 0 - } - fn one() -> Self { - 1 - } -} - -impl Numeric for u32 { - fn zero() -> Self { - 0 - } - fn one() -> Self { - 1 - } -} - -impl Numeric for u64 { - fn zero() -> Self { - 0 - } - fn one() -> Self { - 1 - } -} - -impl Numeric for U128 { - fn zero() -> Self { - U128::from_integer(0) - } - fn one() -> Self { - U128::from_integer(1) - } -} - mod sqrt_tests { use crate::math::sqrt::sqrt;