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