Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::numeric Namespace Reference

Classes

class  DebugEngine
 
class  RandomEngine
 
class  RNG
 
class  sparse_int
 Integer type that stores each bit as a separate digit in the given base. Supports addition with single-pass carry propagation. Used to build plookup tables for bitwise operations (XOR, AND) where two sparse_ints are added and the resulting per-digit values encode the operation's truth table. More...
 
class  uint256_t
 
class  uintx
 

Typedefs

using uint512_t = uintx< uint256_t >
 
using uint1024_t = uintx< uint512_t >
 

Functions

template<typename T >
constexpr size_t count_leading_zeros (T const &u)
 
template<>
constexpr size_t count_leading_zeros< uint32_t > (uint32_t const &u)
 
template<>
constexpr size_t count_leading_zeros< uint64_t > (uint64_t const &u)
 
template<>
constexpr size_t count_leading_zeros< uint128_t > (uint128_t const &u)
 
template<>
constexpr size_t count_leading_zeros< uint256_t > (uint256_t const &u)
 
constexpr uint32_t get_msb32 (const uint32_t in)
 
constexpr uint64_t get_msb64 (const uint64_t in)
 
template<typename T >
constexpr T get_msb (const T in)
 
constexpr uint32_t get_lsb32 (const uint32_t in)
 
constexpr uint64_t get_lsb64 (const uint64_t in)
 
template<typename T >
constexpr T get_lsb (const T in)
 
template<typename T >
constexpr T round_up_power_2 (const T in)
 
template<typename T >
keep_n_lsb (T const &input, size_t num_bits)
 
constexpr uint64_t pow64 (const uint64_t input, const uint64_t exponent)
 
constexpr bool is_power_of_two (uint64_t x)
 
constexpr uint64_t rotate64 (const uint64_t value, const uint64_t rotation)
 
constexpr uint32_t rotate32 (const uint32_t value, const uint32_t rotation)
 
std::vector< uint64_t > slice_input (const uint256_t &input, const uint64_t base, const size_t num_slices)
 Decompose a uint256_t into digits in the given base (least-significant digit first). If num_slices > 0, returns exactly that many digits. If num_slices == 0, returns as many as needed.
 
std::vector< uint64_t > slice_input_using_variable_bases (const uint256_t &input, const std::vector< uint64_t > &bases)
 Decompose a uint256_t using a different base for each digit position (least-significant first). Throws if the input is too large to be fully represented by the given bases.
 
template<uint64_t base, uint64_t num_slices>
constexpr std::array< uint256_t, num_slices > get_base_powers ()
 Compute [1, base, base^2, ..., base^(num_slices-1)] as uint256_t values.
 
template<uint64_t base>
constexpr uint256_t map_into_sparse_form (const uint64_t input)
 Encode a 32-bit value into sparse form: each binary bit of input becomes a digit in the given base. E.g. with base=3, binary 0b101 becomes 1*3^2 + 0*3^1 + 1*3^0 = 10. Used by plookup tables (SHA256, Keccak, AES, Blake2s) to encode XOR/AND via lookup-friendly representations.
 
template<uint64_t base>
constexpr uint64_t map_from_sparse_form (const uint256_t &input)
 Decode a sparse-form uint256_t back to a 32-bit value. Extracts the base-adic digits from most-significant to least-significant, and recovers the original binary value by reading the low bit of each digit.
 
template<typename T >
constexpr T ceil_div (const T &numerator, const T &denominator)
 Computes the ceiling of the division of two integral types.
 
RNGget_debug_randomness (bool reset, std::uint_fast64_t seed)
 
RNGget_randomness ()
 
std::ostream & operator<< (std::ostream &os, uint256_t const &a)
 
template<typename B >
void read (B &it, uint256_t &value)
 
template<typename B >
void write (B &it, uint256_t const &value)
 
template<typename B , typename Params >
void read (B &it, uintx< Params > &value)
 
template<typename B , typename Params >
void write (B &it, uintx< Params > const &value)
 
template<class base_uint >
std::ostream & operator<< (std::ostream &os, uintx< base_uint > const &a)
 
constexpr uint512_t TEST_MODULUS (uint256_t{ "0x04689e957a1242c84a50189c6d96cadca602072d09eac1013b5458a2275d69b1" }, uint256_t{ "0x0925c4b8763cbf9c599a6f7c0348d21cb00b85511637560626edfa5c34c6b38d" })
 

Detailed Description

uint256_t Copyright Aztec 2020

An unsigned 256 bit integer type.

Constructor and all methods are constexpr. Ideally, uint256_t should be able to be treated like any other literal type.

Not optimized for performance, this code doesn't touch any of our hot paths when constructing proofs.

uintx: a wide unsigned integer formed by pairing two base_uint halves (lo, hi).

Instantiated as: uintx<uint256_t> = uint512_t (two 256-bit halves) uintx<uint512_t> = uint1024_t (two 512-bit halves)

Constructor and all methods are constexpr. Not optimized for performance; does not touch hot paths when constructing proofs.

Typedef Documentation

◆ uint1024_t

Definition at line 308 of file uintx.hpp.

◆ uint512_t

Definition at line 306 of file uintx.hpp.

Function Documentation

◆ ceil_div()

template<typename T >
constexpr T bb::numeric::ceil_div ( const T &  numerator,
const T &  denominator 
)
constexpr

Computes the ceiling of the division of two integral types.

Calculates the ceiling of the division of numerator by denominator. Ensures that the denominator is greater than zero and that the type is an integral type.

Template Parameters
TThe integral type of numerator and denominator.
Parameters
numeratorThe value to be divided.
denominatorThe value to divide by.
Returns
The ceiling of the division result.

Definition at line 23 of file general.hpp.

◆ count_leading_zeros()

template<typename T >
constexpr size_t bb::numeric::count_leading_zeros ( T const &  u)
inlineconstexpr

Returns the number of leading 0 bits for a given integer type. Implemented in terms of intrinsics which will use instructions such as bsr or lzcnt for best performance. Undefined behavior when input is 0.

◆ count_leading_zeros< uint128_t >()

template<>
constexpr size_t bb::numeric::count_leading_zeros< uint128_t > ( uint128_t const &  u)
inlineconstexpr

Definition at line 31 of file count_leading_zeros.hpp.

◆ count_leading_zeros< uint256_t >()

template<>
constexpr size_t bb::numeric::count_leading_zeros< uint256_t > ( uint256_t const &  u)
inlineconstexpr

Definition at line 44 of file count_leading_zeros.hpp.

◆ count_leading_zeros< uint32_t >()

template<>
constexpr size_t bb::numeric::count_leading_zeros< uint32_t > ( uint32_t const &  u)
inlineconstexpr

Definition at line 21 of file count_leading_zeros.hpp.

◆ count_leading_zeros< uint64_t >()

template<>
constexpr size_t bb::numeric::count_leading_zeros< uint64_t > ( uint64_t const &  u)
inlineconstexpr

Definition at line 26 of file count_leading_zeros.hpp.

◆ get_base_powers()

template<uint64_t base, uint64_t num_slices>
constexpr std::array< uint256_t, num_slices > bb::numeric::get_base_powers ( )
constexpr

Compute [1, base, base^2, ..., base^(num_slices-1)] as uint256_t values.

Definition at line 65 of file sparse_form.hpp.

◆ get_debug_randomness()

RNG & bb::numeric::get_debug_randomness ( bool  reset,
std::uint_fast64_t  seed 
)

Used by tests to ensure consistent behavior.

Definition at line 217 of file engine.cpp.

◆ get_lsb()

template<typename T >
constexpr T bb::numeric::get_lsb ( const T  in)
inlineconstexpr

Definition at line 70 of file get_msb.hpp.

◆ get_lsb32()

constexpr uint32_t bb::numeric::get_lsb32 ( const uint32_t  in)
inlineconstexpr

Definition at line 54 of file get_msb.hpp.

◆ get_lsb64()

constexpr uint64_t bb::numeric::get_lsb64 ( const uint64_t  in)
inlineconstexpr

Definition at line 62 of file get_msb.hpp.

◆ get_msb()

template<typename T >
constexpr T bb::numeric::get_msb ( const T  in)
inlineconstexpr

Definition at line 49 of file get_msb.hpp.

◆ get_msb32()

constexpr uint32_t bb::numeric::get_msb32 ( const uint32_t  in)
inlineconstexpr

Definition at line 16 of file get_msb.hpp.

◆ get_msb64()

constexpr uint64_t bb::numeric::get_msb64 ( const uint64_t  in)
inlineconstexpr

Definition at line 32 of file get_msb.hpp.

◆ get_randomness()

RNG & bb::numeric::get_randomness ( )

Default engine. If wanting consistent proof construction, uncomment the line to return the debug engine.

Definition at line 230 of file engine.cpp.

◆ is_power_of_two()

constexpr bool bb::numeric::is_power_of_two ( uint64_t  x)
constexpr

Definition at line 35 of file pow.hpp.

◆ keep_n_lsb()

template<typename T >
T bb::numeric::keep_n_lsb ( T const &  input,
size_t  num_bits 
)
inline

Definition at line 12 of file keep_n_lsb.hpp.

◆ map_from_sparse_form()

template<uint64_t base>
constexpr uint64_t bb::numeric::map_from_sparse_form ( const uint256_t input)
constexpr

Decode a sparse-form uint256_t back to a 32-bit value. Extracts the base-adic digits from most-significant to least-significant, and recovers the original binary value by reading the low bit of each digit.

Definition at line 100 of file sparse_form.hpp.

◆ map_into_sparse_form()

template<uint64_t base>
constexpr uint256_t bb::numeric::map_into_sparse_form ( const uint64_t  input)
constexpr

Encode a 32-bit value into sparse form: each binary bit of input becomes a digit in the given base. E.g. with base=3, binary 0b101 becomes 1*3^2 + 0*3^1 + 1*3^0 = 10. Used by plookup tables (SHA256, Keccak, AES, Blake2s) to encode XOR/AND via lookup-friendly representations.

Definition at line 80 of file sparse_form.hpp.

◆ operator<<() [1/2]

std::ostream & bb::numeric::operator<< ( std::ostream &  os,
uint256_t const &  a 
)
inline

Definition at line 258 of file uint256.hpp.

◆ operator<<() [2/2]

template<class base_uint >
std::ostream & bb::numeric::operator<< ( std::ostream &  os,
uintx< base_uint > const &  a 
)
inline

Definition at line 299 of file uintx.hpp.

◆ pow64()

constexpr uint64_t bb::numeric::pow64 ( const uint64_t  input,
const uint64_t  exponent 
)
constexpr

Definition at line 13 of file pow.hpp.

◆ read() [1/2]

template<typename B >
void bb::numeric::read ( B &  it,
uint256_t value 
)
inline

Definition at line 267 of file uint256.hpp.

◆ read() [2/2]

template<typename B , typename Params >
void bb::numeric::read ( B &  it,
uintx< Params > &  value 
)
inline

Definition at line 282 of file uintx.hpp.

◆ rotate32()

constexpr uint32_t bb::numeric::rotate32 ( const uint32_t  value,
const uint32_t  rotation 
)
inlineconstexpr

Definition at line 18 of file rotate.hpp.

◆ rotate64()

constexpr uint64_t bb::numeric::rotate64 ( const uint64_t  value,
const uint64_t  rotation 
)
inlineconstexpr

Definition at line 13 of file rotate.hpp.

◆ round_up_power_2()

template<typename T >
constexpr T bb::numeric::round_up_power_2 ( const T  in)
inlineconstexpr

Definition at line 75 of file get_msb.hpp.

◆ slice_input()

std::vector< uint64_t > bb::numeric::slice_input ( const uint256_t input,
const uint64_t  base,
const size_t  num_slices 
)
inline

Decompose a uint256_t into digits in the given base (least-significant digit first). If num_slices > 0, returns exactly that many digits. If num_slices == 0, returns as many as needed.

Definition at line 23 of file sparse_form.hpp.

◆ slice_input_using_variable_bases()

std::vector< uint64_t > bb::numeric::slice_input_using_variable_bases ( const uint256_t input,
const std::vector< uint64_t > &  bases 
)
inline

Decompose a uint256_t using a different base for each digit position (least-significant first). Throws if the input is too large to be fully represented by the given bases.

Definition at line 46 of file sparse_form.hpp.

◆ TEST_MODULUS()

constexpr uint512_t bb::numeric::TEST_MODULUS ( uint256_t{ "0x04689e957a1242c84a50189c6d96cadca602072d09eac1013b5458a2275d69b1" }  ,
uint256_t{ "0x0925c4b8763cbf9c599a6f7c0348d21cb00b85511637560626edfa5c34c6b38d" }   
)
constexpr

◆ write() [1/2]

template<typename B >
void bb::numeric::write ( B &  it,
uint256_t const &  value 
)
inline

Definition at line 281 of file uint256.hpp.

◆ write() [2/2]

template<typename B , typename Params >
void bb::numeric::write ( B &  it,
uintx< Params > const &  value 
)
inline

Definition at line 292 of file uintx.hpp.