Barretenberg
The ZK-SNARK library at the core of Aztec
|
General class for prime fields see Prime field documentation["field documentation"] for general implementation reference. More...
#include <field_declarations.hpp>
Classes | |
struct | wide_array |
struct | wnaf_table |
Public Types | |
using | View = field |
using | CoefficientAccumulator = field |
using | Params = Params_ |
using | in_buf = const uint8_t * |
using | vec_in_buf = const uint8_t * |
using | out_buf = uint8_t * |
using | vec_out_buf = uint8_t ** |
Public Member Functions | |
field ()=default | |
constexpr | field (const numeric::uint256_t &input) noexcept |
constexpr | field (const uint128_t &input) noexcept |
constexpr | field (const unsigned long input) noexcept |
constexpr | field (const unsigned int input) noexcept |
constexpr | field (const unsigned long long input) noexcept |
constexpr | field (const int input) noexcept |
constexpr | field (const uint64_t a, const uint64_t b, const uint64_t c, const uint64_t d) noexcept |
constexpr | field (const uint512_t &input) noexcept |
Convert a 512-bit big integer into a field element. | |
constexpr | field (std::string input) noexcept |
constexpr | operator bool () const |
constexpr | operator uint8_t () const |
constexpr | operator uint16_t () const |
constexpr | operator uint32_t () const |
constexpr | operator uint64_t () const |
constexpr | operator uint128_t () const |
constexpr | operator uint256_t () const noexcept |
constexpr uint256_t | uint256_t_no_montgomery_conversion () const noexcept |
constexpr | field (const field &other) noexcept=default |
constexpr | field (field &&other) noexcept=default |
constexpr field & | operator= (const field &other) &noexcept=default |
constexpr field & | operator= (field &&other) &noexcept=default |
constexpr | ~field () noexcept=default |
BB_INLINE constexpr field | operator* (const field &other) const noexcept |
BB_INLINE constexpr field | operator+ (const field &other) const noexcept |
BB_INLINE constexpr field | operator- (const field &other) const noexcept |
BB_INLINE constexpr field | operator- () const noexcept |
constexpr field | operator/ (const field &other) const noexcept |
BB_INLINE constexpr field | operator++ () noexcept |
BB_INLINE constexpr field | operator++ (int) noexcept |
BB_INLINE constexpr field & | operator*= (const field &other) &noexcept |
BB_INLINE constexpr field & | operator+= (const field &other) &noexcept |
BB_INLINE constexpr field & | operator-= (const field &other) &noexcept |
constexpr field & | operator/= (const field &other) &noexcept |
BB_INLINE constexpr bool | operator> (const field &other) const noexcept |
Greater-than operator. | |
BB_INLINE constexpr bool | operator< (const field &other) const noexcept |
Less-than operator. | |
BB_INLINE constexpr bool | operator== (const field &other) const noexcept |
BB_INLINE constexpr bool | operator!= (const field &other) const noexcept |
BB_INLINE constexpr field | to_montgomery_form () const noexcept |
BB_INLINE constexpr field | from_montgomery_form () const noexcept |
BB_INLINE constexpr field | sqr () const noexcept |
BB_INLINE constexpr void | self_sqr () &noexcept |
BB_INLINE constexpr field | pow (const uint256_t &exponent) const noexcept |
BB_INLINE constexpr field | pow (uint64_t exponent) const noexcept |
constexpr field | invert () const noexcept |
constexpr std::pair< bool, field > | sqrt () const noexcept |
Compute square root of the field element. | |
constexpr std::pair< bool, field > | sqrt () const noexcept |
BB_INLINE constexpr void | self_neg () &noexcept |
BB_INLINE constexpr void | self_to_montgomery_form () &noexcept |
BB_INLINE constexpr void | self_from_montgomery_form () &noexcept |
BB_INLINE constexpr void | self_conditional_negate (uint64_t predicate) &noexcept |
BB_INLINE constexpr field | reduce_once () const noexcept |
BB_INLINE constexpr void | self_reduce_once () &noexcept |
BB_INLINE constexpr void | self_set_msb () &noexcept |
BB_INLINE constexpr bool | is_msb_set () const noexcept |
BB_INLINE constexpr uint64_t | is_msb_set_word () const noexcept |
BB_INLINE constexpr bool | is_zero () const noexcept |
BB_INLINE std::vector< uint8_t > | to_buffer () const |
BB_INLINE constexpr wide_array | mul_512 (const field &other) const noexcept |
BB_INLINE constexpr wide_array | sqr_512 () const noexcept |
BB_INLINE constexpr field | conditionally_subtract_from_double_modulus (const uint64_t predicate) const noexcept |
void | msgpack_pack (auto &packer) const |
void | msgpack_unpack (auto o) |
void | msgpack_schema (auto &packer) const |
BB_INLINE constexpr field | reduce () const noexcept |
BB_INLINE constexpr field | add (const field &other) const noexcept |
BB_INLINE constexpr field | subtract (const field &other) const noexcept |
BB_INLINE constexpr field | subtract_coarse (const field &other) const noexcept |
BB_INLINE constexpr field | montgomery_mul (const field &other) const noexcept |
BB_INLINE constexpr field | montgomery_mul_big (const field &other) const noexcept |
Mongtomery multiplication for moduli > 2²⁵⁴ | |
BB_INLINE constexpr field | montgomery_square () const noexcept |
constexpr field | tonelli_shanks_sqrt () const noexcept |
Implements an optimized variant of Tonelli-Shanks via lookup tables. Algorithm taken from https://cr.yp.to/papers/sqroot-20011123-retypeset20220327.pdf "FASTER SQUARE ROOTS IN ANNOYING FINITE FIELDS" by D. Bernstein Page 5 "Accelerated Discrete Logarithm". | |
Static Public Member Functions | |
static constexpr field | cube_root_of_unity () |
static constexpr field | zero () |
static constexpr field | neg_one () |
static constexpr field | one () |
static constexpr field | external_coset_generator () |
static constexpr field | tag_coset_generator () |
template<size_t idx> | |
static constexpr field | coset_generator () |
static void | batch_invert (std::span< field > coeffs) noexcept |
static void | batch_invert (field *coeffs, size_t n) noexcept |
static constexpr field | get_root_of_unity (size_t subgroup_size) noexcept |
static void | serialize_to_buffer (const field &value, uint8_t *buffer) |
static field | serialize_from_buffer (const uint8_t *buffer) |
template<class V > | |
static field | reconstruct_from_public (const std::span< const field< V >, PUBLIC_INPUTS_SIZE > &limbs) |
static void | split_into_endomorphism_scalars (const field &k, field &k1, field &k2) |
static std::pair< std::array< uint64_t, 2 >, std::array< uint64_t, 2 > > | split_into_endomorphism_scalars (const field &k) |
static void | split_into_endomorphism_scalars_384 (const field &input, field &k1_out, field &k2_out) |
static BB_INLINE void | __copy (const field &a, field &r) noexcept |
static BB_INLINE void | __swap (field &src, field &dest) noexcept |
static field | random_element (numeric::RNG *engine=nullptr) noexcept |
static constexpr field | multiplicative_generator () noexcept |
static BB_INLINE constexpr void | wasm_madd (uint64_t &left_limb, const std::array< uint64_t, WASM_NUM_LIMBS > &right_limbs, uint64_t &result_0, uint64_t &result_1, uint64_t &result_2, uint64_t &result_3, uint64_t &result_4, uint64_t &result_5, uint64_t &result_6, uint64_t &result_7, uint64_t &result_8) |
Multiply left limb by a sequence of 9 limbs and put into result variables. | |
static BB_INLINE constexpr void | wasm_reduce (uint64_t &result_0, uint64_t &result_1, uint64_t &result_2, uint64_t &result_3, uint64_t &result_4, uint64_t &result_5, uint64_t &result_6, uint64_t &result_7, uint64_t &result_8) |
Perform 29-bit montgomery reduction on 1 limb (result_0 should be zero modulo 2**29 after this) | |
static BB_INLINE constexpr void | wasm_reduce_yuval (uint64_t &result_0, uint64_t &result_1, uint64_t &result_2, uint64_t &result_3, uint64_t &result_4, uint64_t &result_5, uint64_t &result_6, uint64_t &result_7, uint64_t &result_8, uint64_t &result_9) |
Perform 29-bit montgomery reduction on 1 limb using Yuval's method *. | |
static BB_INLINE constexpr std::array< uint64_t, WASM_NUM_LIMBS > | wasm_convert (const uint64_t *data) |
Convert 4 64-bit limbs into 9 29-bit limbs. | |
static BB_INLINE constexpr std::pair< uint64_t, uint64_t > | mul_wide (uint64_t a, uint64_t b) noexcept |
static BB_INLINE constexpr uint64_t | mac (uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t &carry_out) noexcept |
static BB_INLINE constexpr void | mac (uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in, uint64_t &out, uint64_t &carry_out) noexcept |
static BB_INLINE constexpr uint64_t | mac_mini (uint64_t a, uint64_t b, uint64_t c, uint64_t &out) noexcept |
static BB_INLINE constexpr void | mac_mini (uint64_t a, uint64_t b, uint64_t c, uint64_t &out, uint64_t &carry_out) noexcept |
static BB_INLINE constexpr uint64_t | mac_discard_lo (uint64_t a, uint64_t b, uint64_t c) noexcept |
static BB_INLINE constexpr uint64_t | addc (uint64_t a, uint64_t b, uint64_t carry_in, uint64_t &carry_out) noexcept |
static BB_INLINE constexpr uint64_t | sbb (uint64_t a, uint64_t b, uint64_t borrow_in, uint64_t &borrow_out) noexcept |
static BB_INLINE constexpr uint64_t | square_accumulate (uint64_t a, uint64_t b, uint64_t c, uint64_t carry_in_lo, uint64_t carry_in_hi, uint64_t &carry_lo, uint64_t &carry_hi) noexcept |
static constexpr size_t | primitive_root_log_size () noexcept |
static constexpr std::array< field, COSET_GENERATOR_SIZE > | compute_coset_generators () noexcept |
Public Attributes | |
uint64_t | data [4] |
Static Public Attributes | |
static constexpr size_t | PUBLIC_INPUTS_SIZE = Params::PUBLIC_INPUTS_SIZE |
static constexpr uint256_t | modulus |
static constexpr uint256_t | r_squared_uint |
static constexpr std::array< uint64_t, 9 > | wasm_modulus |
static constexpr std::array< uint64_t, 9 > | wasm_r_inv |
static constexpr uint256_t | modulus_minus_two |
static constexpr uint256_t | twice_modulus = modulus + modulus |
static constexpr uint256_t | not_modulus = -modulus |
static constexpr uint256_t | twice_not_modulus = -twice_modulus |
static constexpr size_t | COSET_GENERATOR_SIZE = 15 |
Friends | |
std::ostream & | operator<< (std::ostream &os, const field &a) |
General class for prime fields see Prime field documentation["field documentation"] for general implementation reference.
Params_ |
Definition at line 36 of file field_declarations.hpp.
Definition at line 39 of file field_declarations.hpp.
using bb::field< Params_ >::in_buf = const uint8_t* |
Definition at line 41 of file field_declarations.hpp.
using bb::field< Params_ >::out_buf = uint8_t* |
Definition at line 43 of file field_declarations.hpp.
Definition at line 40 of file field_declarations.hpp.
using bb::field< Params_ >::vec_in_buf = const uint8_t* |
Definition at line 42 of file field_declarations.hpp.
using bb::field< Params_ >::vec_out_buf = uint8_t** |
Definition at line 44 of file field_declarations.hpp.
Definition at line 38 of file field_declarations.hpp.
|
default |
|
inlineconstexprnoexcept |
Definition at line 65 of file field_declarations.hpp.
|
inlineconstexprnoexcept |
Definition at line 71 of file field_declarations.hpp.
|
inlineconstexprnoexcept |
Definition at line 76 of file field_declarations.hpp.
|
inlineconstexprnoexcept |
Definition at line 82 of file field_declarations.hpp.
|
inlineconstexprnoexcept |
Definition at line 89 of file field_declarations.hpp.
|
inlineconstexprnoexcept |
Definition at line 95 of file field_declarations.hpp.
|
inlineconstexprnoexcept |
Definition at line 115 of file field_declarations.hpp.
|
inlineexplicitconstexprnoexcept |
Convert a 512-bit big integer into a field element.
Used for deriving field elements from random values. 512-bits prevents biased output as 2^512>>modulus
Definition at line 124 of file field_declarations.hpp.
|
inlineexplicitconstexprnoexcept |
Definition at line 134 of file field_declarations.hpp.
|
constexprdefaultnoexcept |
|
constexprdefaultnoexcept |
|
inlinestaticnoexcept |
Definition at line 558 of file field_declarations.hpp.
|
inlinestaticnoexcept |
Definition at line 559 of file field_declarations.hpp.
|
constexprnoexcept |
Definition at line 210 of file field_impl_generic.hpp.
|
staticconstexprnoexcept |
Definition at line 109 of file field_impl_generic.hpp.
|
staticnoexcept |
Definition at line 386 of file field_impl.hpp.
|
staticnoexcept |
Definition at line 392 of file field_impl.hpp.
|
staticconstexprnoexcept |
Definition at line 689 of file field_impl.hpp.
|
inlineconstexprnoexcept |
Definition at line 389 of file field_declarations.hpp.
|
inlinestaticconstexpr |
Definition at line 286 of file field_declarations.hpp.
|
inlinestaticconstexpr |
Definition at line 218 of file field_declarations.hpp.
|
inlinestaticconstexpr |
Definition at line 244 of file field_declarations.hpp.
|
constexprnoexcept |
Definition at line 301 of file field_impl.hpp.
|
staticconstexprnoexcept |
Definition at line 652 of file field_impl.hpp.
Definition at line 378 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 636 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 641 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 646 of file field_impl.hpp.
|
staticconstexprnoexcept |
Definition at line 30 of file field_impl_generic.hpp.
|
staticconstexprnoexcept |
Definition at line 46 of file field_impl_generic.hpp.
|
staticconstexprnoexcept |
Definition at line 98 of file field_impl_generic.hpp.
|
staticconstexprnoexcept |
Definition at line 66 of file field_impl_generic.hpp.
|
staticconstexprnoexcept |
Definition at line 83 of file field_impl_generic.hpp.
|
constexprnoexcept |
Definition at line 569 of file field_impl_generic.hpp.
|
constexprnoexcept |
Mongtomery multiplication for moduli > 2²⁵⁴
Explanation of Montgomery form can be found in Introduction to Montgomery form and the difference between WASM and generic versions is explained in Architecture details
Definition at line 317 of file field_impl_generic.hpp.
|
constexprnoexcept |
Definition at line 707 of file field_impl_generic.hpp.
Definition at line 737 of file field_impl.hpp.
|
inline |
Definition at line 573 of file field_declarations.hpp.
Definition at line 757 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 911 of file field_impl_generic.hpp.
|
staticconstexprnoexcept |
Definition at line 18 of file field_impl_generic.hpp.
|
staticconstexprnoexcept |
Definition at line 722 of file field_impl.hpp.
|
inlinestaticconstexpr |
Definition at line 241 of file field_declarations.hpp.
Definition at line 242 of file field_declarations.hpp.
|
inlineexplicitconstexpr |
Definition at line 140 of file field_declarations.hpp.
|
inlineexplicitconstexpr |
Definition at line 171 of file field_declarations.hpp.
|
inlineexplicitconstexpr |
Definition at line 153 of file field_declarations.hpp.
|
inlineconstexprnoexcept |
Definition at line 179 of file field_declarations.hpp.
|
inlineexplicitconstexpr |
Definition at line 159 of file field_declarations.hpp.
|
inlineexplicitconstexpr |
Definition at line 165 of file field_declarations.hpp.
|
inlineexplicitconstexpr |
Definition at line 147 of file field_declarations.hpp.
|
constexprnoexcept |
Definition at line 278 of file field_impl.hpp.
|
constexprnoexcept |
Mutiplication
Definition at line 35 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 49 of file field_impl.hpp.
|
constexprnoexcept |
Addition
Definition at line 102 of file field_impl.hpp.
Definition at line 130 of file field_impl.hpp.
Definition at line 136 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 115 of file field_impl.hpp.
Definition at line 161 of file field_impl.hpp.
|
constexprnoexcept |
Subtraction
Definition at line 148 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 189 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 620 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 625 of file field_impl.hpp.
|
constexprnoexcept |
Less-than operator.
comparison operators exist so that field
is comparible with stl methods that require them. (e.g. std::sort) Finite fields do not have an explicit ordering, these should NEVER be used in algebraic algorithms.
T |
other |
Definition at line 265 of file field_impl.hpp.
|
constexprdefaultnoexcept |
|
constexprdefaultnoexcept |
|
constexprnoexcept |
Definition at line 270 of file field_impl.hpp.
|
constexprnoexcept |
Greater-than operator.
comparison operators exist so that field
is comparible with stl methods that require them. (e.g. std::sort) Finite fields do not have an explicit ordering, these should NEVER be used in algebraic algorithms.
T |
other |
Definition at line 241 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 353 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 373 of file field_impl.hpp.
|
staticconstexprnoexcept |
Definition at line 678 of file field_impl.hpp.
|
staticnoexcept |
Definition at line 665 of file field_impl.hpp.
|
static |
Definition at line 185 of file field_impl_generic.hpp.
Definition at line 326 of file field_impl.hpp.
|
staticconstexprnoexcept |
Definition at line 128 of file field_impl_generic.hpp.
|
constexprnoexcept |
Definition at line 216 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 319 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 204 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 339 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 631 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 83 of file field_impl.hpp.
|
constexprnoexcept |
Definition at line 307 of file field_impl.hpp.
|
inlinestatic |
Definition at line 377 of file field_declarations.hpp.
|
inlinestatic |
Definition at line 375 of file field_declarations.hpp.
|
inlinestatic |
Definition at line 451 of file field_declarations.hpp.
|
inlinestatic |
For short Weierstrass curves y^2 = x^3 + b mod r, if there exists a cube root of unity mod r, we can take advantage of an enodmorphism to decompose a 254 bit scalar into 2 128 bit scalars. \beta = cube root of 1, mod q (q = order of fq) \lambda = cube root of 1, mod r (r = order of fr)
For a point P1 = (X, Y), where Y^2 = X^3 + b, we know that the point P2 = (X * \beta, Y) is also a point on the curve We can represent P2 as a scalar multiplication of P1, where P2 = \lambda * P1
For a generic multiplication of P1 by a 254 bit scalar k, we can decompose k into 2 127 bit scalars (k1, k2), such that k = k1 - (k2 * \lambda)
We can now represent (k * P1) as (k1 * P1) - (k2 * P2), where P2 = (X * \beta, Y). As k1, k2 have half the bit length of k, we have reduced the number of loop iterations of our scalar multiplication algorithm in half
To find k1, k2, We use the extended euclidean algorithm to find 4 short scalars [a1, a2], [b1, b2] such that modulus = (a1 * b2) - (b1 * a2) We then compute scalars c1 = round(b2 * k / r), c2 = round(b1 * k / r), where k1 = (c1 * a1) + (c2 * a2), k2 = -((c1 * b1) + (c2 * b2)) We pre-compute scalars g1 = (2^256 * b1) / n, g2 = (2^256 * b2) / n, to avoid having to perform long division on 512-bit scalars
Definition at line 424 of file field_declarations.hpp.
|
inlinestatic |
Definition at line 498 of file field_declarations.hpp.
Squaring
Definition at line 70 of file field_impl.hpp.
|
constexprnoexcept |
|
constexprnoexcept |
Compute square root of the field element.
Definition at line 598 of file field_impl.hpp.
|
constexprnoexcept |
|
staticconstexprnoexcept |
Definition at line 148 of file field_impl_generic.hpp.
|
constexprnoexcept |
Definition at line 259 of file field_impl_generic.hpp.
|
constexprnoexcept |
T |
other |
Definition at line 291 of file field_impl_generic.hpp.
|
inlinestaticconstexpr |
Definition at line 265 of file field_declarations.hpp.
|
inline |
Definition at line 381 of file field_declarations.hpp.
|
constexprnoexcept |
Definition at line 283 of file field_impl.hpp.
|
constexprnoexcept |
Implements an optimized variant of Tonelli-Shanks via lookup tables. Algorithm taken from https://cr.yp.to/papers/sqroot-20011123-retypeset20220327.pdf "FASTER SQUARE ROOTS IN ANNOYING FINITE FIELDS" by D. Bernstein Page 5 "Accelerated Discrete Logarithm".
T |
Definition at line 448 of file field_impl.hpp.
|
inlineconstexprnoexcept |
Definition at line 185 of file field_declarations.hpp.
|
staticconstexpr |
Convert 4 64-bit limbs into 9 29-bit limbs.
Definition at line 556 of file field_impl_generic.hpp.
|
staticconstexpr |
Multiply left limb by a sequence of 9 limbs and put into result variables.
Definition at line 471 of file field_impl_generic.hpp.
|
staticconstexpr |
Perform 29-bit montgomery reduction on 1 limb (result_0 should be zero modulo 2**29 after this)
Definition at line 499 of file field_impl_generic.hpp.
|
staticconstexpr |
Perform 29-bit montgomery reduction on 1 limb using Yuval's method *.
https://hackmd.io/@Ingonyama/Barret-Montgomery
Definition at line 529 of file field_impl_generic.hpp.
|
inlinestaticconstexpr |
Definition at line 240 of file field_declarations.hpp.
|
friend |
Definition at line 548 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 711 of file field_declarations.hpp.
uint64_t bb::field< Params_ >::data[4] |
Definition at line 195 of file field_declarations.hpp.
Definition at line 197 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 343 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 576 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 47 of file field_declarations.hpp.
Definition at line 204 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 575 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 577 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 207 of file field_declarations.hpp.
|
staticconstexpr |
Definition at line 212 of file field_declarations.hpp.