Skip to content

Commit

Permalink
chore: removing the last element of the borrow flags as it's not need…
Browse files Browse the repository at this point in the history
…ed (#139)

Co-authored-by: Khashayar Barooti <[email protected]>
  • Loading branch information
kashbrti and Khashayar Barooti authored Feb 26, 2025
1 parent 1e9bd8a commit 055a2c1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/fns/expressions.nr
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ unconstrained fn __compute_quadratic_expression_with_borrow_flags<let N: u32, le
rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS],
linear_terms: [[u128; N]; ADD_N],
linear_flags: [bool; ADD_N],
) -> ([u128; N], [u128; N], [Field; 2 * N]) {
) -> ([u128; N], [u128; N], [Field; 2 * N - 2]) {
// TODO, validate we do not overflow N2 when multiplying and N when adding
let mut mulout_p = __compute_quadratic_expression_product(
params,
Expand All @@ -65,7 +65,7 @@ unconstrained fn __compute_quadratic_expression_with_borrow_flags<let N: u32, le
}

// compute borrow flags from mulout_p and mulout_n
let mut borrow_flags: [Field; 2 * N] = [0; 2 * N];
let mut borrow_flags: [Field; 2 * N - 2] = [0; 2 * N - 2];
let borrow_shift: Field = TWO_POW_246; // 2^{246}
let borrow_carry: Field = TWO_POW_126; // 2^{246 - 120} = 2^{126}
let downshift: Field = 1 / (TWO_POW_120 as Field);
Expand Down Expand Up @@ -254,7 +254,7 @@ pub(crate) fn evaluate_quadratic_expression<let N: u32, let MOD_BITS: u32, let L
linear_flags: [bool; ADD_N],
) {
// use an unconstrained function to compute the value of the quotient
let (quotient, _, borrow_flags): ([u128; N], [u128; N], [Field; 2 * N]) = unsafe {
let (quotient, _, borrow_flags): ([u128; N], [u128; N], [Field; 2 * N - 2]) = unsafe {
__compute_quadratic_expression_with_borrow_flags::<_, MOD_BITS, _, _, _, _>(
params,
lhs_terms,
Expand Down
24 changes: 15 additions & 9 deletions src/fns/unconstrained_helpers.nr
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ pub(crate) unconstrained fn __validate_gt_remainder<let N: u32>(
pub(crate) unconstrained fn __neg_with_flags<let N: u32, let MOD_BITS: u32>(
params: P<N, MOD_BITS>,
val: [u128; N],
) -> ([u128; N], [bool; N]) {
) -> ([u128; N], [bool; N - 1]) {
let mut result: [u128; N] = [0; N];
let mut borrow_in: u128 = 0;

let mut borrow_flags: [bool; N] = [false; N];
let mut borrow_flags: [bool; N - 1] = [false; N - 1];
for i in 0..N {
let sub_term = val[i] + borrow_in;
let borrow = (sub_term > params.modulus[i]) as u128;
result[i] = borrow * TWO_POW_120 + params.modulus[i] - sub_term;

borrow_in = borrow;
borrow_flags[i] = borrow as bool;
if (i < N - 1) {
borrow_flags[i] = borrow as bool;
}
}
(result, borrow_flags)
}
Expand All @@ -103,7 +105,7 @@ pub(crate) unconstrained fn __add_with_flags<let N: u32, let MOD_BITS: u32>(
params: P<N, MOD_BITS>,
lhs: [u128; N],
rhs: [u128; N],
) -> ([u128; N], [bool; N], [bool; N], bool) {
) -> ([u128; N], [bool; N], [bool; N - 1], bool) {
let add_res = __helper_add(lhs, rhs);
let overflow = __gte(add_res, params.modulus);

Expand All @@ -116,7 +118,7 @@ pub(crate) unconstrained fn __add_with_flags<let N: u32, let MOD_BITS: u32>(

let mut carry_in: u128 = 0;
let mut borrow_in: u128 = 0;
let mut borrow_flags: [bool; N] = [false; N];
let mut borrow_flags: [bool; N - 1] = [false; N - 1];
let mut carry_flags: [bool; N] = [false; N];
for i in 0..N {
let mut add_term = lhs[i] + rhs[i] + carry_in;
Expand All @@ -133,7 +135,9 @@ pub(crate) unconstrained fn __add_with_flags<let N: u32, let MOD_BITS: u32>(
// Only set `borrow` and `carry` if they differ
if (carry != borrow) {
carry_flags[i] = carry as bool;
borrow_flags[i] = borrow as bool;
if (i < N - 1) {
borrow_flags[i] = borrow as bool;
}
}
}
(result, carry_flags, borrow_flags, overflow)
Expand All @@ -143,7 +147,7 @@ pub(crate) unconstrained fn __sub_with_flags<let N: u32, let MOD_BITS: u32>(
params: P<N, MOD_BITS>,
lhs: [u128; N],
rhs: [u128; N],
) -> ([u128; N], [bool; N], [bool; N], bool) {
) -> ([u128; N], [bool; N], [bool; N - 1], bool) {
let mut one: [u128; N] = [0; N];
one[0] = 1;
let underflow = !__gte(lhs, rhs);
Expand All @@ -152,7 +156,7 @@ pub(crate) unconstrained fn __sub_with_flags<let N: u32, let MOD_BITS: u32>(

let mut carry_in: u128 = 0;
let mut borrow_in: u128 = 0;
let mut borrow_flags: [bool; N] = [false; N];
let mut borrow_flags: [bool; N - 1] = [false; N - 1];
let mut carry_flags: [bool; N] = [false; N];
for i in 0..N {
let mut add_term: u128 = lhs[i] + addend[i] + carry_in;
Expand All @@ -168,7 +172,9 @@ pub(crate) unconstrained fn __sub_with_flags<let N: u32, let MOD_BITS: u32>(
// Only set `borrow` and `carry` if they differ
if (carry != borrow) {
carry_flags[i] = carry as bool;
borrow_flags[i] = borrow as bool;
if (i < N - 1) {
borrow_flags[i] = borrow as bool;
}
}
}
(result, carry_flags, borrow_flags, underflow)
Expand Down

0 comments on commit 055a2c1

Please sign in to comment.