Skip to content

Commit

Permalink
Format the standard library and test programs
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Oct 18, 2024
1 parent 41cd590 commit 926af37
Show file tree
Hide file tree
Showing 241 changed files with 9,715 additions and 5,208 deletions.
18 changes: 12 additions & 6 deletions noir_stdlib/src/array/check_shuffle.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::cmp::Eq;

unconstrained fn __get_shuffle_indices<T, let N: u32>(lhs: [T; N], rhs: [T; N]) -> [Field; N] where T: Eq {
let mut shuffle_indices: [Field;N ] = [0; N];
unconstrained fn __get_shuffle_indices<T, let N: u32>(lhs: [T; N], rhs: [T; N]) -> [Field; N]
where
T: Eq,
{
let mut shuffle_indices: [Field; N] = [0; N];

let mut shuffle_mask: [bool; N] = [false; N];
for i in 0..N {
Expand Down Expand Up @@ -35,7 +38,10 @@ unconstrained fn __get_index<let N: u32>(indices: [Field; N], idx: Field) -> Fie
result
}

pub(crate) fn check_shuffle<T, let N: u32>(lhs: [T; N], rhs: [T; N]) where T: Eq {
pub(crate) fn check_shuffle<T, let N: u32>(lhs: [T; N], rhs: [T; N])
where
T: Eq,
{
unsafe {
let shuffle_indices = __get_shuffle_indices(lhs, rhs);

Expand All @@ -59,7 +65,7 @@ mod test {
struct CompoundStruct {
a: bool,
b: Field,
c: u64
c: u64,
}
impl Eq for CompoundStruct {
fn eq(self, other: Self) -> bool {
Expand Down Expand Up @@ -102,14 +108,14 @@ mod test {
CompoundStruct { a: false, b: -100, c: 54321 },
CompoundStruct { a: true, b: 5, c: 0xffffffffffffffff },
CompoundStruct { a: true, b: 9814, c: 0xeeffee0011001133 },
CompoundStruct { a: false, b: 0x155, c: 0 }
CompoundStruct { a: false, b: 0x155, c: 0 },
];
let rhs: [CompoundStruct; 5] = [
CompoundStruct { a: false, b: 0x155, c: 0 },
CompoundStruct { a: false, b: 0, c: 12345 },
CompoundStruct { a: false, b: -100, c: 54321 },
CompoundStruct { a: true, b: 9814, c: 0xeeffee0011001133 },
CompoundStruct { a: true, b: 5, c: 0xffffffffffffffff }
CompoundStruct { a: true, b: 5, c: 0xffffffffffffffff },
];
check_shuffle(lhs, rhs);
}
Expand Down
65 changes: 36 additions & 29 deletions noir_stdlib/src/array/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ mod quicksort;

impl<T, let N: u32> [T; N] {
/// Returns the length of this array.
///
///
/// ```noir
/// fn len(self) -> Field
/// ```
///
///
/// example
///
///
/// ```noir
/// fn main() {
/// let array = [42, 42];
Expand All @@ -24,7 +24,7 @@ impl<T, let N: u32> [T; N] {
pub fn len(self) -> u32 {}

/// Returns this array as a slice.
///
///
/// ```noir
/// let array = [1, 2];
/// let slice = array.as_slice();
Expand All @@ -34,9 +34,9 @@ impl<T, let N: u32> [T; N] {
pub fn as_slice(self) -> [T] {}

/// Applies a function to each element of this array, returning a new array containing the mapped elements.
///
///
/// Example:
///
///
/// ```rust
/// let a = [1, 2, 3];
/// let b = a.map(|a| a * 2);
Expand All @@ -55,20 +55,20 @@ impl<T, let N: u32> [T; N] {

/// Applies a function to each element of the array, returning the final accumulated value. The first
/// parameter is the initial value.
///
///
/// This is a left fold, so the given function will be applied to the accumulator and first element of
/// the array, then the second, and so on. For a given call the expected result would be equivalent to:
///
///
/// ```rust
/// let a1 = [1];
/// let a2 = [1, 2];
/// let a3 = [1, 2, 3];
///
///
/// let f = |a, b| a - b;
/// a1.fold(10, f); //=> f(10, 1)
/// a2.fold(10, f); //=> f(f(10, 1), 2)
/// a3.fold(10, f); //=> f(f(f(10, 1), 2), 3)
///
///
/// assert_eq(a3.fold(10, f), 10 - 1 - 2 - 3);
/// ```
pub fn fold<U, Env>(self, mut accumulator: U, f: fn[Env](U, T) -> U) -> U {
Expand All @@ -79,9 +79,9 @@ impl<T, let N: u32> [T; N] {
}

/// Same as fold, but uses the first element as the starting element.
///
///
/// Example:
///
///
/// ```noir
/// fn main() {
/// let arr = [1, 2, 3, 4];
Expand All @@ -98,9 +98,9 @@ impl<T, let N: u32> [T; N] {
}

/// Returns true if all the elements in this array satisfy the given predicate.
///
///
/// Example:
///
///
/// ```noir
/// fn main() {
/// let arr = [2, 2, 2, 2, 2];
Expand All @@ -117,9 +117,9 @@ impl<T, let N: u32> [T; N] {
}

/// Returns true if any of the elements in this array satisfy the given predicate.
///
///
/// Example:
///
///
/// ```noir
/// fn main() {
/// let arr = [2, 2, 2, 2, 5];
Expand All @@ -136,14 +136,17 @@ impl<T, let N: u32> [T; N] {
}
}

impl<T, let N: u32> [T; N] where T: Ord + Eq {
impl<T, let N: u32> [T; N]
where
T: Ord + Eq,
{
/// Returns a new sorted array. The original array remains untouched. Notice that this function will
/// only work for arrays of fields or integers, not for any arbitrary type. This is because the sorting
/// logic it uses internally is optimized specifically for these values. If you need a sort function to
/// sort any type, you should use the `sort_via` function.
///
///
/// Example:
///
///
/// ```rust
/// fn main() {
/// let arr = [42, 32];
Expand All @@ -156,21 +159,24 @@ impl<T, let N: u32> [T; N] where T: Ord + Eq {
}
}

impl<T, let N: u32> [T; N] where T: Eq {
/// Returns a new sorted array by sorting it with a custom comparison function.
/// The original array remains untouched.
impl<T, let N: u32> [T; N]
where
T: Eq,
{
/// Returns a new sorted array by sorting it with a custom comparison function.
/// The original array remains untouched.
/// The ordering function must return true if the first argument should be sorted to be before the second argument or is equal to the second argument.
///
///
/// Using this method with an operator like `<` that does not return `true` for equal values will result in an assertion failure for arrays with equal elements.
///
///
/// Example:
///
///
/// ```rust
/// fn main() {
/// let arr = [42, 32]
/// let sorted_ascending = arr.sort_via(|a, b| a <= b);
/// assert(sorted_ascending == [32, 42]); // verifies
///
///
/// let sorted_descending = arr.sort_via(|a, b| a >= b);
/// assert(sorted_descending == [32, 42]); // does not verify
/// }
Expand All @@ -185,7 +191,8 @@ impl<T, let N: u32> [T; N] where T: Eq {
if !is_unconstrained() {
for i in 0..N - 1 {
assert(
ordering(sorted[i], sorted[i + 1]), "Array has not been sorted correctly according to `ordering`."
ordering(sorted[i], sorted[i + 1]),
"Array has not been sorted correctly according to `ordering`.",
);
}
check_shuffle::check_shuffle(self, sorted);
Expand All @@ -198,9 +205,9 @@ impl<T, let N: u32> [T; N] where T: Eq {
impl<let N: u32> [u8; N] {
/// Converts a byte array of type `[u8; N]` to a string. Note that this performs no UTF-8 validation -
/// the given array is interpreted as-is as a string.
///
///
/// Example:
///
///
/// ```rust
/// fn main() {
/// let hi = [104, 105].as_str_unchecked();
Expand Down
14 changes: 11 additions & 3 deletions noir_stdlib/src/array/quicksort.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ unconstrained fn partition<T, Env, let N: u32>(
arr: &mut [T; N],
low: u32,
high: u32,
sortfn: fn[Env](T, T) -> bool
sortfn: fn[Env](T, T) -> bool,
) -> u32 {
let pivot = high;
let mut i = low;
Expand All @@ -20,7 +20,12 @@ unconstrained fn partition<T, Env, let N: u32>(
i
}

unconstrained fn quicksort_recursive<T, Env, let N: u32>(arr: &mut [T; N], low: u32, high: u32, sortfn: fn[Env](T, T) -> bool) {
unconstrained fn quicksort_recursive<T, Env, let N: u32>(
arr: &mut [T; N],
low: u32,
high: u32,
sortfn: fn[Env](T, T) -> bool,
) {
if low < high {
let pivot_index = partition(arr, low, high, sortfn);
if pivot_index > 0 {
Expand All @@ -30,7 +35,10 @@ unconstrained fn quicksort_recursive<T, Env, let N: u32>(arr: &mut [T; N], low:
}
}

pub(crate) unconstrained fn quicksort<T, Env, let N: u32>(_arr: [T; N], sortfn: fn[Env](T, T) -> bool) -> [T; N] {
pub(crate) unconstrained fn quicksort<T, Env, let N: u32>(
_arr: [T; N],
sortfn: fn[Env](T, T) -> bool,
) -> [T; N] {
let mut arr: [T; N] = _arr;
if arr.len() <= 1 {} else {
quicksort_recursive(&mut arr, 0, arr.len() - 1, sortfn);
Expand Down
46 changes: 30 additions & 16 deletions noir_stdlib/src/bigint.nr
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
use crate::ops::{Add, Sub, Mul, Div};
use crate::cmp::Eq;

global bn254_fq = &[0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
0x5d, 0x58, 0x81, 0x81, 0xb6, 0x45, 0x50, 0xb8, 0x29, 0xa0, 0x31, 0xe1, 0x72, 0x4e, 0x64, 0x30];
global bn254_fr = &[1, 0, 0, 240, 147, 245, 225, 67, 145, 112, 185, 121, 72, 232, 51, 40, 93, 88, 129, 129, 182, 69, 80, 184, 41, 160, 49, 225, 114, 78, 100, 48];
global secpk1_fr = &[0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA,
0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
global secpk1_fq = &[0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
global secpr1_fq = &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF];
global secpr1_fr = &[81, 37, 99, 252, 194, 202, 185, 243, 132, 158, 23, 167, 173, 250, 230, 188, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255];
global bn254_fq = &[
0x47, 0xFD, 0x7C, 0xD8, 0x16, 0x8C, 0x20, 0x3C, 0x8d, 0xca, 0x71, 0x68, 0x91, 0x6a, 0x81, 0x97,
0x5d, 0x58, 0x81, 0x81, 0xb6, 0x45, 0x50, 0xb8, 0x29, 0xa0, 0x31, 0xe1, 0x72, 0x4e, 0x64, 0x30,
];
global bn254_fr = &[
1, 0, 0, 240, 147, 245, 225, 67, 145, 112, 185, 121, 72, 232, 51, 40, 93, 88, 129, 129, 182, 69,
80, 184, 41, 160, 49, 225, 114, 78, 100, 48,
];
global secpk1_fr = &[
0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA,
0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
];
global secpk1_fq = &[
0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
];
global secpr1_fq = &[
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
];
global secpr1_fr = &[
81, 37, 99, 252, 194, 202, 185, 243, 132, 158, 23, 167, 173, 250, 230, 188, 255, 255, 255, 255,
255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, 255,
];
// docs:start:big_int_definition
pub struct BigInt {
pointer: u32,
Expand Down Expand Up @@ -50,7 +64,7 @@ pub trait BigField {
}

pub struct Secpk1Fq {
array: [u8;32],
array: [u8; 32],
}

impl BigField for Secpk1Fq {
Expand Down Expand Up @@ -107,7 +121,7 @@ impl Eq for Secpk1Fq {
}

pub struct Secpk1Fr {
array: [u8;32],
array: [u8; 32],
}

impl BigField for Secpk1Fr {
Expand Down Expand Up @@ -164,7 +178,7 @@ impl Eq for Secpk1Fr {
}

pub struct Bn254Fr {
array: [u8;32],
array: [u8; 32],
}

impl BigField for Bn254Fr {
Expand Down Expand Up @@ -221,7 +235,7 @@ impl Eq for Bn254Fr {
}

pub struct Bn254Fq {
array: [u8;32],
array: [u8; 32],
}

impl BigField for Bn254Fq {
Expand Down Expand Up @@ -278,7 +292,7 @@ impl Eq for Bn254Fq {
}

pub struct Secpr1Fq {
array: [u8;32],
array: [u8; 32],
}

impl BigField for Secpr1Fq {
Expand Down Expand Up @@ -335,7 +349,7 @@ impl Eq for Secpr1Fq {
}

pub struct Secpr1Fr {
array: [u8;32],
array: [u8; 32],
}

impl BigField for Secpr1Fr {
Expand Down
Loading

0 comments on commit 926af37

Please sign in to comment.