8#include "../circuit_builders/circuit_builders_fwd.hpp"
9#include "../witness/witness.hpp"
31 ASSERT(first == tail &&
"Pointers refer to different builder objects!");
35template <
typename T,
typename Container> T*
validate_context(
const Container& elements)
38 for (
const auto&
element : elements) {
39 result = validate_context<T>(result,
element.get_context());
44template <
typename Builder>
class bool_t;
45template <
typename Builder_>
class field_t {
220 if (
this == &other) {
244 result.assert_equal(other,
"field_t::copy_as_new_witness, assert_equal");
245 result.tag = other.
tag;
266 *
this = *
this + other;
271 *
this = *
this - other;
276 *
this = *
this * other;
281 *
this = *
this / other;
298 return this_before_operation;
316 ctx->failure(
"field_t::invert denominator is 0");
347 void assert_equal(
const field_t& rhs, std::string
const& msg =
"field_t::assert_equal")
const;
397 void assert_is_not_zero(std::string
const& msg =
"field_t::assert_is_not_zero")
const;
398 void assert_is_zero(std::string
const& msg =
"field_t::assert_is_zero")
const;
427 result.set_free_witness_tag();
482 const auto&
a = (*this);
483 const auto&
b = other;
485 if (
a.is_constant() &&
b.is_constant()) {
502 static_assert(range_constant < bb::fr::modulus >> 1,
503 "ranged_less_than: 2^num_bits must be less than half the field modulus.");
507 field_t predicate_valid =
b.add_two(-(
a) + range_constant - 1, -
field_t(predicate) * range_constant);
#define ASSERT(expression,...)
Implements boolean logic in-circuit.
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.
static field_t reconstruct_from_public(const std::span< const field_t, PUBLIC_INPUTS_SIZE > &limbs)
field_t conditional_negate(const bool_t< Builder > &predicate) const
If predicate's value == true, negate the value, else keep it unchanged.
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.
uint32_t set_public() const
field_t & operator=(const field_t &other)
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.
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.
bool is_normalized() const
field_t operator*=(const field_t &other)
field_t madd(const field_t &to_mul, const field_t &to_add) const
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
field_t operator+(const field_t &other) const
Field addition operator.
bool_t< Builder > operator!=(const field_t &other) const
Compute a bool_t equal to (a != b)
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...
field_t(const bb::fr &value)
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...
static void evaluate_polynomial_identity(const field_t &a, const field_t &b, const field_t &c, const field_t &d)
Given a, b, c, d, constrain a * b + c + d = 0 by creating a big_mul_gate.
field_t operator-() const
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}.
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.
field_t(const unsigned long value)
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.
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,...
bb::fr multiplicative_constant
void unset_free_witness_tag() const
Unset the free witness flag for the field element's tag.
static field_t copy_as_new_witness(Builder &context, field_t const &other)
Builder * get_context() const
field_t(field_t &&other) noexcept
std::pair< field_t< Builder >, field_t< Builder > > 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:
static constexpr bool is_composite
OriginTag get_origin_tag() const
field_t operator-=(const field_t &other)
bb::fr get_value() const
Given a := *this, compute its value given by a.v * a.mul + a.add.
field_t operator*(const field_t &other) const
Field multiplication operator.
field_t normalize() const
Return a new element, where the in-circuit witness contains the actual represented value (multiplicat...
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...
static constexpr uint256_t modulus
static void evaluate_linear_identity(const field_t &a, const field_t &b, const field_t &c, const field_t &d)
Constrain a + b + c + d to be equal to 0.
static field_t from_witness(Builder *ctx, const bb::fr &input)
bool_t< Builder > is_zero() const
Validate whether a field_t element is zero.
field_t pow(const uint32_t &exponent) const
Raise this field element to the power of the provided uint32_t exponent.
field_t(const field_t &other)
static constexpr size_t PUBLIC_INPUTS_SIZE
void convert_constant_to_fixed_witness(Builder *ctx)
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,...
uint32_t get_normalized_witness_index() const
Get the index of a normalized version of this element.
field_t operator+=(const field_t &other)
field_t & operator=(field_t &&other) noexcept
field_t(const unsigned int value)
field_t(const uint256_t &value)
field_t operator++(const int)
void set_free_witness_tag()
Set the free witness flag for the field element's tag.
void set_origin_tag(const OriginTag &new_tag) const
field_t add_two(const field_t &add_b, const field_t &add_c) const
Efficiently compute (this + a + b) using big_mul gate.
field_t(const unsigned long long value)
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.
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...
field_t operator/=(const field_t &other)
bool_t< Builder > operator==(const field_t &other) const
Compute a bool_t equal to (a == b)
uint32_t get_witness_index() const
Get the witness index of the current field element.
bool_t< Builder > ranged_less_than(const field_t< Builder > &other) const
Return (a < b) as bool circuit type. This method assumes that both a and b are < 2^{num_bits} i....
constexpr size_t MAX_NO_WRAP_INTEGER_BIT_LENGTH
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
T * validate_context(T *ptr)
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.
field< Bn254FrParams > fr
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
void unset_free_witness()
static constexpr field one()
static constexpr uint256_t modulus
BB_INLINE constexpr void self_neg() &noexcept
static constexpr field zero()