Barretenberg
The ZK-SNARK library at the core of Aztec
|
#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_t & | operator= (const safe_uint_t &other) |
safe_uint_t & | operator= (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 |
Builder * | get_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< Builder > | create_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 ¤t_max, size_t safety) | |
Definition at line 22 of file safe_uint.hpp.
|
private |
Definition at line 25 of file safe_uint.hpp.
|
private |
Definition at line 24 of file safe_uint.hpp.
|
inlineprivate |
Definition at line 28 of file safe_uint.hpp.
|
inline |
Definition at line 44 of file safe_uint.hpp.
|
inline |
Definition at line 49 of file safe_uint.hpp.
|
inline |
Definition at line 59 of file safe_uint.hpp.
|
inline |
Definition at line 66 of file safe_uint.hpp.
|
inline |
Definition at line 70 of file safe_uint.hpp.
|
inline |
Definition at line 75 of file safe_uint.hpp.
|
inline |
Definition at line 92 of file safe_uint.hpp.
|
inline |
Definition at line 121 of file safe_uint.hpp.
|
inline |
Definition at line 193 of file safe_uint.hpp.
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.
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.
|
inlinestatic |
Definition at line 201 of file safe_uint.hpp.
|
inlinestatic |
Definition at line 80 of file safe_uint.hpp.
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
Builder |
other | |
quotient_bit_size | |
remainder_bit_size | |
description | |
get_quotient |
Definition at line 112 of file safe_uint.cpp.
|
static |
|
inline |
Definition at line 185 of file safe_uint.hpp.
|
inline |
Definition at line 211 of file safe_uint.hpp.
bb::fr bb::stdlib::safe_uint_t< Builder >::get_value | ( | ) | const |
Definition at line 200 of file safe_uint.cpp.
|
inline |
Definition at line 208 of file safe_uint.hpp.
|
inline |
Definition at line 199 of file safe_uint.hpp.
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.
|
inline |
Definition at line 129 of file safe_uint.hpp.
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.
|
inlineexplicit |
Definition at line 97 of file safe_uint.hpp.
|
inline |
Definition at line 167 of file safe_uint.hpp.
|
inline |
Definition at line 168 of file safe_uint.hpp.
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.
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.
|
inline |
Definition at line 159 of file safe_uint.hpp.
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.
|
inline |
Definition at line 153 of file safe_uint.hpp.
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.
Builder |
other |
Definition at line 74 of file safe_uint.cpp.
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.
Builder |
other |
Definition at line 152 of file safe_uint.cpp.
|
inline |
Definition at line 139 of file safe_uint.hpp.
|
inline |
Definition at line 146 of file safe_uint.hpp.
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.
|
inline |
Definition at line 212 of file safe_uint.hpp.
|
inline |
Definition at line 209 of file safe_uint.hpp.
|
inline |
Definition at line 166 of file safe_uint.hpp.
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.
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
.
Builder |
other | |
difference_bit_size | |
description |
Definition at line 41 of file safe_uint.cpp.
|
inline |
Definition at line 213 of file safe_uint.hpp.
uint256_t bb::stdlib::safe_uint_t< Builder >::current_max |
Definition at line 42 of file safe_uint.hpp.
|
staticconstexpr |
Definition at line 40 of file safe_uint.hpp.
|
staticconstexpr |
Definition at line 38 of file safe_uint.hpp.
|
staticconstexpr |
Definition at line 39 of file safe_uint.hpp.
field_ct bb::stdlib::safe_uint_t< Builder >::value |
Definition at line 41 of file safe_uint.hpp.