Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bool.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8#include "../circuit_builders/circuit_builders_fwd.hpp"
9#include "../witness/witness.hpp"
11
12namespace bb::stdlib {
59template <typename Builder> class bool_t {
60 public:
61 bool_t(const bool value = false);
62 bool_t(Builder* parent_context);
63 bool_t(Builder* parent_context, const bool value);
64 bool_t(const witness_t<Builder>& value, const bool& use_range_constraint = false);
65 bool_t(const bool_t& other);
66 bool_t(bool_t&& other);
67
68 bool_t& operator=(const bool other);
69 bool_t& operator=(const witness_t<Builder>& other);
70 bool_t& operator=(const bool_t& other);
71 bool_t& operator=(bool_t&& other);
72
74
75 // bitwise operations
76 bool_t operator&(const bool_t& other) const;
77 bool_t operator|(const bool_t& other) const;
78 bool_t operator^(const bool_t& other) const;
79 bool_t operator!() const;
80
81 // equality checks
82 bool_t operator==(const bool_t& other) const;
83
84 bool_t operator!=(const bool_t& other) const;
85
86 // misc bool ops
87 bool_t operator~() const { return operator!(); }
88
89 bool_t operator&&(const bool_t& other) const;
90
91 bool_t operator||(const bool_t& other) const;
92
93 bool_t implies(const bool_t& other) const;
94
95 bool_t implies_both_ways(const bool_t& other) const;
96
97 // self ops
98 void operator|=(const bool_t& other) { *this = operator|(other); }
99
100 void operator&=(const bool_t& other) { *this = operator&(other); }
101
102 void operator^=(const bool_t& other) { *this = operator^(other); }
103
104 // assertions
105 void assert_equal(const bool_t& rhs, std::string const& msg = "bool_t::assert_equal") const;
106
120 static bool_t conditional_assign(const bool_t<Builder>& predicate, const bool_t& lhs, const bool_t& rhs);
121
122 void must_imply(const bool_t& other, std::string const& msg = "bool_t::must_imply") const;
123
124 bool get_value() const { return witness_bool ^ witness_inverted; }
125
126 bool is_constant() const { return witness_index == IS_CONSTANT; }
127 bool is_inverted() const
128 {
129 if (is_constant()) {
131 }
132 return witness_inverted;
133 }
134
135 bool_t normalize() const;
136
149 uint32_t get_witness_index() const { return normalize().witness_index; }
150
151 Builder* get_context() const { return context; }
152
153 void set_origin_tag(const OriginTag& new_tag) const { tag = new_tag; }
154 OriginTag get_origin_tag() const { return tag; }
159 {
162 context->fix_witness(witness_index, get_value());
164 }
165
166 private:
167 mutable Builder* context = nullptr;
168 mutable bool witness_bool = false;
169 mutable bool witness_inverted = false;
177 mutable uint32_t witness_index = IS_CONSTANT;
178 mutable OriginTag tag{};
179
180 template <typename, typename> friend class bigfield;
181 template <typename> friend class field_t;
182};
183
184template <typename T> inline std::ostream& operator<<(std::ostream& os, bool_t<T> const& v)
185{
186 return os << v.get_value();
187}
188
189} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:67
Implements boolean logic in-circuit.
Definition bool.hpp:59
void operator&=(const bool_t &other)
Definition bool.hpp:100
bool get_value() const
Definition bool.hpp:124
void fix_witness()
Definition bool.hpp:158
bool is_constant() const
Definition bool.hpp:126
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:153
bool_t implies(const bool_t &other) const
Implements implication operator in circuit.
Definition bool.cpp:489
bool is_inverted() const
Definition bool.hpp:127
bool_t normalize() const
A bool_t element is normalized if witness_inverted == false. For a given *this, output its normalized...
Definition bool.cpp:516
bool_t operator&(const bool_t &other) const
Implements AND in circuit.
Definition bool.cpp:159
void set_free_witness_tag()
Definition bool.hpp:155
bool_t operator!() const
Implements negation in circuit.
Definition bool.cpp:336
bool_t(bool_t &&other)
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.
Definition bool.cpp:465
bool_t operator!=(const bool_t &other) const
Implements the not equal operator in circuit.
Definition bool.cpp:403
bool_t operator~() const
Definition bool.hpp:87
void unset_free_witness_tag()
Definition bool.hpp:156
Builder * get_context() const
Definition bool.hpp:151
Builder * context
Definition bool.hpp:167
uint32_t witness_index
Index of the witness in the builder's witness vector.
Definition bool.hpp:177
bool_t operator&&(const bool_t &other) const
Definition bool.cpp:408
bool_t operator||(const bool_t &other) const
Definition bool.cpp:413
void must_imply(const bool_t &other, std::string const &msg="bool_t::must_imply") const
Constrains the (a => b) == true.
Definition bool.cpp:498
bool_t & operator=(const bool other)
Assigns a native bool to bool_t object.
Definition bool.cpp:113
void clear_round_provenance() const
Definition bool.hpp:157
bool_t(const bool_t &other)
void operator^=(const bool_t &other)
Definition bool.hpp:102
void assert_equal(const bool_t &rhs, std::string const &msg="bool_t::assert_equal") const
Implements copy constraint for bool_t elements.
Definition bool.cpp:421
bool witness_inverted
Definition bool.hpp:169
bool_t operator|(const bool_t &other) const
Implements OR in circuit.
Definition bool.cpp:231
uint32_t get_witness_index() const
Get the witness index of the current boolean element.
Definition bool.hpp:149
OriginTag tag
Definition bool.hpp:178
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.
Definition bool.cpp:97
bool_t implies_both_ways(const bool_t &other) const
Implements a "double-implication" (<=>), a.k.a "iff", a.k.a. "biconditional".
Definition bool.cpp:506
OriginTag get_origin_tag() const
Definition bool.hpp:154
bool_t operator^(const bool_t &other) const
Implements XOR in circuit.
Definition bool.cpp:285
bool_t & operator=(const bool_t &other)
Assigns a bool_t to a bool_t object.
void operator|=(const bool_t &other)
Definition bool.hpp:98
bool_t operator==(const bool_t &other) const
Implements equality operator in circuit.
Definition bool.cpp:353
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:245
This file contains part of the logic for the Origin Tag mechanism that tracks the use of in-circuit p...
void unset_free_witness()
void set_free_witness()
void clear_round_provenance()
Clear the round_provenance to address round provenance false positives.