-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
282 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,11 +70,6 @@ jobs: | |
name: noirc_abi_wasm | ||
path: ./tooling/noirc_abi_wasm | ||
|
||
- name: Install wasm-pack | ||
uses: taiki-e/install-action@v2 | ||
with: | ||
tool: [email protected] | ||
|
||
- name: Install Yarn dependencies | ||
uses: ./.github/actions/setup | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
|
||
cd $(dirname "$0")/.. | ||
|
||
command -v $1 >/dev/null 2>&1 && echo "true" || { echo >&2 "$1 is not installed" && echo "false"; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
|
||
cd $(dirname "$0")/.. | ||
|
||
# Install wasm-pack | ||
CARGO_BINSTALL_CHECK=$(./scripts/command-check.sh cargo-binstall) | ||
if [ $CARGO_BINSTALL_CHECK != "true" ]; then | ||
curl -L --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.sh | bash | ||
fi | ||
|
||
cargo-binstall [email protected] -y |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
global PLO: Field = 53438638232309528389504892708671455233; | ||
global PHI: Field = 64323764613183177041862057485226039389; | ||
global TWO_POW_128: Field = 0x100000000000000000000000000000000; | ||
|
||
unconstrained fn decompose_unsafe(x: Field) -> (Field, Field) { | ||
let x_bytes = x.to_le_bytes(32); | ||
|
||
let mut low: Field = 0; | ||
let mut high: Field = 0; | ||
|
||
let mut offset = 1; | ||
for i in 0..16 { | ||
low += (x_bytes[i] as Field) * offset; | ||
high += (x_bytes[i + 16] as Field) * offset; | ||
offset *= 256; | ||
} | ||
|
||
(low, high) | ||
} | ||
|
||
pub fn decompose(x: Field) -> (Field, Field) { | ||
let (xlo, xhi) = decompose_unsafe(x); | ||
let borrow = lt_unsafe(PLO, xlo, 16); | ||
|
||
xlo.assert_max_bit_size(128); | ||
xhi.assert_max_bit_size(128); | ||
|
||
assert_eq(x, xlo + TWO_POW_128 * xhi); | ||
let rlo = PLO - xlo + (borrow as Field) * TWO_POW_128; | ||
let rhi = PHI - xhi - (borrow as Field); | ||
|
||
rlo.assert_max_bit_size(128); | ||
rhi.assert_max_bit_size(128); | ||
|
||
(xlo, xhi) | ||
} | ||
|
||
unconstrained fn lt_unsafe(x: Field, y: Field, num_bytes: u32) -> bool { | ||
let x_bytes = x.__to_le_radix(256, num_bytes); | ||
let y_bytes = y.__to_le_radix(256, num_bytes); | ||
let mut x_is_lt = false; | ||
let mut done = false; | ||
for i in 0..num_bytes { | ||
if (!done) { | ||
let x_byte = x_bytes[num_bytes - 1 - i]; | ||
let y_byte = y_bytes[num_bytes - 1 - i]; | ||
let bytes_match = x_byte == y_byte; | ||
if !bytes_match { | ||
x_is_lt = x_byte < y_byte; | ||
done = true; | ||
} | ||
} | ||
} | ||
x_is_lt | ||
} | ||
|
||
unconstrained fn lte_unsafe(x: Field, y: Field, num_bytes: u32) -> bool { | ||
lt_unsafe(x, y, num_bytes) | (x == y) | ||
} | ||
|
||
pub fn assert_gt(a: Field, b: Field) { | ||
let (alo, ahi) = decompose(a); | ||
let (blo, bhi) = decompose(b); | ||
|
||
let borrow = lte_unsafe(alo, blo, 16); | ||
|
||
let rlo = alo - blo - 1 + (borrow as Field) * TWO_POW_128; | ||
let rhi = ahi - bhi - (borrow as Field); | ||
|
||
rlo.assert_max_bit_size(128); | ||
rhi.assert_max_bit_size(128); | ||
} | ||
|
||
pub fn assert_lt(a: Field, b: Field) { | ||
assert_gt(b, a); | ||
} | ||
|
||
pub fn gt(a: Field, b: Field) -> bool { | ||
if a == b { | ||
false | ||
} else if lt_unsafe(a, b, 32) { | ||
assert_gt(b, a); | ||
false | ||
} else { | ||
assert_gt(a, b); | ||
true | ||
} | ||
} | ||
|
||
pub fn lt(a: Field, b: Field) -> bool { | ||
gt(b, a) | ||
} |
6 changes: 6 additions & 0 deletions
6
noir/test_programs/compile_success_empty/field_comparisons/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "field_comparisons" | ||
type = "bin" | ||
authors = [""] | ||
|
||
[dependencies] |
1 change: 1 addition & 0 deletions
1
noir/test_programs/compile_success_empty/field_comparisons/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
86 changes: 86 additions & 0 deletions
86
noir/test_programs/compile_success_empty/field_comparisons/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use dep::std::field::bn254::{PLO, PHI, TWO_POW_128, decompose, decompose_unsafe, lt_unsafe, lte_unsafe, assert_gt, gt}; | ||
|
||
fn check_plo_phi() { | ||
assert_eq(PLO + PHI * TWO_POW_128, 0); | ||
let p_bytes = dep::std::field::modulus_le_bytes(); | ||
let mut p_low: Field = 0; | ||
let mut p_high: Field = 0; | ||
|
||
let mut offset = 1; | ||
for i in 0..16 { | ||
p_low += (p_bytes[i] as Field) * offset; | ||
p_high += (p_bytes[i + 16] as Field) * offset; | ||
offset *= 256; | ||
} | ||
assert_eq(p_low, PLO); | ||
assert_eq(p_high, PHI); | ||
} | ||
|
||
fn check_decompose_unsafe() { | ||
assert_eq(decompose_unsafe(TWO_POW_128), (0, 1)); | ||
assert_eq(decompose_unsafe(TWO_POW_128 + 0x1234567890), (0x1234567890, 1)); | ||
assert_eq(decompose_unsafe(0x1234567890), (0x1234567890, 0)); | ||
} | ||
|
||
fn check_decompose() { | ||
assert_eq(decompose(TWO_POW_128), (0, 1)); | ||
assert_eq(decompose(TWO_POW_128 + 0x1234567890), (0x1234567890, 1)); | ||
assert_eq(decompose(0x1234567890), (0x1234567890, 0)); | ||
} | ||
|
||
fn check_lt_unsafe() { | ||
assert(lt_unsafe(0, 1, 16)); | ||
assert(lt_unsafe(0, 0x100, 16)); | ||
assert(lt_unsafe(0x100, TWO_POW_128 - 1, 16)); | ||
assert(!lt_unsafe(0, TWO_POW_128, 16)); | ||
} | ||
|
||
fn check_lte_unsafe() { | ||
assert(lte_unsafe(0, 1, 16)); | ||
assert(lte_unsafe(0, 0x100, 16)); | ||
assert(lte_unsafe(0x100, TWO_POW_128 - 1, 16)); | ||
assert(!lte_unsafe(0, TWO_POW_128, 16)); | ||
|
||
assert(lte_unsafe(0, 0, 16)); | ||
assert(lte_unsafe(0x100, 0x100, 16)); | ||
assert(lte_unsafe(TWO_POW_128 - 1, TWO_POW_128 - 1, 16)); | ||
assert(lte_unsafe(TWO_POW_128, TWO_POW_128, 16)); | ||
} | ||
|
||
fn check_assert_gt() { | ||
assert_gt(1, 0); | ||
assert_gt(0x100, 0); | ||
assert_gt((0 - 1), (0 - 2)); | ||
assert_gt(TWO_POW_128, 0); | ||
assert_gt(0 - 1, 0); | ||
} | ||
|
||
fn check_gt() { | ||
assert(gt(1, 0)); | ||
assert(gt(0x100, 0)); | ||
assert(gt((0 - 1), (0 - 2))); | ||
assert(gt(TWO_POW_128, 0)); | ||
assert(!gt(0, 0)); | ||
assert(!gt(0, 0x100)); | ||
assert(gt(0 - 1, 0 - 2)); | ||
assert(!gt(0 - 2, 0 - 1)); | ||
} | ||
|
||
fn checks() { | ||
check_plo_phi(); | ||
check_decompose_unsafe(); | ||
check_decompose(); | ||
check_lt_unsafe(); | ||
check_lte_unsafe(); | ||
check_assert_gt(); | ||
check_gt(); | ||
} | ||
|
||
unconstrained fn checks_in_brillig() { | ||
checks(); | ||
} | ||
|
||
fn main() { | ||
checks(); | ||
checks_in_brillig(); | ||
} |
Oops, something went wrong.