Skip to content

Commit

Permalink
refactor builtin
Browse files Browse the repository at this point in the history
  • Loading branch information
sirasistant committed Oct 25, 2024
1 parent fb0bc8b commit 5950ed7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
26 changes: 8 additions & 18 deletions noir/noir-repo/noir_stdlib/src/field/bn254.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::field::unconstrained_field_less_than;
use crate::field::field_less_than;
use crate::runtime::is_unconstrained;

// The low and high decomposition of the field modulus
Expand Down Expand Up @@ -26,15 +26,11 @@ pub(crate) unconstrained fn decompose_hint(x: Field) -> (Field, Field) {
compute_decomposition(x)
}

unconstrained fn lt_hint(x: Field, y: Field) -> bool {
unconstrained_field_less_than(x, y)
}

unconstrained fn lte_hint(x: Field, y: Field) -> bool {
if x == y {
true
} else {
unconstrained_field_less_than(x, y)
field_less_than(x, y)
}
}

Expand Down Expand Up @@ -78,7 +74,7 @@ pub fn decompose(x: Field) -> (Field, Field) {

pub fn assert_gt(a: Field, b: Field) {
if is_unconstrained() {
assert(unconstrained_field_less_than(b, a));
assert(unsafe { field_less_than(b, a) });
} else {
// Decompose a and b
let a_limbs = decompose(a);
Expand All @@ -95,13 +91,15 @@ pub fn assert_lt(a: Field, b: Field) {

pub fn gt(a: Field, b: Field) -> bool {
if is_unconstrained() {
unconstrained_field_less_than(b, a)
unsafe {
field_less_than(b, a)
}
} else if a == b {
false
} else {
// Take a hint of the comparison and verify it
unsafe {
if lt_hint(a, b) {
if field_less_than(a, b) {
assert_gt(b, a);
false
} else {
Expand All @@ -118,7 +116,7 @@ pub fn lt(a: Field, b: Field) -> bool {

mod tests {
// TODO: Allow imports from "super"
use crate::field::bn254::{assert_gt, decompose, gt, lt_hint, lte_hint, PHI, PLO, TWO_POW_128};
use crate::field::bn254::{assert_gt, decompose, gt, lte_hint, PHI, PLO, TWO_POW_128};

#[test]
fn check_decompose() {
Expand All @@ -134,14 +132,6 @@ mod tests {
assert_eq(decompose(0x1234567890), (0x1234567890, 0));
}

#[test]
unconstrained fn check_lt_hint() {
assert(lt_hint(0, 1));
assert(lt_hint(0, 0x100));
assert(lt_hint(0x100, TWO_POW_128 - 1));
assert(!lt_hint(0 - 1, 0));
}

#[test]
unconstrained fn check_lte_hint() {
assert(lte_hint(0, 1));
Expand Down
20 changes: 18 additions & 2 deletions noir/noir-repo/noir_stdlib/src/field/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ pub comptime fn modulus_le_bytes() -> [u8] {}

/// An unconstrained only built in to efficiently compare fields.
#[builtin(field_less_than)]
pub fn unconstrained_field_less_than(_x: Field, _y: Field) -> bool {}
unconstrained fn __field_less_than(x: Field, y: Field) -> bool {}

pub(crate) unconstrained fn field_less_than(x: Field, y: Field) -> bool {
__field_less_than(x, y)
}

// Convert a 32 byte array to a field element by modding
pub fn bytes32_to_field(bytes32: [u8; 32]) -> Field {
Expand All @@ -233,7 +237,9 @@ pub fn bytes32_to_field(bytes32: [u8; 32]) -> Field {

fn lt_fallback(x: Field, y: Field) -> bool {
if is_unconstrained() {
unconstrained_field_less_than(x, y)
unsafe {
field_less_than(x, y)
}
} else {
let x_bytes: [u8; 32] = x.to_le_bytes();
let y_bytes: [u8; 32] = y.to_le_bytes();
Expand All @@ -255,6 +261,8 @@ fn lt_fallback(x: Field, y: Field) -> bool {
}

mod tests {
use super::field_less_than;

#[test]
// docs:start:to_be_bits_example
fn test_to_be_bits() {
Expand Down Expand Up @@ -312,4 +320,12 @@ mod tests {
assert_eq(Field::from_le_bytes::<8>(bits), field);
}
// docs:end:to_le_radix_example

#[test]
unconstrained fn test_field_less_than() {
assert(field_less_than(0, 1));
assert(field_less_than(0, 0x100));
assert(field_less_than(0x100, 0 - 1));
assert(!field_less_than(0 - 1, 0));
}
}

0 comments on commit 5950ed7

Please sign in to comment.