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;
146 "bool_t: witness value is not 0 or 1");
150 witness_inverted =
false;
152 context->create_bool_gate(witness_index);
153 set_free_witness_tag();
164 bool left = witness_inverted ^ witness_bool;
201 int i_a(
static_cast<int>(witness_inverted));
204 fr q_m{ 1 - 2 * i_b - 2 * i_a + 4 * i_a * i_b };
205 fr q_l{ i_b * (1 - 2 * i_a) };
206 fr q_r{ i_a * (1 - 2 * i_b) };
210 ctx->create_arithmetic_gate(
212 }
else if (!is_constant() && other.
is_constant()) {
218 }
else if (is_constant() && !other.
is_constant()) {
222 result = witness_bool ? other : *
this;
253 const int lhs_inverted =
static_cast<int>(witness_inverted);
255 bb::fr q_m{ -(1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted) };
256 bb::fr q_l{ (1 - 2 * lhs_inverted) * (1 - rhs_inverted) };
257 bb::fr q_r{ (1 - lhs_inverted) * (1 - 2 * rhs_inverted) };
259 bb::fr q_c{ rhs_inverted + lhs_inverted - rhs_inverted * lhs_inverted };
264 ctx->create_arithmetic_gate(
266 }
else if (!is_constant() && other.
is_constant()) {
273 }
else if (is_constant() && !other.
is_constant()) {
277 result = witness_bool ? *this : other;
307 const int lhs_inverted =
static_cast<int>(witness_inverted);
309 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
311 bb::fr q_m{ -2 * aux_prod };
315 bb::fr q_c{ lhs_inverted + rhs_inverted - 2 * rhs_inverted * lhs_inverted };
320 ctx->create_arithmetic_gate(
322 }
else if (!is_constant() && other.
is_constant()) {
327 }
else if (is_constant() && !other.
is_constant()) {
329 result = witness_bool ? !other : other;
375 const int lhs_inverted =
static_cast<int>(witness_inverted);
377 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
378 bb::fr q_m{ 2 * aux_prod };
382 bb::fr q_c{ 1 - lhs_inverted - rhs_inverted + 2 * rhs_inverted * lhs_inverted };
384 ctx->create_arithmetic_gate(
387 }
else if (!is_constant() && (other.
is_constant())) {
392 }
else if (is_constant() && !other.
is_constant()) {
395 result = witness_bool ? other : !other;
406 return operator^(other);
411 return operator&(other);
416 return operator|(other);
433 ctx->assert_equal_constant(rhs.
witness_index, lhs_value, msg);
438 ctx->assert_equal_constant(lhs.
witness_index, rhs_value, msg);
465template <
typename Builder>
473 return result.normalize();
479 if (witness_same || const_same) {
484 return ((predicate && lhs) || (!predicate && rhs)).
normalize();
493 return (!(*
this) || other);
509 return !((*this) ^ other);
524 if (!witness_inverted) {
531 const bool value = witness_bool ^ witness_inverted;
535 const int inverted =
static_cast<int>(witness_inverted);
536 bb::fr q_l{ 1 - 2 * inverted };
541 context->create_arithmetic_gate({ witness_index,
context->zero_idx(), new_witness, q_m, q_l, q_r, q_o, q_c });
543 witness_index = new_witness;
544 witness_bool =
value;
545 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()