Skip to content

Commit 3dfa5c7

Browse files
committed
refactor: use latest noir gadgets
1 parent 077f118 commit 3dfa5c7

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Put this into your Nargo.toml.
99
If you are using Noir:
1010

1111
```toml
12-
nodash = { git = "https://github.com/olehmisar/nodash/", tag = "noir-v0.33.0" }
12+
nodash = { git = "https://github.com/olehmisar/nodash/", tag = "noir-v0.34.1" }
1313
```
1414

1515
If you are using Aztec:
@@ -48,8 +48,6 @@ assert(clamp(2 as u64, 1 as u64, 3 as u64) == 2 as u64);
4848

4949
Equivalent to `abi.encodeWithSelector` in Solidity.
5050

51-
_Note: due to Noir limitations, you have to pass the result array as the last argument. The length of the array should be `4 + N * 32` where `N` is the number of arguments. In the example below, `4 + 2 * 32 == 68`_
52-
5351
```rs
5452
use nodash::solidity::encode_with_selector;
5553

@@ -58,7 +56,7 @@ let args: [Field; 2] = [
5856
0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045, // address
5957
123 // uint256
6058
];
61-
let encoded = encode_with_selector(selector, args, [0; 68]);
59+
let encoded = encode_with_selector(selector, args);
6260
// typeof encoded: [u8; 68]
6361
```
6462

src/lib.nr

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ mod string;
55
pub use string::to_hex_string_bytes;
66
pub use math::{clamp, sqrt::sqrt};
77

8-
pub fn array_concat<T, let L1: u32, let L2: u32, let L: u32>(a: [T; L1], b: [T; L2]) -> [T; L] {
9-
assert(L1 + L2 == L, "array_concat: array lengths do not match");
10-
let mut result = [a[0]; L];
8+
pub fn array_concat<T, let L1: u32, let L2: u32>(a: [T; L1], b: [T; L2]) -> [T; L1 + L2] {
9+
let mut result = [a[0]; L1 + L2];
1110
for i in 0..L1 {
1211
result[i] = a[i];
1312
}
@@ -16,3 +15,15 @@ pub fn array_concat<T, let L1: u32, let L2: u32, let L: u32>(a: [T; L1], b: [T;
1615
}
1716
result
1817
}
18+
19+
mod array_concat_tests {
20+
use crate::array_concat;
21+
22+
#[test]
23+
fn test_simple() {
24+
let a: [u8; 3] = [1, 2, 3];
25+
let b: [u8; 2] = [4, 5];
26+
let c: [u8; 5] = array_concat(a, b);
27+
assert(c == [1, 2, 3, 4, 5]);
28+
}
29+
}

src/math/sqrt.nr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ use std::{ops::{Mul, Div, Add}, cmp::Ord};
22

33
// TODO: check if this is correct
44
pub fn sqrt<T>(value: T) -> T where T: Numeric + Mul + Ord + Add + Div {
5-
let r = sqrt_unconstrained(value);
6-
assert(value >= r * r);
7-
let ONE: T = Numeric::one();
8-
let r_plus_1 = r + ONE;
9-
assert(value < r_plus_1 * r_plus_1);
5+
let r = unsafe {
6+
// Safety: unsafe sqrt is checked to be between bounds
7+
let r = sqrt_unconstrained(value);
8+
assert(value >= r * r);
9+
let ONE: T = Numeric::one();
10+
let r_plus_1 = r + ONE;
11+
assert(value < r_plus_1 * r_plus_1);
12+
r
13+
};
1014
r
1115
}
1216

src/solidity.nr

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
use std;
2-
31
global SELECTOR_LENGTH = 4;
42

5-
pub fn encode_with_selector<let N: u32, let M: u32>(selector: u32, args: [Field; N], mut result: [u8; M]) -> [u8; M] {
6-
// TODO: remove this assert
7-
assert(M == SELECTOR_LENGTH + N * 32, "Invalid result length");
3+
pub fn encode_with_selector<let N: u32>(selector: u32, args: [Field; N]) -> [u8; SELECTOR_LENGTH + N * 32] {
4+
let mut result = [0; SELECTOR_LENGTH + N * 32];
85

96
let selector_bytes: [u8; 4] = (selector as Field).to_be_bytes();
107
for i in 0..SELECTOR_LENGTH {
@@ -31,7 +28,7 @@ mod tests {
3128
0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045,// address
3229
123// uint256
3330
];
34-
let encoded = encode_with_selector(selector, args, [0; 68]);
31+
let encoded = encode_with_selector(selector, args);
3532
let expected: [u8; 68] = [
3633
169, 5, 156, 187, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 218, 107, 242, 105, 100, 175, 157, 126, 237, 158, 3, 229, 52, 21, 211, 122, 169, 96, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123
3734
];
@@ -49,17 +46,11 @@ mod tests {
4946
0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e,
5047
333,
5148
64
52-
],
53-
[0; 164]
49+
]
5450
);
5551
let expected = [
5652
134, 208, 157, 174, 26, 112, 151, 160, 240, 148, 87, 176, 176, 73, 102, 132, 178, 237, 103, 35, 162, 98, 218, 58, 27, 6, 27, 89, 142, 233, 44, 227, 55, 108, 179, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 97, 1, 120, 218, 33, 31, 239, 125, 65, 123, 192, 230, 254, 211, 159, 5, 96, 154, 215, 136, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 183, 248, 188, 99, 187, 202, 209, 129, 85, 32, 19, 8, 200, 243, 84, 11, 7, 248, 79, 94, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 64
5753
];
5854
assert(result == expected);
5955
}
60-
61-
#[test(should_fail_with = "Invalid result length")]
62-
fn test_fails_if_result_length_is_wrong() {
63-
let _ = encode_with_selector(0xbd87d14b, [1, 2], [0; 67]);
64-
}
6556
}

0 commit comments

Comments
 (0)