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

#include <safe_uint.hpp>

Public Member Functions

 safe_uint_t ()
 
 safe_uint_t (field_ct const &value, size_t bit_num, std::string const &description="unknown")
 
 safe_uint_t (const bb::fr &const_value)
 
 safe_uint_t (const uint256_t &const_value)
 
 safe_uint_t (const unsigned int &const_value)
 
 safe_uint_t (const safe_uint_t &other)
 
 safe_uint_t (const bool_ct &other)
 
 operator bool_ct ()
 
safe_uint_t subtract (const safe_uint_t &other, const size_t difference_bit_size, std::string const &description="") const
 Subtraction when you have a pre-determined bound on the difference size.
 
safe_uint_t operator- (const safe_uint_t &other) const
 Subtraction on two safe_uint_t objects.
 
safe_uint_t divide (const safe_uint_t &other, const size_t quotient_bit_size, const size_t remainder_bit_size, std::string const &description="", const std::function< std::pair< uint256_t, uint256_t >(uint256_t, uint256_t)> &get_quotient=[](uint256_t val, uint256_t divisor) { return std::make_pair((uint256_t)(val/(uint256_t) divisor),(uint256_t)(val %(uint256_t) divisor));}) const
 division when you have a pre-determined bound on the sizes of the quotient and remainder
 
safe_uint_t operator/ (const safe_uint_t &other) const
 Potentially less efficient than divide function - bounds remainder and quotient by max of this.
 
safe_uint_t add_two (const safe_uint_t &add_a, const safe_uint_t &add_b) const
 
safe_uint_t madd (const safe_uint_t &to_mul, const safe_uint_t &to_add) const
 
safe_uint_toperator= (const safe_uint_t &other)
 
safe_uint_toperator= (safe_uint_t &&other)
 
safe_uint_t operator+= (const safe_uint_t &other)
 
safe_uint_t operator*= (const safe_uint_t &other)
 
std::array< safe_uint_t< Builder >, 3 > slice (const uint8_t msb, const uint8_t lsb) const
 
void set_public () const
 
 operator field_ct ()
 
 operator field_ct () const
 
safe_uint_t operator+ (const safe_uint_t &other) const
 
safe_uint_t operator* (const safe_uint_t &other) const
 
bool_ct operator== (const safe_uint_t &other) const
 
bool_ct operator!= (const safe_uint_t &other) const
 
safe_uint_t normalize () const
 
bb::fr get_value () const
 
Builderget_context () const
 
bool_ct is_zero () const
 
void assert_equal (const safe_uint_t &rhs, std::string const &msg="safe_uint_t::assert_equal") const
 
void assert_is_not_zero (std::string const &msg="safe_uint_t::assert_is_not_zero") const
 
void assert_is_zero (std::string const &msg="safe_uint_t::assert_is_zero") const
 
bool is_constant () const
 
uint32_t get_witness_index () const
 
void set_origin_tag (OriginTag tag) const
 
OriginTag get_origin_tag () const
 
void set_free_witness_tag ()
 
void unset_free_witness_tag ()
 

Static Public Member Functions

static safe_uint_t< Buildercreate_constant_witness (Builder *parent_context, bb::fr const &value)
 
static safe_uint_t from_witness_index (Builder *parent_context, const uint32_t witness_index)
 
static safe_uint_t conditional_assign (const bool_ct &predicate, const safe_uint_t &lhs, const safe_uint_t &rhs)
 

Public Attributes

field_ct value
 
uint256_t current_max
 

Static Public Attributes

static constexpr size_t MAX_BIT_NUM = bb::fr::modulus.get_msb()
 
static constexpr uint256_t MAX_VALUE = bb::fr::modulus - 1
 
static constexpr size_t IS_UNSAFE = 143
 

Private Types

using field_ct = field_t< Builder >
 
using bool_ct = bool_t< Builder >
 

Private Member Functions

 safe_uint_t (field_ct const &value, const uint256_t &current_max, size_t safety)
 

Detailed Description

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

Definition at line 22 of file safe_uint.hpp.

Member Typedef Documentation

◆ bool_ct

template<typename Builder >
using bb::stdlib::safe_uint_t< Builder >::bool_ct = bool_t<Builder>
private

Definition at line 25 of file safe_uint.hpp.

◆ field_ct

template<typename Builder >
using bb::stdlib::safe_uint_t< Builder >::field_ct = field_t<Builder>
private

Definition at line 24 of file safe_uint.hpp.

Constructor & Destructor Documentation

◆ safe_uint_t() [1/8]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::safe_uint_t ( field_ct const &  value,
const uint256_t current_max,
size_t  safety 
)
inlineprivate

Definition at line 28 of file safe_uint.hpp.

◆ safe_uint_t() [2/8]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::safe_uint_t ( )
inline

Definition at line 44 of file safe_uint.hpp.

◆ safe_uint_t() [3/8]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::safe_uint_t ( field_ct const &  value,
size_t  bit_num,
std::string const &  description = "unknown" 
)
inline

Definition at line 49 of file safe_uint.hpp.

◆ safe_uint_t() [4/8]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::safe_uint_t ( const bb::fr const_value)
inline

Definition at line 59 of file safe_uint.hpp.

◆ safe_uint_t() [5/8]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::safe_uint_t ( const uint256_t const_value)
inline

Definition at line 66 of file safe_uint.hpp.

◆ safe_uint_t() [6/8]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::safe_uint_t ( const unsigned int &  const_value)
inline

Definition at line 70 of file safe_uint.hpp.

◆ safe_uint_t() [7/8]

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

Definition at line 75 of file safe_uint.hpp.

◆ safe_uint_t() [8/8]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::safe_uint_t ( const bool_ct other)
inline

Definition at line 92 of file safe_uint.hpp.

Member Function Documentation

◆ add_two()

template<typename Builder >
safe_uint_t bb::stdlib::safe_uint_t< Builder >::add_two ( const safe_uint_t< Builder > &  add_a,
const safe_uint_t< Builder > &  add_b 
) const
inline

Definition at line 121 of file safe_uint.hpp.

◆ assert_equal()

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

Definition at line 193 of file safe_uint.hpp.

◆ assert_is_not_zero()

template<typename Builder >
void bb::stdlib::safe_uint_t< Builder >::assert_is_not_zero ( std::string const &  msg = "safe_uint_t< Builder >::assert_is_not_zero") const

Definition at line 190 of file safe_uint.cpp.

◆ assert_is_zero()

template<typename Builder >
void bb::stdlib::safe_uint_t< Builder >::assert_is_zero ( std::string const &  msg = "safe_uint_t< Builder >::assert_is_zero") const

Definition at line 185 of file safe_uint.cpp.

◆ conditional_assign()

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

Definition at line 201 of file safe_uint.hpp.

◆ create_constant_witness()

template<typename Builder >
static safe_uint_t< Builder > bb::stdlib::safe_uint_t< Builder >::create_constant_witness ( Builder parent_context,
bb::fr const &  value 
)
inlinestatic

Definition at line 80 of file safe_uint.hpp.

◆ divide()

template<typename Builder >
safe_uint_t< Builder > bb::stdlib::safe_uint_t< Builder >::divide ( const safe_uint_t< Builder > &  other,
const size_t  quotient_bit_size,
const size_t  remainder_bit_size,
std::string const &  description = "",
const std::function< std::pair< uint256_t, uint256_t >(uint256_t, uint256_t)> &  get_quotient = [](uint256_t val, uint256_t divisor) { return std::make_pair((uint256_t)(val / (uint256_t)divisor), (uint256_t)(val % (uint256_t)divisor)); } 
) const

division when you have a pre-determined bound on the sizes of the quotient and remainder

Template Parameters
Builder
Parameters
other
quotient_bit_size
remainder_bit_size
description
get_quotient
Returns
safe_uint_t<Builder>

Definition at line 112 of file safe_uint.cpp.

◆ from_witness_index()

template<typename Builder >
static safe_uint_t bb::stdlib::safe_uint_t< Builder >::from_witness_index ( Builder parent_context,
const uint32_t  witness_index 
)
static

◆ get_context()

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

Definition at line 185 of file safe_uint.hpp.

◆ get_origin_tag()

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

Definition at line 211 of file safe_uint.hpp.

◆ get_value()

template<typename Builder >
bb::fr bb::stdlib::safe_uint_t< Builder >::get_value ( ) const

Definition at line 200 of file safe_uint.cpp.

◆ get_witness_index()

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

Definition at line 208 of file safe_uint.hpp.

◆ is_constant()

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

Definition at line 199 of file safe_uint.hpp.

◆ is_zero()

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

is_zero will return a bool_ct, and add constraints that enforce its correctness N.B. If you want to ENFORCE that a safe_uint_t object is zero, use assert_is_zero

Definition at line 195 of file safe_uint.cpp.

◆ madd()

template<typename Builder >
safe_uint_t bb::stdlib::safe_uint_t< Builder >::madd ( const safe_uint_t< Builder > &  to_mul,
const safe_uint_t< Builder > &  to_add 
) const
inline

Definition at line 129 of file safe_uint.hpp.

◆ normalize()

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

normalize returns a safe_uint_t element where multiplicative_constant = 1 and additive_constant = 0 i.e. the value is defined entirely by the builder variable that witness_index points to If the witness_index is ever needed, normalize should be called first

Will cost 1 constraint if the field element is not already normalized (or is constant)

Definition at line 179 of file safe_uint.cpp.

◆ operator bool_ct()

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::operator bool_ct ( )
inlineexplicit

Definition at line 97 of file safe_uint.hpp.

◆ operator field_ct() [1/2]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::operator field_ct ( )
inline

Definition at line 167 of file safe_uint.hpp.

◆ operator field_ct() [2/2]

template<typename Builder >
bb::stdlib::safe_uint_t< Builder >::operator field_ct ( ) const
inline

Definition at line 168 of file safe_uint.hpp.

◆ operator!=()

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

Definition at line 210 of file safe_uint.cpp.

◆ operator*()

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

Definition at line 22 of file safe_uint.cpp.

◆ operator*=()

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

Definition at line 159 of file safe_uint.hpp.

◆ operator+()

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

Definition at line 17 of file safe_uint.cpp.

◆ operator+=()

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

Definition at line 153 of file safe_uint.hpp.

◆ operator-()

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

Subtraction on two safe_uint_t objects.

The function first checks the case when both operands are constants and there is underflow. Then, it computes the difference and create a safe_uint_t from its value with the same max value as current_max. Constructing the difference safe_uint_t will create a range constraint, which catches underflow as long as the difference value does not end up in the range [0, current_max]. The only case where it is possible that the difference value can end up in this range, is when current_max + other.current_max exceeds MAX_VALUE (the modulus - 1), so we throw an error in this case.

Template Parameters
Builder
Parameters
other
Returns
safe_uint_t<Builder>

Definition at line 74 of file safe_uint.cpp.

◆ operator/()

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

Potentially less efficient than divide function - bounds remainder and quotient by max of this.

Template Parameters
Builder
Parameters
other
Returns
safe_uint_t<Builder>

Definition at line 152 of file safe_uint.cpp.

◆ operator=() [1/2]

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

Definition at line 139 of file safe_uint.hpp.

◆ operator=() [2/2]

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

Definition at line 146 of file safe_uint.hpp.

◆ operator==()

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

Definition at line 205 of file safe_uint.cpp.

◆ set_free_witness_tag()

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

Definition at line 212 of file safe_uint.hpp.

◆ set_origin_tag()

template<typename Builder >
void bb::stdlib::safe_uint_t< Builder >::set_origin_tag ( OriginTag  tag) const
inline

Definition at line 209 of file safe_uint.hpp.

◆ set_public()

template<typename Builder >
void bb::stdlib::safe_uint_t< Builder >::set_public ( ) const
inline

Definition at line 166 of file safe_uint.hpp.

◆ slice()

template<typename Builder >
std::array< safe_uint_t< Builder >, 3 > bb::stdlib::safe_uint_t< Builder >::slice ( const uint8_t  msb,
const uint8_t  lsb 
) const

Definition at line 215 of file safe_uint.cpp.

◆ subtract()

template<typename Builder >
safe_uint_t< Builder > bb::stdlib::safe_uint_t< Builder >::subtract ( const safe_uint_t< Builder > &  other,
const size_t  difference_bit_size,
std::string const &  description = "" 
) const

Subtraction when you have a pre-determined bound on the difference size.

Same as operator- except with this pre-determined bound difference_bit_size.

Template Parameters
Builder
Parameters
other
difference_bit_size
description
Returns
safe_uint_t<Builder>

Definition at line 41 of file safe_uint.cpp.

◆ unset_free_witness_tag()

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

Definition at line 213 of file safe_uint.hpp.

Member Data Documentation

◆ current_max

template<typename Builder >
uint256_t bb::stdlib::safe_uint_t< Builder >::current_max

Definition at line 42 of file safe_uint.hpp.

◆ IS_UNSAFE

template<typename Builder >
constexpr size_t bb::stdlib::safe_uint_t< Builder >::IS_UNSAFE = 143
staticconstexpr

Definition at line 40 of file safe_uint.hpp.

◆ MAX_BIT_NUM

template<typename Builder >
constexpr size_t bb::stdlib::safe_uint_t< Builder >::MAX_BIT_NUM = bb::fr::modulus.get_msb()
staticconstexpr

Definition at line 38 of file safe_uint.hpp.

◆ MAX_VALUE

template<typename Builder >
constexpr uint256_t bb::stdlib::safe_uint_t< Builder >::MAX_VALUE = bb::fr::modulus - 1
staticconstexpr

Definition at line 39 of file safe_uint.hpp.

◆ value

template<typename Builder >
field_ct bb::stdlib::safe_uint_t< Builder >::value

Definition at line 41 of file safe_uint.hpp.


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