9#include "../circuit_builders/circuit_builders.hpp"
21template <
typename Builder>
29template <
typename Builder>
41template <
typename Builder>
46 "bool_t: witness value is not 0 or 1");
49 if (use_range_constraint) {
63template <
typename Builder>
72template <
typename Builder>
75 , witness_bool(other.witness_bool)
76 , witness_inverted(other.witness_inverted)
77 , witness_index(other.witness_index)
84template <
typename Builder>
87 , witness_bool(other.witness_bool)
88 , witness_inverted(other.witness_inverted)
89 , witness_index(other.witness_index)
97template <
typename Builder>
103 const bb::fr value = ctx->get_variable(witness_index);
117 witness_index = IS_CONSTANT;
118 witness_bool = other;
119 witness_inverted =
false;
134 witness_index = other.witness_index;
135 witness_bool = other.witness_bool;
136 witness_inverted = other.witness_inverted;
147 "bool_t: witness value is not 0 or 1");
151 witness_inverted =
false;
153 context->create_bool_gate(witness_index);
154 set_free_witness_tag();
165 bool left = witness_inverted ^ witness_bool;
202 int i_a(
static_cast<int>(witness_inverted));
205 fr q_m{ 1 - 2 * i_b - 2 * i_a + 4 * i_a * i_b };
206 fr q_l{ i_b * (1 - 2 * i_a) };
207 fr q_r{ i_a * (1 - 2 * i_b) };
211 ctx->create_arithmetic_gate(
213 }
else if (!is_constant() && other.
is_constant()) {
219 }
else if (is_constant() && !other.
is_constant()) {
223 result = witness_bool ? other : *
this;
254 const int lhs_inverted =
static_cast<int>(witness_inverted);
256 bb::fr q_m{ -(1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted) };
257 bb::fr q_l{ (1 - 2 * lhs_inverted) * (1 - rhs_inverted) };
258 bb::fr q_r{ (1 - lhs_inverted) * (1 - 2 * rhs_inverted) };
260 bb::fr q_c{ rhs_inverted + lhs_inverted - rhs_inverted * lhs_inverted };
265 ctx->create_arithmetic_gate(
267 }
else if (!is_constant() && other.
is_constant()) {
274 }
else if (is_constant() && !other.
is_constant()) {
278 result = witness_bool ? *this : other;
308 const int lhs_inverted =
static_cast<int>(witness_inverted);
310 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
312 bb::fr q_m{ -2 * aux_prod };
316 bb::fr q_c{ lhs_inverted + rhs_inverted - 2 * rhs_inverted * lhs_inverted };
321 ctx->create_arithmetic_gate(
323 }
else if (!is_constant() && other.
is_constant()) {
328 }
else if (is_constant() && !other.
is_constant()) {
330 result = witness_bool ? !other : other;
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 };
385 ctx->create_arithmetic_gate(
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);
466template <
typename Builder>
474 return result.normalize();
480 if (witness_same || const_same) {
485 return ((predicate && lhs) || (!predicate && rhs)).
normalize();
494 return (!(*
this) || other);
510 return !((*this) ^ other);
525 if (!witness_inverted) {
532 const bool value = witness_bool ^ witness_inverted;
536 const int inverted =
static_cast<int>(witness_inverted);
537 bb::fr q_l{ 1 - 2 * inverted };
542 context->create_arithmetic_gate({ witness_index,
context->zero_idx(), new_witness, q_m, q_l, q_r, q_o, q_c });
544 witness_index = new_witness;
545 witness_bool =
value;
546 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()