Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Field comparisons #4704

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions noir_stdlib/src/field/bn254.nr
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ unconstrained fn decompose_unsafe(x: Field) -> (Field, Field) {
(low, high)
}

// Assert that (alo > blo && ahi >= bhi) || (alo <= blo && ahi > bhi)
fn assert_gt_limbs(a: (Field, Field), b: (Field, Field)) {
let (alo, ahi) = a;
let (blo, bhi) = 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);
}

/// Decompose a single field into two 16 byte fields.
pub fn decompose(x: Field) -> (Field, Field) {
// Take hints of the decomposition
let (xlo, xhi) = decompose_unsafe(x);
let borrow = lt_unsafe(PLO, xlo, 16);

// Range check the limbs
xlo.assert_max_bit_size(128);
Expand All @@ -34,13 +46,8 @@ pub fn decompose(x: Field) -> (Field, Field) {
// Check that the decomposition is correct
assert_eq(x, xlo + TWO_POW_128 * xhi);

// Check that (xlo < plo && xhi <= phi) || (xlo >= plo && xhi < phi)
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);

// Assert that the decomposition of P is greater than the decomposition of x
assert_gt_limbs((PLO, PHI), (xlo, xhi));
(xlo, xhi)
}

Expand Down Expand Up @@ -69,17 +76,11 @@ unconstrained fn lte_unsafe(x: Field, y: Field, num_bytes: u32) -> bool {

pub fn assert_gt(a: Field, b: Field) {
// Decompose a and b
let (alo, ahi) = decompose(a);
let (blo, bhi) = decompose(b);

let borrow = lte_unsafe(alo, blo, 16);
let a_limbs = decompose(a);
let b_limbs = decompose(b);

// Assert that (alo > blo && ahi >= bhi) || (alo <= blo && ahi > bhi)
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);
// Assert that a_limbs is greater than b_limbs
assert_gt_limbs(a_limbs, b_limbs)
}

pub fn assert_lt(a: Field, b: Field) {
Expand Down
Loading