Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#include "field.hpp"
8#include "../bool/bool.hpp"
9#include "../circuit_builders/circuit_builders.hpp"
12#include "field_utils.hpp"
13#include <functional>
14
15using namespace bb;
16
17namespace bb::stdlib {
18
19template <typename Builder>
21 : context(parent_context)
22 , additive_constant(bb::fr::zero())
23 , multiplicative_constant(bb::fr::one())
24 , witness_index(IS_CONSTANT)
25{}
26
27template <typename Builder>
30 , additive_constant(bb::fr::zero())
31 , multiplicative_constant(bb::fr::one())
32 , witness_index(value.witness_index)
33{
35}
36
37template <typename Builder>
39 : context(parent_context)
40 , additive_constant(value)
41 , multiplicative_constant(bb::fr::one())
42 , witness_index(IS_CONSTANT)
43{}
44
45template <typename Builder>
60
61template <typename Builder>
63{
64 field_t<Builder> result(ctx);
65 result.witness_index = witness_index;
66 return result;
67}
68
75template <typename Builder> field_t<Builder>::operator bool_t<Builder>() const
76{
77 // If `this` is a constant field_t element, the resulting bool is also constant.
78 // In this case, `additive_constant` uniquely determines the value of `this`.
79 // After ensuring that `additive_constant` \in {0, 1}, we set the `.witness_bool` field of `result` to match the
80 // value of `additive_constant`.
81 if (is_constant()) {
82 BB_ASSERT(additive_constant == bb::fr::one() || additive_constant == bb::fr::zero());
84 result.witness_bool = (additive_constant == bb::fr::one());
85 result.set_origin_tag(tag);
86 return result;
87 }
88
89 const bool add_constant_check = (additive_constant == bb::fr::zero());
90 const bool mul_constant_check = (multiplicative_constant == bb::fr::one());
91 const bool inverted_check = (additive_constant == bb::fr::one()) && (multiplicative_constant == bb::fr::neg_one());
92 bool result_inverted = false;
93 // Process the elements of the form
94 // a = a.v * 1 + 0 and a = a.v * (-1) + 1
95 // They do not need to be normalized if `a.v` is constrained to be boolean. In the first case, we have
96 // a == a.v,
97 // and in the second case
98 // a == ¬(a.v).
99 // The distinction between the cases is tracked by the .witness_inverted field of bool_t.
100 uint32_t witness_idx = witness_index;
101 if ((add_constant_check && mul_constant_check) || inverted_check) {
102 result_inverted = inverted_check;
103 } else {
104 // In general, the witness has to be normalized.
105 witness_idx = normalize().witness_index;
106 }
107 // Get the normalized value of the witness
108 bb::fr witness = context->get_variable(witness_idx);
109 BB_ASSERT_EQ((witness == bb::fr::zero()) || (witness == bb::fr::one()),
110 true,
111 "Attempting to create a bool_t from a witness_t not satisfying x^2 - x = 0");
112 bool_t result(context, witness == bb::fr::one());
113 result.witness_inverted = result_inverted;
114 result.witness_index = witness_idx;
115 context->create_bool_gate(witness_idx);
116 result.set_origin_tag(tag);
117 return result;
118}
119
124template <typename Builder> field_t<Builder> field_t<Builder>::operator+(const field_t& other) const
125{
127 field_t<Builder> result(ctx);
128 // Ensure that non-constant circuit elements can not be added without context
129 BB_ASSERT(ctx || (is_constant() && other.is_constant()));
130
131 if (witness_indices_match(*this, other) && !is_constant()) {
132 // If summands represent the same circuit variable, i.e. their witness indices coincide, we just need to update
133 // the scaling factors of this variable.
134 result.additive_constant = additive_constant + other.additive_constant;
135 result.multiplicative_constant = multiplicative_constant + other.multiplicative_constant;
136 result.witness_index = witness_index;
137 } else if (is_constant() && other.is_constant()) {
138 // both inputs are constant - don't add a gate
139 result.additive_constant = additive_constant + other.additive_constant;
140 } else if (!is_constant() && other.is_constant()) {
141 // one input is constant - don't add a gate, but update scaling factors
142 result.additive_constant = additive_constant + other.additive_constant;
143 result.multiplicative_constant = multiplicative_constant;
144 result.witness_index = witness_index;
145 } else if (is_constant() && !other.is_constant()) {
146 result.additive_constant = additive_constant + other.additive_constant;
148 result.witness_index = other.witness_index;
149 } else {
150 // The summands are distinct circuit variables, the result needs to be constrained.
151 // a + b = a.v * a.mul + b.v * b.mul + (a.add + b.add)
152 // which leads to the constraint
153 // a.v * q_l + b.v * q_r + result.v * q_o + q_c = 0,
154 // where q_l, q_r, q_0, and q_c are the selectors storing corresponding scaling factors.
155 bb::fr left = ctx->get_variable(witness_index); // =: a.v
156 bb::fr right = ctx->get_variable(other.witness_index); // =: b.v
157 bb::fr result_value = left * multiplicative_constant;
158 result_value += right * other.multiplicative_constant;
159 result_value += additive_constant;
160 result_value += other.additive_constant;
161 result.witness_index = ctx->add_variable(result_value);
162
163 ctx->create_add_gate({ .a = witness_index,
164 .b = other.witness_index,
165 .c = result.witness_index,
166 .a_scaling = multiplicative_constant,
167 .b_scaling = other.multiplicative_constant,
168 .c_scaling = bb::fr::neg_one(),
169 .const_scaling = (additive_constant + other.additive_constant) });
170 }
171 result.tag = OriginTag(tag, other.tag);
172 return result;
173}
177template <typename Builder> field_t<Builder> field_t<Builder>::operator-(const field_t& other) const
178{
179 field_t<Builder> rhs(other);
181 if (!rhs.is_constant()) {
182 // Negate the multiplicative constant of the rhs then feed to `+` operator
184 }
185 return operator+(rhs);
186}
187
192template <typename Builder> field_t<Builder> field_t<Builder>::operator*(const field_t& other) const
193{
195 field_t<Builder> result(ctx);
196 // Ensure that non-constant circuit elements can not be multiplied without context
197 BB_ASSERT(ctx || (is_constant() && other.is_constant()));
198
199 if (is_constant() && other.is_constant()) {
200 // Both inputs are constant - don't add a gate.
201 // The value of a constant is tracked in `.additive_constant`.
202 result.additive_constant = additive_constant * other.additive_constant;
203 } else if (!is_constant() && other.is_constant()) {
204
205 // Here and in the next case, only one input is not constant: don't add a gate, but update scaling factors.
206 // More concretely, let:
207 // a := this;
208 // b := other;
209 // a.v := ctx->variables[a.witness_index], the value of a;
210 // b.v := ctx->variables[b.witness_index], the value of b;
211 // .mul = .multiplicative_constant
212 // .add = .additive_constant
213 // Value of this = a.v * a.mul + a.add;
214 // Value of other = b.add
215 // Value of result = a * b = a.v * [a.mul * b.add] + [a.add * b.add]
216 // ^ ^result.mul ^result.add
217 // ^result.v
218
219 result.additive_constant = additive_constant * other.additive_constant;
220 result.multiplicative_constant = multiplicative_constant * other.additive_constant;
221 // We simply updated the scaling factors of `*this`, so `witness_index` of `result` must be equal to
222 // `this->witness_index`.
223 result.witness_index = witness_index;
224 } else if (is_constant() && !other.is_constant()) {
225 // Only one input is not constant: don't add a gate, but update scaling factors
226 result.additive_constant = additive_constant * other.additive_constant;
227 result.multiplicative_constant = other.multiplicative_constant * additive_constant;
228 // We simply updated the scaling factors of `other`, so `witness_index` of `result` must be equal to
229 // `other->witness_index`.
230 result.witness_index = other.witness_index;
231 } else {
250 bb::fr T0;
251 bb::fr q_m;
252 bb::fr q_l;
253 bb::fr q_r;
254 bb::fr q_c;
255
256 // Compute selector values
257 q_c = additive_constant * other.additive_constant;
258 q_r = additive_constant * other.multiplicative_constant;
259 q_l = multiplicative_constant * other.additive_constant;
260 q_m = multiplicative_constant * other.multiplicative_constant;
261
262 bb::fr left = context->get_variable(witness_index); // =: a.v
263 bb::fr right = context->get_variable(other.witness_index); // =: b.v
264 bb::fr result_value;
266 result_value = left * right;
267 result_value *= q_m;
268 // Scale `b.v` by the constant `a_mul * b_add`
269 T0 = left * q_l;
270 result_value += T0;
271 // Scale `a.v` by the constant `a_add * b_mul`
272 T0 = right * q_r;
273 result_value += T0;
274 result_value += q_c;
275 result.witness_index = ctx->add_variable(result_value);
276 // Constrain
277 // a.v * b.v * q_m + a.v * q_l + b_v * q_r + q_c + result.v * q_o = 0
278 ctx->create_arithmetic_gate({ .a = witness_index,
279 .b = other.witness_index,
280 .c = result.witness_index,
281 .q_m = q_m,
282 .q_l = q_l,
283 .q_r = q_r,
284 .q_o = bb::fr::neg_one(),
285 .q_c = q_c });
286 }
287 result.tag = OriginTag(tag, other.tag);
288 return result;
289}
290
302template <typename Builder> field_t<Builder> field_t<Builder>::operator/(const field_t& other) const
303{
304 // If the denominator is a constant 0, the division is aborted. Otherwise, it is constrained to be non-zero.
305 other.assert_is_not_zero("field_t::operator/ divisor is 0");
306 return divide_no_zero_check(other);
307}
308
312template <typename Builder> field_t<Builder> field_t<Builder>::divide_no_zero_check(const field_t& other) const
313{
314
315 // Let
316 // a := this;
317 // b := other;
318 // q := a / b;
320 field_t<Builder> result(ctx);
321 // Ensure that non-constant circuit elements can not be divided without context
322 BB_ASSERT(ctx || (is_constant() && other.is_constant()));
323
324 bb::fr additive_multiplier = bb::fr::one();
325
326 if (is_constant() && other.is_constant()) {
327 // Both inputs are constant, the result is given by
328 // q = a.add / b.add, if b != 0.
329 // q = a.add , if b == 0
330 if (!(other.additive_constant == bb::fr::zero())) {
331 additive_multiplier = other.additive_constant.invert();
332 }
333 result.additive_constant = additive_constant * additive_multiplier;
334 } else if (!is_constant() && other.is_constant()) {
335 // The numerator is a circuit variable, the denominator is a constant.
336 // The result is obtained by updating the circuit variable `a`
337 // q = a.v * [a.mul / b.add] + a.add / b.add, if b != 0.
338 // q = a , if b == 0
339 // with q.witness_index = a.witness_index.
340 if (!(other.additive_constant == bb::fr::zero())) {
341 additive_multiplier = other.additive_constant.invert();
342 }
343 result.additive_constant = additive_constant * additive_multiplier;
344 result.multiplicative_constant = multiplicative_constant * additive_multiplier;
345 result.witness_index = witness_index;
346 } else if (is_constant() && !other.is_constant()) {
347 // The numerator is a constant, the denominator is a circuit variable.
348 // If a == 0, the result is a constant 0, otherwise the result is a new variable that has to be constrained.
349 if (get_value() == 0) {
350 result.additive_constant = 0;
351 result.multiplicative_constant = 1;
352 result.witness_index = IS_CONSTANT;
353 } else {
354 bb::fr numerator = get_value();
355 bb::fr denominator_inv = other.get_value();
356 denominator_inv = denominator_inv.is_zero() ? 0 : denominator_inv.invert();
357
358 bb::fr out(numerator * denominator_inv);
359 result.witness_index = ctx->add_variable(out);
360 // Define non-zero selector values for an arithmetic gate
361 // q_m := b.mul
362 // q_l := b.add
363 // q_c := a (= a.add, since a is constant)
365 bb::fr q_l = other.additive_constant;
366 bb::fr q_c = -get_value();
367 // The value of the quotient q = a / b has to satisfy
368 // q * (b.v * b.mul + b.add) = a
369 // Create an arithmetic gate to constrain the quotient.
370 // q * b.v * q_m + q * q_l + 0 * b + 0 * c + q_c = 0
371 ctx->create_arithmetic_gate({ .a = result.witness_index,
372 .b = other.witness_index,
373 .c = result.witness_index,
374 .q_m = q_m,
375 .q_l = q_l,
376 .q_r = 0,
377 .q_o = 0,
378 .q_c = q_c });
379 }
380 } else {
381 // Both numerator and denominator are circuit variables. Create a new circuit variable with the value a / b.
382 bb::fr numerator = get_value();
383 bb::fr denominator_inv = other.get_value();
384 denominator_inv = denominator_inv.is_zero() ? 0 : denominator_inv.invert();
386 bb::fr out(numerator * denominator_inv);
387 result.witness_index = ctx->add_variable(out);
388
389 // The value of the quotient q = a / b has to satisfy
390 // q * (b.v * b.mul + b.add) = a.v * a.mul + a.add
391 // Create an arithmetic gate to constrain the quotient
392 // q * b.v * q_m + q * q_l + 0 * c + a.v * q_o + q_c = 0,
393 // where the selector values are defined as follows:
394 // q_m = b.mul;
395 // q_l = b.add;
396 // q_r = 0;
397 // q_o = - a.mul;
398 // q_c = - a.add.
400 bb::fr q_l = other.additive_constant;
401 bb::fr q_r = bb::fr::zero();
402 bb::fr q_o = -multiplicative_constant;
403 bb::fr q_c = -additive_constant;
404
405 ctx->create_arithmetic_gate({ .a = result.witness_index,
406 .b = other.witness_index,
407 .c = witness_index,
408 .q_m = q_m,
409 .q_l = q_l,
410 .q_r = q_r,
411 .q_o = q_o,
412 .q_c = q_c });
414 result.tag = OriginTag(tag, other.tag);
415 return result;
416}
422template <typename Builder> field_t<Builder> field_t<Builder>::pow(const uint32_t& exponent) const
423{
424 if (is_constant()) {
425 return field_t(get_value().pow(exponent));
427 if (exponent == 0) {
429 }
430
431 bool accumulator_initialized = false;
432 field_t<Builder> accumulator;
433 field_t<Builder> running_power = *this;
434 auto shifted_exponent = exponent;
435
436 // Square and multiply, there's no need to constrain the exponent bit decomposition, as it is an integer constant.
437 while (shifted_exponent != 0) {
438 if (shifted_exponent & 1) {
439 if (!accumulator_initialized) {
440 accumulator = running_power;
441 accumulator_initialized = true;
442 } else {
443 accumulator *= running_power;
444 }
445 }
446 if (shifted_exponent >= 2) {
447 // Don't update `running_power` if `shifted_exponent` = 1, as it won't be used anywhere.
448 running_power = running_power.sqr();
449 }
450 shifted_exponent >>= 1;
451 }
452 return accumulator;
453}
454
459template <typename Builder> field_t<Builder> field_t<Builder>::pow(const field_t& exponent) const
460{
461 uint256_t exponent_value = exponent.get_value();
462 BB_ASSERT_LT(exponent_value.get_msb(), 32U);
463
464 if (is_constant() && exponent.is_constant()) {
465 return field_t(get_value().pow(exponent_value));
466 }
467 // Use the constant version that perfoms only the necessary multiplications if the exponent is constant
468 if (exponent.is_constant()) {
469 return pow(static_cast<uint32_t>(exponent_value));
470 }
471
472 auto* ctx = validate_context(context, exponent.context);
473
474 std::array<bool_t<Builder>, 32> exponent_bits;
475 // Collect individual bits as bool_t's
476 for (size_t i = 0; i < exponent_bits.size(); ++i) {
477 uint256_t value_bit = exponent_value & 1;
478 bool_t<Builder> bit;
479 bit = bool_t<Builder>(witness_t<Builder>(ctx, value_bit.data[0]));
480 bit.set_origin_tag(exponent.tag);
481 exponent_bits[31 - i] = bit;
482 exponent_value >>= 1;
483 }
484
485 field_t<Builder> exponent_accumulator(bb::fr::zero());
486 for (const auto& bit : exponent_bits) {
487 exponent_accumulator += exponent_accumulator;
488 exponent_accumulator += bit;
489 }
490 // Constrain the sum of bool_t bits to be equal to the original exponent value.
491 exponent.assert_equal(exponent_accumulator, "field_t::pow exponent accumulator incorrect");
492
493 // Compute the result of exponentiation
494 field_t accumulator(ctx, bb::fr::one());
495 const field_t one(bb::fr::one());
496 for (size_t i = 0; i < 32; ++i) {
497 accumulator *= accumulator;
498 // If current bit == 1, multiply by the base, else propagate the accumulator
499 const field_t multiplier = conditional_assign_internal(exponent_bits[i], *this, one);
500 accumulator *= multiplier;
501 }
502 accumulator = accumulator.normalize();
503 accumulator.tag = OriginTag(tag, exponent.tag);
504 return accumulator;
505}
506
510template <typename Builder> field_t<Builder> field_t<Builder>::madd(const field_t& to_mul, const field_t& to_add) const
511{
512 Builder* ctx = validate_context<Builder>(context, to_mul.context, to_add.context);
513
514 const bool mul_by_const = is_constant() || to_mul.is_constant();
515
516 if (mul_by_const) {
517 // If at least one of the multiplicands is constant, `madd` is efficiently handled by `*` and `+`
518 // operators.
519 return ((*this) * to_mul + to_add);
520 }
521
522 // Let:
523 // a = this;
524 // b = to_mul;
525 // c = to_add;
526 // a.v = ctx->variables[this.witness_index];
527 // b.v = ctx->variables[to_mul.witness_index];
528 // c.v = ctx->variables[to_add.witness_index];
529 // .mul = .multiplicative_constant;
530 // .add = .additive_constant.
531 //
532 // result = a * b + c
533 // = (a.v * a.mul + a.add) * (b.v * b.mul + b.add) + (c.v * c.mul + c.add)
534 // = a.v * b.v * [a.mul * b.mul] + a.v * [a.mul * b.add] + b.v * [b.mul + a.add] + c.v * [c.mul]
535 // + [a.add * b.add + c.add]
536 // = a.v * b.v * [ mul_scaling ] + a.v * [ a_scaling ] + b.v * [ b_scaling ] + c.v * [ c_scaling ]
537 // + [ const_scaling ]
538
539 bb::fr mul_scaling = multiplicative_constant * to_mul.multiplicative_constant;
540 bb::fr a_scaling = multiplicative_constant * to_mul.additive_constant;
541 bb::fr b_scaling = to_mul.multiplicative_constant * additive_constant;
542 bb::fr c_scaling = to_add.multiplicative_constant;
543 bb::fr const_scaling = additive_constant * to_mul.additive_constant + to_add.additive_constant;
544
545 // Note: the value of a constant field_t is wholly tracked by the field_t's `additive_constant` member, which is
546 // accounted for in the above-calculated selectors (`q_`'s). Therefore no witness (`variables[witness_index]`)
547 // exists for constants, and so the field_t's corresponding wire value is set to `0` in the gate equation.
548 bb::fr a = is_constant() ? bb::fr::zero() : ctx->get_variable(witness_index);
549 bb::fr b = to_mul.is_constant() ? bb::fr::zero() : ctx->get_variable(to_mul.witness_index);
550 bb::fr c = to_add.is_constant() ? bb::fr::zero() : ctx->get_variable(to_add.witness_index);
551
552 bb::fr out = a * b * mul_scaling + a * a_scaling + b * b_scaling + c * c_scaling + const_scaling;
553
554 field_t<Builder> result(ctx);
555 result.witness_index = ctx->add_variable(out);
556 ctx->create_big_mul_add_gate({
557 .a = is_constant() ? ctx->zero_idx() : witness_index,
558 .b = to_mul.is_constant() ? ctx->zero_idx() : to_mul.witness_index,
559 .c = to_add.is_constant() ? ctx->zero_idx() : to_add.witness_index,
560 .d = result.witness_index,
561 .mul_scaling = mul_scaling,
562 .a_scaling = a_scaling,
563 .b_scaling = b_scaling,
564 .c_scaling = c_scaling,
565 .d_scaling = bb::fr::neg_one(),
566 .const_scaling = const_scaling,
567 });
568 result.tag = OriginTag(tag, to_mul.tag, to_add.tag);
569 return result;
570}
571
575template <typename Builder> field_t<Builder> field_t<Builder>::add_two(const field_t& add_b, const field_t& add_c) const
576{
577 const bool has_const_summand = is_constant() || add_b.is_constant() || add_c.is_constant();
578
579 if (has_const_summand) {
580 // If at least one of the summands is constant, the summation is efficiently handled by `+` operator
581 return (*this) + add_b + add_c;
582 }
583 Builder* ctx = validate_context<Builder>(context, add_b.context, add_c.context);
584
585 // Let d := a + (b+c), where
586 // a := *this;
587 // b := add_b;
588 // c := add_c;
589 // define selector values by
590 // mul_scaling := 0
591 // a_scaling := a_mul;
592 // b_scaling := b_mul;
593 // c_scaling := c_mul;
594 // d_scaling := -1;
595 // const_scaling := a_add + b_add + c_add;
596 // Create a `big_mul_gate` to constrain
597 // a * b * mul_scaling + a * a_scaling + b * b_scaling + c * c_scaling + d * d_scaling + const_scaling = 0
598
599 bb::fr a_scaling = multiplicative_constant;
600 bb::fr b_scaling = add_b.multiplicative_constant;
601 bb::fr c_scaling = add_c.multiplicative_constant;
602 bb::fr const_scaling = additive_constant + add_b.additive_constant + add_c.additive_constant;
603
604 // Compute the sum of values of all summands
605 bb::fr a = is_constant() ? bb::fr::zero() : ctx->get_variable(witness_index);
606 bb::fr b = add_b.is_constant() ? bb::fr::zero() : ctx->get_variable(add_b.witness_index);
607 bb::fr c = add_c.is_constant() ? bb::fr::zero() : ctx->get_variable(add_c.witness_index);
608
609 bb::fr out = a * a_scaling + b * b_scaling + c * c_scaling + const_scaling;
610
611 field_t<Builder> result(ctx);
612 result.witness_index = ctx->add_variable(out);
613
614 // Constrain the result
615 ctx->create_big_mul_add_gate({
616 .a = is_constant() ? ctx->zero_idx() : witness_index,
617 .b = add_b.is_constant() ? ctx->zero_idx() : add_b.witness_index,
618 .c = add_c.is_constant() ? ctx->zero_idx() : add_c.witness_index,
619 .d = result.witness_index,
620 .mul_scaling = bb::fr::zero(),
621 .a_scaling = a_scaling,
622 .b_scaling = b_scaling,
623 .c_scaling = c_scaling,
624 .d_scaling = bb::fr::neg_one(),
625 .const_scaling = const_scaling,
626 });
627 result.tag = OriginTag(tag, add_b.tag, add_c.tag);
628 return result;
629}
630
638template <typename Builder> field_t<Builder> field_t<Builder>::normalize() const
639{
640 if (is_normalized()) {
641 return *this;
642 }
644
645 // Value of this = this.v * this.mul + this.add; // where this.v = context->variables[this.witness_index]
646 // Normalised result = result.v * 1 + 0; // where result.v = this.v * this.mul + this.add
647 // We need a new gate to enforce that the `result` was correctly calculated from `this`.
649 bb::fr value = context->get_variable(witness_index);
650
651 result.witness_index = context->add_variable(value * multiplicative_constant + additive_constant);
654
655 // The aim of a new `add` gate is to constrain
656 // this.v * this.mul + this.add == result.v
657 // Let
658 // a_scaling := this.mul;
659 // b_scaling := 0;
660 // c_scaling := -1;
661 // const_scaling := this.add;
662 // The `add` gate enforces the relation
663 // this.v * a_scaling + result.v * c_scaling + const_scaling = 0
664
665 context->create_add_gate({ .a = witness_index,
666 .b = context->zero_idx(),
667 .c = result.witness_index,
668 .a_scaling = multiplicative_constant,
669 .b_scaling = bb::fr::zero(),
670 .c_scaling = bb::fr::neg_one(),
671 .const_scaling = additive_constant });
672 result.tag = tag;
673 return result;
674}
675
679template <typename Builder> void field_t<Builder>::assert_is_zero(std::string const& msg) const
680{
681
682 if (is_constant()) {
683 BB_ASSERT_EQ(additive_constant == bb::fr::zero(), true, msg);
684 return;
685 }
686
687 if ((get_value() != bb::fr::zero()) && !context->failed()) {
688 context->failure(msg);
689 }
690 // Aim of a new arithmetic gate: constrain this.v * this.mul + this.add == 0
691 // I.e.:
692 // this.v * 0 * [ 0 ] + this.v * [this.mul] + 0 * [ 0 ] + 0 * [ 0 ] + [this.add] == 0
693 // this.v * 0 * [q_m] + this.v * [ q_l ] + 0 * [q_r] + 0 * [q_o] + [ q_c ] == 0
694
695 context->create_arithmetic_gate({
696 .a = witness_index,
697 .b = context->zero_idx(),
698 .c = context->zero_idx(),
699 .q_m = bb::fr::zero(),
700 .q_l = multiplicative_constant,
701 .q_r = bb::fr::zero(),
702 .q_o = bb::fr::zero(),
703 .q_c = additive_constant,
704 });
705}
706
710template <typename Builder> void field_t<Builder>::assert_is_not_zero(std::string const& msg) const
711{
712
713 if (is_constant()) {
714 BB_ASSERT_EQ(additive_constant != bb::fr::zero(), true, msg);
715 return;
716 }
717
718 if ((get_value() == bb::fr::zero()) && !context->failed()) {
719 context->failure(msg);
720 }
721
722 bb::fr inverse_value = (get_value() == bb::fr::zero()) ? bb::fr::zero() : get_value().invert();
723
724 field_t<Builder> inverse(witness_t<Builder>(context, inverse_value));
725
726 // inverse is added in the circuit for checking that field element is not zero
727 // and it won't be used anymore, so it's needed to add this element in used witnesses
728 mark_witness_as_used(inverse);
729
730 // Aim of a new arithmetic gate: `this` has an inverse (hence is not zero).
731 // I.e.:
732 // (this.v * this.mul + this.add) * inverse.v == 1;
733 // <=> this.v * inverse.v * [this.mul] + this.v * [ 0 ] + inverse.v * [this.add] + 0 * [ 0 ] + [ -1] == 0
734 // <=> this.v * inverse.v * [ q_m ] + this.v * [q_l] + inverse.v * [ q_r ] + 0 * [q_o] + [q_c] == 0
735
736 // (a * mul_const + add_const) * b - 1 = 0
737 context->create_arithmetic_gate({
738 .a = witness_index, // input value
739 .b = inverse.witness_index, // inverse
740 .c = context->zero_idx(), // no output
741 .q_m = multiplicative_constant, // a * b * mul_const
742 .q_l = bb::fr::zero(), // a * 0
743 .q_r = additive_constant, // b * mul_const
744 .q_o = bb::fr::zero(), // c * 0
745 .q_c = bb::fr::neg_one(), // -1
746 });
747}
748
775template <typename Builder> bool_t<Builder> field_t<Builder>::is_zero() const
776{
777 bb::fr native_value = get_value();
778 const bool is_zero_raw = native_value.is_zero();
779
780 if (is_constant()) {
781 // Create a constant bool_t
782 bool_t is_zero(context, is_zero_raw);
783 is_zero.set_origin_tag(get_origin_tag());
784 return is_zero;
785 }
786
787 bool_t is_zero = witness_t(context, is_zero_raw);
788
789 // This can be done out of circuit, as `is_zero = true` implies `I = 1`.
790 bb::fr inverse_native = (is_zero_raw) ? bb::fr::one() : native_value.invert();
791
792 field_t inverse = witness_t(context, inverse_native);
793
794 // Note that `evaluate_polynomial_identity(a, b, c, d)` checks that `a * b + c + d = 0`, so we are using it for the
795 // constraints 1) and 2) above.
796 // More precisely, to check that `a * I - 1 + is_zero = 0`, it creates a `big_mul_gate` given by the equation:
797 // a.v * I.v * mul_scaling + a.v * a_scaling + I.v * b_scaling + is_zero.v * c_scaling + (-1) * d_scaling +
798 // const_scaling = 0
799 // where
800 // muk_scaling := a.mul * I.mul;
801 // a_scaling := a.mul * I.add;
802 // b_scaling := I.mul * a.add;
803 // c_scaling := 1;
804 // d_scaling := 0;
805 // const_scaling := a.add * I.add + is_zero.add - 1;
806 field_t::evaluate_polynomial_identity(*this, inverse, is_zero, bb::fr::neg_one());
807
808 // To check that `-is_zero * I + is_zero = 0`, create a `big_mul_gate` given by the equation:
809 // is_zero.v * (-I).v * mul_scaling + is_zero.v * a_scaling + (-I).v * b_scaling + is_zero.v * c_scaling + 0 *
810 // d_scaling + const_scaling = 0
811 // where
812 // mul_scaling := is_zero.mul * (-I).mul;
813 // a_scaling := is_zero.mul * (-I).add;
814 // b_scaling := (-I).mul * is_zero.add;
815 // c_scaling := is_zero.mul;
816 // d_scaling := 0;
817 // const_scaling := is_zero.add * (-I).add + is_zero.add;
818 field_t::evaluate_polynomial_identity(is_zero, -inverse, is_zero, bb::fr::zero());
819 is_zero.set_origin_tag(tag);
820 return is_zero;
821}
828template <typename Builder> bb::fr field_t<Builder>::get_value() const
829{
830 if (!is_constant()) {
832 return (multiplicative_constant * context->get_variable(witness_index)) + additive_constant;
833 }
834 BB_ASSERT_DEBUG(multiplicative_constant == bb::fr::one());
835 // A constant field_t's value is tracked wholly by its additive_constant member.
836 return additive_constant;
837}
838
842template <typename Builder> bool_t<Builder> field_t<Builder>::operator==(const field_t& other) const
843{
844 return ((*this) - other).is_zero();
845}
846
850template <typename Builder> bool_t<Builder> field_t<Builder>::operator!=(const field_t& other) const
851{
852 return !operator==(other);
853}
854
858template <typename Builder>
860{
861 if (predicate.is_constant()) {
862 field_t result = predicate.get_value() ? -(*this) : *this;
863 result.set_origin_tag(OriginTag(get_origin_tag(), predicate.get_origin_tag()));
864 return result;
865 }
866 // Compute
867 // `predicate` * ( -2 * a ) + a.
868 // If predicate's value == true, then the output is `-a`, else it's `a`
869 static constexpr bb::fr minus_two(-2);
870 return field_t(predicate).madd(*this * minus_two, *this);
871}
872
884template <typename Builder>
886 const field_t& lhs,
887 const field_t& rhs)
888{
889 // If the predicate is constant, the conditional assignment can be done out of circuit
890 if (predicate.is_constant()) {
891 auto result = field_t(predicate.get_value() ? lhs : rhs);
892 result.set_origin_tag(OriginTag(predicate.get_origin_tag(), lhs.get_origin_tag(), rhs.get_origin_tag()));
893 return result;
894 }
895 // If lhs and rhs are the same witness or constant, just return it
896 if (witness_indices_match(lhs, rhs) && (lhs.additive_constant == rhs.additive_constant) &&
898 return lhs;
899 }
900
901 return (lhs - rhs).madd(predicate, rhs);
902}
903
908template <typename Builder>
909void field_t<Builder>::create_range_constraint(const size_t num_bits, std::string const& msg) const
910{
911 if (num_bits == 0) {
912 assert_is_zero("0-bit range_constraint on non-zero field_t.");
913 } else {
914 if (is_constant()) {
915 BB_ASSERT_LT(uint256_t(get_value()).get_msb(), num_bits, msg);
916 } else {
917 context->decompose_into_default_range(
918 normalize().witness_index, num_bits, bb::UltraCircuitBuilder::DEFAULT_PLOOKUP_RANGE_BITNUM, msg);
919 }
920 }
921}
922
930template <typename Builder> void field_t<Builder>::assert_equal(const field_t& rhs, std::string const& msg) const
931{
932 const field_t lhs = *this;
933 Builder* ctx = validate_context(lhs.get_context(), rhs.get_context());
934 if (lhs.is_constant() && rhs.is_constant()) {
935 BB_ASSERT_EQ(lhs.get_value(), rhs.get_value(), "field_t::assert_equal: constants are not equal");
936 return;
937 }
938 if (lhs.is_constant()) {
939 ctx->assert_equal_constant(rhs.get_witness_index(), lhs.get_value(), msg);
940 } else if (rhs.is_constant()) {
941 ctx->assert_equal_constant(lhs.get_witness_index(), rhs.get_value(), msg);
942 } else {
943 // Both are witnesses - save original tags and clear them to allow different transcript/free witness sources
944 // (e.g., proving 2 separate properties about same object through 2 different transcripts)
945 const auto lhs_original_tag = lhs.get_origin_tag();
946 const auto rhs_original_tag = rhs.get_origin_tag();
949
950 if (lhs.is_normalized() || rhs.is_normalized()) {
951 ctx->assert_equal(lhs.get_witness_index(), rhs.get_witness_index(), msg);
952 } else {
953 // Instead of creating 2 gates for normalizing both witnesses and applying a copy constraint, we use a
954 // single `add` gate constraining a - b = 0
955 ctx->create_add_gate({ .a = lhs.witness_index,
956 .b = rhs.witness_index,
957 .c = ctx->zero_idx(),
958 .a_scaling = lhs.multiplicative_constant,
959 .b_scaling = -rhs.multiplicative_constant,
960 .c_scaling = 0,
961 .const_scaling = lhs.additive_constant - rhs.additive_constant });
962 if ((lhs.get_value() != rhs.get_value()) && !ctx->failed()) {
963 ctx->failure(msg);
964 }
965 }
966
967 // Restore tags
968 lhs.set_origin_tag(lhs_original_tag);
969 rhs.set_origin_tag(rhs_original_tag);
970 }
971}
975template <typename Builder> void field_t<Builder>::assert_not_equal(const field_t& rhs, std::string const& msg) const
976{
977 const field_t lhs = *this;
978 const field_t diff = lhs - rhs;
979 diff.assert_is_not_zero(msg);
980}
984template <typename Builder>
985void field_t<Builder>::assert_is_in_set(const std::vector<field_t>& set, std::string const& msg) const
986{
987 const field_t input = *this;
988 field_t product = (input - set[0]);
989 for (size_t i = 1; i < set.size(); i++) {
990 product *= (input - set[i]);
991 }
992 product.assert_is_zero(msg);
993}
994
1002template <typename Builder>
1004 const field_t& T1,
1005 const field_t& T2,
1006 const field_t& T3)
1007{
1008
1010 table[0] = T0; // const coeff
1011 table[1] = T1 - T0; // t0 coeff
1012 table[2] = T2 - T0; // t1 coeff
1013 table[3] = T3.add_two(-table[2], -T1); // t0t1 coeff
1014 return table;
1015}
1016
1020template <typename Builder>
1022 const field_t& T1,
1023 const field_t& T2,
1024 const field_t& T3,
1025 const field_t& T4,
1026 const field_t& T5,
1027 const field_t& T6,
1028 const field_t& T7)
1029{
1031 table[0] = T0; // const coeff
1032 table[1] = T1 - T0; // t0 coeff
1033 table[2] = T2 - T0; // t1 coeff
1034 table[3] = T4 - T0; // t2 coeff
1035 table[4] = T3.add_two(-table[2], -T1); // t0t1 coeff
1036 table[5] = T5.add_two(-table[3], -T1); // t0t2 coeff
1037 table[6] = T6.add_two(-table[3], -T2); // t1t2 coeff
1038 table[7] = T7.add_two(-T6 - T5, T4 - table[4]); // t0t1t2 coeff
1039 return table;
1040}
1041
1046template <typename Builder>
1048 const bool_t<Builder>& t1,
1049 const bool_t<Builder>& t0)
1050{
1051 field_t R0 = field_t(t1).madd(table[3], table[1]);
1052 field_t R1 = R0.madd(field_t(t0), table[0]);
1053 field_t R2 = field_t(t1).madd(table[2], R1);
1054 return R2;
1055}
1056
1068template <typename Builder>
1070 const bool_t<Builder>& t2,
1071 const bool_t<Builder>& t1,
1072 const bool_t<Builder>& t0)
1073{
1074 field_t R0 = field_t(t0).madd(table[7], table[6]);
1075 field_t R1 = field_t(t1).madd(R0, table[3]);
1076 field_t R2 = field_t(t2).madd(R1, table[0]);
1077 field_t R3 = field_t(t0).madd(table[4], table[2]);
1078 field_t R4 = field_t(t1).madd(R3, R2);
1079 field_t R5 = field_t(t2).madd(table[5], table[1]);
1080 field_t R6 = field_t(t0).madd(R5, R4);
1081 return R6;
1082}
1083
1087template <typename Builder>
1089 const field_t& a, const field_t& b, const field_t& c, const field_t& d, const std::string& msg)
1090{
1091 Builder* ctx = validate_context(a.context, b.context, c.context, d.context);
1092
1093 if (a.is_constant() && b.is_constant() && c.is_constant() && d.is_constant()) {
1094 BB_ASSERT_EQ(a.get_value() + b.get_value() + c.get_value() + d.get_value(), 0);
1095 return;
1096 }
1097
1098 const bool identity_holds = (a.get_value() + b.get_value() + c.get_value() + d.get_value()).is_zero();
1099 if (!identity_holds && !ctx->failed()) {
1100 ctx->failure(msg);
1101 }
1102
1103 // validate that a + b + c + d = 0
1104 bb::fr const_scaling = a.additive_constant + b.additive_constant + c.additive_constant + d.additive_constant;
1105
1106 ctx->create_big_add_gate({
1107 .a = a.is_constant() ? ctx->zero_idx() : a.witness_index,
1108 .b = b.is_constant() ? ctx->zero_idx() : b.witness_index,
1109 .c = c.is_constant() ? ctx->zero_idx() : c.witness_index,
1110 .d = d.is_constant() ? ctx->zero_idx() : d.witness_index,
1111 .a_scaling = a.multiplicative_constant,
1112 .b_scaling = b.multiplicative_constant,
1113 .c_scaling = c.multiplicative_constant,
1114 .d_scaling = d.multiplicative_constant,
1115 .const_scaling = const_scaling,
1116 });
1117}
1123template <typename Builder>
1125 const field_t& a, const field_t& b, const field_t& c, const field_t& d, const std::string& msg)
1126{
1127 if (a.is_constant() && b.is_constant() && c.is_constant() && d.is_constant()) {
1128 BB_ASSERT((a.get_value() * b.get_value() + c.get_value() + d.get_value()).is_zero());
1129 return;
1130 }
1131
1132 Builder* ctx = validate_context(a.context, b.context, c.context, d.context);
1133
1134 const bool identity_holds = ((a.get_value() * b.get_value()) + c.get_value() + d.get_value()).is_zero();
1135 if (!identity_holds && !ctx->failed()) {
1136 ctx->failure(msg);
1137 }
1138
1139 // validate that a * b + c + d = 0
1140 bb::fr mul_scaling = a.multiplicative_constant * b.multiplicative_constant;
1141 bb::fr a_scaling = a.multiplicative_constant * b.additive_constant;
1142 bb::fr b_scaling = b.multiplicative_constant * a.additive_constant;
1143 bb::fr c_scaling = c.multiplicative_constant;
1144 bb::fr d_scaling = d.multiplicative_constant;
1145 bb::fr const_scaling = a.additive_constant * b.additive_constant + c.additive_constant + d.additive_constant;
1146
1147 ctx->create_big_mul_add_gate({
1148 .a = a.is_constant() ? ctx->zero_idx() : a.witness_index,
1149 .b = b.is_constant() ? ctx->zero_idx() : b.witness_index,
1150 .c = c.is_constant() ? ctx->zero_idx() : c.witness_index,
1151 .d = d.is_constant() ? ctx->zero_idx() : d.witness_index,
1152 .mul_scaling = mul_scaling,
1153 .a_scaling = a_scaling,
1154 .b_scaling = b_scaling,
1155 .c_scaling = c_scaling,
1156 .d_scaling = d_scaling,
1157 .const_scaling = const_scaling,
1158 });
1159}
1160
1168{
1169 if (input.empty()) {
1170 return field_t(bb::fr::zero());
1171 }
1172
1173 if (input.size() == 1) {
1174 return input[0].normalize();
1175 }
1176
1177 std::vector<field_t> accumulator;
1178 field_t constant_term = bb::fr::zero();
1179
1180 // Remove constant terms from input field elements
1181 for (const auto& element : input) {
1182 if (element.is_constant()) {
1183 constant_term += element;
1184 } else {
1185 accumulator.emplace_back(element);
1186 }
1187 }
1188 if (accumulator.empty()) {
1189 return constant_term;
1190 }
1191 // Add the accumulated constant term to the first witness. It does not create any gates - only the additive
1192 // constant of `accumulator[0]` is updated.
1193 accumulator[0] += constant_term;
1194
1195 // At this point, the `accumulator` vector consisting of witnesses is not empty, so we can extract the context.
1196 Builder* ctx = validate_context<Builder>(accumulator);
1197
1198 // Step 2: compute output value
1199 size_t num_elements = accumulator.size();
1200 bb::fr output = bb::fr::zero();
1201 for (const auto& acc : accumulator) {
1202 output += acc.get_value();
1203 }
1204
1205 // Pad the accumulator with zeroes so that its size is a multiple of 3.
1206 const size_t num_padding_wires = (num_elements % 3) == 0 ? 0 : 3 - (num_elements % 3);
1207 for (size_t i = 0; i < num_padding_wires; ++i) {
1208 accumulator.emplace_back(field_t<Builder>::from_witness_index(ctx, ctx->zero_idx()));
1209 }
1210 num_elements = accumulator.size();
1211 const size_t num_gates = (num_elements / 3);
1212 // Last gate is handled separetely
1213 const size_t last_gate_idx = num_gates - 1;
1214
1215 field_t total = witness_t(ctx, output);
1216 field_t accumulating_total = total;
1217
1218 // Let
1219 // a_i := accumulator[3*i];
1220 // b_i := accumulator[3*i+1];
1221 // c_i := accumulator[3*i+2];
1222 // d_0 := total;
1223 // d_i := total - \sum_(j < 3*i) accumulator[j];
1224 // which leads us to equations
1225 // d_{i+1} = d_{i} - a_i - b_i - c_i for i = 0, ..., last_idx - 1;
1226 // 0 = d_{i} - a_i - b_i - c_i for i = last_gate_idx,
1227 // that are turned into constraints below.
1228
1229 for (size_t i = 0; i < last_gate_idx; ++i) {
1230 // For i < last_gate_idx, we create a `big_add_gate` constraint
1231 // a_i.v * a_scaling + b_i.v * b_scaling + c_i.v * c_scaling + d_i.v * d_scaling + const_scaling +
1232 // w_4_omega = 0
1233 // where
1234 // a_scaling := a_i.mul
1235 // b_scaling := b_i.mul
1236 // c_scaling := c_i.mul
1237 // d_scaling := -1
1238 // const_scaling := a_i.add + b_i.add + c_i.add
1239 // w_4_omega := d_{i+1}
1240 ctx->create_big_add_gate(
1241 {
1242 .a = accumulator[3 * i].witness_index,
1243 .b = accumulator[3 * i + 1].witness_index,
1244 .c = accumulator[3 * i + 2].witness_index,
1245 .d = accumulating_total.witness_index,
1246 .a_scaling = accumulator[3 * i].multiplicative_constant,
1247 .b_scaling = accumulator[3 * i + 1].multiplicative_constant,
1248 .c_scaling = accumulator[3 * i + 2].multiplicative_constant,
1249 .d_scaling = -1,
1250 .const_scaling = accumulator[3 * i].additive_constant + accumulator[3 * i + 1].additive_constant +
1251 accumulator[3 * i + 2].additive_constant,
1252 },
1253 /*use_next_gate_w_4 = */ true);
1254 bb::fr new_total = accumulating_total.get_value() - accumulator[3 * i].get_value() -
1255 accumulator[3 * i + 1].get_value() - accumulator[3 * i + 2].get_value();
1256 accumulating_total = witness_t<Builder>(ctx, new_total);
1257 }
1258
1259 // For i = last_gate_idx, we create a `big_add_gate` constraining
1260 // a_i.v * a_scaling + b_i.v * b_scaling + c_i.v * c_scaling + d_i.v * d_scaling + const_scaling = 0
1261 ctx->create_big_add_gate({
1262 .a = accumulator[3 * last_gate_idx].witness_index,
1263 .b = accumulator[3 * last_gate_idx + 1].witness_index,
1264 .c = accumulator[3 * last_gate_idx + 2].witness_index,
1265 .d = accumulating_total.witness_index,
1266 .a_scaling = accumulator[3 * last_gate_idx].multiplicative_constant,
1267 .b_scaling = accumulator[3 * last_gate_idx + 1].multiplicative_constant,
1268 .c_scaling = accumulator[3 * last_gate_idx + 2].multiplicative_constant,
1269 .d_scaling = -1,
1270 .const_scaling = accumulator[3 * last_gate_idx].additive_constant +
1271 accumulator[3 * last_gate_idx + 1].additive_constant +
1272 accumulator[3 * last_gate_idx + 2].additive_constant,
1273 });
1274 OriginTag new_tag{};
1275 for (const auto& single_input : input) {
1276 new_tag = OriginTag(new_tag, single_input.tag);
1277 }
1278 total.tag = new_tag;
1279 return total.normalize();
1280}
1281
1290template <typename Builder>
1292 const size_t num_bits) const
1293{
1294 BB_ASSERT(lsb_index < num_bits);
1296
1297 const uint256_t value = get_value();
1298 const uint256_t hi = value >> lsb_index;
1299 const uint256_t lo = value % (uint256_t(1) << lsb_index);
1300
1301 if (is_constant()) {
1302 // If `*this` is constant, we can return the split values directly
1303 BB_ASSERT(lo + (hi << lsb_index) == value);
1305 }
1306
1307 // Handle edge case when lsb_index == 0
1308 if (lsb_index == 0) {
1309 BB_ASSERT(hi == value);
1310 BB_ASSERT(lo == 0);
1311 create_range_constraint(num_bits, "split_at: hi value too large.");
1312 return std::make_pair(field_t<Builder>(0), *this);
1313 }
1314
1315 Builder* ctx = get_context();
1316 BB_ASSERT(ctx != nullptr);
1317
1318 field_t<Builder> lo_wit(witness_t(ctx, lo));
1319 field_t<Builder> hi_wit(witness_t(ctx, hi));
1320
1321 // Ensure that `lo_wit` is in the range [0, 2^lsb_index - 1]
1322 lo_wit.create_range_constraint(lsb_index, "split_at: lo value too large.");
1323
1324 // Ensure that `hi_wit` is in the range [0, 2^(num_bits - lsb_index) - 1]
1325 hi_wit.create_range_constraint(num_bits - lsb_index, "split_at: hi value too large.");
1326
1327 // Check that *this = lo_wit + hi_wit * 2^{lsb_index}
1328 const field_t<Builder> reconstructed = lo_wit + (hi_wit * field_t<Builder>(uint256_t(1) << lsb_index));
1329 assert_equal(reconstructed, "split_at: decomposition failed");
1330
1331 // Set the origin tag for both witnesses
1332 lo_wit.set_origin_tag(tag);
1333 hi_wit.set_origin_tag(tag);
1334
1335 return std::make_pair(lo_wit, hi_wit);
1336}
1337
1339template class field_t<bb::MegaCircuitBuilder>;
1340
1341} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:67
#define BB_ASSERT_DEBUG(expression,...)
Definition assert.hpp:54
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:77
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:137
static constexpr size_t DEFAULT_PLOOKUP_RANGE_BITNUM
constexpr uint64_t get_msb() const
Implements boolean logic in-circuit.
Definition bool.hpp:59
bool get_value() const
Definition bool.hpp:124
bool is_constant() const
Definition bool.hpp:126
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:153
uint32_t witness_index
Index of the witness in the builder's witness vector.
Definition bool.hpp:177
bool witness_inverted
Definition bool.hpp:169
OriginTag tag
Definition bool.hpp:178
OriginTag get_origin_tag() const
Definition bool.hpp:154
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:679
field_t conditional_negate(const bool_t< Builder > &predicate) const
If predicate's value == true, negate the value, else keep it unchanged.
Definition field.cpp:859
Builder_ Builder
Definition field.hpp:47
void assert_is_in_set(const std::vector< field_t > &set, std::string const &msg="field_t::assert_not_in_set") const
Constrain *this \in set by enforcing that P(X) = \prod_{s \in set} (X - s) is 0 at X = *this.
Definition field.cpp:985
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:930
void assert_not_equal(const field_t &rhs, std::string const &msg="field_t::assert_not_equal") const
Constrain *this to be not equal to rhs.
Definition field.cpp:975
bool is_normalized() const
Definition field.hpp:430
field_t madd(const field_t &to_mul, const field_t &to_add) const
Definition field.cpp:510
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:62
field_t operator+(const field_t &other) const
Field addition operator.
Definition field.cpp:124
bool_t< Builder > operator!=(const field_t &other) const
Compute a bool_t equal to (a != b)
Definition field.cpp:850
bb::fr additive_constant
Definition field.hpp:93
static field_t select_from_three_bit_table(const std::array< field_t, 8 > &table, const bool_t< Builder > &t2, const bool_t< Builder > &t1, const bool_t< Builder > &t0)
Given a multilinear polynomial in 3 variables, which is represented by a table of monomial coefficien...
Definition field.cpp:1069
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:1124
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:1167
field_t operator-() const
Definition field.hpp:335
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:909
field_t divide_no_zero_check(const field_t &other) const
Given field elements a = *this and b = other, output a / b without checking whether b = 0.
Definition field.cpp:312
Builder * context
Definition field.hpp:56
static std::array< field_t, 8 > preprocess_three_bit_table(const field_t &T0, const field_t &T1, const field_t &T2, const field_t &T3, const field_t &T4, const field_t &T5, const field_t &T6, const field_t &T7)
Given a table T of size 8, outputs the monomial coefficients of the multilinear polynomial in t0,...
Definition field.cpp:1021
bb::fr multiplicative_constant
Definition field.hpp:94
Builder * get_context() const
Definition field.hpp:419
field_t sqr() const
Definition field.hpp:271
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:885
OriginTag get_origin_tag() const
Definition field.hpp:346
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
Definition field.cpp:828
field_t operator*(const field_t &other) const
Field multiplication operator.
Definition field.cpp:192
field_t(Builder *parent_context=nullptr)
Definition field.cpp:20
field_t normalize() const
Return a new element, where the in-circuit witness contains the actual represented value (multiplicat...
Definition field.cpp:638
static field_t select_from_two_bit_table(const std::array< field_t, 4 > &table, const bool_t< Builder > &t1, const bool_t< Builder > &t0)
Given a multilinear polynomial in 2 variables, which is represented by a table of monomial coefficien...
Definition field.cpp:1047
bool_t< Builder > is_zero() const
Validate whether a field_t element is zero.
Definition field.cpp:775
field_t pow(const uint32_t &exponent) const
Raise this field element to the power of the provided uint32_t exponent.
Definition field.cpp:422
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:1088
bool is_constant() const
Definition field.hpp:429
static std::array< field_t, 4 > preprocess_two_bit_table(const field_t &T0, const field_t &T1, const field_t &T2, const field_t &T3)
Given a table T of size 4, outputs the monomial coefficients of the multilinear polynomial in t0,...
Definition field.cpp:1003
void set_free_witness_tag()
Set the free witness flag for the field element's tag.
Definition field.hpp:351
void set_origin_tag(const OriginTag &new_tag) const
Definition field.hpp:345
uint32_t witness_index
Definition field.hpp:144
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:575
std::pair< field_t< Builder >, field_t< Builder > > no_wrap_split_at(const size_t lsb_index, const size_t num_bits=grumpkin::MAX_NO_WRAP_INTEGER_BIT_LENGTH) const
Splits the field element into (lo, hi), where:
Definition field.cpp:1291
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:710
field_t operator/(const field_t &other) const
Since in divide_no_zero_check, we check by the constraint , if , we can set to any value and it wil...
Definition field.cpp:302
bool_t< Builder > operator==(const field_t &other) const
Compute a bool_t equal to (a == b)
Definition field.cpp:842
uint32_t get_witness_index() const
Get the witness index of the current field element.
Definition field.hpp:506
StrictMock< MockContext > context
FF a
FF b
constexpr size_t MAX_NO_WRAP_INTEGER_BIT_LENGTH
Definition grumpkin.hpp:15
constexpr T get_msb(const T in)
Definition get_msb.hpp:47
T * validate_context(T *ptr)
Definition field.hpp:16
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...
Definition biggroup.hpp:995
void mark_witness_as_used(const field_t< Builder > &field)
Mark a field_t witness as used (for UltraBuilder only).
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)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static constexpr field neg_one()
static constexpr field one()
constexpr field invert() const noexcept
BB_INLINE constexpr void self_neg() &noexcept
BB_INLINE constexpr bool is_zero() const noexcept
static constexpr field zero()