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: Complete, auditors: [Sergei], commit: 72f52e7bad0fc1e36da575fbc2e6bfa1b1104aec}
3// external_1: { status: Complete, auditors: [@ed25519, @JakubHeba (Spearbit)], commit:
4// b463d7c1c52fec2f4e39acfd21219464b00a39d8}
5// external_2: { status: not started, auditors: [], commit: }
6// =====================
7
8#pragma once
9#include "../circuit_builders/circuit_builders_fwd.hpp"
10#include "../witness/witness.hpp"
12
13namespace bb::stdlib {
60template <typename Builder> class bool_t {
61 public:
62 bool_t(const bool value = false);
63 bool_t(Builder* parent_context);
64 bool_t(Builder* parent_context, const bool value);
65 bool_t(const witness_t<Builder>& value, const bool& use_range_constraint = false);
66 bool_t(const bool_t& other);
67 bool_t(bool_t&& other);
68
69 bool_t& operator=(const bool other);
70 bool_t& operator=(const witness_t<Builder>& other);
71 bool_t& operator=(const bool_t& other);
72 bool_t& operator=(bool_t&& other);
73
75
76 // bitwise operations
77 bool_t operator&(const bool_t& other) const;
78 bool_t operator|(const bool_t& other) const;
79 bool_t operator^(const bool_t& other) const;
80 bool_t operator!() const;
81
82 // equality checks
83 bool_t operator==(const bool_t& other) const;
84
85 bool_t operator!=(const bool_t& other) const;
86
87 // misc bool ops
88 bool_t operator~() const { return operator!(); }
89
90 bool_t operator&&(const bool_t& other) const;
91
92 bool_t operator||(const bool_t& other) const;
93
94 bool_t implies(const bool_t& other) const;
95
96 bool_t implies_both_ways(const bool_t& other) const;
97
98 // self ops
99 void operator|=(const bool_t& other) { *this = operator|(other); }
100
101 void operator&=(const bool_t& other) { *this = operator&(other); }
102
103 void operator^=(const bool_t& other) { *this = operator^(other); }
104
105 // assertions
106 void assert_equal(const bool_t& rhs, std::string const& msg = "bool_t::assert_equal") const;
107
121 static bool_t conditional_assign(const bool_t<Builder>& predicate, const bool_t& lhs, const bool_t& rhs);
122
123 void must_imply(const bool_t& other, std::string const& msg = "bool_t::must_imply") const;
124
125 bool get_value() const { return witness_bool ^ witness_inverted; }
126
127 bool is_constant() const { return witness_index == IS_CONSTANT; }
128 bool is_inverted() const
129 {
130 if (is_constant()) {
132 }
133 return witness_inverted;
134 }
135
136 bool_t normalize() const;
137
150 uint32_t get_witness_index() const { return normalize().witness_index; }
151
152 Builder* get_context() const { return context; }
153
154 void set_origin_tag(const OriginTag& new_tag) const { tag = new_tag; }
155 OriginTag get_origin_tag() const { return tag; }
160 {
163 context->fix_witness(witness_index, get_value());
165 }
166
167 private:
168 mutable Builder* context = nullptr;
169 mutable bool witness_bool = false;
170 mutable bool witness_inverted = false;
178 mutable uint32_t witness_index = IS_CONSTANT;
179 mutable OriginTag tag{};
180
181 template <typename, typename> friend class bigfield;
182 template <typename> friend class field_t;
183};
184
185template <typename T> inline std::ostream& operator<<(std::ostream& os, bool_t<T> const& v)
186{
187 return os << v.get_value();
188}
189
190} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
Implements boolean logic in-circuit.
Definition bool.hpp:60
void operator&=(const bool_t &other)
Definition bool.hpp:101
bool get_value() const
Definition bool.hpp:125
void fix_witness()
Definition bool.hpp:159
bool is_constant() const
Definition bool.hpp:127
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:154
bool_t implies(const bool_t &other) const
Implements implication operator in circuit.
Definition bool.cpp:491
bool is_inverted() const
Definition bool.hpp:128
bool_t normalize() const
A bool_t element is normalized if witness_inverted == false. For a given *this, output its normalized...
Definition bool.cpp:518
bool_t operator&(const bool_t &other) const
Implements AND in circuit.
Definition bool.cpp:161
void set_free_witness_tag()
Definition bool.hpp:156
bool_t operator!() const
Implements negation in circuit.
Definition bool.cpp:338
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:467
bool_t operator!=(const bool_t &other) const
Implements the not equal operator in circuit.
Definition bool.cpp:405
bool_t operator~() const
Definition bool.hpp:88
void unset_free_witness_tag()
Definition bool.hpp:157
Builder * get_context() const
Definition bool.hpp:152
Builder * context
Definition bool.hpp:168
uint32_t witness_index
Index of the witness in the builder's witness vector.
Definition bool.hpp:178
bool_t operator&&(const bool_t &other) const
Definition bool.cpp:410
bool_t operator||(const bool_t &other) const
Definition bool.cpp:415
void must_imply(const bool_t &other, std::string const &msg="bool_t::must_imply") const
Constrains the (a => b) == true.
Definition bool.cpp:500
bool_t & operator=(const bool other)
Assigns a native bool to bool_t object.
Definition bool.cpp:114
void clear_round_provenance() const
Definition bool.hpp:158
bool_t(const bool_t &other)
void operator^=(const bool_t &other)
Definition bool.hpp:103
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:423
bool witness_inverted
Definition bool.hpp:170
bool_t operator|(const bool_t &other) const
Implements OR in circuit.
Definition bool.cpp:233
uint32_t get_witness_index() const
Get the witness index of the current boolean element.
Definition bool.hpp:150
OriginTag tag
Definition bool.hpp:179
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:98
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:508
OriginTag get_origin_tag() const
Definition bool.hpp:155
bool_t operator^(const bool_t &other) const
Implements XOR in circuit.
Definition bool.cpp:287
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:99
bool_t operator==(const bool_t &other) const
Implements equality operator in circuit.
Definition bool.cpp:355
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:254
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.