Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bool.cpp
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#include "bool.hpp"
9#include "../circuit_builders/circuit_builders.hpp"
13
14using namespace bb;
15
16namespace bb::stdlib {
17
21template <typename Builder>
23 : witness_bool(value)
24{}
25
29template <typename Builder>
31 : context(parent_context)
32{}
33
41template <typename Builder>
42bool_t<Builder>::bool_t(const witness_t<Builder>& value, const bool& use_range_constraint)
44{
45 BB_ASSERT((value.witness == bb::fr::zero()) || (value.witness == bb::fr::one()),
46 "bool_t: witness value is not 0 or 1");
47 witness_index = value.witness_index;
48
49 if (use_range_constraint) {
50 // Create a range constraint gate
51 context->create_small_range_constraint(witness_index, 1, "bool_t: witness value is not 0 or 1");
52 } else {
53 // Create an arithmetic gate to enforce the relation x^2 = x
54 context->create_bool_gate(witness_index);
55 }
56 witness_bool = (value.witness == bb::fr::one());
57 witness_inverted = false;
59}
63template <typename Builder>
64bool_t<Builder>::bool_t(Builder* parent_context, const bool value)
65 : context(parent_context)
66 , witness_bool(value)
67{}
68
72template <typename Builder>
75 , witness_bool(other.witness_bool)
76 , witness_inverted(other.witness_inverted)
77 , witness_index(other.witness_index)
78 , tag(other.tag)
79{}
84template <typename Builder>
86 : context(other.context)
87 , witness_bool(other.witness_bool)
88 , witness_inverted(other.witness_inverted)
89 , witness_index(other.witness_index)
90 , tag(other.tag)
91{}
97template <typename Builder>
99{
100 BB_ASSERT(witness_index != IS_CONSTANT);
101 bool_t<Builder> result(ctx);
102 result.witness_index = witness_index;
103 const bb::fr value = ctx->get_variable(witness_index);
104 // It does not create a constraint.
105 BB_ASSERT_EQ(value * value - value, 0, "bool_t: creating a witness bool from a non-boolean value");
106 result.witness_bool = (value == 1);
107 result.witness_inverted = false;
108 return result;
109}
110
114template <typename Builder> bool_t<Builder>& bool_t<Builder>::operator=(const bool other)
115{
116 context = nullptr;
117 witness_index = IS_CONSTANT;
118 witness_bool = other;
119 witness_inverted = false;
120 return *this;
122
126template <typename Builder> bool_t<Builder>& bool_t<Builder>::operator=(const bool_t& other) = default;
127
131template <typename Builder> bool_t<Builder>& bool_t<Builder>::operator=(bool_t&& other)
132{
133 context = other.context;
134 witness_index = other.witness_index;
135 witness_bool = other.witness_bool;
136 witness_inverted = other.witness_inverted;
137 tag = other.tag;
138 return *this;
139}
144template <typename Builder> bool_t<Builder>& bool_t<Builder>::operator=(const witness_t<Builder>& other)
145{
146 BB_ASSERT((other.witness == bb::fr::one()) || (other.witness == bb::fr::zero()),
147 "bool_t: witness value is not 0 or 1");
148 context = other.context;
149 witness_bool = other.witness == bb::fr::one();
150 witness_index = other.witness_index;
151 witness_inverted = false;
152 // Constrain x := other.witness by the relation x^2 = x
153 context->create_bool_gate(witness_index);
154 set_free_witness_tag();
155 return *this;
156}
157
161template <typename Builder> bool_t<Builder> bool_t<Builder>::operator&(const bool_t& other) const
162{
163 Builder* ctx = validate_context<Builder>(context, other.context);
164 bool_t<Builder> result(ctx);
165 bool left = witness_inverted ^ witness_bool;
166 bool right = other.witness_inverted ^ other.witness_bool;
167 result.witness_bool = left && right;
168
169 BB_ASSERT(ctx || (is_constant() && other.is_constant()));
170 if (!is_constant() && !other.is_constant()) {
172 result.witness_index = ctx->add_variable(value);
173
202 int i_a(static_cast<int>(witness_inverted));
203 int i_b(static_cast<int>(other.witness_inverted));
204
205 fr q_m{ 1 - 2 * i_b - 2 * i_a + 4 * i_a * i_b };
206 fr q_l{ i_b * (1 - 2 * i_a) };
207 fr q_r{ i_a * (1 - 2 * i_b) };
208 fr q_o{ -1 };
209 fr q_c{ i_a * i_b };
210
211 ctx->create_arithmetic_gate(
212 { witness_index, other.witness_index, result.witness_index, q_m, q_l, q_r, q_o, q_c });
213 } else if (!is_constant() && other.is_constant()) {
215 // If rhs is a constant true, the output is determined by the lhs. Otherwise the output is a constant
216 // `false`.
217 result = other.witness_bool ? *this : other;
218
219 } else if (is_constant() && !other.is_constant()) {
220 BB_ASSERT(!witness_inverted);
221 // If lhs is a constant true, the output is determined by the rhs. Otherwise the output is a constant
222 // `false`.
223 result = witness_bool ? other : *this;
224 }
225
226 result.tag = OriginTag(tag, other.tag);
227 return result;
228}
229
233template <typename Builder> bool_t<Builder> bool_t<Builder>::operator|(const bool_t& other) const
234{
235 Builder* ctx = validate_context<Builder>(context, other.context);
236
237 bool_t<Builder> result(ctx);
238
239 BB_ASSERT(ctx || (is_constant() && other.is_constant()));
240
241 result.witness_bool = (witness_bool ^ witness_inverted) | (other.witness_bool ^ other.witness_inverted);
243 if (!is_constant() && !other.is_constant()) {
244 result.witness_index = ctx->add_variable(value);
245 // Let
246 // a := lhs = *this;
247 // b := rhs = other;
248 // The result is given by
249 // a + b - a * b = [-(1 - 2*i_a) * (1 - 2*i_b)] * w_a w_b +
250 // [(1 - 2 * i_a) * (1 - i_b)] * w_a
251 // [(1 - 2 * i_b) * (1 - i_a)] * w_b
252 // [i_a + i_b - i_a * i_b] * 1
253 const int rhs_inverted = static_cast<int>(other.witness_inverted);
254 const int lhs_inverted = static_cast<int>(witness_inverted);
255
256 bb::fr q_m{ -(1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted) };
257 bb::fr q_l{ (1 - 2 * lhs_inverted) * (1 - rhs_inverted) };
258 bb::fr q_r{ (1 - lhs_inverted) * (1 - 2 * rhs_inverted) };
259 bb::fr q_o{ bb::fr::neg_one() };
260 bb::fr q_c{ rhs_inverted + lhs_inverted - rhs_inverted * lhs_inverted };
261
262 // Let r := a | b;
263 // Constrain
264 // q_m * w_a * w_b + q_l * w_a + q_r * w_b + q_o * r + q_c = 0
265 ctx->create_arithmetic_gate(
266 { witness_index, other.witness_index, result.witness_index, q_m, q_l, q_r, q_o, q_c });
267 } else if (!is_constant() && other.is_constant()) {
268 BB_ASSERT_EQ(other.witness_inverted, false);
269
270 // If we are computing a | b and b is a constant `true`, the result is a constant `true` that does not
271 // depend on `a`.
272 result = other.witness_bool ? other : *this;
273
274 } else if (is_constant() && !other.is_constant()) {
275 // If we are computing a | b and `a` is a constant `true`, the result is a constant `true` that does not
276 // depend on `b`.
277 BB_ASSERT_EQ(witness_inverted, false);
278 result = witness_bool ? *this : other;
279 }
280 result.tag = OriginTag(tag, other.tag);
281 return result;
282}
283
287template <typename Builder> bool_t<Builder> bool_t<Builder>::operator^(const bool_t& other) const
288{
289 Builder* ctx = validate_context<Builder>(context, other.context);
290 bool_t<Builder> result(ctx);
291
292 BB_ASSERT(ctx || (is_constant() && other.is_constant()));
293
294 result.witness_bool = (witness_bool ^ witness_inverted) ^ (other.witness_bool ^ other.witness_inverted);
296
297 if (!is_constant() && !other.is_constant()) {
298 result.witness_index = ctx->add_variable(value);
299 // Let
300 // a := lhs = *this;
301 // b := rhs = other;
302 // The result is given by
303 // a + b - 2 * a * b = [-2 *(1 - 2*i_a) * (1 - 2*i_b)] * w_a w_b +
304 // [(1 - 2 * i_a) * (1 - 2 * i_b)] * w_a
305 // [(1 - 2 * i_b) * (1 - 2 * i_a)] * w_b
306 // [i_a + i_b - 2 * i_a * i_b] * 1]
307 const int rhs_inverted = static_cast<int>(other.witness_inverted);
308 const int lhs_inverted = static_cast<int>(witness_inverted);
309 // Compute the value that's being used in several selectors
310 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
311
312 bb::fr q_m{ -2 * aux_prod };
313 bb::fr q_l{ aux_prod };
314 bb::fr q_r{ aux_prod };
315 bb::fr q_o{ bb::fr::neg_one() };
316 bb::fr q_c{ lhs_inverted + rhs_inverted - 2 * rhs_inverted * lhs_inverted };
317
318 // Let r := a ^ b;
319 // Constrain
320 // q_m * w_a * w_b + q_l * w_a + q_r * w_b + q_o * r + q_c = 0
321 ctx->create_arithmetic_gate(
322 { witness_index, other.witness_index, result.witness_index, q_m, q_l, q_r, q_o, q_c });
323 } else if (!is_constant() && other.is_constant()) {
324 // witness ^ 1 = !witness
325 BB_ASSERT_EQ(other.witness_inverted, false);
326 result = other.witness_bool ? !*this : *this;
327
328 } else if (is_constant() && !other.is_constant()) {
329 BB_ASSERT_EQ(witness_inverted, false);
330 result = witness_bool ? !other : other;
331 }
332 result.tag = OriginTag(tag, other.tag);
333 return result;
334}
338template <typename Builder> bool_t<Builder> bool_t<Builder>::operator!() const
339{
340 bool_t<Builder> result(*this);
341 if (result.is_constant()) {
342 BB_ASSERT(!witness_inverted);
343 // Negate the value of a constant bool_t element.
344 result.witness_bool = !result.witness_bool;
345 } else {
346 // Negate the `inverted` flag of a witnees bool_t element.
347 result.witness_inverted = !result.witness_inverted;
348 }
349 return result;
350}
351
355template <typename Builder> bool_t<Builder> bool_t<Builder>::operator==(const bool_t& other) const
356{
357 Builder* ctx = validate_context<Builder>(context, other.context);
358 bool_t<Builder> result(ctx);
359 BB_ASSERT(ctx || (is_constant() && other.is_constant()));
360
361 result.witness_bool = (witness_bool ^ witness_inverted) == (other.witness_bool ^ other.witness_inverted);
362 if (!is_constant() && !other.is_constant()) {
363 const bb::fr value = result.witness_bool ? bb::fr::one() : bb::fr::zero();
364
365 result.witness_index = context->add_variable(value);
366
367 // Let
368 // a := lhs = *this;
369 // b := rhs = other;
370 // The result is given by
371 // 1 - a - b + 2 a * b = [2 *(1 - 2*i_a) * (1 - 2*i_b)] * w_a w_b +
372 // [-(1 - 2 * i_a) * (1 - 2 * i_b)] * w_a +
373 // [-(1 - 2 * i_b) * (1 - 2 * i_a)] * w_b +
374 // [1 - i_a - i_b + 2 * i_a * i_b] * 1
375 const int rhs_inverted = static_cast<int>(other.witness_inverted);
376 const int lhs_inverted = static_cast<int>(witness_inverted);
377 // Compute the value that's being used in several selectors
378 const int aux_prod = (1 - 2 * rhs_inverted) * (1 - 2 * lhs_inverted);
379 bb::fr q_m{ 2 * aux_prod };
380 bb::fr q_l{ -aux_prod };
381 bb::fr q_r{ -aux_prod };
382 bb::fr q_o{ bb::fr::neg_one() };
383 bb::fr q_c{ 1 - lhs_inverted - rhs_inverted + 2 * rhs_inverted * lhs_inverted };
384
385 ctx->create_arithmetic_gate(
386 { witness_index, other.witness_index, result.witness_index, q_m, q_l, q_r, q_o, q_c });
387
388 } else if (!is_constant() && (other.is_constant())) {
389 // Compare *this with a constant other. If other == true, then we're checking *this == true. In this case we
390 // propagate *this without adding extra constraints, otherwise (if other = false), we propagate !*this.
391 BB_ASSERT_EQ(other.witness_inverted, false);
392 result = other.witness_bool ? *this : !(*this);
393 } else if (is_constant() && !other.is_constant()) {
394 // Completely analogous to the previous case.
395 BB_ASSERT_EQ(witness_inverted, false);
396 result = witness_bool ? other : !other;
397 }
398
399 result.tag = OriginTag(tag, other.tag);
400 return result;
401}
405template <typename Builder> bool_t<Builder> bool_t<Builder>::operator!=(const bool_t<Builder>& other) const
406{
407 return operator^(other);
408}
409
410template <typename Builder> bool_t<Builder> bool_t<Builder>::operator&&(const bool_t<Builder>& other) const
411{
412 return operator&(other);
413}
414
415template <typename Builder> bool_t<Builder> bool_t<Builder>::operator||(const bool_t<Builder>& other) const
416{
417 return operator|(other);
418}
419
423template <typename Builder> void bool_t<Builder>::assert_equal(const bool_t& rhs, std::string const& msg) const
424{
425 const bool_t lhs = *this;
426 Builder* ctx = validate_context<Builder>(rhs.get_context(), lhs.get_context());
427
428 if (lhs.is_constant() && rhs.is_constant()) {
429 BB_ASSERT_EQ(lhs.get_value(), rhs.get_value(), "bool_t::assert_equal: constants are not equal");
430 } else if (lhs.is_constant()) {
432 // if rhs is inverted, flip the value of the lhs constant
433 const bool lhs_value = rhs.witness_inverted ? !lhs.witness_bool : lhs.witness_bool;
434 ctx->assert_equal_constant(rhs.witness_index, lhs_value, msg);
435 } else if (rhs.is_constant()) {
437 // if lhs is inverted, flip the value of the rhs constant
438 const bool rhs_value = lhs.witness_inverted ? !rhs.witness_bool : rhs.witness_bool;
439 ctx->assert_equal_constant(lhs.witness_index, rhs_value, msg);
440 } else {
441 // Both are witnesses - save original tags and clear them to allow different transcript/free witness sources
442 // (e.g., proving 2 separate properties about same object through 2 different transcripts)
443 const auto lhs_original_tag = lhs.get_origin_tag();
444 const auto rhs_original_tag = rhs.get_origin_tag();
447
448 bool_t left = lhs;
449 bool_t right = rhs;
450 // we need to normalize iff lhs or rhs has an inverted witness (but not both)
451 if (lhs.witness_inverted ^ rhs.witness_inverted) {
452 left = left.normalize();
453 right = right.normalize();
454 }
455 ctx->assert_equal(left.witness_index, right.witness_index, msg);
456
457 // Restore tags
458 lhs.set_origin_tag(lhs_original_tag);
459 rhs.set_origin_tag(rhs_original_tag);
460 }
461}
462
466template <typename Builder>
468 const bool_t& lhs,
469 const bool_t& rhs)
470{
471 if (predicate.is_constant()) {
472 auto result = bool_t(predicate.get_value() ? lhs : rhs);
473 result.set_origin_tag(OriginTag(predicate.get_origin_tag(), lhs.get_origin_tag(), rhs.get_origin_tag()));
474 return result.normalize();
475 }
476
477 bool same = lhs.witness_index == rhs.witness_index;
478 bool witness_same = same && !lhs.is_constant() && (lhs.witness_inverted == rhs.witness_inverted);
479 bool const_same = same && lhs.is_constant() && (lhs.witness_bool == rhs.witness_bool);
480 if (witness_same || const_same) {
481 return lhs.normalize();
482 }
483 // Boolean operations can preserve inverted flags when constants are involved
484 // (e.g., inverted_witness && constant_true returns inverted_witness)
485 return ((predicate && lhs) || (!predicate && rhs)).normalize();
486}
487
491template <typename Builder> bool_t<Builder> bool_t<Builder>::implies(const bool_t<Builder>& other) const
492{
493 // Thanks to negation operator being free, this computation requires at most 1 gate.
494 return (!(*this) || other); // P => Q is equiv. to !P || Q (not(P) or Q).
495}
496
500template <typename Builder> void bool_t<Builder>::must_imply(const bool_t& other, std::string const& msg) const
501{
502 implies(other).assert_equal(true, msg);
503}
504
508template <typename Builder> bool_t<Builder> bool_t<Builder>::implies_both_ways(const bool_t<Builder>& other) const
509{
510 return !((*this) ^ other); // P <=> Q is equiv. to !(P ^ Q) (not(P xor Q)).
511}
512
518template <typename Builder> bool_t<Builder> bool_t<Builder>::normalize() const
519{
520 if (is_constant()) {
521 BB_ASSERT(!witness_inverted);
522 return *this;
523 }
524
525 if (!witness_inverted) {
526 return *this;
527 }
528 // Let a := *this, need to constrain a = a_norm
529 // [1 - 2 i_a] w_a + [-1] w_a_norm + [i_a] = 0
530 // ^ ^ ^
531 // q_l q_o q_c
532 const bool value = witness_bool ^ witness_inverted;
533
534 uint32_t new_witness = context->add_variable(bb::fr{ static_cast<int>(value) });
535
536 const int inverted = static_cast<int>(witness_inverted);
537 bb::fr q_l{ 1 - 2 * inverted };
538 bb::fr q_c{ inverted };
539 bb::fr q_o = bb::fr::neg_one();
540 bb::fr q_m = bb::fr::zero();
541 bb::fr q_r = bb::fr::zero();
542 context->create_arithmetic_gate({ witness_index, context->zero_idx(), new_witness, q_m, q_l, q_r, q_o, q_c });
543
544 witness_index = new_witness;
545 witness_bool = value;
546 witness_inverted = false;
547 return *this;
548}
549
551template class bool_t<bb::MegaCircuitBuilder>;
552
553} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
Implements boolean logic in-circuit.
Definition bool.hpp:60
bool get_value() const
Definition bool.hpp:125
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_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
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
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(const bool value=false)
Construct a constant bool_t object from a bool value.
Definition bool.cpp:22
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 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
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) const
Implements equality operator in circuit.
Definition bool.cpp:355
StrictMock< MockContext > context
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
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()