9#include "../circuit_builders/circuit_builders.hpp"
21template <
typename Builder>
31template <
typename Builder>
45template <
typename Builder>
50 "bool_t: witness value is not 0 or 1");
53 if (use_range_constraint) {
67template <
typename Builder>
78template <
typename Builder>
81 , witness_bool(other.witness_bool)
82 , witness_inverted(other.witness_inverted)
83 , witness_index(other.witness_index)
90template <
typename Builder>
93 , witness_bool(other.witness_bool)
94 , witness_inverted(other.witness_inverted)
95 , witness_index(other.witness_index)
103template <
typename Builder>
108 result.witness_index = witness_index;
109 const bb::fr value = ctx->get_variable(witness_index);
112 result.witness_bool = (
value == 1);
113 result.witness_inverted =
false;
116 result.set_free_witness_tag();
126 witness_index = IS_CONSTANT;
127 witness_bool = other;
128 witness_inverted =
false;
144 witness_index = other.witness_index;
145 witness_bool = other.witness_bool;
146 witness_inverted = other.witness_inverted;
157 "bool_t: witness value is not 0 or 1");
161 witness_inverted =
false;
163 context->create_bool_gate(witness_index);
164 set_free_witness_tag();
175 bool left = witness_inverted ^ witness_bool;
212 int i_a(
static_cast<int>(witness_inverted));
215 fr q_m{ 1 - 2 * i_b - 2 * i_a + 4 * i_a * i_b };
216 fr q_l{ i_b * (1 - 2 * i_a) };
217 fr q_r{ i_a * (1 - 2 * i_b) };
221 ctx->create_arithmetic_gate(
223 }
else if (!is_constant() && other.
is_constant()) {
229 }
else if (is_constant() && !other.
is_constant()) {
233 result = witness_bool ? other : *
this;
264 const int lhs_inverted =
static_cast<int>(witness_inverted);
266 bb::fr q_m{ -(1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted) };
267 bb::fr q_l{ (1 - 2 * lhs_inverted) * (1 - rhs_inverted) };
268 bb::fr q_r{ (1 - lhs_inverted) * (1 - 2 * rhs_inverted) };
270 bb::fr q_c{ rhs_inverted + lhs_inverted - rhs_inverted * lhs_inverted };
275 ctx->create_arithmetic_gate(
277 }
else if (!is_constant() && other.
is_constant()) {
284 }
else if (is_constant() && !other.
is_constant()) {
288 result = witness_bool ? *this : other;
318 const int lhs_inverted =
static_cast<int>(witness_inverted);
320 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
322 bb::fr q_m{ -2 * aux_prod };
326 bb::fr q_c{ lhs_inverted + rhs_inverted - 2 * rhs_inverted * lhs_inverted };
331 ctx->create_arithmetic_gate(
333 }
else if (!is_constant() && other.
is_constant()) {
338 }
else if (is_constant() && !other.
is_constant()) {
340 result = witness_bool ? !other : other;
386 const int lhs_inverted =
static_cast<int>(witness_inverted);
388 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
389 bb::fr q_m{ 2 * aux_prod };
393 bb::fr q_c{ 1 - lhs_inverted - rhs_inverted + 2 * rhs_inverted * lhs_inverted };
395 ctx->create_arithmetic_gate(
398 }
else if (!is_constant() && (other.
is_constant())) {
403 }
else if (is_constant() && !other.
is_constant()) {
406 result = witness_bool ? other : !other;
417 return operator^(other);
422 return operator&(other);
427 return operator|(other);
444 ctx->assert_equal_constant(rhs.witness_index, lhs_value, msg);
445 }
else if (rhs.is_constant()) {
448 const bool rhs_value = lhs.
witness_inverted ? !rhs.witness_bool : rhs.witness_bool;
449 ctx->assert_equal_constant(lhs.
witness_index, rhs_value, msg);
454 const auto rhs_original_tag = rhs.get_origin_tag();
457 rhs.set_origin_tag(empty_tag);
470 rhs.set_origin_tag(rhs_original_tag);
477template <
typename Builder>
485 return result.normalize();
491 if (witness_same || const_same) {
496 return ((predicate && lhs) || (!predicate && rhs)).
normalize();
505 return (!(*
this) || other);
521 return !((*this) ^ other);
536 if (!witness_inverted) {
543 const bool value = witness_bool ^ witness_inverted;
547 const int inverted =
static_cast<int>(witness_inverted);
548 bb::fr q_l{ 1 - 2 * inverted };
553 context->create_arithmetic_gate({ witness_index,
context->zero_idx(), new_witness, q_m, q_l, q_r, q_o, q_c });
555 witness_index = new_witness;
556 witness_bool =
value;
557 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 OriginTag constant()
static constexpr field neg_one()
static constexpr field one()
static constexpr field zero()