Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bigfield_impl.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Suyash], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
13#include <cstdint>
14#include <tuple>
15
16#include "../circuit_builders/circuit_builders.hpp"
17#include "bigfield.hpp"
18
19#include "../field/field.hpp"
21
22namespace bb::stdlib {
23
24template <typename Builder, typename T>
26 : context(parent_context)
27 , binary_basis_limbs{ Limb(field_t<Builder>(parent_context, bb::fr(0))),
28 Limb(field_t<Builder>(parent_context, bb::fr(0))),
29 Limb(field_t<Builder>(parent_context, bb::fr(0))),
30 Limb(field_t<Builder>(parent_context, bb::fr(0))) }
31 , prime_basis_limb(context, 0)
32{}
33
34template <typename Builder, typename T>
36 : context(parent_context)
37 , binary_basis_limbs{ Limb(field_t<Builder>(parent_context, bb::fr(value.slice(0, NUM_LIMB_BITS)))),
38 Limb(field_t<Builder>(parent_context, bb::fr(value.slice(NUM_LIMB_BITS, NUM_LIMB_BITS * 2)))),
39 Limb(field_t<Builder>(parent_context,
40 bb::fr(value.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3)))),
41 Limb(field_t<Builder>(parent_context,
42 bb::fr(value.slice(NUM_LIMB_BITS * 3, NUM_LIMB_BITS * 4)))) }
43 , prime_basis_limb(context, value)
44{
46}
47
48template <typename Builder, typename T>
50 const field_t<Builder>& high_bits_in,
51 const bool can_overflow,
52 const size_t maximum_bitlength)
53{
54 BB_ASSERT_EQ(low_bits_in.is_constant(), high_bits_in.is_constant());
55 BB_ASSERT((can_overflow == true && maximum_bitlength == 0) ||
56 (can_overflow == false && (maximum_bitlength == 0 || maximum_bitlength > (3 * NUM_LIMB_BITS))));
57
58 // Check that the values of two parts are within specified bounds
59 BB_ASSERT_LT(uint256_t(low_bits_in.get_value()), uint256_t(1) << (NUM_LIMB_BITS * 2));
60 BB_ASSERT_LT(uint256_t(high_bits_in.get_value()), uint256_t(1) << (NUM_LIMB_BITS * 2));
61
62 context = low_bits_in.context == nullptr ? high_bits_in.context : low_bits_in.context;
67 if (!low_bits_in.is_constant()) {
68 // Decompose the low bits into 2 limbs and range constrain them.
69 const auto limb_witnesses =
70 decompose_non_native_field_double_width_limb(context, low_bits_in.get_witness_index());
71 limb_0.witness_index = limb_witnesses[0];
72 limb_1.witness_index = limb_witnesses[1];
73 field_t<Builder>::evaluate_linear_identity(low_bits_in, -limb_0, -limb_1 * shift_1, field_t<Builder>(0));
74 } else {
75 uint256_t slice_0 = uint256_t(low_bits_in.additive_constant).slice(0, NUM_LIMB_BITS);
76 uint256_t slice_1 = uint256_t(low_bits_in.additive_constant).slice(NUM_LIMB_BITS, 2 * NUM_LIMB_BITS);
77 limb_0 = field_t(context, bb::fr(slice_0));
78 limb_1 = field_t(context, bb::fr(slice_1));
79 }
80
81 // If we wish to continue working with this element with lazy reductions - i.e. not moding out again after each
82 // addition we apply a more limited range - 2^s for smallest s such that p<2^s (this is the case can_overflow ==
83 // false)
84 uint64_t num_last_limb_bits = (can_overflow) ? NUM_LIMB_BITS : NUM_LAST_LIMB_BITS;
85
86 // if maximum_bitlength is set, this supercedes can_overflow
87 if (maximum_bitlength > 0) {
88 BB_ASSERT_GT(maximum_bitlength, 3 * NUM_LIMB_BITS);
89 BB_ASSERT_LTE(maximum_bitlength, 4 * NUM_LIMB_BITS);
90 num_last_limb_bits = maximum_bitlength - (3 * NUM_LIMB_BITS);
91 }
92 // We create the high limb values similar to the low limb ones above
93 const uint64_t num_high_limb_bits = NUM_LIMB_BITS + num_last_limb_bits;
94 if (!high_bits_in.is_constant()) {
95 // Decompose the high bits into 2 limbs and range constrain them.
96 const auto limb_witnesses = decompose_non_native_field_double_width_limb(
97 context, high_bits_in.get_witness_index(), static_cast<size_t>(num_high_limb_bits));
98 limb_2.witness_index = limb_witnesses[0];
99 limb_3.witness_index = limb_witnesses[1];
100 field_t<Builder>::evaluate_linear_identity(high_bits_in, -limb_2, -limb_3 * shift_1, field_t<Builder>(0));
101 } else {
102 uint256_t slice_2 = uint256_t(high_bits_in.additive_constant).slice(0, NUM_LIMB_BITS);
103 uint256_t slice_3 = uint256_t(high_bits_in.additive_constant).slice(NUM_LIMB_BITS, num_high_limb_bits);
104 limb_2 = field_t(context, bb::fr(slice_2));
105 limb_3 = field_t(context, bb::fr(slice_3));
106 }
107 binary_basis_limbs[0] = Limb(limb_0, DEFAULT_MAXIMUM_LIMB);
108 binary_basis_limbs[1] = Limb(limb_1, DEFAULT_MAXIMUM_LIMB);
109 binary_basis_limbs[2] = Limb(limb_2, DEFAULT_MAXIMUM_LIMB);
110 if (maximum_bitlength > 0) {
111 uint256_t max_limb_value = (uint256_t(1) << (maximum_bitlength - (3 * NUM_LIMB_BITS))) - 1;
112 binary_basis_limbs[3] = Limb(limb_3, max_limb_value);
113 } else {
114 binary_basis_limbs[3] =
115 Limb(limb_3, can_overflow ? DEFAULT_MAXIMUM_LIMB : DEFAULT_MAXIMUM_MOST_SIGNIFICANT_LIMB);
117 prime_basis_limb = low_bits_in + (high_bits_in * shift_2);
118 auto new_tag = OriginTag(low_bits_in.tag, high_bits_in.tag);
119 set_origin_tag(new_tag);
120}
121
122template <typename Builder, typename T>
124 : context(other.context)
125 , binary_basis_limbs{ other.binary_basis_limbs[0],
126 other.binary_basis_limbs[1],
127 other.binary_basis_limbs[2],
128 other.binary_basis_limbs[3] }
129 , prime_basis_limb(other.prime_basis_limb)
130{}
131
132template <typename Builder, typename T>
134 : context(other.context)
135 , binary_basis_limbs{ other.binary_basis_limbs[0],
136 other.binary_basis_limbs[1],
137 other.binary_basis_limbs[2],
138 other.binary_basis_limbs[3] }
139 , prime_basis_limb(other.prime_basis_limb)
140{}
141
142template <typename Builder, typename T>
144 const uint512_t& value,
145 const bool can_overflow,
146 const size_t maximum_bitlength)
147{
148 BB_ASSERT((can_overflow == true && maximum_bitlength == 0) ||
149 (can_overflow == false && (maximum_bitlength == 0 || maximum_bitlength > (3 * NUM_LIMB_BITS))));
151 limbs[0] = value.slice(0, NUM_LIMB_BITS).lo;
152 limbs[1] = value.slice(NUM_LIMB_BITS, NUM_LIMB_BITS * 2).lo;
153 limbs[2] = value.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3).lo;
154 limbs[3] = value.slice(NUM_LIMB_BITS * 3, NUM_LIMB_BITS * 4).lo;
155
156 field_t<Builder> limb_0(ctx);
157 field_t<Builder> limb_1(ctx);
158 field_t<Builder> limb_2(ctx);
159 field_t<Builder> limb_3(ctx);
160 field_t<Builder> prime_limb(ctx);
161 limb_0.witness_index = ctx->add_variable(bb::fr(limbs[0]));
162 limb_1.witness_index = ctx->add_variable(bb::fr(limbs[1]));
163 limb_2.witness_index = ctx->add_variable(bb::fr(limbs[2]));
164 limb_3.witness_index = ctx->add_variable(bb::fr(limbs[3]));
165 prime_limb.witness_index = ctx->add_variable(limb_0.get_value() + limb_1.get_value() * shift_1 +
166 limb_2.get_value() * shift_2 + limb_3.get_value() * shift_3);
167 // evaluate prime basis limb with addition gate that taps into the 4th wire in the next gate
168 ctx->create_big_add_gate({ limb_1.get_witness_index(),
169 limb_2.get_witness_index(),
170 limb_3.get_witness_index(),
171 prime_limb.get_witness_index(),
172 shift_1,
173 shift_2,
174 shift_3,
175 -1,
176 0 },
177 true);
178 // NOTE(https://github.com/AztecProtocol/barretenberg/issues/879): Optimisation opportunity to use a single gate
179 // (and remove dummy gate). Currently, dummy gate is necessary for preceeding big add gate as these gates fall in
180 // the arithmetic block. More details on the linked Github issue.
181 ctx->create_unconstrained_gate(
182 ctx->blocks.arithmetic, ctx->zero_idx(), ctx->zero_idx(), ctx->zero_idx(), limb_0.get_witness_index());
183
184 uint64_t num_last_limb_bits = (can_overflow) ? NUM_LIMB_BITS : NUM_LAST_LIMB_BITS;
185
186 bigfield result(ctx);
187 result.binary_basis_limbs[0] = Limb(limb_0, DEFAULT_MAXIMUM_LIMB);
188 result.binary_basis_limbs[1] = Limb(limb_1, DEFAULT_MAXIMUM_LIMB);
189 result.binary_basis_limbs[2] = Limb(limb_2, DEFAULT_MAXIMUM_LIMB);
190 result.binary_basis_limbs[3] =
191 Limb(limb_3, can_overflow ? DEFAULT_MAXIMUM_LIMB : DEFAULT_MAXIMUM_MOST_SIGNIFICANT_LIMB);
192
193 // if maximum_bitlength is set, this supercedes can_overflow
194 if (maximum_bitlength > 0) {
195 BB_ASSERT_GT(maximum_bitlength, 3 * NUM_LIMB_BITS);
196 num_last_limb_bits = maximum_bitlength - (3 * NUM_LIMB_BITS);
197 uint256_t max_limb_value = (uint256_t(1) << num_last_limb_bits) - 1;
198 result.binary_basis_limbs[3].maximum_value = max_limb_value;
199 }
200 result.prime_basis_limb = prime_limb;
201 ctx->range_constrain_two_limbs(limb_0.get_witness_index(),
202 limb_1.get_witness_index(),
203 static_cast<size_t>(NUM_LIMB_BITS),
204 static_cast<size_t>(NUM_LIMB_BITS),
205 "bigfield::create_from_u512_as_witness: limb 0 or 1 too large");
206 ctx->range_constrain_two_limbs(limb_2.get_witness_index(),
207 limb_3.get_witness_index(),
208 static_cast<size_t>(NUM_LIMB_BITS),
209 static_cast<size_t>(num_last_limb_bits),
210 "bigfield::create_from_u512_as_witness: limb 2 or 3 too large");
211
212 // Mark the element as coming out of nowhere
213 result.set_free_witness_tag();
214
215 return result;
216}
217
218template <typename Builder, typename T> bigfield<Builder, T>::bigfield(const byte_array<Builder>& bytes)
219{
220 BB_ASSERT_EQ(bytes.size(), 32U); // we treat input as a 256-bit big integer
221 const auto split_byte_into_nibbles = [](Builder* ctx, const field_t<Builder>& split_byte) {
222 const uint64_t byte_val = uint256_t(split_byte.get_value()).data[0];
223 const uint64_t lo_nibble_val = byte_val & 15ULL;
224 const uint64_t hi_nibble_val = byte_val >> 4;
225
226 const field_t<Builder> lo_nibble(witness_t<Builder>(ctx, lo_nibble_val));
227 const field_t<Builder> hi_nibble(witness_t<Builder>(ctx, hi_nibble_val));
228 lo_nibble.create_range_constraint(4, "bigfield: lo_nibble too large");
229 hi_nibble.create_range_constraint(4, "bigfield: hi_nibble too large");
230
231 const uint256_t hi_nibble_shift = uint256_t(1) << 4;
232 const field_t<Builder> sum = lo_nibble + (hi_nibble * hi_nibble_shift);
233 sum.assert_equal(split_byte);
234 lo_nibble.set_origin_tag(split_byte.tag);
235 hi_nibble.set_origin_tag(split_byte.tag);
236 return std::make_pair(lo_nibble, hi_nibble);
237 };
238
239 const auto reconstruct_two_limbs = [&split_byte_into_nibbles](Builder* ctx,
240 const field_t<Builder>& hi_bytes,
241 const field_t<Builder>& lo_bytes,
242 const field_t<Builder>& split_byte) {
243 const auto [lo_nibble, hi_nibble] = split_byte_into_nibbles(ctx, split_byte);
244
245 const uint256_t hi_bytes_shift = uint256_t(1) << 4;
246 const uint256_t lo_nibble_shift = uint256_t(1) << 64;
247 field_t<Builder> hi_limb = hi_nibble + hi_bytes * hi_bytes_shift;
248 field_t<Builder> lo_limb = lo_bytes + lo_nibble * lo_nibble_shift;
249 return std::make_pair(lo_limb, hi_limb);
250 };
251 Builder* ctx = bytes.get_context();
252
253 // The input bytes are interpreted as a 256-bit integer, which is split into 4 limbs as follows:
254 //
255 // overlap byte overlap byte
256 // ↓ ↓
257 // [ b31 b30 ... b25 b24 | b23 | b22 b21 ... b16 b15 | b14 b13 ... b8 b7 | b06 | b5 b4 ... b1 b0 ]
258 // |--------------------------|--------------------------|-----------------------|----------------------|
259 // ↑ 68 bits ↑ 68 bits ↑ 68 bits ↑ 52 bits ↑
260 // [ limb l0 | limb l1 | limb l2 | limb l3 ]
261 //
262 const field_t<Builder> hi_8_bytes(bytes.slice(0, 6));
263 const field_t<Builder> mid_split_byte(bytes.slice(6, 1));
264 const field_t<Builder> mid_8_bytes(bytes.slice(7, 8));
265
266 const field_t<Builder> lo_8_bytes(bytes.slice(15, 8));
267 const field_t<Builder> lo_split_byte(bytes.slice(23, 1));
268 const field_t<Builder> lolo_8_bytes(bytes.slice(24, 8));
269
270 const auto [limb0, limb1] = reconstruct_two_limbs(ctx, lo_8_bytes, lolo_8_bytes, lo_split_byte);
271 const auto [limb2, limb3] = reconstruct_two_limbs(ctx, hi_8_bytes, mid_8_bytes, mid_split_byte);
272
273 const auto res = bigfield::unsafe_construct_from_limbs(limb0, limb1, limb2, limb3, true);
275 const auto num_last_limb_bits = 256 - (NUM_LIMB_BITS * 3);
276 res.binary_basis_limbs[3].maximum_value = (uint64_t(1) << num_last_limb_bits);
277 *this = res;
278 set_origin_tag(bytes.get_origin_tag());
279}
280
281template <typename Builder, typename T> bigfield<Builder, T>& bigfield<Builder, T>::operator=(const bigfield& other)
282{
283 if (this == &other) {
284 return *this;
285 }
286 context = other.context;
287 binary_basis_limbs[0] = other.binary_basis_limbs[0];
288 binary_basis_limbs[1] = other.binary_basis_limbs[1];
289 binary_basis_limbs[2] = other.binary_basis_limbs[2];
290 binary_basis_limbs[3] = other.binary_basis_limbs[3];
291 prime_basis_limb = other.prime_basis_limb;
292 return *this;
293}
294
295template <typename Builder, typename T> bigfield<Builder, T>& bigfield<Builder, T>::operator=(bigfield&& other) noexcept
296{
297 context = other.context;
298 binary_basis_limbs[0] = other.binary_basis_limbs[0];
299 binary_basis_limbs[1] = other.binary_basis_limbs[1];
300 binary_basis_limbs[2] = other.binary_basis_limbs[2];
301 binary_basis_limbs[3] = other.binary_basis_limbs[3];
302 prime_basis_limb = other.prime_basis_limb;
303 return *this;
304}
305
306template <typename Builder, typename T> uint512_t bigfield<Builder, T>::get_value() const
307{
308 uint512_t t0 = uint256_t(binary_basis_limbs[0].element.get_value());
309 uint512_t t1 = uint256_t(binary_basis_limbs[1].element.get_value());
310 uint512_t t2 = uint256_t(binary_basis_limbs[2].element.get_value());
311 uint512_t t3 = uint256_t(binary_basis_limbs[3].element.get_value());
312 return t0 + (t1 << (NUM_LIMB_BITS)) + (t2 << (2 * NUM_LIMB_BITS)) + (t3 << (3 * NUM_LIMB_BITS));
313}
314
315template <typename Builder, typename T> uint512_t bigfield<Builder, T>::get_maximum_value() const
316{
317 uint512_t t0 = uint512_t(binary_basis_limbs[0].maximum_value);
318 uint512_t t1 = uint512_t(binary_basis_limbs[1].maximum_value) << NUM_LIMB_BITS;
319 uint512_t t2 = uint512_t(binary_basis_limbs[2].maximum_value) << (NUM_LIMB_BITS * 2);
320 uint512_t t3 = uint512_t(binary_basis_limbs[3].maximum_value) << (NUM_LIMB_BITS * 3);
321 return t0 + t1 + t2 + t3;
322}
323
324template <typename Builder, typename T>
326 const uint256_t& other_maximum_value) const
327{
328 reduction_check();
329 BB_ASSERT_LTE(uint512_t(other_maximum_value) + uint512_t(binary_basis_limbs[0].maximum_value),
330 uint512_t(get_maximum_unreduced_limb_value()));
331 // needed cause a constant doesn't have a valid context
332 Builder* ctx = context ? context : other.context;
333
334 if (is_constant() && other.is_constant()) {
335 return bigfield(ctx, uint256_t((get_value() + uint256_t(other.get_value())) % modulus_u512));
336 }
337
338 bigfield result;
339 // If the original value is constant, we have to reinitialize the higher limbs to be witnesses when adding a witness
340 if (is_constant()) {
341 auto context = other.context;
342 for (size_t i = 1; i < NUM_LIMBS; i++) {
343 // Construct a witness element from the original constant limb
344 result.binary_basis_limbs[i] =
345 Limb(field_t<Builder>::from_witness(context, binary_basis_limbs[i].element.get_value()),
346 binary_basis_limbs[i].maximum_value);
347 // Ensure it is fixed
348 result.binary_basis_limbs[i].element.fix_witness();
349 result.context = ctx;
350 }
351 } else {
352
353 // if this element is a witness, then all limbs will be witnesses
354 result = *this;
355 }
356 result.binary_basis_limbs[0].maximum_value = binary_basis_limbs[0].maximum_value + other_maximum_value;
357
358 result.binary_basis_limbs[0].element = binary_basis_limbs[0].element + other;
359 result.prime_basis_limb = prime_basis_limb + other;
360 result.set_origin_tag(OriginTag(get_origin_tag(), other.tag));
361 return result;
362}
363
364template <typename Builder, typename T>
366{
367 reduction_check();
368 other.reduction_check();
369 // needed cause a constant doesn't have a valid context
370 Builder* ctx = context ? context : other.context;
371
372 if (is_constant() && other.is_constant()) {
373 auto result = bigfield(ctx, uint256_t((get_value() + other.get_value()) % modulus_u512));
374 result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
375 return result;
376 }
377 bigfield result(ctx);
378 result.binary_basis_limbs[0].maximum_value =
379 binary_basis_limbs[0].maximum_value + other.binary_basis_limbs[0].maximum_value;
380 result.binary_basis_limbs[1].maximum_value =
381 binary_basis_limbs[1].maximum_value + other.binary_basis_limbs[1].maximum_value;
382 result.binary_basis_limbs[2].maximum_value =
383 binary_basis_limbs[2].maximum_value + other.binary_basis_limbs[2].maximum_value;
384 result.binary_basis_limbs[3].maximum_value =
385 binary_basis_limbs[3].maximum_value + other.binary_basis_limbs[3].maximum_value;
386
387 // If both the elements are witnesses, we use an optimized addition trick that uses 4 gates instead of 5.
388 //
389 // Naively, we would need 5 gates to add two bigfield elements: 4 gates to add the binary basis limbs and
390 // 1 gate to add the prime basis limbs.
391 //
392 // In the optimized version, we fit 15 witnesses into 4 gates (4 + 4 + 4 + 3 = 15), and we add the prime basis limbs
393 // and one of the binary basis limbs in the first gate.
394 // gate 1: z.limb_0 = x.limb_0 + y.limb_0 && z.prime_limb = x.prime_limb + y.prime_limb
395 // gate 2: z.limb_1 = x.limb_1 + y.limb_1
396 // gate 3: z.limb_2 = x.limb_2 + y.limb_2
397 // gate 4: z.limb_3 = x.limb_3 + y.limb_3
398 //
399 bool both_witness = !is_constant() && !other.is_constant();
400 bool both_prime_limb_multiplicative_constant_one =
401 (prime_basis_limb.multiplicative_constant == 1 && other.prime_basis_limb.multiplicative_constant == 1);
402 if (both_prime_limb_multiplicative_constant_one && both_witness) {
403 bool limbconst = is_constant() || other.is_constant() ||
404 field_ct::witness_indices_match(prime_basis_limb, other.prime_basis_limb);
405 if (!limbconst) {
406 // Extract witness indices and multiplicative constants for binary basis limbs
407 std::array<std::pair<uint32_t, bb::fr>, NUM_LIMBS> x_scaled;
410
411 for (size_t i = 0; i < NUM_LIMBS; ++i) {
412 const auto& x_limb = binary_basis_limbs[i].element;
413 const auto& y_limb = other.binary_basis_limbs[i].element;
414
415 x_scaled[i] = { x_limb.witness_index, x_limb.multiplicative_constant };
416 y_scaled[i] = { y_limb.witness_index, y_limb.multiplicative_constant };
417 c_adds[i] = bb::fr(x_limb.additive_constant + y_limb.additive_constant);
418 }
419
420 // Extract witness indices for prime basis limb
421 uint32_t x_prime(prime_basis_limb.witness_index);
422 uint32_t y_prime(other.prime_basis_limb.witness_index);
423 bb::fr c_prime(prime_basis_limb.additive_constant + other.prime_basis_limb.additive_constant);
424
425 const auto output_witnesses =
426 ctx->evaluate_non_native_field_addition({ x_scaled[0], y_scaled[0], c_adds[0] },
427 { x_scaled[1], y_scaled[1], c_adds[1] },
428 { x_scaled[2], y_scaled[2], c_adds[2] },
429 { x_scaled[3], y_scaled[3], c_adds[3] },
430 { x_prime, y_prime, c_prime });
431
432 result.binary_basis_limbs[0].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[0]);
433 result.binary_basis_limbs[1].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[1]);
434 result.binary_basis_limbs[2].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[2]);
435 result.binary_basis_limbs[3].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[3]);
436 result.prime_basis_limb = field_t<Builder>::from_witness_index(ctx, output_witnesses[4]);
437 result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
438 return result;
439 }
440 }
441
442 // If one of the elements is a constant or its prime limb does not have a multiplicative constant of 1, we
443 // use the standard addition method. This will not use additional gates because field addition with one constant
444 // does not require any additional gates.
445 result.binary_basis_limbs[0].element = binary_basis_limbs[0].element + other.binary_basis_limbs[0].element;
446 result.binary_basis_limbs[1].element = binary_basis_limbs[1].element + other.binary_basis_limbs[1].element;
447 result.binary_basis_limbs[2].element = binary_basis_limbs[2].element + other.binary_basis_limbs[2].element;
448 result.binary_basis_limbs[3].element = binary_basis_limbs[3].element + other.binary_basis_limbs[3].element;
449 result.prime_basis_limb = prime_basis_limb + other.prime_basis_limb;
451 result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
452 return result;
453}
454
455template <typename Builder, typename T>
457{
458 reduction_check();
459 add_a.reduction_check();
460 add_b.reduction_check();
461
462 Builder* ctx = (context == nullptr) ? (add_a.context == nullptr ? add_b.context : add_a.context) : context;
463
464 if (is_constant() && add_a.is_constant() && add_b.is_constant()) {
465 auto result = bigfield(ctx, uint256_t((get_value() + add_a.get_value() + add_b.get_value()) % modulus_u512));
466 result.set_origin_tag(OriginTag(this->get_origin_tag(), add_a.get_origin_tag(), add_b.get_origin_tag()));
467 return result;
468 }
469
470 bigfield result(ctx);
471 result.binary_basis_limbs[0].maximum_value = binary_basis_limbs[0].maximum_value +
472 add_a.binary_basis_limbs[0].maximum_value +
473 add_b.binary_basis_limbs[0].maximum_value;
474 result.binary_basis_limbs[1].maximum_value = binary_basis_limbs[1].maximum_value +
475 add_a.binary_basis_limbs[1].maximum_value +
476 add_b.binary_basis_limbs[1].maximum_value;
477 result.binary_basis_limbs[2].maximum_value = binary_basis_limbs[2].maximum_value +
478 add_a.binary_basis_limbs[2].maximum_value +
479 add_b.binary_basis_limbs[2].maximum_value;
480 result.binary_basis_limbs[3].maximum_value = binary_basis_limbs[3].maximum_value +
481 add_a.binary_basis_limbs[3].maximum_value +
482 add_b.binary_basis_limbs[3].maximum_value;
483
484 result.binary_basis_limbs[0].element =
485 binary_basis_limbs[0].element.add_two(add_a.binary_basis_limbs[0].element, add_b.binary_basis_limbs[0].element);
486 result.binary_basis_limbs[1].element =
487 binary_basis_limbs[1].element.add_two(add_a.binary_basis_limbs[1].element, add_b.binary_basis_limbs[1].element);
488 result.binary_basis_limbs[2].element =
489 binary_basis_limbs[2].element.add_two(add_a.binary_basis_limbs[2].element, add_b.binary_basis_limbs[2].element);
490 result.binary_basis_limbs[3].element =
491 binary_basis_limbs[3].element.add_two(add_a.binary_basis_limbs[3].element, add_b.binary_basis_limbs[3].element);
492 result.prime_basis_limb = prime_basis_limb.add_two(add_a.prime_basis_limb, add_b.prime_basis_limb);
493 result.set_origin_tag(OriginTag(this->get_origin_tag(), add_a.get_origin_tag(), add_b.get_origin_tag()));
494 return result;
495}
496
497template <typename Builder, typename T>
500 Builder* ctx = context ? context : other.context;
501 reduction_check();
502 other.reduction_check();
503
504 if (is_constant() && other.is_constant()) {
505 uint512_t left = get_value() % modulus_u512;
506 uint512_t right = other.get_value() % modulus_u512;
507 uint512_t out = (left + modulus_u512 - right) % modulus_u512;
508
509 auto result = bigfield(ctx, uint256_t(out.lo));
510 result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
511 return result;
513
514 if (other.is_constant()) {
515 uint512_t right = other.get_value() % modulus_u512;
516 uint512_t neg_right = (modulus_u512 - right) % modulus_u512;
517 bigfield summand = bigfield(ctx, uint256_t(neg_right.lo));
518 summand.set_origin_tag(OriginTag(other.get_origin_tag()));
519 return operator+(summand);
520 }
521
539 bigfield result(ctx);
540
549 uint256_t limb_0_maximum_value = other.binary_basis_limbs[0].maximum_value;
550
551 // Compute maximum shift factor for limb_0
552 uint64_t limb_0_borrow_shift = std::max(limb_0_maximum_value.get_msb() + 1, NUM_LIMB_BITS);
554 // Compute the maximum negative value of limb_1, including the bits limb_0 may need to borrow
555 uint256_t limb_1_maximum_value =
556 other.binary_basis_limbs[1].maximum_value + (uint256_t(1) << (limb_0_borrow_shift - NUM_LIMB_BITS));
557
558 // repeat the above for the remaining limbs
559 uint64_t limb_1_borrow_shift = std::max(limb_1_maximum_value.get_msb() + 1, NUM_LIMB_BITS);
560 uint256_t limb_2_maximum_value =
561 other.binary_basis_limbs[2].maximum_value + (uint256_t(1) << (limb_1_borrow_shift - NUM_LIMB_BITS));
562 uint64_t limb_2_borrow_shift = std::max(limb_2_maximum_value.get_msb() + 1, NUM_LIMB_BITS);
563
564 uint256_t limb_3_maximum_value =
565 other.binary_basis_limbs[3].maximum_value + (uint256_t(1) << (limb_2_borrow_shift - NUM_LIMB_BITS));
566
575 uint1024_t constant_to_add_factor =
576 (uint1024_t(limb_3_maximum_value) << (NUM_LIMB_BITS * 3)) / uint1024_t(modulus_u512) + uint1024_t(1);
577 uint512_t constant_to_add = constant_to_add_factor.lo * modulus_u512;
603 uint256_t t0(uint256_t(1) << limb_0_borrow_shift);
604 uint256_t t1((uint256_t(1) << limb_1_borrow_shift) - (uint256_t(1) << (limb_0_borrow_shift - NUM_LIMB_BITS)));
605 uint256_t t2((uint256_t(1) << limb_2_borrow_shift) - (uint256_t(1) << (limb_1_borrow_shift - NUM_LIMB_BITS)));
606 uint256_t t3(uint256_t(1) << (limb_2_borrow_shift - NUM_LIMB_BITS));
607
612 uint256_t to_add_0 = uint256_t(constant_to_add.slice(0, NUM_LIMB_BITS)) + t0;
613 uint256_t to_add_1 = uint256_t(constant_to_add.slice(NUM_LIMB_BITS, NUM_LIMB_BITS * 2)) + t1;
614 uint256_t to_add_2 = uint256_t(constant_to_add.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3)) + t2;
615 uint256_t to_add_3 = uint256_t(constant_to_add.slice(NUM_LIMB_BITS * 3, NUM_LIMB_BITS * 4)) - t3;
616
620 result.binary_basis_limbs[0].maximum_value = binary_basis_limbs[0].maximum_value + to_add_0;
621 result.binary_basis_limbs[1].maximum_value = binary_basis_limbs[1].maximum_value + to_add_1;
622 result.binary_basis_limbs[2].maximum_value = binary_basis_limbs[2].maximum_value + to_add_2;
623 result.binary_basis_limbs[3].maximum_value = binary_basis_limbs[3].maximum_value + to_add_3;
624
628 result.binary_basis_limbs[0].element = binary_basis_limbs[0].element + bb::fr(to_add_0);
629 result.binary_basis_limbs[1].element = binary_basis_limbs[1].element + bb::fr(to_add_1);
630 result.binary_basis_limbs[2].element = binary_basis_limbs[2].element + bb::fr(to_add_2);
631 result.binary_basis_limbs[3].element = binary_basis_limbs[3].element + bb::fr(to_add_3);
632
633 bool both_witness = !is_constant() && !other.is_constant();
634 bool both_prime_limb_multiplicative_constant_one =
635 (prime_basis_limb.multiplicative_constant == 1 && other.prime_basis_limb.multiplicative_constant == 1);
636 if (both_prime_limb_multiplicative_constant_one && both_witness) {
637 bool limbconst = is_constant() || other.is_constant() ||
638 field_ct::witness_indices_match(prime_basis_limb, other.prime_basis_limb);
639
640 if (!limbconst) {
641 // Extract witness indices and multiplicative constants for binary basis limbs
642 std::array<std::pair<uint32_t, bb::fr>, NUM_LIMBS> x_scaled;
643 std::array<std::pair<uint32_t, bb::fr>, NUM_LIMBS> y_scaled;
645
646 for (size_t i = 0; i < NUM_LIMBS; ++i) {
647 const auto& x_limb = result.binary_basis_limbs[i].element;
648 const auto& y_limb = other.binary_basis_limbs[i].element;
649
650 x_scaled[i] = { x_limb.witness_index, x_limb.multiplicative_constant };
651 y_scaled[i] = { y_limb.witness_index, y_limb.multiplicative_constant };
652 c_diffs[i] = bb::fr(x_limb.additive_constant - y_limb.additive_constant);
653 }
654
655 // Extract witness indices for prime basis limb
656 uint32_t x_prime(prime_basis_limb.witness_index);
657 uint32_t y_prime(other.prime_basis_limb.witness_index);
658 bb::fr c_prime(prime_basis_limb.additive_constant - other.prime_basis_limb.additive_constant);
659 uint512_t constant_to_add_mod_native = (constant_to_add) % prime_basis.modulus;
660 c_prime += bb::fr(constant_to_add_mod_native.lo);
661
662 const auto output_witnesses =
663 ctx->evaluate_non_native_field_subtraction({ x_scaled[0], y_scaled[0], c_diffs[0] },
664 { x_scaled[1], y_scaled[1], c_diffs[1] },
665 { x_scaled[2], y_scaled[2], c_diffs[2] },
666 { x_scaled[3], y_scaled[3], c_diffs[3] },
667 { x_prime, y_prime, c_prime });
668
669 result.binary_basis_limbs[0].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[0]);
670 result.binary_basis_limbs[1].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[1]);
671 result.binary_basis_limbs[2].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[2]);
672 result.binary_basis_limbs[3].element = field_t<Builder>::from_witness_index(ctx, output_witnesses[3]);
673 result.prime_basis_limb = field_t<Builder>::from_witness_index(ctx, output_witnesses[4]);
674
675 result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
676 return result;
677 }
678 }
679
680 result.binary_basis_limbs[0].element -= other.binary_basis_limbs[0].element;
681 result.binary_basis_limbs[1].element -= other.binary_basis_limbs[1].element;
682 result.binary_basis_limbs[2].element -= other.binary_basis_limbs[2].element;
683 result.binary_basis_limbs[3].element -= other.binary_basis_limbs[3].element;
684
688 uint512_t constant_to_add_mod_native = (constant_to_add) % prime_basis.modulus;
689 field_t prime_basis_to_add(ctx, bb::fr(constant_to_add_mod_native.lo));
690 result.prime_basis_limb = prime_basis_limb + prime_basis_to_add;
691 result.prime_basis_limb -= other.prime_basis_limb;
692 return result;
693}
694
695template <typename Builder, typename T>
697{
698 // First we do basic reduction checks of individual elements
699 reduction_check();
700 other.reduction_check();
701 Builder* ctx = context ? context : other.context;
702 // Now we can actually compute the quotient and remainder values
703 const auto [quotient_value, remainder_value] = compute_quotient_remainder_values(*this, other, {});
704 bigfield remainder;
705 bigfield quotient;
706 // If operands are constant, define result as a constant value and return
707 if (is_constant() && other.is_constant()) {
708 remainder = bigfield(ctx, uint256_t(remainder_value.lo));
709 remainder.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
710 return remainder;
711 } else {
712 // when writing a*b = q*p + r we wish to enforce r<2^s for smallest s such that p<2^s
713 // hence the second constructor call is with can_overflow=false. This will allow using r in more additions
714 // mod 2^t without needing to apply the mod, where t=4*NUM_LIMB_BITS
715
716 // Check if the product overflows CRT or the quotient can't be contained in a range proof and reduce
717 // accordingly
718 auto [reduction_required, num_quotient_bits] =
719 get_quotient_reduction_info({ get_maximum_value() }, { other.get_maximum_value() }, {});
720 if (reduction_required) {
721 if (get_maximum_value() > other.get_maximum_value()) {
722 self_reduce();
723 } else {
724 other.self_reduce();
725 }
726 return (*this).operator*(other);
727 }
728 quotient = create_from_u512_as_witness(ctx, quotient_value, false, num_quotient_bits);
729 remainder = create_from_u512_as_witness(ctx, remainder_value);
730 };
731
732 // Call `evaluate_multiply_add` to validate the correctness of our computed quotient and remainder
733 unsafe_evaluate_multiply_add(*this, other, {}, quotient, { remainder });
734
735 remainder.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
736 return remainder;
737}
738
739template <typename Builder, typename T>
741{
742
743 return internal_div({ *this }, other, true);
744}
753template <typename Builder, typename T>
755{
756 BB_ASSERT_GT(terms.size(), 0U);
757
758 if (terms.size() == 1) {
759 return terms[0];
760 }
761
762 bigfield acc = terms[0];
763 for (size_t i = 1; i < (terms.size() + 1) / 2; i++) {
764 acc = acc.add_two(terms[2 * i - 1], terms[2 * i]);
765 }
766 if ((terms.size() & 1) == 0) {
767 acc += terms[terms.size() - 1];
768 }
769 return acc;
770}
771
781template <typename Builder, typename T>
783 const bigfield& denominator,
784 bool check_for_zero)
785{
786 BB_ASSERT_LT(numerators.size(), MAXIMUM_SUMMAND_COUNT);
787 if (numerators.empty()) {
788 if (check_for_zero) {
789 // We do not want to trigger division by zero in the empty sum case
790 denominator.assert_is_not_equal(zero());
791 }
792 return bigfield<Builder, T>(denominator.get_context(), uint256_t(0));
793 }
794
795 denominator.reduction_check();
796 Builder* ctx = denominator.context;
797 uint512_t numerator_values(0);
798 bool numerator_constant = true;
799 OriginTag tag = denominator.get_origin_tag();
800 for (const auto& numerator_element : numerators) {
801 ctx = (ctx == nullptr) ? numerator_element.get_context() : ctx;
802 numerator_element.reduction_check();
803 numerator_values += numerator_element.get_value();
804 numerator_constant = numerator_constant && (numerator_element.is_constant());
805 tag = OriginTag(tag, numerator_element.get_origin_tag());
806 }
807
808 // a / b = c
809 // => c * b = a mod p
810 const uint1024_t left = uint1024_t(numerator_values);
811 const uint1024_t right = uint1024_t(denominator.get_value());
812 const uint1024_t modulus(target_basis.modulus);
813 // We don't want to trigger the uint assert
814 uint512_t inverse_value(0);
815 if (right.lo != uint512_t(0)) {
816 inverse_value = right.lo.invmod(target_basis.modulus).lo;
817 }
818 uint1024_t inverse_1024(inverse_value);
819 inverse_value = ((left * inverse_1024) % modulus).lo;
820
821 const uint1024_t quotient_1024 =
822 (uint1024_t(inverse_value) * right + unreduced_zero().get_value() - left) / modulus;
823 const uint512_t quotient_value = quotient_1024.lo;
824
825 bigfield inverse;
826 bigfield quotient;
827 if (numerator_constant && denominator.is_constant()) {
828 if (check_for_zero) {
829 // We want to avoid division by zero in the constant case
830 BB_ASSERT(denominator.get_value() != uint512_t(0), "bigfield: division by zero in constant division");
831 }
832 inverse = bigfield(ctx, uint256_t(inverse_value));
833 inverse.set_origin_tag(tag);
834 return inverse;
835 } else {
836 // NOTE(https://github.com/AztecProtocol/aztec-packages/issues/15385): We can do a simplification when the
837 // denominator is constant. We can compute its inverse out-of-circuit and then multiply it with the numerator.
838 // We only add the check if the result is non-constant
839 std::vector<uint1024_t> numerator_max;
840 for (const auto& n : numerators) {
841 numerator_max.push_back(n.get_maximum_value());
842 }
843
844 auto [reduction_required, num_quotient_bits] =
845 get_quotient_reduction_info({ static_cast<uint512_t>(DEFAULT_MAXIMUM_REMAINDER) },
846 { denominator.get_maximum_value() },
847 { unreduced_zero() },
848 numerator_max);
849 if (reduction_required) {
850
851 denominator.self_reduce();
852 return internal_div(numerators, denominator, check_for_zero);
853 }
854 // We do this after the quotient check, since this creates gates and we don't want to do this twice
855 if (check_for_zero) {
856 denominator.assert_is_not_equal(zero());
857 }
858
859 quotient = create_from_u512_as_witness(ctx, quotient_value, false, num_quotient_bits);
860 inverse = create_from_u512_as_witness(ctx, inverse_value);
861 }
862
863 inverse.set_origin_tag(tag);
864 unsafe_evaluate_multiply_add(denominator, inverse, { unreduced_zero() }, quotient, numerators);
865 return inverse;
866}
867
874template <typename Builder, typename T>
876 const bigfield& denominator)
877{
878 return internal_div(numerators, denominator, false);
879}
880
881template <typename Builder, typename T>
883{
884 return internal_div({ *this }, denominator, false);
885}
886
892template <typename Builder, typename T>
894 const bigfield& denominator)
895{
896 return internal_div(numerators, denominator, true);
897}
898
899template <typename Builder, typename T> bigfield<Builder, T> bigfield<Builder, T>::sqr() const
900{
901 reduction_check();
902 Builder* ctx = context;
903
904 const auto [quotient_value, remainder_value] = compute_quotient_remainder_values(*this, *this, {});
905
906 bigfield remainder;
907 bigfield quotient;
908 if (is_constant()) {
909 remainder = bigfield(ctx, uint256_t(remainder_value.lo));
910 return remainder;
911 } else {
912
913 auto [reduction_required, num_quotient_bits] = get_quotient_reduction_info(
914 { get_maximum_value() }, { get_maximum_value() }, {}, { DEFAULT_MAXIMUM_REMAINDER });
915 if (reduction_required) {
916 self_reduce();
917 return sqr();
918 }
919
920 quotient = create_from_u512_as_witness(ctx, quotient_value, false, num_quotient_bits);
921 remainder = create_from_u512_as_witness(ctx, remainder_value);
922 };
923
924 unsafe_evaluate_square_add(*this, {}, quotient, remainder);
925 remainder.set_origin_tag(get_origin_tag());
926 return remainder;
927}
928
929template <typename Builder, typename T>
931{
932 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
933 reduction_check();
934
935 Builder* ctx = context;
936
938 bool add_constant = true;
939 for (const auto& add_element : to_add) {
940 add_element.reduction_check();
941 add_values += add_element.get_value();
942 add_constant = add_constant && (add_element.is_constant());
943 }
944
945 const uint1024_t left(get_value());
946 const uint1024_t right(get_value());
947 const uint1024_t add_right(add_values);
948 const uint1024_t modulus(target_basis.modulus);
949
950 bigfield remainder;
951 bigfield quotient;
952 if (is_constant()) {
953 if (add_constant) {
954
955 const auto [quotient_1024, remainder_1024] = (left * right + add_right).divmod(modulus);
956 remainder = bigfield(ctx, uint256_t(remainder_1024.lo.lo));
957 // Merge tags
958 OriginTag new_tag = get_origin_tag();
959 for (auto& element : to_add) {
960 new_tag = OriginTag(new_tag, element.get_origin_tag());
961 }
962 remainder.set_origin_tag(new_tag);
963 return remainder;
964 } else {
965
966 const auto [quotient_1024, remainder_1024] = (left * right).divmod(modulus);
967 std::vector<bigfield> new_to_add;
968 for (auto& add_element : to_add) {
969 new_to_add.push_back(add_element);
970 }
971
972 new_to_add.push_back(bigfield(ctx, remainder_1024.lo.lo));
973 return sum(new_to_add);
974 }
975 } else {
976
977 // Check the quotient fits the range proof
978 auto [reduction_required, num_quotient_bits] = get_quotient_reduction_info(
979 { get_maximum_value() }, { get_maximum_value() }, to_add, { DEFAULT_MAXIMUM_REMAINDER });
980
981 if (reduction_required) {
982 self_reduce();
983 return sqradd(to_add);
984 }
985 const auto [quotient_1024, remainder_1024] = (left * right + add_right).divmod(modulus);
986 uint512_t quotient_value = quotient_1024.lo;
987 uint256_t remainder_value = remainder_1024.lo.lo;
988
989 quotient = create_from_u512_as_witness(ctx, quotient_value, false, num_quotient_bits);
990 remainder = create_from_u512_as_witness(ctx, remainder_value);
991 };
992 OriginTag new_tag = get_origin_tag();
993 for (auto& element : to_add) {
994 new_tag = OriginTag(new_tag, element.get_origin_tag());
995 }
996 remainder.set_origin_tag(new_tag);
997 unsafe_evaluate_square_add(*this, to_add, quotient, remainder);
998 return remainder;
999}
1000
1001template <typename Builder, typename T> bigfield<Builder, T> bigfield<Builder, T>::pow(const uint32_t exponent) const
1002{
1003 // Just return one immediately
1004 if (exponent == 0) {
1005 return bigfield(uint256_t(1));
1006 }
1008 // If this is a constant, compute result directly
1009 if (is_constant()) {
1010 auto base_val = get_value();
1011 uint512_t result_val = 1;
1012 uint512_t base = base_val % modulus_u512;
1013 uint32_t shifted_exponent = exponent;
1014
1015 // Fast modular exponentiation
1016 while (shifted_exponent > 0) {
1017 if (shifted_exponent & 1) {
1018 result_val = (uint1024_t(result_val) * uint1024_t(base) % uint1024_t(modulus_u512)).lo;
1019 }
1020 base = (uint1024_t(base) * uint1024_t(base) % uint1024_t(modulus_u512)).lo;
1021 shifted_exponent >>= 1;
1022 }
1023 return bigfield(this->context, uint256_t(result_val.lo));
1024 }
1025
1026 bool accumulator_initialized = false;
1027 bigfield accumulator;
1028 bigfield running_power = *this;
1029 uint32_t shifted_exponent = exponent;
1030
1031 // Square and multiply
1032 while (shifted_exponent != 0) {
1033 if (shifted_exponent & 1) {
1034 if (!accumulator_initialized) {
1035 accumulator = running_power;
1036 accumulator_initialized = true;
1037 } else {
1038 accumulator *= running_power;
1039 }
1040 }
1041 shifted_exponent >>= 1;
1042
1043 // Only square if there are more bits to process.
1044 // It is important to avoid squaring in the final iteration as it otherwise results in
1045 // unwanted gates and variables in the circuit.
1046 if (shifted_exponent != 0) {
1047 running_power = running_power.sqr();
1048 }
1049 }
1050 return accumulator;
1051}
1052
1053template <typename Builder, typename T>
1055{
1056 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
1057 Builder* ctx = context ? context : to_mul.context;
1058 reduction_check();
1059 to_mul.reduction_check();
1060
1062 bool add_constant = true;
1063
1064 for (const auto& add_element : to_add) {
1065 add_element.reduction_check();
1066 add_values += add_element.get_value();
1067 add_constant = add_constant && (add_element.is_constant());
1068 }
1069
1070 const uint1024_t left(get_value());
1071 const uint1024_t mul_right(to_mul.get_value());
1072 const uint1024_t add_right(add_values);
1073 const uint1024_t modulus(target_basis.modulus);
1074
1075 const auto [quotient_1024, remainder_1024] = (left * mul_right + add_right).divmod(modulus);
1076
1077 const uint512_t quotient_value = quotient_1024.lo;
1078 const uint512_t remainder_value = remainder_1024.lo;
1079
1080 bigfield remainder;
1081 bigfield quotient;
1082 if (is_constant() && to_mul.is_constant() && add_constant) {
1083 remainder = bigfield(ctx, uint256_t(remainder_value.lo));
1084 return remainder;
1085 } else if (is_constant() && to_mul.is_constant()) {
1086 const auto [_, mul_remainder_1024] = (left * mul_right).divmod(modulus);
1087 std::vector<bigfield> to_add_copy(to_add);
1088 to_add_copy.push_back(bigfield(ctx, uint256_t(mul_remainder_1024.lo.lo)));
1089 return bigfield::sum(to_add_copy);
1090 } else {
1091 auto [reduction_required, num_quotient_bits] = get_quotient_reduction_info(
1092 { get_maximum_value() }, { to_mul.get_maximum_value() }, to_add, { DEFAULT_MAXIMUM_REMAINDER });
1093 if (reduction_required) {
1094 if (get_maximum_value() > to_mul.get_maximum_value()) {
1095 self_reduce();
1096 } else {
1097 to_mul.self_reduce();
1098 }
1099 return (*this).madd(to_mul, to_add);
1100 }
1101 quotient = create_from_u512_as_witness(ctx, quotient_value, false, num_quotient_bits);
1102 remainder = create_from_u512_as_witness(ctx, remainder_value);
1103 };
1104
1105 // We need to manually propagate the origin tag
1106 OriginTag new_tag = OriginTag(get_origin_tag(), to_mul.get_origin_tag());
1107 for (auto& element : to_add) {
1108 new_tag = OriginTag(new_tag, element.get_origin_tag());
1109 }
1110 remainder.set_origin_tag(new_tag);
1111 quotient.set_origin_tag(new_tag);
1112 unsafe_evaluate_multiply_add(*this, to_mul, to_add, quotient, { remainder });
1113
1114 return remainder;
1115}
1128template <typename Builder, typename T>
1130 std::vector<bigfield>& mul_right,
1131 const std::vector<bigfield>& to_add)
1132{
1133 BB_ASSERT_EQ(mul_left.size(), mul_right.size());
1134 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
1135 BB_ASSERT_LTE(mul_left.size(), MAXIMUM_SUMMAND_COUNT);
1136
1137 const size_t number_of_products = mul_left.size();
1138 // Get the maximum values of elements
1139 std::vector<uint512_t> max_values_left;
1140 std::vector<uint512_t> max_values_right;
1141
1142 max_values_left.reserve(number_of_products);
1143 max_values_right.reserve(number_of_products);
1144 // Do regular reduction checks for all elements
1145 for (auto& left_element : mul_left) {
1146 left_element.reduction_check();
1147 max_values_left.emplace_back(left_element.get_maximum_value());
1148 }
1149
1150 for (auto& right_element : mul_right) {
1151 right_element.reduction_check();
1152 max_values_right.emplace_back(right_element.get_maximum_value());
1153 }
1154
1155 // Perform CRT checks for the whole evaluation
1156 // 1. Check if we can overflow CRT modulus
1157 // 2. Check if the quotient actually fits in our range proof.
1158 // 3. If we haven't passed one of the checks, reduce accordingly, starting with the largest product
1159
1160 // We only get the bitlength of range proof if there is no reduction
1161 bool reduction_required = std::get<0>(
1162 get_quotient_reduction_info(max_values_left, max_values_right, to_add, { DEFAULT_MAXIMUM_REMAINDER }));
1163
1164 if (reduction_required) {
1165
1166 // We are out of luck and have to reduce the elements to keep the intermediate result below CRT modulus
1167 // For that we need to compute the maximum update - how much reducing each element is going to update the
1168 // quotient.
1169 // Contents of the tuple: | Qmax_before-Qmax_after | product number | argument number |
1171
1172 // We use this lambda function before the loop and in the loop itself
1173 // It computes the maximum value update from reduction of each element
1174 auto compute_updates = [](std::vector<std::tuple<uint1024_t, size_t, size_t>>& maxval_updates,
1175 std::vector<bigfield>& m_left,
1176 std::vector<bigfield>& m_right,
1177 size_t number_of_products) {
1178 maxval_updates.resize(0);
1179 maxval_updates.reserve(number_of_products * 2);
1180 // Compute all reduction differences
1181 for (size_t i = 0; i < number_of_products; i++) {
1182 uint1024_t original_left = static_cast<uint1024_t>(m_left[i].get_maximum_value());
1183 uint1024_t original_right = static_cast<uint1024_t>(m_right[i].get_maximum_value());
1184 uint1024_t original_product = original_left * original_right;
1185 if (m_left[i].is_constant()) {
1186 // If the multiplicand is constant, we can't reduce it, so the update is 0.
1187 maxval_updates.emplace_back(std::tuple<uint1024_t, size_t, size_t>(0, i, 0));
1188 } else {
1189 uint1024_t new_product = DEFAULT_MAXIMUM_REMAINDER * original_right;
1190 if (new_product > original_product) {
1191 throw_or_abort("bigfield: This should never happen");
1192 }
1193 maxval_updates.emplace_back(
1194 std::tuple<uint1024_t, size_t, size_t>(original_product - new_product, i, 0));
1195 }
1196 if (m_right[i].is_constant()) {
1197 // If the multiplicand is constant, we can't reduce it, so the update is 0.
1198 maxval_updates.emplace_back(std::tuple<uint1024_t, size_t, size_t>(0, i, 1));
1199 } else {
1200 uint1024_t new_product = DEFAULT_MAXIMUM_REMAINDER * original_left;
1201 if (new_product > original_product) {
1202 throw_or_abort("bigfield: This should never happen");
1203 }
1204 maxval_updates.emplace_back(
1205 std::tuple<uint1024_t, size_t, size_t>(original_product - new_product, i, 1));
1206 }
1207 }
1208 };
1209
1210 auto compare_update_tuples = [](std::tuple<uint1024_t, size_t, size_t>& left_element,
1212 return std::get<0>(left_element) > std::get<0>(right_element);
1213 };
1214
1215 // Now we loop through, reducing 1 element each time. This is costly in code, but allows us to use fewer
1216 // gates
1217
1218 while (reduction_required) {
1219 // Compute the possible reduction updates
1220 compute_updates(maximum_value_updates, mul_left, mul_right, number_of_products);
1221
1222 // Sort the vector, larger values first
1223 std::sort(maximum_value_updates.begin(), maximum_value_updates.end(), compare_update_tuples);
1224
1225 // We choose the largest update
1226 auto [update_size, largest_update_product_index, multiplicand_index] = maximum_value_updates[0];
1227 if (!update_size) {
1228 throw_or_abort("bigfield: Can't reduce further");
1229 }
1230 // Reduce the larger of the multiplicands that compose the product
1231 if (multiplicand_index == 0) {
1232 mul_left[largest_update_product_index].self_reduce();
1233 } else {
1234 mul_right[largest_update_product_index].self_reduce();
1235 }
1236
1237 for (size_t i = 0; i < number_of_products; i++) {
1238 max_values_left[i] = mul_left[i].get_maximum_value();
1239 max_values_right[i] = mul_right[i].get_maximum_value();
1240 }
1241 reduction_required = std::get<0>(
1242 get_quotient_reduction_info(max_values_left, max_values_right, to_add, { DEFAULT_MAXIMUM_REMAINDER }));
1243 }
1244
1245 // Now we have reduced everything exactly to the point of no overflow. There is probably a way to use even
1246 // fewer reductions, but for now this will suffice.
1247 }
1248}
1249
1259template <typename Builder, typename T>
1261 const std::vector<bigfield>& mul_right,
1262 const std::vector<bigfield>& to_add,
1263 bool fix_remainder_to_zero)
1264{
1265 BB_ASSERT_EQ(mul_left.size(), mul_right.size());
1266 BB_ASSERT_LTE(mul_left.size(), MAXIMUM_SUMMAND_COUNT);
1267 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
1268
1269 std::vector<bigfield> mutable_mul_left(mul_left);
1270 std::vector<bigfield> mutable_mul_right(mul_right);
1271
1272 const size_t number_of_products = mul_left.size();
1273
1274 const uint1024_t modulus(target_basis.modulus);
1275 uint1024_t worst_case_product_sum(0);
1276 uint1024_t add_right_constant_sum(0);
1277
1278 // First we do all constant optimizations
1279 bool add_constant = true;
1280 std::vector<bigfield> new_to_add;
1281
1282 OriginTag new_tag = OriginTag::constant(); // Initialize as CONSTANT so merging with input tags works correctly
1283 // Merge all tags. Do it in pairs (logically a submitted value can be masked by a challenge)
1284 for (auto [left_element, right_element] : zip_view(mul_left, mul_right)) {
1285 new_tag = OriginTag(new_tag, OriginTag(left_element.get_origin_tag(), right_element.get_origin_tag()));
1286 }
1287 for (auto& element : to_add) {
1288 new_tag = OriginTag(new_tag, element.get_origin_tag());
1289 }
1290
1291 for (const auto& add_element : to_add) {
1292 add_element.reduction_check();
1293 if (add_element.is_constant()) {
1294 add_right_constant_sum += uint1024_t(add_element.get_value());
1295 } else {
1296 add_constant = false;
1297 new_to_add.push_back(add_element);
1298 }
1299 }
1300
1301 // Compute the product sum
1302 // Optimize constant use
1303 uint1024_t sum_of_constant_products(0);
1304 std::vector<bigfield> new_input_left;
1305 std::vector<bigfield> new_input_right;
1306 bool product_sum_constant = true;
1307 for (size_t i = 0; i < number_of_products; i++) {
1308 if (mutable_mul_left[i].is_constant() && mutable_mul_right[i].is_constant()) {
1309 // If constant, just add to the sum
1310 sum_of_constant_products +=
1311 uint1024_t(mutable_mul_left[i].get_value()) * uint1024_t(mutable_mul_right[i].get_value());
1312 } else {
1313 // If not, add to nonconstant sum and remember the elements
1314 new_input_left.push_back(mutable_mul_left[i]);
1315 new_input_right.push_back(mutable_mul_right[i]);
1316 product_sum_constant = false;
1317 }
1318 }
1319
1320 Builder* ctx = nullptr;
1321 // Search through all multiplicands on the left
1322 for (auto& el : mutable_mul_left) {
1323 if (el.context) {
1324 ctx = el.context;
1325 break;
1326 }
1327 }
1328 // And on the right
1329 if (!ctx) {
1330 for (auto& el : mutable_mul_right) {
1331 if (el.context) {
1332 ctx = el.context;
1333 break;
1334 }
1335 }
1336 }
1337 if (product_sum_constant) {
1338 if (add_constant) {
1339 // Simply return the constant, no need unsafe_multiply_add
1340 const auto [quotient_1024, remainder_1024] =
1341 (sum_of_constant_products + add_right_constant_sum).divmod(modulus);
1342 BB_ASSERT(!fix_remainder_to_zero || remainder_1024 == 0);
1343 auto result = bigfield(ctx, uint256_t(remainder_1024.lo.lo));
1344 result.set_origin_tag(new_tag);
1345 return result;
1346 } else {
1347 const auto [quotient_1024, remainder_1024] =
1348 (sum_of_constant_products + add_right_constant_sum).divmod(modulus);
1349 uint256_t remainder_value = remainder_1024.lo.lo;
1350 bigfield result;
1351 if (remainder_value == uint256_t(0)) {
1352 // No need to add extra term to new_to_add
1353 result = sum(new_to_add);
1354 } else {
1355 // Add the constant term
1356 new_to_add.push_back(bigfield(ctx, uint256_t(remainder_value)));
1357 result = sum(new_to_add);
1358 }
1359 if (fix_remainder_to_zero) {
1360 result.self_reduce();
1361 result.assert_equal(zero());
1362 }
1363 result.set_origin_tag(new_tag);
1364 return result;
1365 }
1366 }
1367
1368 // Now that we know that there is at least 1 non-constant multiplication, we can start estimating reductions.
1369 BB_ASSERT(ctx != nullptr);
1370
1371 // Compute the constant term we're adding
1372 const auto [_, constant_part_remainder_1024] = (sum_of_constant_products + add_right_constant_sum).divmod(modulus);
1373 const uint256_t constant_part_remainder_256 = constant_part_remainder_1024.lo.lo;
1374
1375 if (constant_part_remainder_256 != uint256_t(0)) {
1376 new_to_add.push_back(bigfield(ctx, constant_part_remainder_256));
1377 }
1378 // Compute added sum
1379 uint1024_t add_right_final_sum(0);
1380 uint1024_t add_right_maximum(0);
1381 for (const auto& add_element : new_to_add) {
1382 // Technically not needed, but better to leave just in case
1383 add_element.reduction_check();
1384 add_right_final_sum += uint1024_t(add_element.get_value());
1385
1386 add_right_maximum += uint1024_t(add_element.get_maximum_value());
1387 }
1388 const size_t final_number_of_products = new_input_left.size();
1389
1390 // We need to check if it is possible to reduce the products enough
1391 worst_case_product_sum = uint1024_t(final_number_of_products) * uint1024_t(DEFAULT_MAXIMUM_REMAINDER) *
1392 uint1024_t(DEFAULT_MAXIMUM_REMAINDER);
1393
1394 // Check that we can actually reduce the products enough, this assert will probably never get triggered
1395 BB_ASSERT_LT(worst_case_product_sum + add_right_maximum, get_maximum_crt_product());
1396
1397 // We've collapsed all constants, checked if we can compute the sum of products in the worst case, time to check
1398 // if we need to reduce something
1399 perform_reductions_for_mult_madd(new_input_left, new_input_right, new_to_add);
1400 uint1024_t sum_of_products_final(0);
1401 for (size_t i = 0; i < final_number_of_products; i++) {
1402 sum_of_products_final += uint1024_t(new_input_left[i].get_value()) * uint1024_t(new_input_right[i].get_value());
1403 }
1404
1405 // Get the number of range proof bits for the quotient
1406 const size_t num_quotient_bits = get_quotient_max_bits({ DEFAULT_MAXIMUM_REMAINDER });
1407
1408 // Compute the quotient and remainder
1409 const auto [quotient_1024, remainder_1024] = (sum_of_products_final + add_right_final_sum).divmod(modulus);
1410
1411 // If we are establishing an identity and the remainder has to be zero, we need to check, that it actually is
1412
1413 if (fix_remainder_to_zero) {
1414 // This is not the only check. Circuit check is coming later :)
1415 BB_ASSERT_EQ(remainder_1024.lo, uint512_t(0));
1416 }
1417 const uint512_t quotient_value = quotient_1024.lo;
1418 const uint512_t remainder_value = remainder_1024.lo;
1419
1420 bigfield remainder;
1421 bigfield quotient;
1422 // Constrain quotient to mitigate CRT overflow attacks
1423 quotient = create_from_u512_as_witness(ctx, quotient_value, false, num_quotient_bits);
1424
1425 if (fix_remainder_to_zero) {
1426 remainder = zero();
1427 // remainder needs to be defined as wire value and not selector values to satisfy
1428 // Ultra's bigfield custom gates
1429 remainder.convert_constant_to_fixed_witness(ctx);
1430 } else {
1431 remainder = create_from_u512_as_witness(ctx, remainder_value);
1432 }
1433
1434 // We need to manually propagate the origin tag
1435 quotient.set_origin_tag(new_tag);
1436 remainder.set_origin_tag(new_tag);
1437
1438 unsafe_evaluate_multiple_multiply_add(new_input_left, new_input_right, new_to_add, quotient, { remainder });
1439
1440 return remainder;
1441}
1442
1448template <typename Builder, typename T>
1450 const bigfield& right_a,
1451 const bigfield& left_b,
1452 const bigfield& right_b,
1453 const std::vector<bigfield>& to_add)
1454{
1455 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
1456 left_a.reduction_check();
1457 right_a.reduction_check();
1458 left_b.reduction_check();
1459 right_b.reduction_check();
1460
1461 std::vector<bigfield> mul_left = { left_a, left_b };
1462 std::vector<bigfield> mul_right = { right_a, right_b };
1463
1464 return mult_madd(mul_left, mul_right, to_add);
1465}
1466
1485template <typename Builder, typename T>
1487 const std::vector<bigfield>& mul_right,
1488 const bigfield& divisor,
1489 const std::vector<bigfield>& to_sub,
1490 bool enable_divisor_nz_check)
1491{
1492 // Check the basics
1493 BB_ASSERT_EQ(mul_left.size(), mul_right.size());
1494 BB_ASSERT((divisor.get_value() % modulus_u512) != 0, "bigfield: Division by zero in msub_div");
1495
1496 OriginTag new_tag = divisor.get_origin_tag();
1497 for (auto [left_element, right_element] : zip_view(mul_left, mul_right)) {
1498 new_tag = OriginTag(new_tag, OriginTag(left_element.get_origin_tag(), right_element.get_origin_tag()));
1499 }
1500 for (auto& element : to_sub) {
1501 new_tag = OriginTag(new_tag, element.get_origin_tag());
1502 }
1503 // Get the context
1504 Builder* ctx = divisor.context;
1505 if (ctx == NULL) {
1506 for (auto& el : mul_left) {
1507 if (el.context != NULL) {
1508 ctx = el.context;
1509 break;
1510 }
1511 }
1512 }
1513 if (ctx == NULL) {
1514 for (auto& el : mul_right) {
1515 if (el.context != NULL) {
1516 ctx = el.context;
1517 break;
1518 }
1519 }
1520 }
1521 if (ctx == NULL) {
1522 for (auto& el : to_sub) {
1523 if (el.context != NULL) {
1524 ctx = el.context;
1525 break;
1526 }
1527 }
1528 }
1529 const size_t num_multiplications = mul_left.size();
1530 native product_native = 0;
1531 bool products_constant = true;
1532
1533 // This check is optional, because it is heavy and often we don't need it at all
1534 if (enable_divisor_nz_check) {
1535 divisor.assert_is_not_equal(zero());
1536 }
1537
1538 // Compute the sum of products
1539 for (size_t i = 0; i < num_multiplications; ++i) {
1540 const native mul_left_native(uint512_t(mul_left[i].get_value() % modulus_u512).lo);
1541 const native mul_right_native(uint512_t(mul_right[i].get_value() % modulus_u512).lo);
1542 product_native += (mul_left_native * -mul_right_native);
1543 products_constant = products_constant && mul_left[i].is_constant() && mul_right[i].is_constant();
1544 }
1545
1546 // Compute the sum of to_sub
1547 native sub_native(0);
1548 bool sub_constant = true;
1549 for (const auto& sub : to_sub) {
1550 sub_native += (uint512_t(sub.get_value() % modulus_u512).lo);
1551 sub_constant = sub_constant && sub.is_constant();
1552 }
1553
1554 native divisor_native(uint512_t(divisor.get_value() % modulus_u512).lo);
1555
1556 // Compute the result
1557 const native result_native = (product_native - sub_native) / divisor_native;
1558
1559 const uint1024_t result_value = uint1024_t(uint512_t(static_cast<uint256_t>(result_native)));
1560
1561 // If everything is constant, then we just return the constant
1562 if (sub_constant && products_constant && divisor.is_constant()) {
1563 auto result = bigfield(ctx, uint256_t(result_value.lo.lo));
1564 result.set_origin_tag(new_tag);
1565 return result;
1566 }
1567
1568 BB_ASSERT(ctx != NULL);
1569 // Create the result witness
1570 bigfield result = create_from_u512_as_witness(ctx, result_value.lo);
1571
1572 // We need to manually propagate the origin tag
1573 result.set_origin_tag(new_tag);
1574
1575 std::vector<bigfield> eval_left{ result };
1576 std::vector<bigfield> eval_right{ divisor };
1577 for (const auto& in : mul_left) {
1578 eval_left.emplace_back(in);
1579 }
1580 for (const auto& in : mul_right) {
1581 eval_right.emplace_back(in);
1582 }
1583
1584 mult_madd(eval_left, eval_right, to_sub, true);
1585
1586 return result;
1587}
1588
1589template <typename Builder, typename T>
1591{
1592 Builder* ctx = context ? context : predicate.context;
1593
1594 if (is_constant() && predicate.is_constant()) {
1595 auto result = *this;
1596 if (predicate.get_value()) {
1597 BB_ASSERT_LT(get_value(), modulus_u512);
1598 uint512_t out_val = (modulus_u512 - get_value()) % modulus_u512;
1599 result = bigfield(ctx, out_val.lo);
1600 }
1601 result.set_origin_tag(OriginTag(get_origin_tag(), predicate.get_origin_tag()));
1602 return result;
1603 }
1604 reduction_check();
1605
1606 // We want to check:
1607 // predicate = 1 ==> (0 - *this)
1608 // predicate = 0 ==> *this
1609 //
1610 // We just use the conditional_assign method to do this as it costs the same number of gates as computing
1611 // p * (0 - *this) + (1 - p) * (*this)
1612 //
1613 bigfield<Builder, T> negative_this = zero() - *this;
1614 bigfield<Builder, T> result = bigfield<Builder, T>::conditional_assign(predicate, negative_this, *this);
1615
1616 return result;
1617}
1618
1619template <typename Builder, typename T>
1621 const bool_t<Builder>& predicate) const
1622{
1623 // If the predicate is constant, the conditional selection can be done out of circuit
1624 if (predicate.is_constant()) {
1625 bigfield result = predicate.get_value() ? other : *this;
1626 result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag(), predicate.get_origin_tag()));
1627 return result;
1628 }
1629
1630 // If both elements are the same, we can just return one of them
1631 auto is_limb_same = [](const field_ct& a, const field_ct& b) {
1632 const bool is_witness_index_same = field_ct::witness_indices_match(a, b);
1633 const bool is_add_constant_same = a.additive_constant == b.additive_constant;
1634 const bool is_mul_constant_same = a.multiplicative_constant == b.multiplicative_constant;
1635 return is_witness_index_same && is_add_constant_same && is_mul_constant_same;
1636 };
1637
1638 bool is_limb_0_same = is_limb_same(binary_basis_limbs[0].element, other.binary_basis_limbs[0].element);
1639 bool is_limb_1_same = is_limb_same(binary_basis_limbs[1].element, other.binary_basis_limbs[1].element);
1640 bool is_limb_2_same = is_limb_same(binary_basis_limbs[2].element, other.binary_basis_limbs[2].element);
1641 bool is_limb_3_same = is_limb_same(binary_basis_limbs[3].element, other.binary_basis_limbs[3].element);
1642 bool is_prime_limb_same = is_limb_same(prime_basis_limb, other.prime_basis_limb);
1643 if (is_limb_0_same && is_limb_1_same && is_limb_2_same && is_limb_3_same && is_prime_limb_same) {
1644 return *this;
1645 }
1646
1647 Builder* ctx = context ? context : (other.context ? other.context : predicate.context);
1648
1649 // For each limb, we must select:
1650 // `this` if predicate == 0
1651 // `other` if predicate == 1
1652 //
1653 // Thus, we compute the resulting limb as follows:
1654 // result.limb := predicate * (other.limb - this.limb) + this.limb.
1655 //
1656 // Note that each call to `madd` will add a gate as predicate is a witness at this point.
1657 // There can be edge cases where `this` and `other` are both constants and only differ in one limb.
1658 // In such a case, the `madd` for the differing limb will be a no-op (i.e., redundant gate), as the
1659 // difference will be zero. For example,
1660 // binary limbs prime limb
1661 // this: (0x5, 0x1, 0x0, 0x0) (0x100000000000000005)
1662 // other: (0x7, 0x1, 0x0, 0x0) (0x100000000000000007)
1663 // Here, the `madd` for the second, third and fourth binary limbs will be a no-op, as the difference
1664 // between `this` and `other` is zero for those limbs.
1665 //
1666 // We allow this to happen because we want to maintain limb consistency (i.e., all limbs either witness or
1667 // constant).
1668 field_ct binary_limb_0 = field_ct(predicate).madd(
1669 other.binary_basis_limbs[0].element - binary_basis_limbs[0].element, binary_basis_limbs[0].element);
1670 field_ct binary_limb_1 = field_ct(predicate).madd(
1671 other.binary_basis_limbs[1].element - binary_basis_limbs[1].element, binary_basis_limbs[1].element);
1672 field_ct binary_limb_2 = field_ct(predicate).madd(
1673 other.binary_basis_limbs[2].element - binary_basis_limbs[2].element, binary_basis_limbs[2].element);
1674 field_ct binary_limb_3 = field_ct(predicate).madd(
1675 other.binary_basis_limbs[3].element - binary_basis_limbs[3].element, binary_basis_limbs[3].element);
1676 field_ct prime_limb = field_ct(predicate).madd(other.prime_basis_limb - prime_basis_limb, prime_basis_limb);
1677
1678 bigfield result(ctx);
1679 // the maximum of the maximal values of elements is large enough
1680 result.binary_basis_limbs[0] =
1681 Limb(binary_limb_0, std::max(binary_basis_limbs[0].maximum_value, other.binary_basis_limbs[0].maximum_value));
1682 result.binary_basis_limbs[1] =
1683 Limb(binary_limb_1, std::max(binary_basis_limbs[1].maximum_value, other.binary_basis_limbs[1].maximum_value));
1684 result.binary_basis_limbs[2] =
1685 Limb(binary_limb_2, std::max(binary_basis_limbs[2].maximum_value, other.binary_basis_limbs[2].maximum_value));
1686 result.binary_basis_limbs[3] =
1687 Limb(binary_limb_3, std::max(binary_basis_limbs[3].maximum_value, other.binary_basis_limbs[3].maximum_value));
1688 result.prime_basis_limb = prime_limb;
1689 result.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag(), predicate.tag));
1690 return result;
1691}
1692
1713template <typename Builder, typename T> bool_t<Builder> bigfield<Builder, T>::operator==(const bigfield& other) const
1714{
1715 Builder* ctx = context ? context : other.get_context();
1716 auto lhs = get_value() % modulus_u512;
1717 auto rhs = other.get_value() % modulus_u512;
1718 bool is_equal_raw = (lhs == rhs);
1719 if (is_constant() && other.is_constant()) {
1720 return is_equal_raw;
1721 }
1722
1723 // The context should not be null at this point.
1724 BB_ASSERT(ctx != NULL);
1725 bool_t<Builder> is_equal = witness_t<Builder>(ctx, is_equal_raw);
1726
1727 // We need to manually propagate the origin tag
1728 is_equal.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
1729
1730 bigfield diff = (*this) - other;
1731 native diff_native = native((diff.get_value() % modulus_u512).lo);
1732 native inverse_native = is_equal_raw ? 0 : diff_native.invert();
1733
1734 bigfield inverse = bigfield::from_witness(ctx, inverse_native);
1735
1736 // We need to manually propagate the origin tag
1737 inverse.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
1738
1739 bigfield multiplicand = bigfield::conditional_assign(is_equal, one(), inverse);
1740
1741 bigfield product = diff * multiplicand;
1742
1744
1745 product.prime_basis_limb.assert_equal(result);
1746 product.binary_basis_limbs[0].element.assert_equal(result);
1747 product.binary_basis_limbs[1].element.assert_equal(0);
1748 product.binary_basis_limbs[2].element.assert_equal(0);
1749 product.binary_basis_limbs[3].element.assert_equal(0);
1750 is_equal.set_origin_tag(OriginTag(get_origin_tag(), other.get_origin_tag()));
1751 return is_equal;
1752}
1753
1754template <typename Builder, typename T> void bigfield<Builder, T>::reduction_check() const
1755{
1756 if (is_constant()) {
1757 uint256_t reduced_value = (get_value() % modulus_u512).lo;
1758 bigfield reduced(context, uint256_t(reduced_value));
1759 // Save tags
1760 const auto origin_tags = std::vector({ binary_basis_limbs[0].element.get_origin_tag(),
1761 binary_basis_limbs[1].element.get_origin_tag(),
1762 binary_basis_limbs[2].element.get_origin_tag(),
1763 binary_basis_limbs[3].element.get_origin_tag(),
1764 prime_basis_limb.get_origin_tag() });
1765
1766 // Directly assign to mutable members (avoiding assignment operator)
1767 binary_basis_limbs[0] = reduced.binary_basis_limbs[0];
1768 binary_basis_limbs[1] = reduced.binary_basis_limbs[1];
1769 binary_basis_limbs[2] = reduced.binary_basis_limbs[2];
1770 binary_basis_limbs[3] = reduced.binary_basis_limbs[3];
1771 prime_basis_limb = reduced.prime_basis_limb;
1772
1773 // Preserve origin tags (useful in simulator)
1774 binary_basis_limbs[0].element.set_origin_tag(origin_tags[0]);
1775 binary_basis_limbs[1].element.set_origin_tag(origin_tags[1]);
1776 binary_basis_limbs[2].element.set_origin_tag(origin_tags[2]);
1777 binary_basis_limbs[3].element.set_origin_tag(origin_tags[3]);
1778 prime_basis_limb.set_origin_tag(origin_tags[4]);
1779 return;
1780 }
1781
1782 uint256_t maximum_unreduced_limb_value = get_maximum_unreduced_limb_value();
1783 bool limb_overflow_test_0 = binary_basis_limbs[0].maximum_value > maximum_unreduced_limb_value;
1784 bool limb_overflow_test_1 = binary_basis_limbs[1].maximum_value > maximum_unreduced_limb_value;
1785 bool limb_overflow_test_2 = binary_basis_limbs[2].maximum_value > maximum_unreduced_limb_value;
1786 bool limb_overflow_test_3 = binary_basis_limbs[3].maximum_value > maximum_unreduced_limb_value;
1787 if (get_maximum_value() > get_maximum_unreduced_value() || limb_overflow_test_0 || limb_overflow_test_1 ||
1788 limb_overflow_test_2 || limb_overflow_test_3) {
1789 self_reduce();
1790 }
1791}
1792
1793template <typename Builder, typename T> void bigfield<Builder, T>::sanity_check() const
1794{
1795
1796 uint256_t prohibited_limb_value = get_prohibited_limb_value();
1797 bool limb_overflow_test_0 = binary_basis_limbs[0].maximum_value > prohibited_limb_value;
1798 bool limb_overflow_test_1 = binary_basis_limbs[1].maximum_value > prohibited_limb_value;
1799 bool limb_overflow_test_2 = binary_basis_limbs[2].maximum_value > prohibited_limb_value;
1800 bool limb_overflow_test_3 = binary_basis_limbs[3].maximum_value > prohibited_limb_value;
1801 // max_val < sqrt(2^T * n)
1802 // Note this is a static assertion, so it is not checked at runtime
1803 BB_ASSERT(!(get_maximum_value() > get_prohibited_value() || limb_overflow_test_0 || limb_overflow_test_1 ||
1804 limb_overflow_test_2 || limb_overflow_test_3));
1805}
1806
1807template <typename Builder, typename T>
1808void bigfield<Builder, T>::assert_zero_if(const bool_t<Builder>& predicate, std::string const& msg) const
1809{
1810 // Assert that all limbs are zero when predicate is true
1811 const field_ct predicate_field = field_ct(predicate);
1812 (binary_basis_limbs[0].element * predicate_field).assert_is_zero(msg + ": binary limb 0 not zero");
1813 (binary_basis_limbs[1].element * predicate_field).assert_is_zero(msg + ": binary limb 1 not zero");
1814 (binary_basis_limbs[2].element * predicate_field).assert_is_zero(msg + ": binary limb 2 not zero");
1815 (binary_basis_limbs[3].element * predicate_field).assert_is_zero(msg + ": binary limb 3 not zero");
1816 (prime_basis_limb * predicate_field).assert_is_zero(msg + ": prime limb not zero");
1817}
1818
1819// Underneath performs unsafe_assert_less_than(modulus)
1820// create a version with mod 2^t element part in [0,p-1]
1821// After range-constraining to size 2^s, we check (p-1)-a is non-negative as integer.
1822// We perform subtraction using carries on blocks of size 2^b. The operations inside the blocks are done mod r
1823// Including the effect of carries the operation inside each limb is in the range [-2^b-1,2^{b+1}]
1824// Assuming this values are all distinct mod r, which happens e.g. if r/2>2^{b+1}, then if all limb values are
1825// non-negative at the end of subtraction, we know the subtraction result is positive as integers and a<p
1826template <typename Builder, typename T> void bigfield<Builder, T>::assert_is_in_field(std::string const& msg) const
1827{
1828 assert_less_than(modulus, msg == "bigfield::assert_is_in_field" ? "bigfield::assert_less_than" : msg);
1829}
1830
1831// Asserts that the element is < upper_limit. We first range constrain the limbs and then calls
1832// unsafe_assert_less_than(upper_limit).
1833template <typename Builder, typename T>
1834void bigfield<Builder, T>::assert_less_than(const uint256_t& upper_limit, std::string const& msg) const
1835{
1836 // For constant bigfields, just verify the value is in range (no circuit constraints needed)
1837 if (is_constant()) {
1838 BB_ASSERT((get_value() % modulus_u512).lo < upper_limit, msg);
1839 return;
1840 }
1841
1842 bool is_default_msg = msg == "bigfield::assert_less_than";
1843
1844 // Range constrain the binary basis limbs of the element to respective limb sizes.
1845 // This is required because the comparison is done using subtractions, which can result in overflows.
1846 // Range constrain the first two limbs each to NUM_LIMB_BITS
1847 auto ctx = get_context();
1848 ctx->range_constrain_two_limbs(binary_basis_limbs[0].element.get_witness_index(),
1849 binary_basis_limbs[1].element.get_witness_index(),
1850 static_cast<size_t>(NUM_LIMB_BITS),
1851 static_cast<size_t>(NUM_LIMB_BITS),
1852 is_default_msg ? "bigfield::assert_less_than: limb 0 or 1 too large" : msg);
1853
1854 // Range constrain the last two limbs to NUM_LIMB_BITS and NUM_LAST_LIMB_BITS
1855 ctx->range_constrain_two_limbs(binary_basis_limbs[2].element.get_witness_index(),
1856 binary_basis_limbs[3].element.get_witness_index(),
1857 static_cast<size_t>(NUM_LIMB_BITS),
1858 static_cast<size_t>(NUM_LAST_LIMB_BITS),
1859 is_default_msg ? "bigfield::assert_less_than: limb 2 or 3 too large" : msg);
1860
1861 // Now we can check that the element is < upper_limit.
1862 unsafe_assert_less_than(upper_limit, is_default_msg ? "bigfield::unsafe_assert_less_than" : msg);
1863}
1864
1865// Return (a < b) as bool circuit type.
1866template <typename Builder, typename T>
1867bool_t<Builder> bigfield<Builder, T>::is_less_than(const uint256_t& upper_limit, std::string const& msg) const
1868{
1869 bool is_default_msg = msg == "bigfield::is_less_than";
1870
1871 Builder* ctx = get_context();
1872
1873 // Range constraint the limbs, this is required by the ranged_less_than function
1874 ctx->range_constrain_two_limbs(binary_basis_limbs[0].element.get_witness_index(),
1875 binary_basis_limbs[1].element.get_witness_index(),
1876 static_cast<size_t>(NUM_LIMB_BITS),
1877 static_cast<size_t>(NUM_LIMB_BITS),
1878 is_default_msg ? "bigfield::is_less_than: limb 0 or 1 too large" : msg);
1879
1880 ctx->range_constrain_two_limbs(binary_basis_limbs[2].element.get_witness_index(),
1881 binary_basis_limbs[3].element.get_witness_index(),
1882 static_cast<size_t>(NUM_LIMB_BITS),
1883 static_cast<size_t>(NUM_LIMB_BITS),
1884 is_default_msg ? "bigfield::is_less_than: limb 2 or 3 too large" : msg);
1885
1886 const uint256_t upper_limit_value_0 = upper_limit.slice(0, NUM_LIMB_BITS);
1887 const uint256_t upper_limit_value_1 = upper_limit.slice(NUM_LIMB_BITS, NUM_LIMB_BITS * 2);
1888 const uint256_t upper_limit_value_2 = upper_limit.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3);
1889 const uint256_t upper_limit_value_3 = upper_limit.slice(NUM_LIMB_BITS * 3, NUM_LIMB_BITS * 4);
1890
1891 bool_t<Builder> third_limb_is_smaller =
1892 binary_basis_limbs[3].element.template ranged_less_than<NUM_LIMB_BITS>(field_t<Builder>(upper_limit_value_3));
1893 bool_t<Builder> third_limb_is_equal = binary_basis_limbs[3].element == field_t<Builder>(upper_limit_value_3);
1894
1895 bool_t<Builder> second_limb_is_smaller =
1896 binary_basis_limbs[2].element.template ranged_less_than<NUM_LIMB_BITS>(field_t<Builder>(upper_limit_value_2));
1897 bool_t<Builder> second_limb_is_equal = binary_basis_limbs[2].element == field_t<Builder>(upper_limit_value_2);
1898
1899 bool_t<Builder> first_limb_is_smaller =
1900 binary_basis_limbs[1].element.template ranged_less_than<NUM_LIMB_BITS>(field_t<Builder>(upper_limit_value_1));
1901 bool_t<Builder> first_limb_is_equal = binary_basis_limbs[1].element == field_t<Builder>(upper_limit_value_1);
1902
1903 bool_t<Builder> zeroth_limb_is_smaller =
1904 binary_basis_limbs[0].element.template ranged_less_than<NUM_LIMB_BITS>(field_t<Builder>(upper_limit_value_0));
1905
1906 // Limb comparison: we start from the most-significant limb and proceed to the least-significant limb
1907 bool_t<Builder> result =
1908 third_limb_is_smaller || (third_limb_is_equal && second_limb_is_smaller) ||
1909 (third_limb_is_equal && second_limb_is_equal && first_limb_is_smaller) ||
1910 (third_limb_is_equal && second_limb_is_equal && first_limb_is_equal && zeroth_limb_is_smaller);
1911
1912 return result;
1913}
1914
1915// Reduces the element mod p. This is a strict reduction mod p, so the output is guaranteed to be < p.
1916template <typename Builder, typename T> void bigfield<Builder, T>::reduce_mod_target_modulus() const
1917{
1918 // First we lazy-reduce the element mod p, and constrain the output/remainder to be < 2^s where s = ceil(log2(p)).
1919 // This brings the element into the range [0, 2^s) such that the limbs of the reduced element are all range
1920 // constrained to < 2^b (last limb < 2^(s - 3b)).
1921 self_reduce();
1922
1923 // Then we constrain the element to be < target modulus using strict comparison.
1924 unsafe_assert_less_than(modulus);
1925}
1926
1927// Asserts that the element is < upper_limit. We mark this as unsafe because it assumes that the element is already
1928// range constrained to < 2^s where s = ceil(log2(p)).
1929template <typename Builder, typename T>
1930void bigfield<Builder, T>::unsafe_assert_less_than(const uint256_t& upper_limit, std::string const& msg) const
1931{
1932 // Warning: this assumes we have run circuit construction at least once in debug mode where large non reduced
1933 // constants are NOT allowed via ASSERT
1934 if (is_constant()) {
1935 BB_ASSERT_LT(get_value(), static_cast<uint512_t>(upper_limit));
1936 return;
1937 }
1938
1939 BB_ASSERT(upper_limit != 0);
1940 // The circuit checks that limit - this >= 0, so if we are doing a less_than comparison, we need to subtract 1
1941 // from the limit
1942 uint256_t strict_upper_limit = upper_limit - uint256_t(1);
1943 uint256_t value = get_value().lo;
1944
1945 const uint256_t upper_limit_value_0 = strict_upper_limit.slice(0, NUM_LIMB_BITS);
1946 const uint256_t upper_limit_value_1 = strict_upper_limit.slice(NUM_LIMB_BITS, NUM_LIMB_BITS * 2);
1947 const uint256_t upper_limit_value_2 = strict_upper_limit.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3);
1948 const uint256_t upper_limit_value_3 = strict_upper_limit.slice(NUM_LIMB_BITS * 3, NUM_LIMB_BITS * 4);
1949
1950 const uint256_t val_0 = value.slice(0, NUM_LIMB_BITS);
1951 const uint256_t val_1 = value.slice(NUM_LIMB_BITS, NUM_LIMB_BITS * 2);
1952 const uint256_t val_2 = value.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3);
1953
1954 bool borrow_0_value = val_0 > upper_limit_value_0;
1955 bool borrow_1_value = (val_1 + uint256_t(borrow_0_value)) > (upper_limit_value_1);
1956 bool borrow_2_value = (val_2 + uint256_t(borrow_1_value)) > (upper_limit_value_2);
1957
1958 field_t<Builder> upper_limit_0(context, upper_limit_value_0);
1959 field_t<Builder> upper_limit_1(context, upper_limit_value_1);
1960 field_t<Builder> upper_limit_2(context, upper_limit_value_2);
1961 field_t<Builder> upper_limit_3(context, upper_limit_value_3);
1962 bool_t<Builder> borrow_0(witness_t<Builder>(context, borrow_0_value));
1963 bool_t<Builder> borrow_1(witness_t<Builder>(context, borrow_1_value));
1964 bool_t<Builder> borrow_2(witness_t<Builder>(context, borrow_2_value));
1965 // Unset free witness tag because these are auxiliary witnesses
1966 borrow_0.unset_free_witness_tag();
1967 borrow_1.unset_free_witness_tag();
1968 borrow_2.unset_free_witness_tag();
1969
1970 // The way we use borrows here ensures that we are checking that upper_limit - binary_basis > 0.
1971 // We check that the result in each limb is > 0.
1972 // If the modulus part in this limb is smaller, we simply borrow the value from the higher limb.
1973 // The prover can rearrange the borrows the way they like. The important thing is that the borrows are
1974 // constrained.
1975 field_t<Builder> r0 =
1976 upper_limit_0 - binary_basis_limbs[0].element + (static_cast<field_t<Builder>>(borrow_0) * shift_1);
1977 field_t<Builder> r1 = upper_limit_1 - binary_basis_limbs[1].element +
1978 (static_cast<field_t<Builder>>(borrow_1) * shift_1) - static_cast<field_t<Builder>>(borrow_0);
1979 field_t<Builder> r2 = upper_limit_2 - binary_basis_limbs[2].element +
1980 (static_cast<field_t<Builder>>(borrow_2) * shift_1) - static_cast<field_t<Builder>>(borrow_1);
1981 field_t<Builder> r3 = upper_limit_3 - binary_basis_limbs[3].element - static_cast<field_t<Builder>>(borrow_2);
1982
1983 // We need to range constrain the r0,r1,r2,r3 values to ensure they are "small enough".
1984 get_context()->range_constrain_two_limbs(
1985 r0.get_witness_index(),
1986 r1.get_witness_index(),
1987 static_cast<size_t>(NUM_LIMB_BITS),
1988 static_cast<size_t>(NUM_LIMB_BITS),
1989 msg == "bigfield::unsafe_assert_less_than" ? "bigfield::unsafe_assert_less_than: r0 or r1 too large" : msg);
1990 get_context()->range_constrain_two_limbs(
1991 r2.get_witness_index(),
1992 r3.get_witness_index(),
1993 static_cast<size_t>(NUM_LIMB_BITS),
1994 static_cast<size_t>(NUM_LIMB_BITS),
1995 msg == "bigfield::unsafe_assert_less_than" ? "bigfield::unsafe_assert_less_than: r2 or r3 too large" : msg);
1996}
1997
1998// check elements are equal mod p by proving their integer difference is a multiple of p.
1999// This relies on the minus operator for a-b increasing a by a multiple of p large enough so diff is non-negative
2000// When one of the elements is a constant and another is a witness we check equality of limbs, so if the witness
2001// bigfield element is in an unreduced form, it needs to be reduced first. We don't have automatice reduced form
2002// detection for now, so it is up to the circuit writer to detect this
2003template <typename Builder, typename T>
2004void bigfield<Builder, T>::assert_equal(const bigfield& other, std::string const& msg) const
2005{
2006 Builder* ctx = this->context ? this->context : other.context;
2007 if (is_constant() && other.is_constant()) {
2008 std::cerr << "bigfield: calling assert equal on 2 CONSTANT bigfield elements...is this intended?" << std::endl;
2009 BB_ASSERT_EQ(get_value(), other.get_value(), "We expect constants to be less than the target modulus");
2010 return;
2011 } else if (other.is_constant()) {
2012 // NOTE(https://github.com/AztecProtocol/barretenberg/issues/998): This can lead to a situation where
2013 // an honest prover cannot satisfy the constraints, because `this` is not reduced, but `other` is, i.e.,
2014 // `this` = kp + r and `other` = r
2015 // where k is a positive integer. In such a case, the prover cannot satisfy the constraints
2016 // because the limb-differences would not be 0 mod r. Therefore, an honest prover needs to make sure that
2017 // `this` is reduced before calling this method. Also `other` should never be greater than the modulus by
2018 // design. As a precaution, we assert that the circuit-constant `other` is less than the modulus.
2019 BB_ASSERT_LT(other.get_value(), modulus_u512);
2020 field_t<Builder> t0 = (binary_basis_limbs[0].element - other.binary_basis_limbs[0].element);
2021 field_t<Builder> t1 = (binary_basis_limbs[1].element - other.binary_basis_limbs[1].element);
2022 field_t<Builder> t2 = (binary_basis_limbs[2].element - other.binary_basis_limbs[2].element);
2023 field_t<Builder> t3 = (binary_basis_limbs[3].element - other.binary_basis_limbs[3].element);
2024 field_t<Builder> t4 = (prime_basis_limb - other.prime_basis_limb);
2025 t0.assert_is_zero();
2026 t1.assert_is_zero();
2027 t2.assert_is_zero();
2028 t3.assert_is_zero();
2029 t4.assert_is_zero();
2030 return;
2031 } else if (is_constant()) {
2032 other.assert_equal(*this, msg);
2033 return;
2034 } else {
2035 // Catch the error if the reduced value of the two elements are not equal
2036 uint512_t lhs_reduced_value = get_value() % modulus_u512;
2037 uint512_t rhs_reduced_value = other.get_value() % modulus_u512;
2038 if ((lhs_reduced_value != rhs_reduced_value) && !get_context()->failed()) {
2039 get_context()->failure(msg);
2040 }
2041
2042 // Remove tags, we don't want to cause violations on assert_equal
2043 const auto original_tag = get_origin_tag();
2044 const auto other_original_tag = other.get_origin_tag();
2045 auto empty_tag = OriginTag::constant(); // Disable origin checking during intermediate operations
2046 set_origin_tag(empty_tag);
2047 other.set_origin_tag(empty_tag);
2048
2049 bigfield diff = *this - other;
2050 const uint512_t diff_val = diff.get_value();
2051 const uint512_t modulus(target_basis.modulus);
2052
2053 const auto [quotient_512, remainder_512] = (diff_val).divmod(modulus);
2054 if (remainder_512 != 0) {
2055 std::cerr << "bigfield: remainder not zero!" << std::endl;
2056 }
2057 bigfield quotient;
2058
2059 const size_t num_quotient_bits = get_quotient_max_bits({ 0 });
2060 quotient = bigfield(witness_t(ctx, fr(quotient_512.slice(0, NUM_LIMB_BITS * 2).lo)),
2061 witness_t(ctx, fr(quotient_512.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 4).lo)),
2062 false,
2063 num_quotient_bits);
2064 unsafe_evaluate_multiply_add(diff, { one() }, {}, quotient, { zero() });
2065
2066 // Restore tags
2067 set_origin_tag(original_tag);
2068 other.set_origin_tag(other_original_tag);
2069 }
2070}
2071
2072// construct a proof that points are different mod p, when they are different mod r
2073// WARNING: This method doesn't have perfect completeness - for points equal mod r (or with certain difference kp
2074// mod r) but different mod p, you can't construct a proof. The chances of an honest prover running afoul of this
2075// condition are extremely small (TODO: compute probability) Note also that the number of constraints depends on how
2076// much the values have overflown beyond p e.g. due to an addition chain The function is based on the following.
2077// Suppose a-b = 0 mod p. Then a-b = k*p for k in a range [-R,L] for largest L and R such that L*p>= a, R*p>=b.
2078// And also a-b = k*p mod r for such k. Thus we can verify a-b is non-zero mod p by taking the product of such values
2079// (a-b-kp) and showing it's non-zero mod r
2080template <typename Builder, typename T>
2081void bigfield<Builder, T>::assert_is_not_equal(const bigfield& other, std::string const& msg) const
2082{
2083 // Why would we use this for 2 constants? Turns out, in biggroup
2084 const auto get_overload_count = [target_modulus = modulus_u512](const uint512_t& maximum_value) {
2085 uint512_t target = target_modulus;
2086 size_t overload_count = 0;
2087 while (target <= maximum_value) {
2088 ++overload_count;
2089 target += target_modulus;
2090 }
2091 return overload_count;
2092 };
2093 const size_t lhs_overload_count = get_overload_count(get_maximum_value());
2094 const size_t rhs_overload_count = get_overload_count(other.get_maximum_value());
2095
2096 // if (a == b) then (a == b mod n)
2097 // to save gates, we only check that (a == b mod n)
2098
2099 // if numeric val of a = a' + p.q
2100 // we want to check (a' + p.q == b mod n)
2101 const field_t<Builder> base_diff = prime_basis_limb - other.prime_basis_limb;
2102 auto diff = base_diff;
2103 field_t<Builder> prime_basis(get_context(), modulus);
2104 field_t<Builder> prime_basis_accumulator = prime_basis;
2105 // Each loop iteration adds 1 gate
2106 // (prime_basis and prime_basis accumulator are constant so only the * operator adds a gate)
2107 for (size_t i = 0; i < lhs_overload_count; ++i) {
2108 diff = diff * (base_diff - prime_basis_accumulator);
2109 prime_basis_accumulator += prime_basis;
2110 }
2111 prime_basis_accumulator = prime_basis;
2112 for (size_t i = 0; i < rhs_overload_count; ++i) {
2113 diff = diff * (base_diff + prime_basis_accumulator);
2114 prime_basis_accumulator += prime_basis;
2115 }
2116 diff.assert_is_not_zero(msg);
2117}
2118
2119// We reduce an element's mod 2^t representation (t=4*NUM_LIMB_BITS) to size 2^s for smallest s with 2^s>p
2120// This is much cheaper than actually reducing mod p and suffices for addition chains (where we just need not to
2121// overflow 2^t) We also reduce any "spillage" inside the first 3 limbs, so that their range is NUM_LIMB_BITS and
2122// not larger
2123template <typename Builder, typename T> void bigfield<Builder, T>::self_reduce() const
2124{
2125 // Warning: this assumes we have run circuit construction at least once in debug mode where large non reduced
2126 // constants are disallowed via ASSERT
2127 if (is_constant()) {
2128 return;
2129 }
2130 OriginTag new_tag = get_origin_tag();
2131 const auto [quotient_value, remainder_value] = get_value().divmod(target_basis.modulus);
2132
2133 bigfield quotient(context);
2134
2135 uint512_t maximum_quotient_size = get_maximum_value() / target_basis.modulus;
2136 uint64_t maximum_quotient_bits = maximum_quotient_size.get_msb() + 1;
2137 if ((maximum_quotient_bits & 1ULL) == 1ULL) {
2138 ++maximum_quotient_bits;
2139 }
2140
2141 BB_ASSERT_LTE(maximum_quotient_bits, NUM_LIMB_BITS);
2142 uint32_t quotient_limb_index = context->add_variable(bb::fr(quotient_value.lo));
2143 field_t<Builder> quotient_limb = field_t<Builder>::from_witness_index(context, quotient_limb_index);
2144 context->create_limbed_range_constraint(quotient_limb.get_witness_index(),
2145 static_cast<size_t>(maximum_quotient_bits));
2146
2147 BB_ASSERT_LT((uint1024_t(1) << maximum_quotient_bits) * uint1024_t(modulus_u512) + DEFAULT_MAXIMUM_REMAINDER,
2148 get_maximum_crt_product());
2149 quotient.binary_basis_limbs[0] = Limb(quotient_limb, uint256_t(1) << maximum_quotient_bits);
2153 quotient.prime_basis_limb = quotient_limb;
2154 // this constructor with can_overflow=false will enforce remainder of size<2^s
2155 bigfield remainder = bigfield(
2156 witness_t(context, fr(remainder_value.slice(0, NUM_LIMB_BITS * 2).lo)),
2157 witness_t(context, fr(remainder_value.slice(NUM_LIMB_BITS * 2, NUM_LIMB_BITS * 3 + NUM_LAST_LIMB_BITS).lo)));
2158
2159 unsafe_evaluate_multiply_add(*this, one(), {}, quotient, { remainder });
2160 binary_basis_limbs[0] =
2161 remainder.binary_basis_limbs[0]; // Combination of const method and mutable variables is good practice?
2162 binary_basis_limbs[1] = remainder.binary_basis_limbs[1];
2163 binary_basis_limbs[2] = remainder.binary_basis_limbs[2];
2164 binary_basis_limbs[3] = remainder.binary_basis_limbs[3];
2165 prime_basis_limb = remainder.prime_basis_limb;
2166 set_origin_tag(new_tag);
2167} // namespace stdlib
2168
2169template <typename Builder, typename T>
2171 const bigfield& input_to_mul,
2172 const std::vector<bigfield>& to_add,
2173 const bigfield& input_quotient,
2174 const std::vector<bigfield>& input_remainders)
2175{
2176
2177 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
2178 BB_ASSERT_LTE(input_remainders.size(), MAXIMUM_SUMMAND_COUNT);
2179 // Sanity checks
2180 input_left.sanity_check();
2181 input_to_mul.sanity_check();
2182 input_quotient.sanity_check();
2183 for (auto& el : to_add) {
2184 el.sanity_check();
2185 }
2186 for (auto& el : input_remainders) {
2187 el.sanity_check();
2188 }
2189
2190 std::vector<bigfield> remainders(input_remainders);
2191
2192 bigfield left = input_left;
2193 bigfield to_mul = input_to_mul;
2194 bigfield quotient = input_quotient;
2195
2196 // Either of the multiplicand must be a witness.
2197 BB_ASSERT(!left.is_constant() || !to_mul.is_constant());
2198 Builder* ctx = left.context ? left.context : to_mul.context;
2199
2200 // Compute the maximum value of the product of the two inputs: max(a * b)
2201 uint512_t max_ab_lo(0);
2202 uint512_t max_ab_hi(0);
2203 std::tie(max_ab_lo, max_ab_hi) = compute_partial_schoolbook_multiplication(left.get_binary_basis_limb_maximums(),
2205
2206 // Compute the maximum value of the product of the quotient and neg_modulus: max(q * p')
2207 uint512_t max_q_neg_p_lo(0);
2208 uint512_t max_q_neg_p_hi(0);
2209 std::tie(max_q_neg_p_lo, max_q_neg_p_hi) = compute_partial_schoolbook_multiplication(
2210 neg_modulus_mod_binary_basis_limbs_u256, quotient.get_binary_basis_limb_maximums());
2211
2212 // Compute the maximum value that needs to be borrowed from the hi limbs to the lo limb.
2213 // Check the README for the explanation of the borrow.
2214 uint256_t max_remainders_lo(0);
2215 for (const auto& remainder : input_remainders) {
2216 max_remainders_lo += remainder.binary_basis_limbs[0].maximum_value +
2217 (remainder.binary_basis_limbs[1].maximum_value << NUM_LIMB_BITS);
2218 }
2219
2220 // While performing the subtraction of remainder r as:
2221 //
2222 // (a * b + q * p') - (r)
2223 //
2224 // we want to ensure that the lower limbs do not underflow. So we add a borrow value
2225 // to the lower limbs and subtract it from the higher limbs. Naturally, such a borrow value
2226 // must be a multiple of 2^2L (where L = NUM_LIMB_BITS). Let borrow_lo_value be the value
2227 // borrowed from the hi limbs, then we must have:
2228 //
2229 // borrow_lo_value * 2^(2L) >= max_remainders_lo
2230 //
2231 // Thus, we can compute the minimum borrow_lo_value as:
2232 //
2233 // borrow_lo_value = ⌈ max_remainders_lo / 2^(2L) ⌉
2234 //
2235 uint256_t borrow_lo_value =
2236 (max_remainders_lo + ((uint256_t(1) << (2 * NUM_LIMB_BITS)) - 1)) >> (2 * NUM_LIMB_BITS);
2237 field_t<Builder> borrow_lo(ctx, bb::fr(borrow_lo_value));
2238
2239 uint512_t max_a0(0);
2240 uint512_t max_a1(0);
2241 for (size_t i = 0; i < to_add.size(); ++i) {
2242 max_a0 += to_add[i].binary_basis_limbs[0].maximum_value +
2243 (to_add[i].binary_basis_limbs[1].maximum_value << NUM_LIMB_BITS);
2244 max_a1 += to_add[i].binary_basis_limbs[2].maximum_value +
2245 (to_add[i].binary_basis_limbs[3].maximum_value << NUM_LIMB_BITS);
2246 }
2247 const uint512_t max_lo = max_ab_lo + max_q_neg_p_lo + max_remainders_lo + max_a0;
2248 const uint512_t max_lo_carry = max_lo >> (2 * NUM_LIMB_BITS);
2249 const uint512_t max_hi = max_ab_hi + max_q_neg_p_hi + max_a1 + max_lo_carry;
2250
2251 uint64_t max_lo_bits = (max_lo.get_msb() + 1);
2252 uint64_t max_hi_bits = max_hi.get_msb() + 1;
2253
2254 BB_ASSERT(max_lo_bits > (2 * NUM_LIMB_BITS));
2255 BB_ASSERT(max_hi_bits > (2 * NUM_LIMB_BITS));
2256
2257 uint64_t carry_lo_msb = max_lo_bits - (2 * NUM_LIMB_BITS);
2258 uint64_t carry_hi_msb = max_hi_bits - (2 * NUM_LIMB_BITS);
2259
2260 if (max_lo_bits < (2 * NUM_LIMB_BITS)) {
2261 carry_lo_msb = 0;
2262 }
2263 if (max_hi_bits < (2 * NUM_LIMB_BITS)) {
2264 carry_hi_msb = 0;
2265 }
2266
2267 // The custom bigfield multiplication gate requires inputs are witnesses.
2268 // If we're using constant values, instantiate them as circuit variables
2269 //
2270 // Explanation:
2271 // The bigfield multiplication gate expects witnesses and disallows circuit constants
2272 // because allowing circuit constants would lead to complex circuit logic to support
2273 // different combinations of constant and witness inputs. Particularly, bigfield multiplication
2274 // gate enforces constraints of the form: a * b - q * p + r = 0, where:
2275 //
2276 // input left a = (a3 || a2 || a1 || a0)
2277 // input right b = (b3 || b2 || b1 || b0)
2278 // quotient q = (q3 || q2 || q1 || q0)
2279 // remainder r = (r3 || r2 || r1 || r0)
2280 //
2281 // | a1 | b1 | r0 | lo_0 | <-- product gate 1: check lo_0
2282 // | a0 | b0 | a3 | b3 |
2283 // | a2 | b2 | r3 | hi_0 |
2284 // | a1 | b1 | r2 | hi_1 |
2285 //
2286 // Example constaint: lo_0 = (a1 * b0 + a0 * b1) * 2^b + (a0 * b0) - r0
2287 // ==> w4 = (w1 * w'2 + w'1 * w2) * 2^b + (w'1 * w'2) - w3
2288 //
2289 // If a, b both are witnesses, this special gate performs 3 field multiplications per gate.
2290 // If b was a constant, then we would need to no field multiplications, but instead update the
2291 // the limbs of a with multiplicative and additive constants. This just makes the circuit logic
2292 // more complex, so we disallow constants. If there are constants, we convert them to fixed witnesses (at the
2293 // expense of 1 extra gate per constant).
2294 //
2295 const auto convert_constant_to_fixed_witness = [ctx](const bigfield& input) {
2296 bigfield output(input);
2297 // Save the original tag before converting to witnesses
2298 auto original_tag = input.get_origin_tag();
2299 output.prime_basis_limb =
2300 field_t<Builder>::from_witness_index(ctx, ctx->put_constant_variable(input.prime_basis_limb.get_value()));
2302 ctx, ctx->put_constant_variable(input.binary_basis_limbs[0].element.get_value()));
2304 ctx, ctx->put_constant_variable(input.binary_basis_limbs[1].element.get_value()));
2306 ctx, ctx->put_constant_variable(input.binary_basis_limbs[2].element.get_value()));
2308 ctx, ctx->put_constant_variable(input.binary_basis_limbs[3].element.get_value()));
2309 output.context = ctx;
2310 // Restore the original tag after converting to witnesses
2311 output.set_origin_tag(original_tag);
2312 return output;
2313 };
2314 if (left.is_constant()) {
2315 left = convert_constant_to_fixed_witness(left);
2316 }
2317 if (to_mul.is_constant()) {
2318 to_mul = convert_constant_to_fixed_witness(to_mul);
2319 }
2320 if (quotient.is_constant()) {
2321 quotient = convert_constant_to_fixed_witness(quotient);
2322 }
2323 if (remainders[0].is_constant()) {
2324 remainders[0] = convert_constant_to_fixed_witness(remainders[0]);
2325 }
2326
2327 std::vector<field_t<Builder>> limb_0_accumulator{ remainders[0].binary_basis_limbs[0].element };
2328 std::vector<field_t<Builder>> limb_2_accumulator{ remainders[0].binary_basis_limbs[2].element };
2329 std::vector<field_t<Builder>> prime_limb_accumulator{ remainders[0].prime_basis_limb };
2330 for (size_t i = 1; i < remainders.size(); ++i) {
2331 limb_0_accumulator.emplace_back(remainders[i].binary_basis_limbs[0].element);
2332 limb_0_accumulator.emplace_back(remainders[i].binary_basis_limbs[1].element * shift_1);
2333 limb_2_accumulator.emplace_back(remainders[i].binary_basis_limbs[2].element);
2334 limb_2_accumulator.emplace_back(remainders[i].binary_basis_limbs[3].element * shift_1);
2335 prime_limb_accumulator.emplace_back(remainders[i].prime_basis_limb);
2336 }
2337 for (const auto& add : to_add) {
2338 limb_0_accumulator.emplace_back(-add.binary_basis_limbs[0].element);
2339 limb_0_accumulator.emplace_back(-add.binary_basis_limbs[1].element * shift_1);
2340 limb_2_accumulator.emplace_back(-add.binary_basis_limbs[2].element);
2341 limb_2_accumulator.emplace_back(-add.binary_basis_limbs[3].element * shift_1);
2342 prime_limb_accumulator.emplace_back(-add.prime_basis_limb);
2343 }
2344
2345 const auto& t0 = remainders[0].binary_basis_limbs[1].element;
2346 const auto& t1 = remainders[0].binary_basis_limbs[3].element;
2347 bool needs_normalize = (t0.additive_constant != 0 || t0.multiplicative_constant != 1);
2348 needs_normalize = needs_normalize || (t1.additive_constant != 0 || t1.multiplicative_constant != 1);
2349
2350 if (needs_normalize) {
2351 limb_0_accumulator.emplace_back(remainders[0].binary_basis_limbs[1].element * shift_1);
2352 limb_2_accumulator.emplace_back(remainders[0].binary_basis_limbs[3].element * shift_1);
2353 }
2354
2355 std::array<field_t<Builder>, NUM_LIMBS> remainder_limbs{
2356 field_t<Builder>::accumulate(limb_0_accumulator),
2357 needs_normalize ? field_t<Builder>::from_witness_index(ctx, ctx->zero_idx())
2358 : remainders[0].binary_basis_limbs[1].element,
2359 field_t<Builder>::accumulate(limb_2_accumulator),
2360 needs_normalize ? field_t<Builder>::from_witness_index(ctx, ctx->zero_idx())
2361 : remainders[0].binary_basis_limbs[3].element,
2362 };
2363 field_t<Builder> remainder_prime_limb = field_t<Builder>::accumulate(prime_limb_accumulator);
2364
2369 {
2370 remainder_limbs[0].get_witness_index(),
2371 remainder_limbs[1].get_witness_index(),
2372 remainder_limbs[2].get_witness_index(),
2373 remainder_limbs[3].get_witness_index(),
2374 },
2375 { neg_modulus_mod_binary_basis_limbs[0],
2376 neg_modulus_mod_binary_basis_limbs[1],
2377 neg_modulus_mod_binary_basis_limbs[2],
2378 neg_modulus_mod_binary_basis_limbs[3] },
2379 };
2380
2381 // N.B. this method DOES NOT evaluate the prime field component of the non-native field mul
2382 const auto [lo_idx, hi_idx] = ctx->evaluate_non_native_field_multiplication(witnesses);
2383
2385 to_mul.prime_basis_limb,
2386 quotient.prime_basis_limb * negative_prime_modulus_mod_native_basis,
2387 -remainder_prime_limb,
2388 "bigfield: prime limb identity failed");
2389
2390 field_t lo = field_t<Builder>::from_witness_index(ctx, lo_idx) + borrow_lo;
2392
2393 // if both the hi and lo output limbs have less than 70 bits, we can use our custom
2394 // limb accumulation gate (accumulates 2 field elements, each composed of 5 14-bit limbs, in 3 gates)
2395 if (carry_lo_msb <= 70 && carry_hi_msb <= 70) {
2396 ctx->range_constrain_two_limbs(hi.get_witness_index(),
2397 lo.get_witness_index(),
2398 static_cast<size_t>(carry_hi_msb),
2399 static_cast<size_t>(carry_lo_msb),
2400 "bigfield::unsafe_evaluate_multiply_add: carries too large");
2401 } else {
2402 hi.create_range_constraint(static_cast<size_t>(carry_hi_msb), "bigfield: carry_hi too large");
2403 lo.create_range_constraint(static_cast<size_t>(carry_lo_msb), "bigfield: carry_lo too large");
2404 }
2405}
2406
2407template <typename Builder, typename T>
2409 const std::vector<bigfield>& input_right,
2410 const std::vector<bigfield>& to_add,
2411 const bigfield& input_quotient,
2412 const std::vector<bigfield>& input_remainders)
2413{
2414 BB_ASSERT_EQ(input_left.size(), input_right.size());
2415 BB_ASSERT_LTE(input_left.size(), MAXIMUM_SUMMAND_COUNT);
2416 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
2417 BB_ASSERT_LTE(input_remainders.size(), MAXIMUM_SUMMAND_COUNT);
2418
2419 // Sanity checks
2420 bool is_left_constant = true;
2421 for (auto& el : input_left) {
2422 el.sanity_check();
2423 is_left_constant &= el.is_constant();
2424 }
2425 bool is_right_constant = true;
2426 for (auto& el : input_right) {
2427 el.sanity_check();
2428 is_right_constant &= el.is_constant();
2429 }
2430 for (auto& el : to_add) {
2431 el.sanity_check();
2432 }
2433 input_quotient.sanity_check();
2434 for (auto& el : input_remainders) {
2435 el.sanity_check();
2436 }
2437
2438 // We must have at least one left or right multiplicand as witnesses.
2439 BB_ASSERT(!is_left_constant || !is_right_constant);
2440
2441 std::vector<bigfield> remainders(input_remainders);
2442 std::vector<bigfield> left(input_left);
2443 std::vector<bigfield> right(input_right);
2444 bigfield quotient = input_quotient;
2445 const size_t num_multiplications = input_left.size();
2446
2447 // Fetch the context
2448 Builder* ctx = nullptr;
2449 for (const auto& el : input_left) {
2450 if (el.context) {
2451 ctx = el.context;
2452 break;
2453 }
2454 }
2455 if (ctx == nullptr) {
2456 for (const auto& el : input_right) {
2457 if (el.context) {
2458 ctx = el.context;
2459 break;
2460 }
2461 }
2462 }
2463 BB_ASSERT(ctx != nullptr);
2464
2471 uint512_t max_lo = 0;
2472 uint512_t max_hi = 0;
2473
2474 // Compute the maximum value that needs to be borrowed from the hi limbs to the lo limb.
2475 // Check the README for the explanation of the borrow.
2476 uint256_t max_remainders_lo(0);
2477 for (const auto& remainder : input_remainders) {
2478 max_remainders_lo += remainder.binary_basis_limbs[0].maximum_value +
2479 (remainder.binary_basis_limbs[1].maximum_value << NUM_LIMB_BITS);
2480 }
2481
2482 // While performing the subtraction of (sum of) remainder(s) as:
2483 //
2484 // (Σi ai * bi + q * p') - (Σj rj)
2485 //
2486 // we want to ensure that the lower limbs do not underflow. So we add a borrow value
2487 // to the lower limbs and subtract it from the higher limbs. Naturally, such a borrow value
2488 // must be a multiple of 2^2L (where L = NUM_LIMB_BITS). Let borrow_lo_value be the value
2489 // borrowed from the hi limbs, then we must have:
2490 //
2491 // borrow_lo_value * 2^(2L) >= max_remainders_lo
2492 //
2493 // Thus, we can compute the minimum borrow_lo_value as:
2494 //
2495 // borrow_lo_value = ⌈ max_remainders_lo / 2^(2L) ⌉
2496 //
2497 uint256_t borrow_lo_value =
2498 (max_remainders_lo + ((uint256_t(1) << (2 * NUM_LIMB_BITS)) - 1)) >> (2 * NUM_LIMB_BITS);
2499 field_t<Builder> borrow_lo(ctx, bb::fr(borrow_lo_value));
2500
2501 // Compute the maximum value of the quotient times modulus.
2502 const auto [max_q_neg_p_lo, max_q_neg_p_hi] = compute_partial_schoolbook_multiplication(
2503 neg_modulus_mod_binary_basis_limbs_u256, quotient.get_binary_basis_limb_maximums());
2504
2505 // update max_lo, max_hi with quotient limb product terms.
2506 max_lo += max_q_neg_p_lo + max_remainders_lo;
2507 max_hi += max_q_neg_p_hi;
2508
2509 // Compute maximum value of addition terms in `to_add` and add to max_lo, max_hi
2510 uint512_t max_a0(0);
2511 uint512_t max_a1(0);
2512 for (size_t i = 0; i < to_add.size(); ++i) {
2513 max_a0 += to_add[i].binary_basis_limbs[0].maximum_value +
2514 (to_add[i].binary_basis_limbs[1].maximum_value << NUM_LIMB_BITS);
2515 max_a1 += to_add[i].binary_basis_limbs[2].maximum_value +
2516 (to_add[i].binary_basis_limbs[3].maximum_value << NUM_LIMB_BITS);
2517 }
2518 max_lo += max_a0;
2519 max_hi += max_a1;
2520
2521 // Compute the maximum value of our multiplication products and add to max_lo, max_hi
2522 for (size_t i = 0; i < num_multiplications; ++i) {
2523 const auto [product_lo, product_hi] = compute_partial_schoolbook_multiplication(
2524 left[i].get_binary_basis_limb_maximums(), right[i].get_binary_basis_limb_maximums());
2525 max_lo += product_lo;
2526 max_hi += product_hi;
2527 }
2528
2529 const uint512_t max_lo_carry = max_lo >> (2 * NUM_LIMB_BITS);
2530 max_hi += max_lo_carry;
2531 // Compute the maximum number of bits in `max_lo` and `max_hi` - this defines the range constraint values we
2532 // will need to apply to validate our product
2533 uint64_t max_lo_bits = (max_lo.get_msb() + 1);
2534 uint64_t max_hi_bits = max_hi.get_msb() + 1;
2535
2536 // The custom bigfield multiplication gate requires inputs are witnesses.
2537 // If we're using constant values, instantiate them as circuit variables
2538 //
2539 // Explanation:
2540 // The bigfield multiplication gate expects witnesses and disallows circuit constants
2541 // because allowing circuit constants would lead to complex circuit logic to support
2542 // different combinations of constant and witness inputs. Particularly, bigfield multiplication
2543 // gate enforces constraints of the form: a * b - q * p + r = 0, where:
2544 //
2545 // input left a = (a3 || a2 || a1 || a0)
2546 // input right b = (b3 || b2 || b1 || b0)
2547 // quotient q = (q3 || q2 || q1 || q0)
2548 // remainder r = (r3 || r2 || r1 || r0)
2549 //
2550 // | a1 | b1 | r0 | lo_0 | <-- product gate 1: check lo_0
2551 // | a0 | b0 | a3 | b3 |
2552 // | a2 | b2 | r3 | hi_0 |
2553 // | a1 | b1 | r2 | hi_1 |
2554 //
2555 // Example constaint: lo_0 = (a1 * b0 + a0 * b1) * 2^b + (a0 * b0) - r0
2556 // ==> w4 = (w1 * w'2 + w'1 * w2) * 2^b + (w'1 * w'2) - w3
2557 //
2558 // If a, b both are witnesses, this special gate performs 3 field multiplications per gate.
2559 // If b was a constant, then we would need to no field multiplications, but instead update the
2560 // the limbs of a with multiplicative and additive constants. This just makes the circuit logic
2561 // more complex, so we disallow constants. If there are constants, we convert them to fixed witnesses (at the
2562 // expense of 1 extra gate per constant).
2563 //
2564 const auto convert_constant_to_fixed_witness = [ctx](const bigfield& input) {
2565 BB_ASSERT(input.is_constant());
2566 bigfield output(input);
2567 // Save the original tag before converting to witnesses
2568 auto original_tag = input.get_origin_tag();
2569 output.prime_basis_limb =
2570 field_t<Builder>::from_witness_index(ctx, ctx->put_constant_variable(input.prime_basis_limb.get_value()));
2572 ctx, ctx->put_constant_variable(input.binary_basis_limbs[0].element.get_value()));
2574 ctx, ctx->put_constant_variable(input.binary_basis_limbs[1].element.get_value()));
2576 ctx, ctx->put_constant_variable(input.binary_basis_limbs[2].element.get_value()));
2578 ctx, ctx->put_constant_variable(input.binary_basis_limbs[3].element.get_value()));
2579 output.context = ctx;
2580 // Restore the original tag after converting to witnesses
2581 output.set_origin_tag(original_tag);
2582 return output;
2583 };
2584
2585 // evalaute a nnf mul and add into existing lohi output for our extra product terms
2586 // we need to add the result of (left_b * right_b) into lo_1_idx and hi_1_idx
2587 // our custom gate evaluates: ((a * b) + (q * neg_modulus) - r) / 2^{136} = lo + hi * 2^{136}
2588 // where q is a 'quotient' bigfield and neg_modulus is defined by selector polynomial values
2589 // The custom gate costs 7 constraints, which is cheaper than computing `a * b` using multiplication +
2590 // addition gates But....we want to obtain `left_a * right_b + lo_1 + hi_1 * 2^{136} = lo + hi * 2^{136}` If
2591 // we set `neg_modulus = [2^{136}, 0, 0, 0]` and `q = [lo_1, 0, hi_1, 0]`, then we will add `lo_1` into
2592 // `lo`, and `lo_1/2^{136} + hi_1` into `hi`. we can then subtract off `lo_1/2^{136}` from `hi`, by setting
2593 // `r = [0, 0, lo_1, 0]` This saves us 2 addition gates as we don't have to add together the outputs of two
2594 // calls to `evaluate_non_native_field_multiplication`
2595 std::vector<field_t<Builder>> limb_0_accumulator;
2596 std::vector<field_t<Builder>> limb_2_accumulator;
2597 std::vector<field_t<Builder>> prime_limb_accumulator;
2598
2599 for (size_t i = 0; i < num_multiplications; ++i) {
2600 if (left[i].is_constant()) {
2601 left[i] = convert_constant_to_fixed_witness(left[i]);
2602 }
2603 if (right[i].is_constant()) {
2604 right[i] = convert_constant_to_fixed_witness(right[i]);
2605 }
2606
2607 if (i > 0) {
2609 left[i].get_binary_basis_limb_witness_indices(),
2610 right[i].get_binary_basis_limb_witness_indices(),
2611 };
2612
2613 const auto [lo_2_idx, hi_2_idx] = ctx->queue_partial_non_native_field_multiplication(mul_witnesses);
2614
2617 // Set CONSTANT tags so these intermediate results are absorbed during tag merging
2618 // The final tag will come from the remainders which have properly merged tags from mult_madd
2619 auto const_tag = OriginTag::constant();
2620 lo_2.set_origin_tag(const_tag);
2621 hi_2.set_origin_tag(const_tag);
2622
2623 limb_0_accumulator.emplace_back(-lo_2);
2624 limb_2_accumulator.emplace_back(-hi_2);
2625 prime_limb_accumulator.emplace_back(-(left[i].prime_basis_limb * right[i].prime_basis_limb));
2626 }
2627 }
2628 if (quotient.is_constant()) {
2629 quotient = convert_constant_to_fixed_witness(quotient);
2630 }
2631
2632 bool no_remainders = remainders.empty();
2633 if (!no_remainders) {
2634 limb_0_accumulator.emplace_back(remainders[0].binary_basis_limbs[0].element);
2635 limb_2_accumulator.emplace_back(remainders[0].binary_basis_limbs[2].element);
2636 prime_limb_accumulator.emplace_back(remainders[0].prime_basis_limb);
2637 }
2638 for (size_t i = 1; i < remainders.size(); ++i) {
2639 limb_0_accumulator.emplace_back(remainders[i].binary_basis_limbs[0].element);
2640 limb_0_accumulator.emplace_back(remainders[i].binary_basis_limbs[1].element * shift_1);
2641 limb_2_accumulator.emplace_back(remainders[i].binary_basis_limbs[2].element);
2642 limb_2_accumulator.emplace_back(remainders[i].binary_basis_limbs[3].element * shift_1);
2643 prime_limb_accumulator.emplace_back(remainders[i].prime_basis_limb);
2644 }
2645 for (const auto& add : to_add) {
2646 limb_0_accumulator.emplace_back(-add.binary_basis_limbs[0].element);
2647 limb_0_accumulator.emplace_back(-add.binary_basis_limbs[1].element * shift_1);
2648 limb_2_accumulator.emplace_back(-add.binary_basis_limbs[2].element);
2649 limb_2_accumulator.emplace_back(-add.binary_basis_limbs[3].element * shift_1);
2650 prime_limb_accumulator.emplace_back(-add.prime_basis_limb);
2651 }
2652
2653 field_t<Builder> accumulated_lo = field_t<Builder>::accumulate(limb_0_accumulator);
2654 field_t<Builder> accumulated_hi = field_t<Builder>::accumulate(limb_2_accumulator);
2655 if (accumulated_lo.is_constant()) {
2656 accumulated_lo =
2657 field_t<Builder>::from_witness_index(ctx, ctx->put_constant_variable(accumulated_lo.get_value()));
2658 }
2659 if (accumulated_hi.is_constant()) {
2660 accumulated_hi =
2661 field_t<Builder>::from_witness_index(ctx, ctx->put_constant_variable(accumulated_hi.get_value()));
2662 }
2663 field_t<Builder> remainder1 = no_remainders ? field_t<Builder>::from_witness_index(ctx, ctx->zero_idx())
2664 : remainders[0].binary_basis_limbs[1].element;
2665 if (remainder1.is_constant()) {
2666 remainder1 = field_t<Builder>::from_witness_index(ctx, ctx->put_constant_variable(remainder1.get_value()));
2667 }
2668 field_t<Builder> remainder3 = no_remainders ? field_t<Builder>::from_witness_index(ctx, ctx->zero_idx())
2669 : remainders[0].binary_basis_limbs[3].element;
2670 if (remainder3.is_constant()) {
2671 remainder3 = field_t<Builder>::from_witness_index(ctx, ctx->put_constant_variable(remainder3.get_value()));
2672 }
2673 std::array<field_t<Builder>, NUM_LIMBS> remainder_limbs{
2674 accumulated_lo,
2675 remainder1,
2676 accumulated_hi,
2677 remainder3,
2678 };
2679 field_t<Builder> remainder_prime_limb = field_t<Builder>::accumulate(prime_limb_accumulator);
2680
2682 left[0].get_binary_basis_limb_witness_indices(),
2683 right[0].get_binary_basis_limb_witness_indices(),
2685 {
2686 remainder_limbs[0].get_witness_index(),
2687 remainder_limbs[1].get_witness_index(),
2688 remainder_limbs[2].get_witness_index(),
2689 remainder_limbs[3].get_witness_index(),
2690 },
2691 { neg_modulus_mod_binary_basis_limbs[0],
2692 neg_modulus_mod_binary_basis_limbs[1],
2693 neg_modulus_mod_binary_basis_limbs[2],
2694 neg_modulus_mod_binary_basis_limbs[3] },
2695 };
2696
2697 const auto [lo_1_idx, hi_1_idx] = ctx->evaluate_non_native_field_multiplication(witnesses);
2698
2699 field_t<Builder>::evaluate_polynomial_identity(left[0].prime_basis_limb,
2700 right[0].prime_basis_limb,
2701 quotient.prime_basis_limb * negative_prime_modulus_mod_native_basis,
2702 -remainder_prime_limb,
2703 "bigfield: prime limb identity failed");
2704
2705 field_t lo = field_t<Builder>::from_witness_index(ctx, lo_1_idx) + borrow_lo;
2707
2708 BB_ASSERT(max_lo_bits > (2 * NUM_LIMB_BITS));
2709 BB_ASSERT(max_hi_bits > (2 * NUM_LIMB_BITS));
2710
2711 uint64_t carry_lo_msb = max_lo_bits - (2 * NUM_LIMB_BITS);
2712 uint64_t carry_hi_msb = max_hi_bits - (2 * NUM_LIMB_BITS);
2713
2714 if (max_lo_bits < (2 * NUM_LIMB_BITS)) {
2715 carry_lo_msb = 0;
2716 }
2717 if (max_hi_bits < (2 * NUM_LIMB_BITS)) {
2718 carry_hi_msb = 0;
2719 }
2720
2721 // if both the hi and lo output limbs have less than 70 bits, we can use our custom
2722 // limb accumulation gate (accumulates 2 field elements, each composed of 5 14-bit limbs, in 3 gates)
2723 if (carry_lo_msb <= 70 && carry_hi_msb <= 70) {
2724 ctx->range_constrain_two_limbs(hi.get_witness_index(),
2725 lo.get_witness_index(),
2726 static_cast<size_t>(carry_hi_msb),
2727 static_cast<size_t>(carry_lo_msb),
2728 "bigfield::unsafe_evaluate_multiply_add: carries too large");
2729 } else {
2730 hi.create_range_constraint(static_cast<size_t>(carry_hi_msb), "bigfield: carry_hi too large");
2731 lo.create_range_constraint(static_cast<size_t>(carry_lo_msb), "bigfield: carry_lo too large");
2732 }
2733}
2734
2735template <typename Builder, typename T>
2737 const std::vector<bigfield>& to_add,
2738 const bigfield& quotient,
2739 const bigfield& remainder)
2740{
2741 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
2742
2743 // Suppose input is:
2744 // x = (x3 || x2 || x1 || x0)
2745 //
2746 // x * x = (x0 * x0) +
2747 // (2 • x0 * x1) • 2^b +
2748 // (2 • x0 * x2 + x1 * x1) • 2^{2b} +
2749 // (2 • x0 * x3 + 2 • x1 * x2) • 2^{3b}.
2750 //
2751 // We need 6 multiplications to compute the above, which can be computed using two custom multiplication gates.
2752 // Since each custom bigfield gate can compute 3, we can compute the above using 2 custom multiplication gates
2753 // (as against 3 gates if we used the current bigfield multiplication gate).
2754 // We however avoid this optimization for now and end up using the existing bigfield multiplication gate.
2755 //
2756 unsafe_evaluate_multiply_add(left, left, to_add, quotient, { remainder });
2757}
2758
2759template <typename Builder, typename T>
2761 const bigfield& a, const bigfield& b, const std::vector<bigfield>& to_add)
2762{
2763 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
2764
2766 for (const auto& add_element : to_add) {
2767 add_element.reduction_check();
2768 add_values += add_element.get_value();
2769 }
2770
2771 const uint1024_t left(a.get_value());
2772 const uint1024_t right(b.get_value());
2773 const uint1024_t add_right(add_values);
2774 const uint1024_t modulus(target_basis.modulus);
2775
2776 const auto [quotient_1024, remainder_1024] = (left * right + add_right).divmod(modulus);
2777
2778 return { quotient_1024.lo, remainder_1024.lo };
2779}
2780
2781template <typename Builder, typename T>
2783 const std::vector<uint512_t>& bs,
2784 const std::vector<uint512_t>& to_add)
2785{
2786 BB_ASSERT_EQ(as.size(), bs.size());
2787 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
2788
2790 for (const auto& add_element : to_add) {
2791 add_values += add_element;
2792 }
2793 uint1024_t product_sum(0);
2794 for (size_t i = 0; i < as.size(); i++) {
2795 product_sum += uint1024_t(as[i]) * uint1024_t(bs[i]);
2796 }
2797 const uint1024_t add_right(add_values);
2798 const uint1024_t modulus(target_basis.modulus);
2799
2800 const auto [quotient_1024, remainder_1024] = (product_sum + add_right).divmod(modulus);
2801
2802 return quotient_1024.lo;
2803}
2804template <typename Builder, typename T>
2806 const std::vector<uint512_t>& bs_max,
2807 const std::vector<bigfield>& to_add,
2808 const std::vector<uint1024_t>& remainders_max)
2809{
2810 BB_ASSERT_EQ(as_max.size(), bs_max.size());
2811
2812 BB_ASSERT_LTE(to_add.size(), MAXIMUM_SUMMAND_COUNT);
2813 BB_ASSERT_LTE(as_max.size(), MAXIMUM_SUMMAND_COUNT);
2814 BB_ASSERT_LTE(remainders_max.size(), MAXIMUM_SUMMAND_COUNT);
2815
2816 // Check if the product sum can overflow CRT modulus
2817 if (mul_product_overflows_crt_modulus(as_max, bs_max, to_add)) {
2818 return std::pair<bool, size_t>(true, 0);
2819 }
2820 const size_t num_quotient_bits = get_quotient_max_bits(remainders_max);
2821 std::vector<uint512_t> to_add_max;
2822 for (auto& added_element : to_add) {
2823 to_add_max.push_back(added_element.get_maximum_value());
2824 }
2825 // Get maximum value of quotient
2826 const uint512_t maximum_quotient = compute_maximum_quotient_value(as_max, bs_max, to_add_max);
2827
2828 // Check if the quotient can fit into the range proof
2829 if (maximum_quotient >= (uint512_t(1) << num_quotient_bits)) {
2830 return std::pair<bool, size_t>(true, 0);
2831 }
2832 return std::pair<bool, size_t>(false, num_quotient_bits);
2833}
2834
2835template <typename Builder, typename T>
2838{
2839 const uint512_t b0_inner = (a_limbs[1] * b_limbs[0]);
2840 const uint512_t b1_inner = (a_limbs[0] * b_limbs[1]);
2841 const uint512_t c0_inner = (a_limbs[1] * b_limbs[1]);
2842 const uint512_t c1_inner = (a_limbs[2] * b_limbs[0]);
2843 const uint512_t c2_inner = (a_limbs[0] * b_limbs[2]);
2844 const uint512_t d0_inner = (a_limbs[3] * b_limbs[0]);
2845 const uint512_t d1_inner = (a_limbs[2] * b_limbs[1]);
2846 const uint512_t d2_inner = (a_limbs[1] * b_limbs[2]);
2847 const uint512_t d3_inner = (a_limbs[0] * b_limbs[3]);
2848
2849 const uint512_t r0_inner = (a_limbs[0] * b_limbs[0]); // c0 := a0 * b0
2850 const uint512_t r1_inner = b0_inner + b1_inner; // c1 := a1 * b0 + a0 * b1
2851 const uint512_t r2_inner = c0_inner + c1_inner + c2_inner; // c2 := a2 * b0 + a1 * b1 + a0 * b2
2852 const uint512_t r3_inner = d0_inner + d1_inner + d2_inner + d3_inner; // c3 := a3 * b0 + a2 * b1 + a1 * b2 + a0 * b3
2853 const uint512_t lo_val = r0_inner + (r1_inner << NUM_LIMB_BITS); // lo := c0 + c1 * 2^b
2854 const uint512_t hi_val = r2_inner + (r3_inner << NUM_LIMB_BITS); // hi := c2 + c3 * 2^b
2855 return std::pair<uint512_t, uint512_t>(lo_val, hi_val);
2856}
2857
2869template <typename Builder, typename T>
2871 const uint32_t limb_idx,
2872 const size_t num_limb_bits)
2873{
2874 BB_ASSERT_LT(uint256_t(ctx->get_variable(limb_idx)), (uint256_t(1) << num_limb_bits));
2875 constexpr bb::fr LIMB_MASK = (uint256_t(1) << NUM_LIMB_BITS) - 1;
2876 const uint256_t value = ctx->get_variable(limb_idx);
2877 const uint256_t low = value & LIMB_MASK;
2878 const uint256_t hi = value >> NUM_LIMB_BITS;
2879 BB_ASSERT_EQ(low + (hi << NUM_LIMB_BITS), value);
2880
2881 const uint32_t low_idx = ctx->add_variable(bb::fr(low));
2882 const uint32_t hi_idx = ctx->add_variable(bb::fr(hi));
2883
2884 BB_ASSERT_GT(num_limb_bits, NUM_LIMB_BITS);
2885 const size_t lo_bits = NUM_LIMB_BITS;
2886 const size_t hi_bits = num_limb_bits - NUM_LIMB_BITS;
2887 ctx->range_constrain_two_limbs(
2888 low_idx, hi_idx, lo_bits, hi_bits, "decompose_non_native_field_double_width_limb: limbs too large");
2889
2890 return std::array<uint32_t, 2>{ low_idx, hi_idx };
2891}
2892
2893} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_GT(left, right,...)
Definition assert.hpp:113
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:158
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:143
constexpr uint256_t slice(uint64_t start, uint64_t end) const
constexpr uint64_t get_msb() const
constexpr uintx slice(const uint64_t start, const uint64_t end) const
Definition uintx.hpp:81
constexpr uint64_t get_msb() const
Definition uintx.hpp:68
static void unsafe_evaluate_multiple_multiply_add(const std::vector< bigfield > &input_left, const std::vector< bigfield > &input_right, const std::vector< bigfield > &to_add, const bigfield &input_quotient, const std::vector< bigfield > &input_remainders)
Evaluate a relation involving multiple multiplications and additions.
static bigfield conditional_assign(const bool_t< Builder > &predicate, const bigfield &lhs, const bigfield &rhs)
Definition bigfield.hpp:568
static bigfield msub_div(const std::vector< bigfield > &mul_left, const std::vector< bigfield > &mul_right, const bigfield &divisor, const std::vector< bigfield > &to_sub, bool enable_divisor_nz_check=true)
Builder * get_context() const
Definition bigfield.hpp:689
bigfield operator*(const bigfield &other) const
Evaluate a non-native field multiplication: (a * b = c mod p) where p == target_basis....
bigfield conditional_select(const bigfield &other, const bool_t< Builder > &predicate) const
Create an element which is equal to either this or other based on the predicate.
static bigfield div_check_denominator_nonzero(const std::vector< bigfield > &numerators, const bigfield &denominator)
static bigfield sum(const std::vector< bigfield > &terms)
Create constraints for summing these terms.
bigfield(const field_t< Builder > &low_bits, const field_t< Builder > &high_bits, const bool can_overflow=false, const size_t maximum_bitlength=0)
Constructs a new bigfield object from two field elements representing the low and high bits.
static void unsafe_evaluate_square_add(const bigfield &left, const std::vector< bigfield > &to_add, const bigfield &quotient, const bigfield &remainder)
Evaluate a square with several additions.
bigfield madd(const bigfield &to_mul, const std::vector< bigfield > &to_add) const
Compute a * b + ...to_add = c mod p.
bigfield conditional_negate(const bool_t< Builder > &predicate) const
static bigfield mult_madd(const std::vector< bigfield > &mul_left, const std::vector< bigfield > &mul_right, const std::vector< bigfield > &to_add, bool fix_remainder_to_zero=false)
void set_origin_tag(const bb::OriginTag &tag) const
Definition bigfield.hpp:691
uint512_t get_value() const
void assert_is_in_field(std::string const &msg="bigfield::assert_is_in_field") const
static bigfield internal_div(const std::vector< bigfield > &numerators, const bigfield &denominator, bool check_for_zero)
void assert_less_than(const uint256_t &upper_limit, std::string const &msg="bigfield::assert_less_than") const
bigfield add_to_lower_limb(const field_t< Builder > &other, const uint256_t &other_maximum_value) const
Add a field element to the lower limb. CAUTION (the element has to be constrained before using this f...
void set_free_witness_tag()
Set the free witness flag for the bigfield.
Definition bigfield.hpp:718
bigfield & operator=(const bigfield &other)
void convert_constant_to_fixed_witness(Builder *builder)
Definition bigfield.hpp:665
uint512_t get_maximum_value() const
std::array< uint256_t, NUM_LIMBS > get_binary_basis_limb_maximums()
Get the maximum values of the binary basis limbs.
static uint512_t compute_maximum_quotient_value(const std::vector< uint512_t > &as, const std::vector< uint512_t > &bs, const std::vector< uint512_t > &to_add)
Compute the maximum possible value of quotient of a*b+\sum(to_add)
bigfield sqradd(const std::vector< bigfield > &to_add) const
Square and add operator, computes a * a + ...to_add = c mod p.
bigfield add_two(const bigfield &add_a, const bigfield &add_b) const
Create constraints for summing three bigfield elements efficiently.
std::array< uint32_t, NUM_LIMBS > get_binary_basis_limb_witness_indices() const
Get the witness indices of the (normalized) binary basis limbs.
Definition bigfield.hpp:965
bb::OriginTag get_origin_tag() const
Definition bigfield.hpp:706
static bigfield from_witness(Builder *ctx, const bb::field< T > &input)
Definition bigfield.hpp:297
void reduction_check() const
Check if the bigfield element needs to be reduced.
void assert_zero_if(const bool_t< Builder > &predicate, std::string const &msg="bigfield::assert_zero_if failed") const
static constexpr uint256_t modulus
Definition bigfield.hpp:315
bool_t< Builder > is_less_than(const uint256_t &upper_limit, std::string const &msg="bigfield::is_less_than") const
static bigfield dual_madd(const bigfield &left_a, const bigfield &right_a, const bigfield &left_b, const bigfield &right_b, const std::vector< bigfield > &to_add)
bigfield sqr() const
Square operator, computes a * a = c mod p.
static void perform_reductions_for_mult_madd(std::vector< bigfield > &mul_left, std::vector< bigfield > &mul_right, const std::vector< bigfield > &to_add)
Performs individual reductions on the supplied elements as well as more complex reductions to prevent...
bool is_constant() const
Check if the bigfield is constant, i.e. its prime limb is constant.
Definition bigfield.hpp:603
static std::array< uint32_t, 2 > decompose_non_native_field_double_width_limb(Builder *ctx, const uint32_t limb_idx, const size_t num_limb_bits=(2 *NUM_LIMB_BITS))
Decompose a single witness into two limbs, range constrained to NUM_LIMB_BITS (68) and num_limb_bits ...
void reduce_mod_target_modulus() const
static std::pair< uint512_t, uint512_t > compute_quotient_remainder_values(const bigfield &a, const bigfield &b, const std::vector< bigfield > &to_add)
Compute the quotient and remainder values for dividing (a * b + (to_add[0] + ... + to_add[-1])) with ...
void unsafe_assert_less_than(const uint256_t &upper_limit, std::string const &msg="bigfield::unsafe_assert_less_than") const
Assert that the current bigfield is less than the given upper limit.
bigfield operator+(const bigfield &other) const
Adds two bigfield elements. Inputs are reduced to the modulus if necessary. Requires 4 gates if both ...
void assert_equal(const bigfield &other, std::string const &msg="bigfield::assert_equal") const
static bigfield create_from_u512_as_witness(Builder *ctx, const uint512_t &value, const bool can_overflow=false, const size_t maximum_bitlength=0)
Creates a bigfield element from a uint512_t. Bigfield element is constructed as a witness and not a c...
bigfield pow(const uint32_t exponent) const
Raise the bigfield element to the power of (out-of-circuit) exponent.
static std::pair< bool, size_t > get_quotient_reduction_info(const std::vector< uint512_t > &as_max, const std::vector< uint512_t > &bs_max, const std::vector< bigfield > &to_add, const std::vector< uint1024_t > &remainders_max={ DEFAULT_MAXIMUM_REMAINDER })
Check for 2 conditions (CRT modulus is overflown or the maximum quotient doesn't fit into range proof...
static bigfield unsafe_construct_from_limbs(const field_t< Builder > &a, const field_t< Builder > &b, const field_t< Builder > &c, const field_t< Builder > &d, const bool can_overflow=false)
Construct a bigfield element from binary limbs that are already reduced.
Definition bigfield.hpp:158
void sanity_check() const
Perform a sanity check on a value that is about to interact with another value.
static void unsafe_evaluate_multiply_add(const bigfield &input_left, const bigfield &input_to_mul, const std::vector< bigfield > &to_add, const bigfield &input_quotient, const std::vector< bigfield > &input_remainders)
Evaluate a multiply add identity with several added elements and several remainders.
field_t< Builder > prime_basis_limb
Represents a bigfield element in the prime basis: (a mod n) where n is the native modulus.
Definition bigfield.hpp:86
static bigfield div_without_denominator_check(const std::vector< bigfield > &numerators, const bigfield &denominator)
std::array< Limb, NUM_LIMBS > binary_basis_limbs
Represents a bigfield element in the binary basis. A bigfield element is represented as a combination...
Definition bigfield.hpp:81
bool_t< Builder > operator==(const bigfield &other) const
Validate whether two bigfield elements are equal to each other.
bigfield operator-() const
Negation operator, works by subtracting this from zero.
Definition bigfield.hpp:457
static std::pair< uint512_t, uint512_t > compute_partial_schoolbook_multiplication(const std::array< uint256_t, NUM_LIMBS > &a_limbs, const std::array< uint256_t, NUM_LIMBS > &b_limbs)
Compute the partial multiplication of two uint256_t arrays using schoolbook multiplication.
void self_reduce() const
Reduce the bigfield element modulo the target modulus.
void assert_is_not_equal(const bigfield &other, std::string const &msg="bigfield: prime limb diff is zero, but expected non-zero") const
bigfield operator/(const bigfield &other) const
Implements boolean logic in-circuit.
Definition bool.hpp:60
bool get_value() const
Definition bool.hpp:125
bool is_constant() const
Definition bool.hpp:127
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:154
void unset_free_witness_tag()
Definition bool.hpp:157
Builder * context
Definition bool.hpp:168
OriginTag tag
Definition bool.hpp:179
OriginTag get_origin_tag() const
Definition bool.hpp:155
Represents a dynamic array of bytes in-circuit.
byte_array slice(size_t offset) const
Slice bytes from the byte array starting at offset. Does not add any constraints.
size_t size() const
Builder * get_context() const
bb::OriginTag get_origin_tag() const
void assert_is_zero(std::string const &msg="field_t::assert_is_zero") const
Enforce a copy constraint between *this and 0 stored at zero_idx of the Builder.
Definition field.cpp:687
void assert_equal(const field_t &rhs, std::string const &msg="field_t::assert_equal") const
Copy constraint: constrain that *this field is equal to rhs element.
Definition field.cpp:940
field_t madd(const field_t &to_mul, const field_t &to_add) const
Definition field.cpp:518
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:67
bb::fr additive_constant
Definition field.hpp:94
static void evaluate_polynomial_identity(const field_t &a, const field_t &b, const field_t &c, const field_t &d, const std::string &msg="field_t::evaluate_polynomial_identity")
Given a, b, c, d, constrain a * b + c + d = 0 by creating a big_mul_gate.
Definition field.cpp:1135
static field_t accumulate(const std::vector< field_t > &input)
Efficiently compute the sum of vector entries. Using big_add_gate we reduce the number of gates neede...
Definition field.cpp:1178
static bool witness_indices_match(const field_t &a, const field_t &b)
Check if two field elements have the same witness index (for identity checks).
Definition field.hpp:531
void create_range_constraint(size_t num_bits, std::string const &msg="field_t::range_constraint") const
Let x = *this.normalize(), constrain x.v < 2^{num_bits}.
Definition field.cpp:919
Builder * context
Definition field.hpp:57
bb::fr multiplicative_constant
Definition field.hpp:95
static field_t conditional_assign_internal(const bool_t< Builder > &predicate, const field_t &lhs, const field_t &rhs)
If predicate == true then return lhs, else return rhs.
Definition field.cpp:893
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:836
static void evaluate_linear_identity(const field_t &a, const field_t &b, const field_t &c, const field_t &d, const std::string &msg="field_t::evaluate_linear_identity")
Constrain a + b + c + d to be equal to 0.
Definition field.cpp:1099
bool is_constant() const
Definition field.hpp:441
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:357
uint32_t witness_index
Definition field.hpp:145
field_t add_two(const field_t &add_b, const field_t &add_c) const
Efficiently compute (this + a + b) using big_mul gate.
Definition field.cpp:583
void assert_is_not_zero(std::string const &msg="field_t::assert_is_not_zero") const
Constrain *this to be non-zero by establishing that it has an inverse.
Definition field.cpp:718
uint32_t get_witness_index() const
Get the witness index of the current field element.
Definition field.hpp:518
StrictMock< MockContext > context
FF a
FF b
stdlib::field_t< Builder > field_ct
void add_values(TreeType &tree, const std::vector< NullifierLeafValue > &values)
uintx< uint256_t > uint512_t
Definition uintx.hpp:306
uintx< uint512_t > uint1024_t
Definition uintx.hpp:308
std::conditional_t< IsGoblinBigGroup< C, Fq, Fr, G >, element_goblin::goblin_element< C, goblin_field< C >, Fr, G >, element_default::element< C, Fq, Fr, G > > element
element wraps either element_default::element or element_goblin::goblin_element depending on parametr...
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
Univariate< Fr, domain_end > operator+(const Fr &ff, const Univariate< Fr, domain_end > &uv)
field< Bn254FrParams > fr
Definition fr.hpp:155
C slice(C const &container, size_t start)
Definition container.hpp:9
Inner sum(Cont< Inner, Args... > const &in)
Definition container.hpp:70
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
static OriginTag constant()
constexpr field invert() const noexcept
Represents a single limb of a bigfield element, with its value and maximum value.
Definition bigfield.hpp:45
void throw_or_abort(std::string const &err)