Skip to content

Commit

Permalink
Merge pull request #2 from noir-lang/kb/noir-0.36.0
Browse files Browse the repository at this point in the history
feat!: update to support Noir 0.36.0
  • Loading branch information
kashbrti authored Oct 24, 2024
2 parents 7fa6af0 + 8a2b799 commit b857a7d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "noir_sort"
type = "lib"
authors = [""]
compiler_version = ">=0.32.0"
compiler_version = ">=0.36.0"

[dependencies]
check_shuffle = {tag = "v0.1.0", git = "https://github.com/noir-lang/noir_check_shuffle"}
49 changes: 29 additions & 20 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use dep::check_shuffle::{check_shuffle, get_shuffle_indices};
* Note: sort_extended is likely more efficient as constraining `x < y` can typically be described
* more efficiently than returning a boolean that describes whether `x < y`
**/
pub fn sort<T, let N: u32>(input: [T; N]) -> [T; N] where T: std::cmp::Ord + std::cmp::Eq {
pub fn sort<T, let N: u32>(input: [T; N]) -> [T; N]
where
T: std::cmp::Ord + std::cmp::Eq,
{
let sorted = quicksort(input);

for i in 0..N - 1 {
Expand All @@ -29,7 +32,10 @@ pub fn sort<T, let N: u32>(input: [T; N]) -> [T; N] where T: std::cmp::Ord + std
* Note: sort_extended is likely more efficient as constraining `x < y` can typically be described
* more efficiently than returning a boolean that describes whether `x < y`
**/
pub fn sort_via<T, let N: u32>(input: [T; N], sortfn: fn(T, T) -> bool) -> [T; N] where T: std::cmp::Eq {
pub fn sort_via<T, let N: u32>(input: [T; N], sortfn: fn(T, T) -> bool) -> [T; N]
where
T: std::cmp::Eq,
{
let sorted = quicksort_explicit(input, sortfn);

for i in 0..N - 1 {
Expand All @@ -56,8 +62,11 @@ pub fn sort_via<T, let N: u32>(input: [T; N], sortfn: fn(T, T) -> bool) -> [T; N
pub fn sort_extended<T, let N: u32>(
input: [T; N],
sortfn: fn(T, T) -> bool,
sortfn_assert: fn(T, T) -> ()
) -> [T; N] where T: std::cmp::Eq {
sortfn_assert: fn(T, T) -> (),
) -> [T; N]
where
T: std::cmp::Eq,
{
let sorted = quicksort_explicit(input, sortfn);

for i in 0..N - 1 {
Expand All @@ -67,15 +76,18 @@ pub fn sort_extended<T, let N: u32>(
sorted
}

struct SortResult<T, let N: u32>{
struct SortResult<T, let N: u32> {
sorted: [T; N],
sort_indices: [Field; N]
sort_indices: [Field; N],
}
pub fn sort_advanced<T, let N: u32>(
input: [T; N],
sortfn: fn(T, T) -> bool,
sortfn_assert: fn(T, T) -> ()
) -> SortResult<T, N> where T: std::cmp::Eq {
sortfn_assert: fn(T, T) -> (),
) -> SortResult<T, N>
where
T: std::cmp::Eq,
{
let sorted = quicksort_explicit(input, sortfn);

let sort_indices = get_shuffle_indices(input, sorted);
Expand All @@ -102,7 +114,7 @@ mod test {
let b = _b as Field;

let diff = b - a;
diff.assert_max_bit_size(32);
diff.assert_max_bit_size::<32>();
}

#[test]
Expand Down Expand Up @@ -150,25 +162,22 @@ fn unconditional_lt(_a: u32, _b: u32) {
let b = _b as Field;

let diff = b - a;
diff.assert_max_bit_size(32);
diff.assert_max_bit_size::<32>();
}

struct TestStruct {
a: bool,
b: u32,
c: Field
c: Field,
}

impl std::cmp::Eq for TestStruct {
fn eq(self, other: Self) -> bool {

(self.a == other.a)
& (self.b == other.b)
& (self.c == other.c)
(self.a == other.a) & (self.b == other.b) & (self.c == other.c)
}
}

unconstrained pub fn get_lt_predicate_f(x: Field, y: Field) -> bool {
pub unconstrained fn get_lt_predicate_f(x: Field, y: Field) -> bool {
let a = x as u32;
let b = y as u32;
let r = a < b;
Expand All @@ -179,7 +188,7 @@ pub fn lt_f(x: Field, y: Field) -> bool {
let predicate = get_lt_predicate_f(x, y);
let delta = y as Field - x as Field;
let lt_parameter = 2 * (predicate as Field) * delta - predicate as Field - delta;
lt_parameter.assert_max_bit_size(32);
lt_parameter.assert_max_bit_size::<32>();

predicate
}
Expand Down Expand Up @@ -211,11 +220,11 @@ fn unconditional_lte(lhs: TestStruct, rhs: TestStruct) {
// a < b as u32 implies
// b - a > 0
let diff = lhs.b as Field - rhs.b as Field;
diff.assert_max_bit_size(32);
diff.assert_max_bit_size::<32>();

// a < b as Field (32 bit condition)
let diff = lhs.c as Field - rhs.c as Field;
diff.assert_max_bit_size(32);
diff.assert_max_bit_size::<32>();
}

global Num: u32 = 100;
Expand All @@ -238,7 +247,7 @@ global Num: u32 = 100;

fn unconditional_lt_f(a: Field, b: Field) {
let diff = b - a;
diff.assert_max_bit_size(32);
diff.assert_max_bit_size::<32>();
}

// 5,089
Expand Down
15 changes: 12 additions & 3 deletions src/quicksort/quicksort.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ impl<T, let N: u32> Swap for [T; N] {
}
}

unconstrained fn partition<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32) -> u32 where T: std::cmp::Ord {
unconstrained fn partition<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32) -> u32
where
T: std::cmp::Ord,
{
let pivot = high;
let mut i = low;
for j in low..high {
Expand All @@ -23,7 +26,10 @@ unconstrained fn partition<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32)
i
}

unconstrained fn quicksort_recursive<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32) where T: std::cmp::Ord {
unconstrained fn quicksort_recursive<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32)
where
T: std::cmp::Ord,
{
if low < high {
let pivot_index = partition(arr, low, high);
if pivot_index > 0 {
Expand All @@ -33,7 +39,10 @@ unconstrained fn quicksort_recursive<T, let N: u32>(arr: &mut [T; N], low: u32,
}
}

unconstrained pub fn quicksort<T, let N: u32>(_arr: [T; N]) -> [T; N] where T: std::cmp::Ord {
pub unconstrained fn quicksort<T, let N: u32>(_arr: [T; N]) -> [T; N]
where
T: std::cmp::Ord,
{
let mut arr: [T; N] = _arr;
if arr.len() <= 1 {} else {
quicksort_recursive(&mut arr, 0, arr.len() - 1);
Expand Down
11 changes: 8 additions & 3 deletions src/quicksort/quicksort_explicit.nr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ impl<T, let N: u32> Swap for [T; N] {
}
}

unconstrained fn partition<T, let N: u32>(arr: &mut [T; N], low: u32, high: u32, sortfn: fn(T, T) -> bool) -> u32 {
unconstrained fn partition<T, let N: u32>(
arr: &mut [T; N],
low: u32,
high: u32,
sortfn: fn(T, T) -> bool,
) -> u32 {
let pivot = high;
let mut i = low;
for j in low..high {
Expand All @@ -27,7 +32,7 @@ unconstrained fn quicksort_recursive<T, let N: u32>(
arr: &mut [T; N],
low: u32,
high: u32,
sortfn: fn(T, T) -> bool
sortfn: fn(T, T) -> bool,
) {
if low < high {
let pivot_index = partition(arr, low, high, sortfn);
Expand All @@ -38,7 +43,7 @@ unconstrained fn quicksort_recursive<T, let N: u32>(
}
}

unconstrained pub fn quicksort<T, let N: u32>(_arr: [T; N], sortfn: fn(T, T) -> bool) -> [T; N] {
pub unconstrained fn quicksort<T, let N: u32>(_arr: [T; N], sortfn: fn(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

0 comments on commit b857a7d

Please sign in to comment.