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