8#include "../circuit_builders/circuit_builders.hpp"
20template <
typename Builder>
28template <
typename Builder>
40template <
typename Builder>
45 "bool_t: witness value is not 0 or 1");
48 if (use_range_constraint) {
62template <
typename Builder>
71template <
typename Builder>
74 , witness_bool(other.witness_bool)
75 , witness_inverted(other.witness_inverted)
76 , witness_index(other.witness_index)
83template <
typename Builder>
86 , witness_bool(other.witness_bool)
87 , witness_inverted(other.witness_inverted)
88 , witness_index(other.witness_index)
96template <
typename Builder>
102 const bb::fr value = ctx->get_variable(witness_index);
116 witness_index = IS_CONSTANT;
117 witness_bool = other;
118 witness_inverted =
false;
133 witness_index = other.witness_index;
134 witness_bool = other.witness_bool;
135 witness_inverted = other.witness_inverted;
149 witness_inverted =
false;
151 context->create_bool_gate(witness_index);
152 set_free_witness_tag();
163 bool left = witness_inverted ^ witness_bool;
200 int i_a(
static_cast<int>(witness_inverted));
203 fr q_m{ 1 - 2 * i_b - 2 * i_a + 4 * i_a * i_b };
204 fr q_l{ i_b * (1 - 2 * i_a) };
205 fr q_r{ i_a * (1 - 2 * i_b) };
209 ctx->create_arithmetic_gate(
211 }
else if (!is_constant() && other.
is_constant()) {
217 }
else if (is_constant() && !other.
is_constant()) {
221 result = witness_bool ? other : *
this;
252 const int lhs_inverted =
static_cast<int>(witness_inverted);
254 bb::fr q_m{ -(1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted) };
255 bb::fr q_l{ (1 - 2 * lhs_inverted) * (1 - rhs_inverted) };
256 bb::fr q_r{ (1 - lhs_inverted) * (1 - 2 * rhs_inverted) };
258 bb::fr q_c{ rhs_inverted + lhs_inverted - rhs_inverted * lhs_inverted };
263 ctx->create_arithmetic_gate(
265 }
else if (!is_constant() && other.
is_constant()) {
272 }
else if (is_constant() && !other.
is_constant()) {
276 result = witness_bool ? *this : other;
306 const int lhs_inverted =
static_cast<int>(witness_inverted);
308 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
310 bb::fr q_m{ -2 * aux_prod };
314 bb::fr q_c{ lhs_inverted + rhs_inverted - 2 * rhs_inverted * lhs_inverted };
319 ctx->create_arithmetic_gate(
321 }
else if (!is_constant() && other.
is_constant()) {
326 }
else if (is_constant() && !other.
is_constant()) {
328 result = witness_bool ? !other : other;
374 const int lhs_inverted =
static_cast<int>(witness_inverted);
376 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
377 bb::fr q_m{ 2 * aux_prod };
381 bb::fr q_c{ 1 - lhs_inverted - rhs_inverted + 2 * rhs_inverted * lhs_inverted };
383 ctx->create_arithmetic_gate(
386 }
else if (!is_constant() && (other.
is_constant())) {
391 }
else if (is_constant() && !other.
is_constant()) {
394 result = witness_bool ? other : !other;
405 return operator^(other);
410 return operator&(other);
415 return operator|(other);
432 ctx->assert_equal_constant(rhs.
witness_index, lhs_value, msg);
437 ctx->assert_equal_constant(lhs.
witness_index, rhs_value, msg);
464template <
typename Builder>
472 return result.normalize();
478 if (witness_same || const_same) {
483 return ((predicate && lhs) || (!predicate && rhs)).
normalize();
492 return (!(*
this) || other);
508 return !((*this) ^ other);
523 if (!witness_inverted) {
530 const bool value = witness_bool ^ witness_inverted;
534 const int inverted =
static_cast<int>(witness_inverted);
535 bb::fr q_l{ 1 - 2 * inverted };
540 context->create_arithmetic_gate({ witness_index,
context->zero_idx(), new_witness, q_m, q_l, q_r, q_o, q_c });
542 witness_index = new_witness;
543 witness_bool =
value;
544 witness_inverted =
false;
#define BB_ASSERT(expression,...)
#define BB_ASSERT_EQ(actual, expected,...)
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)
Conditionally assign lhs or rhs based on predicate, always returns normalized result.
bool_t operator!=(const bool_t &other) const
Implements the not equal operator in circuit.
Builder * get_context() const
uint32_t witness_index
Index of the witness in the builder's witness vector.
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()