Skip to content

Commit

Permalink
feat: div_ceil
Browse files Browse the repository at this point in the history
  • Loading branch information
olehmisar committed Oct 17, 2024
1 parent 64d01b1 commit 893f5df
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 47 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, let L1: u32, let L2: u32>(a: [T; L1], b: [T; L2]) -> [T; L1 + L2] {
let mut result = [a[0]; L1 + L2];
Expand Down
29 changes: 27 additions & 2 deletions src/math.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
mod sqrt;
mod numeric;

use std::{ops::{Add, Div, Rem}, cmp::Eq};
use numeric::Numeric;

pub fn clamp<T>(x: T, min: T, max: T) -> T where T: Ord {
if (x < min) {
Expand All @@ -10,8 +14,17 @@ pub fn clamp<T>(x: T, min: T, max: T) -> T where T: Ord {
}
}

mod clamp_tests {
use crate::math::clamp;
pub fn div_ceil<T>(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() {
Expand All @@ -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);
}
}
50 changes: 50 additions & 0 deletions src/math/numeric.nr
Original file line number Diff line number Diff line change
@@ -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)
}
}
43 changes: 1 addition & 42 deletions src/math/sqrt.nr
Original file line number Diff line number Diff line change
@@ -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<T>(value: T) -> T where T: Numeric + Mul + Ord + Add + Div {
Expand Down Expand Up @@ -39,48 +40,6 @@ unconstrained fn sqrt_unconstrained<T>(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;

Expand Down

0 comments on commit 893f5df

Please sign in to comment.