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
107 static bool_t conditional_assign(const bool_t<Builder>& predicate, const bool_t& lhs, const bool_t& rhs);
108
109 void must_imply(const bool_t& other, std::string const& msg = "bool_t::must_imply") const;
110
111 bool get_value() const { return witness_bool ^ witness_inverted; }
112
113 bool is_constant() const { return witness_index == IS_CONSTANT; }
114 bool is_inverted() const
115 {
116 if (is_constant()) {
118 }
119 return witness_inverted;
120 }
121
122 bool_t normalize() const;
123
125
126 Builder* get_context() const { return context; }
127
128 void set_origin_tag(const OriginTag& new_tag) const { tag = new_tag; }
129 OriginTag get_origin_tag() const { return tag; }
133 {
136 context->fix_witness(witness_index, get_value());
138 }
139
140 private:
141 mutable Builder* context = nullptr;
142 mutable bool witness_bool = false;
143 mutable bool witness_inverted = false;
144 mutable uint32_t witness_index = IS_CONSTANT;
145 mutable OriginTag tag{};
146
147 template <typename, typename> friend class bigfield;
148 template <typename> friend class field_t;
149};
150
151template <typename T> inline std::ostream& operator<<(std::ostream& os, bool_t<T> const& v)
152{
153 return os << v.get_value();
154}
155
156} // namespace bb::stdlib
#define ASSERT(expression,...)
Definition assert.hpp:77
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:111
void fix_witness()
Definition bool.hpp:132
bool is_constant() const
Definition bool.hpp:113
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:128
bool_t implies(const bool_t &other) const
Implements implication operator in circuit.
Definition bool.cpp:478
bool is_inverted() const
Definition bool.hpp:114
bool_t normalize() const
A bool_t element is normalized if witness_inverted == false. For a given *this, output its normalized...
Definition bool.cpp:505
bool_t operator&(const bool_t &other) const
Implements AND in circuit.
Definition bool.cpp:166
void set_free_witness_tag()
Definition bool.hpp:130
uint32_t get_normalized_witness_index() const
Definition bool.hpp:124
bool_t operator!() const
Implements negation in circuit.
Definition bool.cpp:339
bool_t(bool_t &&other)
static bool_t conditional_assign(const bool_t< Builder > &predicate, const bool_t &lhs, const bool_t &rhs)
Implements the ternary operator - if predicate == true then return lhs, else return rhs.
Definition bool.cpp:456
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:87
void unset_free_witness_tag()
Definition bool.hpp:131
Builder * get_context() const
Definition bool.hpp:126
Builder * context
Definition bool.hpp:141
uint32_t witness_index
Definition bool.hpp:144
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:487
bool_t & operator=(const bool other)
Assigns a native bool to bool_t object.
Definition bool.cpp:112
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:423
bool witness_inverted
Definition bool.hpp:143
bool_t operator|(const bool_t &other) const
Implements OR in circuit.
Definition bool.cpp:237
OriginTag tag
Definition bool.hpp:145
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:96
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:495
OriginTag get_origin_tag() const
Definition bool.hpp:129
bool_t operator^(const bool_t &other) const
Implements XOR in circuit.
Definition bool.cpp:289
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:356
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()