8#include "../circuit_builders/circuit_builders.hpp"
19template <
typename Builder>
27template <
typename Builder>
39template <
typename Builder>
44 "bool_t: witness value is not 0 or 1");
47 if (use_range_constraint) {
61template <
typename Builder>
70template <
typename Builder>
73 , witness_bool(other.witness_bool)
74 , witness_inverted(other.witness_inverted)
75 , witness_index(other.witness_index)
82template <
typename Builder>
85 , witness_bool(other.witness_bool)
86 , witness_inverted(other.witness_inverted)
87 , witness_index(other.witness_index)
95template <
typename Builder>
98 ASSERT(witness_index != IS_CONSTANT);
101 const bb::fr value = ctx->get_variable(witness_index);
115 witness_index = IS_CONSTANT;
116 witness_bool = other;
117 witness_inverted =
false;
140 witness_index = other.witness_index;
141 witness_bool = other.witness_bool;
142 witness_inverted = other.witness_inverted;
156 witness_inverted =
false;
158 context->create_bool_gate(witness_index);
159 set_free_witness_tag();
169 bool left = witness_inverted ^ witness_bool;
206 int i_a(
static_cast<int>(witness_inverted));
209 fr q_m{ 1 - 2 * i_b - 2 * i_a + 4 * i_a * i_b };
210 fr q_l{ i_b * (1 - 2 * i_a) };
211 fr q_r{ i_a * (1 - 2 * i_b) };
217 }
else if (!is_constant() && other.
is_constant()) {
223 }
else if (is_constant() && !other.
is_constant()) {
224 ASSERT(!witness_inverted);
227 result = witness_bool ? other : *
this;
256 const int lhs_inverted =
static_cast<int>(witness_inverted);
258 bb::fr q_m{ -(1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted) };
259 bb::fr q_l{ (1 - 2 * lhs_inverted) * (1 - rhs_inverted) };
260 bb::fr q_r{ (1 - lhs_inverted) * (1 - 2 * rhs_inverted) };
262 bb::fr q_c{ rhs_inverted + lhs_inverted - rhs_inverted * lhs_inverted };
269 }
else if (!is_constant() && other.
is_constant()) {
276 }
else if (is_constant() && !other.
is_constant()) {
280 result = witness_bool ? *this : other;
309 const int lhs_inverted =
static_cast<int>(witness_inverted);
311 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
313 bb::fr q_m{ -2 * aux_prod };
317 bb::fr q_c{ lhs_inverted + rhs_inverted - 2 * rhs_inverted * lhs_inverted };
323 { witness_index, other.
witness_index, result.witness_index, q_m, q_l, q_r, q_o, q_c });
324 }
else if (!is_constant() && other.
is_constant()) {
329 }
else if (is_constant() && !other.
is_constant()) {
331 result = witness_bool ? !other : other;
343 ASSERT(!witness_inverted);
376 const int lhs_inverted =
static_cast<int>(witness_inverted);
378 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
379 bb::fr q_m{ 2 * aux_prod };
383 bb::fr q_c{ 1 - lhs_inverted - rhs_inverted + 2 * rhs_inverted * lhs_inverted };
388 }
else if (!is_constant() && (other.
is_constant())) {
393 }
else if (is_constant() && !other.
is_constant()) {
396 result = witness_bool ? other : !other;
407 return operator^(other);
412 return operator&(other);
417 return operator|(other);
434 ctx->assert_equal_constant(rhs.
witness_index, lhs_value, msg);
439 ctx->assert_equal_constant(lhs.
witness_index, rhs_value, msg);
455template <
typename Builder>
469 if (witness_same || const_same) {
472 return (predicate && lhs) || (!predicate && rhs);
481 return (!(*
this) || other);
497 return !((*this) ^ other);
508 ASSERT(!witness_inverted);
512 if (!witness_inverted) {
519 const bool value = witness_bool ^ witness_inverted;
523 const int inverted =
static_cast<int>(witness_inverted);
524 bb::fr q_l{ 1 - 2 * inverted };
529 context->create_poly_gate({ witness_index, witness_index, new_witness, q_m, q_l, q_r, q_o, q_c });
531 witness_index = new_witness;
532 witness_bool =
value;
533 witness_inverted =
false;
#define BB_ASSERT_EQ(actual, expected,...)
#define ASSERT(expression,...)
Implements boolean logic in-circuit.
void set_origin_tag(const OriginTag &new_tag) const
bool_t implies(const bool_t &other) const
Implements implication operator in circuit.
bool_t normalize() const
A bool_t element is normalized if witness_inverted == false. For a given *this, output its normalized...
bool_t operator&(const bool_t &other) const
Implements AND in circuit.
void set_free_witness_tag()
bool_t operator!() const
Implements negation in circuit.
static bool_t conditional_assign(const bool_t< Builder > &predicate, const bool_t &lhs, const bool_t &rhs)
Implements the ternary operator - if predicate == true then return lhs, else return rhs.
bool_t operator!=(const bool_t &other) const
Implements the not equal operator in circuit.
Builder * get_context() const
bool_t operator&&(const bool_t &other) const
bool_t(const bool value=false)
Construct a constant bool_t object from a bool value.
bool_t operator||(const bool_t &other) const
void must_imply(const bool_t &other, std::string const &msg="bool_t::must_imply") const
Constrains the (a => b) == true.
bool_t & operator=(const bool other)
Assigns a native bool to bool_t object.
void assert_equal(const bool_t &rhs, std::string const &msg="bool_t::assert_equal") const
Implements copy constraint for bool_t elements.
bool_t operator|(const bool_t &other) const
Implements OR in circuit.
static bool_t from_witness_index_unsafe(Builder *ctx, uint32_t witness_index)
Create a bool_t from a witness index that is known to contain a constrained bool value.
bool_t implies_both_ways(const bool_t &other) const
Implements a "double-implication" (<=>), a.k.a "iff", a.k.a. "biconditional".
OriginTag get_origin_tag() const
bool_t operator^(const bool_t &other) const
Implements XOR in circuit.
bool_t operator==(const bool_t &other) const
Implements equality operator in circuit.
StrictMock< MockContext > context
Entry point for Barretenberg command-line interface.
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
static constexpr field neg_one()
static constexpr field one()
static constexpr field zero()