Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::stdlib::bool_t< Builder > Class Template Reference

Implements boolean logic in-circuit. More...

#include <bool.hpp>

Public Member Functions

 bool_t (const bool value=false)
 Construct a constant bool_t object from a bool value.
 
 bool_t (Builder *parent_context)
 Construct a constant bool_t object with a given Builder argument, its value is false.
 
 bool_t (Builder *parent_context, const bool value)
 Construct a constant bool_t object from a bool value with a given Builder argument.
 
 bool_t (const witness_t< Builder > &value, const bool &use_range_constraint=false)
 Construct a bool_t object from a witness, note that the value stored at witness_index is constrained to be 0 or 1.
 
 bool_t (const bool_t &other)
 
 bool_t (bool_t &&other)
 
bool_toperator= (const bool other)
 Assigns a native bool to bool_t object.
 
bool_toperator= (const witness_t< Builder > &other)
 Assigns a witness_t to a bool_t. As above, the value stored at witness_index is constrained to be 0 or 1.
 
bool_toperator= (const bool_t &other)
 Assigns a bool_t to a bool_t object.
 
bool_toperator= (bool_t &&other)
 Assigns a bool_t to a bool_t object.
 
bool_t operator& (const bool_t &other) const
 Implements AND in circuit.
 
bool_t operator| (const bool_t &other) const
 Implements OR in circuit.
 
bool_t operator^ (const bool_t &other) const
 Implements XOR in circuit.
 
bool_t operator! () const
 Implements negation in circuit.
 
bool_t operator== (const bool_t &other) const
 Implements equality operator in circuit.
 
bool_t operator!= (const bool_t &other) const
 Implements the not equal operator in circuit.
 
bool_t operator~ () const
 
bool_t operator&& (const bool_t &other) const
 
bool_t operator|| (const bool_t &other) const
 
bool_t implies (const bool_t &other) const
 Implements implication operator in circuit.
 
bool_t implies_both_ways (const bool_t &other) const
 Implements a "double-implication" (<=>), a.k.a "iff", a.k.a. "biconditional".
 
void operator|= (const bool_t &other)
 
void operator&= (const bool_t &other)
 
void operator^= (const bool_t &other)
 
void assert_equal (const bool_t &rhs, std::string const &msg="bool_t::assert_equal") const
 Implements copy constraint for bool_t elements.
 
void must_imply (const bool_t &other, std::string const &msg="bool_t::must_imply") const
 Constrains the (a => b) == true.
 
bool get_value () const
 
bool is_constant () const
 
bool is_inverted () const
 
bool_t normalize () const
 A bool_t element is normalized if witness_inverted == false. For a given *this, output its normalized version.
 
uint32_t get_witness_index () const
 Get the witness index of the current boolean element.
 
Builderget_context () const
 
void set_origin_tag (const OriginTag &new_tag) const
 
OriginTag get_origin_tag () const
 
void set_free_witness_tag ()
 
void unset_free_witness_tag ()
 
void clear_round_provenance () const
 
void fix_witness ()
 

Static Public Member Functions

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.
 
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.
 

Private Attributes

Buildercontext = nullptr
 
bool witness_bool = false
 
bool witness_inverted = false
 
uint32_t witness_index = IS_CONSTANT
 Index of the witness in the builder's witness vector.
 
OriginTag tag {}
 

Friends

template<typename , typename >
class bigfield
 
template<typename >
class field_t
 

Detailed Description

template<typename Builder>
class bb::stdlib::bool_t< Builder >

Implements boolean logic in-circuit.

Representing bools in-circuit

To avoid constraining negation operations, we represent an in-circuit boolean \( a \) by a witness value \( w_a \) and an witness_inverted flag \( i_a \). The value of \( a \) is defined via the equation:

\begin{align} w_a \oplus i_a = w_a + i_a - 2 \cdot i_a \cdot w_a \end{align}

and can be read from the following table

w_a i_a a = w_a + i_a - 2 i_a w_a
0 0 0
0 1 1
1 0 1
1 1 0

When a new bool_t element \( a \) is created, its witness_inverted flag is set to false and its witness_value is constrained to be \( 0 \) or \( 1\). More precisely, if \( a \) is a witness, then we add a boolean constraint ensuring \( w_a^2 = w_a \), if \( a \) is a constant bool_t element, then it's checked by an ASSERT.

To negate \( a \) we simply flip the flag. Other basic operations are deduced from their algebraic representations and the result is constrained to satisfy corresponding algebraic equation.

Detailed example of an operation (logical OR)

For example, to produce \( a || b \) in circuit, we compute

\begin{align} a + b - a \cdot b = ( w_a \cdot (1- 2 i_a) + i_a) + ( w_b \cdot (1 - 2 i_b) + i_b) - ( w_a \cdot (1-2 i_a) + i_a) ( w_b \cdot (1 - 2 i_b) + i_b) \end{align}

Thus we can use a poly gate to constrain the result of a || b as follows:

\begin{align} q_m \cdot w_a \cdot w_b + q_l \cdot w_a + q_r \cdot w_b + q_o \cdot (a || b) + q_c = 0\end{align}

where

\begin{eqnarray*} q_m &=& -(1 - 2*i_a) * (1 - 2*i_b) \\ q_l &=& (1 - 2 * i_a) * (1 - i_b) \\ q_r &=& (1 - 2 * i_b) * (1 - i_a) \\ q_o &=& -1 \\ q_c &=& i_a + i_b - i_a * i_b \end{eqnarray*}

As \( w_a \) and \( w_b \) are constrained to be boolean, \( i_a \), \( i_b\) are boolean flags, we see that \( (a || b)^2 = (a || b)\) (as a field element).

Definition at line 59 of file bool.hpp.

Constructor & Destructor Documentation

◆ bool_t() [1/6]

template<typename Builder >
bb::stdlib::bool_t< Builder >::bool_t ( const bool  value = false)

Construct a constant bool_t object from a bool value.

Definition at line 21 of file bool.cpp.

◆ bool_t() [2/6]

template<typename Builder >
bb::stdlib::bool_t< Builder >::bool_t ( Builder parent_context)

Construct a constant bool_t object with a given Builder argument, its value is false.

Definition at line 29 of file bool.cpp.

◆ bool_t() [3/6]

template<typename Builder >
bb::stdlib::bool_t< Builder >::bool_t ( Builder parent_context,
const bool  value 
)

Construct a constant bool_t object from a bool value with a given Builder argument.

Definition at line 63 of file bool.cpp.

◆ bool_t() [4/6]

template<typename Builder >
bb::stdlib::bool_t< Builder >::bool_t ( const witness_t< Builder > &  value,
const bool &  use_range_constraint = false 
)

Construct a bool_t object from a witness, note that the value stored at witness_index is constrained to be 0 or 1.

Parameters
valueA witness, which is constrained to be boolean inside of this constructor.
use_range_constraintIn case we need to create bool_t in a loop, it is more efficient to apply the range constraint gates instead of creating arithmetic gates.

Definition at line 41 of file bool.cpp.

◆ bool_t() [5/6]

template<typename Builder >
bb::stdlib::bool_t< Builder >::bool_t ( const bool_t< Builder > &  other)

◆ bool_t() [6/6]

template<typename Builder >
bb::stdlib::bool_t< Builder >::bool_t ( bool_t< Builder > &&  other)

Member Function Documentation

◆ assert_equal()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::assert_equal ( const bool_t< Builder > &  rhs,
std::string const &  msg = "bool_t< Builder >::assert_equal" 
) const

Implements copy constraint for bool_t elements.

Definition at line 421 of file bool.cpp.

◆ clear_round_provenance()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::clear_round_provenance ( ) const
inline

Definition at line 157 of file bool.hpp.

◆ conditional_assign()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::conditional_assign ( const bool_t< Builder > &  predicate,
const bool_t< Builder > &  lhs,
const bool_t< Builder > &  rhs 
)
static

Conditionally assign lhs or rhs based on predicate, always returns normalized result.

Implements the ternary operator - if predicate == true then return lhs, else return rhs.

Returns normalized bool_t to prevent concept leakage. All branches normalize their results:

  • Constant predicate: normalize selected value
  • Same witness: normalize lhs
  • Boolean operations: normalize result (can be unnormalized when constants are involved)
Parameters
predicateThe condition
lhsValue to return if predicate is true
rhsValue to return if predicate is false
Returns
bool_t Normalized result

Definition at line 465 of file bool.cpp.

◆ fix_witness()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::fix_witness ( )
inline

Definition at line 158 of file bool.hpp.

◆ from_witness_index_unsafe()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::from_witness_index_unsafe ( Builder ctx,
uint32_t  witness_index 
)
static

Create a bool_t from a witness index that is known to contain a constrained bool value.

Warning
The witness value is not constrained to be boolean. We simply perform an out-of-circuit sanity check.

Definition at line 97 of file bool.cpp.

◆ get_context()

template<typename Builder >
Builder * bb::stdlib::bool_t< Builder >::get_context ( ) const
inline

Definition at line 151 of file bool.hpp.

◆ get_origin_tag()

template<typename Builder >
OriginTag bb::stdlib::bool_t< Builder >::get_origin_tag ( ) const
inline

Definition at line 154 of file bool.hpp.

◆ get_value()

template<typename Builder >
bool bb::stdlib::bool_t< Builder >::get_value ( ) const
inline

Definition at line 124 of file bool.hpp.

◆ get_witness_index()

template<typename Builder >
uint32_t bb::stdlib::bool_t< Builder >::get_witness_index ( ) const
inline

Get the witness index of the current boolean element.

Returns the witness index of a normalized version of this element, where the witness actually contains the boolean value (not inverted). This is the safe default that prevents soundness vulnerabilities.

Within the bool_t class implementation, the raw witness_index member can be accessed directly when needed.

Returns
uint32_t The normalized witness index

Definition at line 149 of file bool.hpp.

◆ implies()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::implies ( const bool_t< Builder > &  other) const

Implements implication operator in circuit.

Definition at line 489 of file bool.cpp.

◆ implies_both_ways()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::implies_both_ways ( const bool_t< Builder > &  other) const

Implements a "double-implication" (<=>), a.k.a "iff", a.k.a. "biconditional".

Definition at line 506 of file bool.cpp.

◆ is_constant()

template<typename Builder >
bool bb::stdlib::bool_t< Builder >::is_constant ( ) const
inline

Definition at line 126 of file bool.hpp.

◆ is_inverted()

template<typename Builder >
bool bb::stdlib::bool_t< Builder >::is_inverted ( ) const
inline

Definition at line 127 of file bool.hpp.

◆ must_imply()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::must_imply ( const bool_t< Builder > &  other,
std::string const &  msg = "bool_t< Builder >::must_imply" 
) const

Constrains the (a => b) == true.

Definition at line 498 of file bool.cpp.

◆ normalize()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::normalize ( ) const

A bool_t element is normalized if witness_inverted == false. For a given *this, output its normalized version.

Warning
The witness index of *this as well as its witness_inverted flag are re-written.

Definition at line 516 of file bool.cpp.

◆ operator!()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator! ( ) const

Implements negation in circuit.

Definition at line 336 of file bool.cpp.

◆ operator!=()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator!= ( const bool_t< Builder > &  other) const

Implements the not equal operator in circuit.

Definition at line 403 of file bool.cpp.

◆ operator&()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator& ( const bool_t< Builder > &  other) const

Implements AND in circuit.

A bool can be represented by a witness value w and an 'inverted' flag i

A bool's value is defined via the equation: w + i - 2.i.w

w i w + i - 2.i.w
0 0 0
0 1 1
1 0 1
1 1 0

For two bools (w_a, i_a), (w_b, i_b), the & operation is expressed as:

(w_a + i_a - 2.i_a.w_a).(w_b + i_b - 2.i_b.w_b)

This can be rearranged to:

 w_a.w_b.(1 - 2.i_b - 2.i_a + 4.i_a.i_b)     -> q_m coefficient
  • w_a.(i_b.(1 - 2.i_a)) -> q_1 coefficient
  • w_b.(i_a.(1 - 2.i_b)) -> q_2 coefficient
  • i_a.i_b -> q_c coefficient

Note that since the value of the product above is always boolean, we don't need to add an explicit range constraint for result.

Definition at line 159 of file bool.cpp.

◆ operator&&()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator&& ( const bool_t< Builder > &  other) const

Definition at line 408 of file bool.cpp.

◆ operator&=()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::operator&= ( const bool_t< Builder > &  other)
inline

Definition at line 100 of file bool.hpp.

◆ operator=() [1/4]

template<typename Builder >
bool_t< Builder > & bb::stdlib::bool_t< Builder >::operator= ( bool_t< Builder > &&  other)

Assigns a bool_t to a bool_t object.

Definition at line 130 of file bool.cpp.

◆ operator=() [2/4]

template<typename Builder >
bool_t< Builder > & bb::stdlib::bool_t< Builder >::operator= ( const bool  other)

Assigns a native bool to bool_t object.

Definition at line 113 of file bool.cpp.

◆ operator=() [3/4]

template<typename Builder >
bool_t< Builder > & bb::stdlib::bool_t< Builder >::operator= ( const bool_t< Builder > &  other)
default

Assigns a bool_t to a bool_t object.

◆ operator=() [4/4]

template<typename Builder >
bool_t< Builder > & bb::stdlib::bool_t< Builder >::operator= ( const witness_t< Builder > &  other)

Assigns a witness_t to a bool_t. As above, the value stored at witness_index is constrained to be 0 or 1.

Definition at line 143 of file bool.cpp.

◆ operator==()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator== ( const bool_t< Builder > &  other) const

Implements equality operator in circuit.

Definition at line 353 of file bool.cpp.

◆ operator^()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator^ ( const bool_t< Builder > &  other) const

Implements XOR in circuit.

Definition at line 285 of file bool.cpp.

◆ operator^=()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::operator^= ( const bool_t< Builder > &  other)
inline

Definition at line 102 of file bool.hpp.

◆ operator|()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator| ( const bool_t< Builder > &  other) const

Implements OR in circuit.

Definition at line 231 of file bool.cpp.

◆ operator|=()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::operator|= ( const bool_t< Builder > &  other)
inline

Definition at line 98 of file bool.hpp.

◆ operator||()

template<typename Builder >
bool_t< Builder > bb::stdlib::bool_t< Builder >::operator|| ( const bool_t< Builder > &  other) const

Definition at line 413 of file bool.cpp.

◆ operator~()

template<typename Builder >
bool_t bb::stdlib::bool_t< Builder >::operator~ ( ) const
inline

Definition at line 87 of file bool.hpp.

◆ set_free_witness_tag()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::set_free_witness_tag ( )
inline

Definition at line 155 of file bool.hpp.

◆ set_origin_tag()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::set_origin_tag ( const OriginTag new_tag) const
inline

Definition at line 153 of file bool.hpp.

◆ unset_free_witness_tag()

template<typename Builder >
void bb::stdlib::bool_t< Builder >::unset_free_witness_tag ( )
inline

Definition at line 156 of file bool.hpp.

Friends And Related Symbol Documentation

◆ bigfield

template<typename Builder >
template<typename , typename >
friend class bigfield
friend

Definition at line 180 of file bool.hpp.

◆ field_t

template<typename Builder >
template<typename >
friend class field_t
friend

Definition at line 181 of file bool.hpp.

Member Data Documentation

◆ context

template<typename Builder >
Builder* bb::stdlib::bool_t< Builder >::context = nullptr
mutableprivate

Definition at line 167 of file bool.hpp.

◆ tag

template<typename Builder >
OriginTag bb::stdlib::bool_t< Builder >::tag {}
mutableprivate

Definition at line 178 of file bool.hpp.

◆ witness_bool

template<typename Builder >
bool bb::stdlib::bool_t< Builder >::witness_bool = false
mutableprivate

Definition at line 168 of file bool.hpp.

◆ witness_index

template<typename Builder >
uint32_t bb::stdlib::bool_t< Builder >::witness_index = IS_CONSTANT
mutableprivate

Index of the witness in the builder's witness vector.

This is the raw witness index which may point to an inverted boolean value. External code should use get_witness_index() which returns a normalized witness containing the actual boolean value (not inverted). Direct access is restricted to internal bool_t methods.

Definition at line 177 of file bool.hpp.

◆ witness_inverted

template<typename Builder >
bool bb::stdlib::bool_t< Builder >::witness_inverted = false
mutableprivate

Definition at line 169 of file bool.hpp.


The documentation for this class was generated from the following files: