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);
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
73 // bitwise operations
74 bool_t operator&(const bool_t& other) const;
75 bool_t operator|(const bool_t& other) const;
76 bool_t operator^(const bool_t& other) const;
77 bool_t operator!() const;
78
79 // equality checks
80 bool_t operator==(const bool_t& other) const;
81
82 bool_t operator!=(const bool_t& other) const;
83
84 // misc bool ops
85 bool_t operator~() const { return operator!(); }
86
87 bool_t operator&&(const bool_t& other) const;
88
89 bool_t operator||(const bool_t& other) const;
90
91 bool_t implies(const bool_t& other) const;
92
93 bool_t implies_both_ways(const bool_t& other) const;
94
95 // self ops
96 void operator|=(const bool_t& other) { *this = operator|(other); }
97
98 void operator&=(const bool_t& other) { *this = operator&(other); }
99
100 void operator^=(const bool_t& other) { *this = operator^(other); }
101
102 // assertions
103 void assert_equal(const bool_t& rhs, std::string const& msg = "bool_t::assert_equal") const;
104
105 static bool_t conditional_assign(const bool_t<Builder>& predicate, const bool_t& lhs, const bool_t& rhs);
106
107 void must_imply(const bool_t& other, std::string const& msg = "bool_t::must_imply") const;
108
109 bool get_value() const { return witness_bool ^ witness_inverted; }
110
111 bool is_constant() const { return witness_index == IS_CONSTANT; }
112
113 bool_t normalize() const;
114
116
117 Builder* get_context() const { return context; }
118
119 void set_origin_tag(const OriginTag& new_tag) const { tag = new_tag; }
120 OriginTag get_origin_tag() const { return tag; }
124 {
127 context->fix_witness(witness_index, get_value());
129 }
130 mutable Builder* context = nullptr;
131 mutable bool witness_bool = false;
132 mutable bool witness_inverted = false;
133 mutable uint32_t witness_index = IS_CONSTANT;
134 mutable OriginTag tag{};
135};
136
137template <typename T> inline std::ostream& operator<<(std::ostream& os, bool_t<T> const& v)
138{
139 return os << v.get_value();
140}
141
142} // namespace bb::stdlib
#define ASSERT(expression,...)
Definition assert.hpp:49
Implements boolean logic in-circuit.
Definition bool.hpp:59
void operator&=(const bool_t &other)
Definition bool.hpp:98
bool get_value() const
Definition bool.hpp:109
void fix_witness()
Definition bool.hpp:123
bool is_constant() const
Definition bool.hpp:111
void set_origin_tag(const OriginTag &new_tag) const
Definition bool.hpp:119
bool_t implies(const bool_t &other) const
Implements implication operator in circuit.
Definition bool.cpp:451
bool_t normalize() const
A bool_t element is normalized if witness_inverted == false. For a given *this, output its normalized...
Definition bool.cpp:478
bool_t operator&(const bool_t &other) const
Implements AND in circuit.
Definition bool.cpp:139
void set_free_witness_tag()
Definition bool.hpp:121
uint32_t get_normalized_witness_index() const
Definition bool.hpp:115
bool_t operator!() const
Implements negation in circuit.
Definition bool.cpp:312
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:429
bool_t operator!=(const bool_t &other) const
Implements the not equal operator in circuit.
Definition bool.cpp:378
bool_t operator~() const
Definition bool.hpp:85
void unset_free_witness_tag()
Definition bool.hpp:122
Builder * get_context() const
Definition bool.hpp:117
Builder * context
Definition bool.hpp:130
uint32_t witness_index
Definition bool.hpp:133
bool_t operator&&(const bool_t &other) const
Definition bool.cpp:383
bool_t operator||(const bool_t &other) const
Definition bool.cpp:388
void must_imply(const bool_t &other, std::string const &msg="bool_t::must_imply") const
Constrains the (a => b) == true.
Definition bool.cpp:460
bool_t & operator=(const bool other)
Assigns a native bool to bool_t object.
Definition bool.cpp:85
bool_t(const bool_t &other)
void operator^=(const bool_t &other)
Definition bool.hpp:100
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:396
bool witness_inverted
Definition bool.hpp:132
bool_t operator|(const bool_t &other) const
Implements OR in circuit.
Definition bool.cpp:210
OriginTag tag
Definition bool.hpp:134
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:468
OriginTag get_origin_tag() const
Definition bool.hpp:120
bool_t operator^(const bool_t &other) const
Implements XOR in circuit.
Definition bool.cpp:262
void operator|=(const bool_t &other)
Definition bool.hpp:96
bool_t operator==(const bool_t &other) const
Implements equality operator in circuit.
Definition bool.cpp:329
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:246
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()