Skip to content

Commit 055a2c1

Browse files
kashbrtiKhashayar Barooti
andauthored
chore: removing the last element of the borrow flags as it's not needed (#139)
Co-authored-by: Khashayar Barooti <[email protected]>
1 parent 1e9bd8a commit 055a2c1

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

src/fns/expressions.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ unconstrained fn __compute_quadratic_expression_with_borrow_flags<let N: u32, le
4040
rhs_flags: [[bool; RHS_N]; NUM_PRODUCTS],
4141
linear_terms: [[u128; N]; ADD_N],
4242
linear_flags: [bool; ADD_N],
43-
) -> ([u128; N], [u128; N], [Field; 2 * N]) {
43+
) -> ([u128; N], [u128; N], [Field; 2 * N - 2]) {
4444
// TODO, validate we do not overflow N2 when multiplying and N when adding
4545
let mut mulout_p = __compute_quadratic_expression_product(
4646
params,
@@ -65,7 +65,7 @@ unconstrained fn __compute_quadratic_expression_with_borrow_flags<let N: u32, le
6565
}
6666

6767
// compute borrow flags from mulout_p and mulout_n
68-
let mut borrow_flags: [Field; 2 * N] = [0; 2 * N];
68+
let mut borrow_flags: [Field; 2 * N - 2] = [0; 2 * N - 2];
6969
let borrow_shift: Field = TWO_POW_246; // 2^{246}
7070
let borrow_carry: Field = TWO_POW_126; // 2^{246 - 120} = 2^{126}
7171
let downshift: Field = 1 / (TWO_POW_120 as Field);
@@ -254,7 +254,7 @@ pub(crate) fn evaluate_quadratic_expression<let N: u32, let MOD_BITS: u32, let L
254254
linear_flags: [bool; ADD_N],
255255
) {
256256
// use an unconstrained function to compute the value of the quotient
257-
let (quotient, _, borrow_flags): ([u128; N], [u128; N], [Field; 2 * N]) = unsafe {
257+
let (quotient, _, borrow_flags): ([u128; N], [u128; N], [Field; 2 * N - 2]) = unsafe {
258258
__compute_quadratic_expression_with_borrow_flags::<_, MOD_BITS, _, _, _, _>(
259259
params,
260260
lhs_terms,

src/fns/unconstrained_helpers.nr

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,20 @@ pub(crate) unconstrained fn __validate_gt_remainder<let N: u32>(
8383
pub(crate) unconstrained fn __neg_with_flags<let N: u32, let MOD_BITS: u32>(
8484
params: P<N, MOD_BITS>,
8585
val: [u128; N],
86-
) -> ([u128; N], [bool; N]) {
86+
) -> ([u128; N], [bool; N - 1]) {
8787
let mut result: [u128; N] = [0; N];
8888
let mut borrow_in: u128 = 0;
8989

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

9696
borrow_in = borrow;
97-
borrow_flags[i] = borrow as bool;
97+
if (i < N - 1) {
98+
borrow_flags[i] = borrow as bool;
99+
}
98100
}
99101
(result, borrow_flags)
100102
}
@@ -103,7 +105,7 @@ pub(crate) unconstrained fn __add_with_flags<let N: u32, let MOD_BITS: u32>(
103105
params: P<N, MOD_BITS>,
104106
lhs: [u128; N],
105107
rhs: [u128; N],
106-
) -> ([u128; N], [bool; N], [bool; N], bool) {
108+
) -> ([u128; N], [bool; N], [bool; N - 1], bool) {
107109
let add_res = __helper_add(lhs, rhs);
108110
let overflow = __gte(add_res, params.modulus);
109111

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

117119
let mut carry_in: u128 = 0;
118120
let mut borrow_in: u128 = 0;
119-
let mut borrow_flags: [bool; N] = [false; N];
121+
let mut borrow_flags: [bool; N - 1] = [false; N - 1];
120122
let mut carry_flags: [bool; N] = [false; N];
121123
for i in 0..N {
122124
let mut add_term = lhs[i] + rhs[i] + carry_in;
@@ -133,7 +135,9 @@ pub(crate) unconstrained fn __add_with_flags<let N: u32, let MOD_BITS: u32>(
133135
// Only set `borrow` and `carry` if they differ
134136
if (carry != borrow) {
135137
carry_flags[i] = carry as bool;
136-
borrow_flags[i] = borrow as bool;
138+
if (i < N - 1) {
139+
borrow_flags[i] = borrow as bool;
140+
}
137141
}
138142
}
139143
(result, carry_flags, borrow_flags, overflow)
@@ -143,7 +147,7 @@ pub(crate) unconstrained fn __sub_with_flags<let N: u32, let MOD_BITS: u32>(
143147
params: P<N, MOD_BITS>,
144148
lhs: [u128; N],
145149
rhs: [u128; N],
146-
) -> ([u128; N], [bool; N], [bool; N], bool) {
150+
) -> ([u128; N], [bool; N], [bool; N - 1], bool) {
147151
let mut one: [u128; N] = [0; N];
148152
one[0] = 1;
149153
let underflow = !__gte(lhs, rhs);
@@ -152,7 +156,7 @@ pub(crate) unconstrained fn __sub_with_flags<let N: u32, let MOD_BITS: u32>(
152156

153157
let mut carry_in: u128 = 0;
154158
let mut borrow_in: u128 = 0;
155-
let mut borrow_flags: [bool; N] = [false; N];
159+
let mut borrow_flags: [bool; N - 1] = [false; N - 1];
156160
let mut carry_flags: [bool; N] = [false; N];
157161
for i in 0..N {
158162
let mut add_term: u128 = lhs[i] + addend[i] + carry_in;
@@ -168,7 +172,9 @@ pub(crate) unconstrained fn __sub_with_flags<let N: u32, let MOD_BITS: u32>(
168172
// Only set `borrow` and `carry` if they differ
169173
if (carry != borrow) {
170174
carry_flags[i] = carry as bool;
171-
borrow_flags[i] = borrow as bool;
175+
if (i < N - 1) {
176+
borrow_flags[i] = borrow as bool;
177+
}
172178
}
173179
}
174180
(result, carry_flags, borrow_flags, underflow)

0 commit comments

Comments
 (0)