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

#include <bigfield.hpp>

Classes

struct  Basis
 
struct  Limb
 Represents a single limb of a bigfield element, with its value and maximum value. More...
 

Public Types

using View = bigfield
 
using CoefficientAccumulator = bigfield
 
using TParams = T
 
using native = bb::field< T >
 
using field_ct = field_t< Builder >
 

Public Member Functions

 bigfield (const field_t< Builder > &low_bits, const field_t< Builder > &high_bits, const bool can_overflow=false, const size_t maximum_bitlength=0)
 Constructs a new bigfield object from two field elements representing the low and high bits.
 
 bigfield (Builder *parent_context=nullptr)
 Constructs a zero bigfield object: all limbs are set to zero.
 
 bigfield (Builder *parent_context, const uint256_t &value)
 Constructs a new bigfield object from a uint256_t value.
 
 bigfield (const uint256_t &value)
 
 bigfield (const int value)
 Constructs a new bigfield object from an int value. We first need to to construct a field element from the value to avoid bugs that have to do with the value being negative.
 
 bigfield (const unsigned long value)
 
 bigfield (const unsigned long long value)
 
 bigfield (const native value)
 Construct a new bigfield object from bb::fq. We first convert to uint256_t as field elements are in Montgomery form internally.
 
 bigfield (const byte_array< Builder > &bytes)
 Constructs a bigfield element from a 256-bit byte array.
 
 bigfield (const bigfield &other)
 
 bigfield (bigfield &&other) noexcept
 
 ~bigfield ()=default
 
bigfieldoperator= (const bigfield &other)
 
bigfieldoperator= (bigfield &&other) noexcept
 
byte_array< Builderto_byte_array () const
 Convert the bigfield element to a byte array. Concatenates byte arrays of the high (2L bits) and low (2L bits) parts of the bigfield element.
 
uint512_t get_value () const
 
uint512_t get_maximum_value () const
 
bigfield add_to_lower_limb (const field_t< Builder > &other, const uint256_t &other_maximum_value) const
 Add a field element to the lower limb. CAUTION (the element has to be constrained before using this function)
 
bigfield operator+ (const bigfield &other) const
 Adds two bigfield elements. Inputs are reduced to the modulus if necessary. Requires 4 gates if both elements are witnesses.
 
bigfield add_two (const bigfield &add_a, const bigfield &add_b) const
 Create constraints for summing three bigfield elements efficiently.
 
bigfield operator- (const bigfield &other) const
 Subtraction operator.
 
bigfield operator* (const bigfield &other) const
 Evaluate a non-native field multiplication: (a * b = c mod p) where p == target_basis.modulus.
 
bigfield operator/ (const bigfield &other) const
 
bigfield operator- () const
 Negation operator, works by subtracting this from zero.
 
bigfield operator+= (const bigfield &other)
 
bigfield operator-= (const bigfield &other)
 
bigfield operator*= (const bigfield &other)
 
bigfield operator/= (const bigfield &other)
 
bigfield sqr () const
 Square operator, computes a * a = c mod p.
 
bigfield sqradd (const std::vector< bigfield > &to_add) const
 Square and add operator, computes a * a + ...to_add = c mod p.
 
bigfield pow (const uint32_t exponent) const
 Raise the bigfield element to the power of (out-of-circuit) exponent.
 
bigfield madd (const bigfield &to_mul, const std::vector< bigfield > &to_add) const
 Compute a * b + ...to_add = c mod p.
 
bigfield div_without_denominator_check (const bigfield &denominator)
 
bigfield conditional_negate (const bool_t< Builder > &predicate) const
 
bigfield conditional_select (const bigfield &other, const bool_t< Builder > &predicate) const
 Create an element which is equal to either this or other based on the predicate.
 
bool_t< Builderoperator== (const bigfield &other) const
 Validate whether two bigfield elements are equal to each other.
 
void assert_is_in_field () const
 
void assert_less_than (const uint256_t &upper_limit) const
 
void assert_equal (const bigfield &other) const
 
void assert_is_not_equal (const bigfield &other) const
 
void self_reduce () const
 
bool is_constant () const
 Check if the bigfield is constant, i.e. its prime limb is constant.
 
bigfield invert () const
 Inverting function with the assumption that the bigfield element we are calling invert on is not zero.
 
void convert_constant_to_fixed_witness (Builder *builder)
 
void fix_witness ()
 
Builderget_context () const
 
void set_origin_tag (const bb::OriginTag &tag) const
 
bb::OriginTag get_origin_tag () const
 
void set_free_witness_tag ()
 Set the free witness flag for the bigfield.
 
void unset_free_witness_tag ()
 Unset the free witness flag for the bigfield.
 
uint32_t set_public () const
 Set the witness indices of the binary basis limbs to public.
 

Static Public Member Functions

static bigfield unsafe_construct_from_limbs (const field_t< Builder > &a, const field_t< Builder > &b, const field_t< Builder > &c, const field_t< Builder > &d, const bool can_overflow=false)
 Construct a bigfield element from binary limbs that are already reduced.
 
static bigfield construct_from_limbs (const field_t< Builder > &a, const field_t< Builder > &b, const field_t< Builder > &c, const field_t< Builder > &d, const bool can_overflow=false)
 Construct a bigfield element from binary limbs that are already reduced and ensure they are range constrained.
 
static bigfield unsafe_construct_from_limbs (const field_t< Builder > &a, const field_t< Builder > &b, const field_t< Builder > &c, const field_t< Builder > &d, const field_t< Builder > &prime_limb, const bool can_overflow=false)
 Construct a bigfield element from binary limbs and a prime basis limb that are already reduced.
 
static bigfield create_from_u512_as_witness (Builder *ctx, const uint512_t &value, const bool can_overflow=false, const size_t maximum_bitlength=0)
 Creates a bigfield element from a uint512_t. Bigfield element is constructed as a witness and not a circuit constant.
 
static bigfield from_witness (Builder *ctx, const bb::field< T > &input)
 
static void perform_reductions_for_mult_madd (std::vector< bigfield > &mul_left, std::vector< bigfield > &mul_right, const std::vector< bigfield > &to_add)
 Performs individual reductions on the supplied elements as well as more complex reductions to prevent CRT modulus overflow and to fit the quotient inside the range proof.
 
static bigfield mult_madd (const std::vector< bigfield > &mul_left, const std::vector< bigfield > &mul_right, const std::vector< bigfield > &to_add, bool fix_remainder_to_zero=false)
 
static bigfield dual_madd (const bigfield &left_a, const bigfield &right_a, const bigfield &left_b, const bigfield &right_b, const std::vector< bigfield > &to_add)
 
static bigfield msub_div (const std::vector< bigfield > &mul_left, const std::vector< bigfield > &mul_right, const bigfield &divisor, const std::vector< bigfield > &to_sub, bool enable_divisor_nz_check=false)
 
static bigfield sum (const std::vector< bigfield > &terms)
 Create constraints for summing these terms.
 
static bigfield internal_div (const std::vector< bigfield > &numerators, const bigfield &denominator, bool check_for_zero)
 
static bigfield div_without_denominator_check (const std::vector< bigfield > &numerators, const bigfield &denominator)
 
static bigfield div_check_denominator_nonzero (const std::vector< bigfield > &numerators, const bigfield &denominator)
 
static bigfield conditional_assign (const bool_t< Builder > &predicate, const bigfield &lhs, const bigfield &rhs)
 
static bigfield one ()
 
static bigfield zero ()
 
static constexpr bigfield unreduced_zero ()
 Create an unreduced 0 ~ p*k, where p*k is the minimal multiple of modulus that should be reduced.
 
static bigfield reconstruct_from_public (const std::span< const field_ct, PUBLIC_INPUTS_SIZE > &limbs)
 Reconstruct a bigfield from limbs (generally stored in the public inputs)
 
static constexpr uint512_t get_maximum_unreduced_value ()
 
static constexpr uint512_t get_prohibited_value ()
 
static constexpr uint1024_t get_maximum_crt_product ()
 Compute the maximum product of two bigfield elements in CRT: M = 2^t * n.
 
static size_t get_quotient_max_bits (const std::vector< uint1024_t > &remainders_max)
 Compute the maximum number of bits for quotient range proof to protect against CRT underflow.
 
static bool mul_product_overflows_crt_modulus (const uint1024_t &a_max, const uint1024_t &b_max, const std::vector< bigfield > &to_add)
 
static bool mul_product_overflows_crt_modulus (const std::vector< uint512_t > &as_max, const std::vector< uint512_t > &bs_max, const std::vector< bigfield > &to_add)
 
static constexpr uint256_t get_maximum_unreduced_limb_value ()
 
static constexpr uint256_t get_prohibited_limb_value ()
 

Public Attributes

Buildercontext
 
std::array< Limb, NUM_LIMBSbinary_basis_limbs
 Represents a bigfield element in the binary basis. A bigfield element is represented as a combination of 4 binary basis limbs: a = a[0] + a[1] * 2^L + a[2] * 2^2L + a[3] * 2^3L.
 
field_t< Builderprime_basis_limb
 Represents a bigfield element in the prime basis: (a mod n) where n is the native modulus.
 

Static Public Attributes

static constexpr size_t PUBLIC_INPUTS_SIZE = BIGFIELD_PUBLIC_INPUTS_SIZE
 
static constexpr size_t NUM_LIMBS = 4
 
static constexpr uint256_t modulus = (uint256_t(T::modulus_0, T::modulus_1, T::modulus_2, T::modulus_3))
 
static constexpr uint512_t modulus_u512 = static_cast<uint512_t>(modulus)
 
static constexpr uint64_t NUM_LIMB_BITS = NUM_LIMB_BITS_IN_FIELD_SIMULATION
 
static constexpr uint64_t NUM_LAST_LIMB_BITS = modulus_u512.get_msb() + 1 - (NUM_LIMB_BITS * 3)
 
static const uint1024_t DEFAULT_MAXIMUM_REMAINDER
 
static constexpr uint256_t DEFAULT_MAXIMUM_LIMB = (uint256_t(1) << NUM_LIMB_BITS) - uint256_t(1)
 
static constexpr uint256_t DEFAULT_MAXIMUM_MOST_SIGNIFICANT_LIMB
 
static constexpr uint64_t LOG2_BINARY_MODULUS = NUM_LIMB_BITS * NUM_LIMBS
 
static constexpr bool is_composite = true
 
static constexpr size_t MAXIMUM_SUMMAND_COUNT_LOG = 4
 
static constexpr size_t MAXIMUM_SUMMAND_COUNT = 1 << MAXIMUM_SUMMAND_COUNT_LOG
 
static constexpr uint256_t prime_basis_maximum_limb
 
static constexpr Basis prime_basis { uint512_t(bb::fr::modulus), bb::fr::modulus.get_msb() + 1 }
 
static constexpr Basis binary_basis { uint512_t(1) << LOG2_BINARY_MODULUS, LOG2_BINARY_MODULUS }
 
static constexpr Basis target_basis { modulus_u512, static_cast<size_t>(modulus_u512.get_msb() + 1) }
 
static constexpr bb::fr shift_1 = bb::fr(uint256_t(1) << NUM_LIMB_BITS)
 
static constexpr bb::fr shift_2 = bb::fr(uint256_t(1) << (NUM_LIMB_BITS * 2))
 
static constexpr bb::fr shift_3 = bb::fr(uint256_t(1) << (NUM_LIMB_BITS * 3))
 
static constexpr bb::fr shift_right_1 = bb::fr(1) / shift_1
 
static constexpr bb::fr shift_right_2 = bb::fr(1) / shift_2
 
static constexpr bb::fr negative_prime_modulus_mod_binary_basis = -bb::fr(uint256_t(modulus_u512))
 
static constexpr uint512_t negative_prime_modulus = binary_basis.modulus - target_basis.modulus
 
static constexpr std::array< uint256_t, NUM_LIMBSneg_modulus_limbs_u256
 
static constexpr std::array< bb::fr, NUM_LIMBSneg_modulus_limbs
 
static constexpr uint64_t MAX_ADDITION_LOG = 10
 
static constexpr uint64_t MAXIMUM_LIMB_SIZE_THAT_WOULDNT_OVERFLOW
 
static constexpr uint64_t MAX_UNREDUCED_LIMB_BITS = NUM_LIMB_BITS + 10
 
static constexpr uint64_t PROHIBITED_LIMB_BITS = MAX_UNREDUCED_LIMB_BITS + 5
 

Private Member Functions

std::array< uint32_t, NUM_LIMBSget_binary_basis_limb_witness_indices () const
 Get the witness indices of the (normalized) binary basis limbs.
 
void reduction_check () const
 Check if the bigfield element needs to be reduced.
 
void sanity_check () const
 Perform a sanity check on a value that is about to interact with another value.
 
std::array< uint256_t, NUM_LIMBSget_binary_basis_limb_maximums ()
 Get the maximum values of the binary basis limbs.
 

Static Private Member Functions

static std::pair< uint512_t, uint512_tcompute_quotient_remainder_values (const bigfield &a, const bigfield &b, const std::vector< bigfield > &to_add)
 Compute the quotient and remainder values for dividing (a * b + (to_add[0] + ... + to_add[-1])) with p.
 
static uint512_t compute_maximum_quotient_value (const std::vector< uint512_t > &as, const std::vector< uint512_t > &bs, const std::vector< uint512_t > &to_add)
 Compute the maximum possible value of quotient of a*b+\sum(to_add)
 
static std::pair< bool, size_t > get_quotient_reduction_info (const std::vector< uint512_t > &as_max, const std::vector< uint512_t > &bs_max, const std::vector< bigfield > &to_add, const std::vector< uint1024_t > &remainders_max={ DEFAULT_MAXIMUM_REMAINDER })
 Check for 2 conditions (CRT modulus is overflown or the maximum quotient doesn't fit into range proof). Also returns the length of quotient's range proof if there is no need to reduce.
 
static void unsafe_evaluate_multiply_add (const bigfield &input_left, const bigfield &input_to_mul, const std::vector< bigfield > &to_add, const bigfield &input_quotient, const std::vector< bigfield > &input_remainders)
 Evaluate a multiply add identity with several added elements and several remainders.
 
static void unsafe_evaluate_multiple_multiply_add (const std::vector< bigfield > &input_left, const std::vector< bigfield > &input_right, const std::vector< bigfield > &to_add, const bigfield &input_quotient, const std::vector< bigfield > &input_remainders)
 Evaluate a relation involving multiple multiplications and additions.
 
static void unsafe_evaluate_square_add (const bigfield &left, const std::vector< bigfield > &to_add, const bigfield &quotient, const bigfield &remainder)
 Evaluate a square with several additions.
 
static std::pair< uint512_t, uint512_tcompute_partial_schoolbook_multiplication (const std::array< uint256_t, NUM_LIMBS > &a_limbs, const std::array< uint256_t, NUM_LIMBS > &b_limbs)
 Compute the partial multiplication of two uint256_t arrays using schoolbook multiplication.
 

Detailed Description

template<typename Builder, typename T>
class bb::stdlib::bigfield< Builder, T >

Definition at line 21 of file bigfield.hpp.

Member Typedef Documentation

◆ CoefficientAccumulator

template<typename Builder , typename T >
using bb::stdlib::bigfield< Builder, T >::CoefficientAccumulator = bigfield

Definition at line 25 of file bigfield.hpp.

◆ field_ct

template<typename Builder , typename T >
using bb::stdlib::bigfield< Builder, T >::field_ct = field_t<Builder>

Definition at line 28 of file bigfield.hpp.

◆ native

template<typename Builder , typename T >
using bb::stdlib::bigfield< Builder, T >::native = bb::field<T>

Definition at line 27 of file bigfield.hpp.

◆ TParams

template<typename Builder , typename T >
using bb::stdlib::bigfield< Builder, T >::TParams = T

Definition at line 26 of file bigfield.hpp.

◆ View

template<typename Builder , typename T >
using bb::stdlib::bigfield< Builder, T >::View = bigfield

Definition at line 24 of file bigfield.hpp.

Constructor & Destructor Documentation

◆ bigfield() [1/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const field_t< Builder > &  low_bits,
const field_t< Builder > &  high_bits,
const bool  can_overflow = false,
const size_t  maximum_bitlength = 0 
)

Constructs a new bigfield object from two field elements representing the low and high bits.

Parameters
low_bitsThe field element representing the low 2L bits of the bigfield.
high_bitsThe field element representing the high 2L bits of the bigfield.
can_overflowWhether the bigfield can overflow the modulus.
maximum_bitlengthThe maximum bitlength of the bigfield. If 0, it defaults to |p| (target modulus bitlength).

Definition at line 44 of file bigfield_impl.hpp.

◆ bigfield() [2/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( Builder parent_context = nullptr)

Constructs a zero bigfield object: all limbs are set to zero.

This constructor is used to create a bigfield element without any context. It is useful for creating bigfield elements that will be later assigned to a context.

Definition at line 25 of file bigfield_impl.hpp.

◆ bigfield() [3/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( Builder parent_context,
const uint256_t value 
)

Constructs a new bigfield object from a uint256_t value.

Parameters
parent_contextThe circuit context in which the bigfield element will be created.
valueThe uint256_t value to be converted to a bigfield element.

Definition at line 32 of file bigfield_impl.hpp.

◆ bigfield() [4/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const uint256_t value)
inlineexplicit

Definition at line 117 of file bigfield.hpp.

◆ bigfield() [5/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const int  value)
inline

Constructs a new bigfield object from an int value. We first need to to construct a field element from the value to avoid bugs that have to do with the value being negative.

Definition at line 126 of file bigfield.hpp.

◆ bigfield() [6/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const unsigned long  value)
inline

Definition at line 131 of file bigfield.hpp.

◆ bigfield() [7/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const unsigned long long  value)
inline

Definition at line 136 of file bigfield.hpp.

◆ bigfield() [8/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const native  value)
inline

Construct a new bigfield object from bb::fq. We first convert to uint256_t as field elements are in Montgomery form internally.

Parameters
value

Definition at line 146 of file bigfield.hpp.

◆ bigfield() [9/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const byte_array< Builder > &  bytes)

Constructs a bigfield element from a 256-bit byte array.

This constructor interprets the input byte array as a 256-bit little-endian integer, and decomposes it into 4 limbs as follows:

  • Limb 0: 68 bits — bytes b24 to b31 and the high 4 bits of b23
  • Limb 1: 68 bits — bytes b14 to b22, and the low 4 bits of b23
  • Limb 2: 68 bits — bytes b7 to b14 and the high 4 bits of b6
  • Limb 3: 52 bits — bytes b0 to b5 and the low 4 bits of b6
Parameters
bytesThe 32-byte input representing the 256-bit integer (little-endian).

Definition at line 210 of file bigfield_impl.hpp.

◆ bigfield() [10/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( const bigfield< Builder, T > &  other)

Definition at line 117 of file bigfield_impl.hpp.

◆ bigfield() [11/11]

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::bigfield ( bigfield< Builder, T > &&  other)
noexcept

Definition at line 127 of file bigfield_impl.hpp.

◆ ~bigfield()

template<typename Builder , typename T >
bb::stdlib::bigfield< Builder, T >::~bigfield ( )
default

Member Function Documentation

◆ add_to_lower_limb()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::add_to_lower_limb ( const field_t< Builder > &  other,
const uint256_t other_maximum_value 
) const

Add a field element to the lower limb. CAUTION (the element has to be constrained before using this function)

Sometimes we need to add a small constrained value to a bigfield element (for example, a boolean value), but we don't want to construct a full bigfield element for that as it would take too many gates. If the maximum value of the field element being added is small enough, we can simply add it to the lowest limb and increase its maximum value. That will create 2 additional constraints instead of 5/3 needed to add 2 bigfield elements and several needed to construct a bigfield element.

Template Parameters
BuilderBuilder
TField Parameters
Parameters
otherField element that will be added to the lower
other_maximum_valueThe maximum value of other
Returns
bigfield<Builder, T> Result

Definition at line 315 of file bigfield_impl.hpp.

◆ add_two()

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

Create constraints for summing three bigfield elements efficiently.

Template Parameters
Builder
T
Parameters
add_a
add_b
Returns
The sum of three terms

Uses five gates (one for each limb) to add three bigfield elements.

Definition at line 459 of file bigfield_impl.hpp.

◆ assert_equal()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::assert_equal ( const bigfield< Builder, T > &  other) const

Definition at line 1888 of file bigfield_impl.hpp.

◆ assert_is_in_field()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::assert_is_in_field ( ) const

Definition at line 1819 of file bigfield_impl.hpp.

◆ assert_is_not_equal()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::assert_is_not_equal ( const bigfield< Builder, T > &  other) const

Definition at line 1948 of file bigfield_impl.hpp.

◆ assert_less_than()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::assert_less_than ( const uint256_t upper_limit) const

Definition at line 1824 of file bigfield_impl.hpp.

◆ compute_maximum_quotient_value()

template<typename Builder , typename T >
uint512_t bb::stdlib::bigfield< Builder, T >::compute_maximum_quotient_value ( const std::vector< uint512_t > &  as,
const std::vector< uint512_t > &  bs,
const std::vector< uint512_t > &  to_add 
)
staticprivate

Compute the maximum possible value of quotient of a*b+\sum(to_add)

Parameters
asMultiplicands
bsMultipliers
to_addAdded elements
Returns
uint512_t The maximum value of quotient

Definition at line 2590 of file bigfield_impl.hpp.

◆ compute_partial_schoolbook_multiplication()

template<typename Builder , typename T >
std::pair< uint512_t, uint512_t > bb::stdlib::bigfield< Builder, T >::compute_partial_schoolbook_multiplication ( const std::array< uint256_t, NUM_LIMBS > &  a_limbs,
const std::array< uint256_t, NUM_LIMBS > &  b_limbs 
)
staticprivate

Compute the partial multiplication of two uint256_t arrays using schoolbook multiplication.

Parameters
a_limbs
b_limbs
Returns
std::pair<uint512_t, uint512_t>

Regular schoolbook multiplication of two arrays each with L = 4 limbs will produce a result of size 2 * L - 1 = 7. In this context, we can ignore the last three limbs as those terms have multiplicands: (2^4L, 2^5L, 2^6L) and since we are working modulo 2^t = 2^4L, those terms will always be zero. This is why we call this helper function "partial schoolbook multiplication".

Definition at line 2644 of file bigfield_impl.hpp.

◆ compute_quotient_remainder_values()

template<typename Builder , typename T >
std::pair< uint512_t, uint512_t > bb::stdlib::bigfield< Builder, T >::compute_quotient_remainder_values ( const bigfield< Builder, T > &  a,
const bigfield< Builder, T > &  b,
const std::vector< bigfield< Builder, T > > &  to_add 
)
staticprivate

Compute the quotient and remainder values for dividing (a * b + (to_add[0] + ... + to_add[-1])) with p.

Parameters
aLeft multiplicand
bRight multiplier
to_addVector of elements to add
Returns
std::pair<uint512_t, uint512_t> Quotient and remainder values

Definition at line 2568 of file bigfield_impl.hpp.

◆ conditional_assign()

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

Definition at line 594 of file bigfield.hpp.

◆ conditional_negate()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::conditional_negate ( const bool_t< Builder > &  predicate) const

Definition at line 1594 of file bigfield_impl.hpp.

◆ conditional_select()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::conditional_select ( const bigfield< Builder, T > &  other,
const bool_t< Builder > &  predicate 
) const

Create an element which is equal to either this or other based on the predicate.

Template Parameters
Builder
T
Parameters
otherThe other bigfield element
predicatePredicate controlling the result (0 for this, 1 for the other)
Returns
Resulting element

Definition at line 1624 of file bigfield_impl.hpp.

◆ construct_from_limbs()

template<typename Builder , typename T >
static bigfield bb::stdlib::bigfield< Builder, T >::construct_from_limbs ( const field_t< Builder > &  a,
const field_t< Builder > &  b,
const field_t< Builder > &  c,
const field_t< Builder > &  d,
const bool  can_overflow = false 
)
inlinestatic

Construct a bigfield element from binary limbs that are already reduced and ensure they are range constrained.

Definition at line 185 of file bigfield.hpp.

◆ convert_constant_to_fixed_witness()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::convert_constant_to_fixed_witness ( Builder builder)
inline

Create a witness form a constant. This way the value of the witness is fixed and public.

Definition at line 677 of file bigfield.hpp.

◆ create_from_u512_as_witness()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::create_from_u512_as_witness ( Builder ctx,
const uint512_t value,
const bool  can_overflow = false,
const size_t  maximum_bitlength = 0 
)
static

Creates a bigfield element from a uint512_t. Bigfield element is constructed as a witness and not a circuit constant.

Parameters
ctx
value
can_overflowCan the input value have more than log2(modulus) bits?
maximum_bitlengthProvide the explicit maximum bitlength if known. Otherwise bigfield max value will be either log2(modulus) bits iff can_overflow = false, or (4 * NUM_LIMB_BITS) iff can_overflow = true
Returns
bigfield<Builder, T>

This method is 1 gate more efficient than constructing from 2 field_ct elements.

Definition at line 137 of file bigfield_impl.hpp.

◆ div_check_denominator_nonzero()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::div_check_denominator_nonzero ( const std::vector< bigfield< Builder, T > > &  numerators,
const bigfield< Builder, T > &  denominator 
)
static

Div method with constraints for denominator!=0.

Similar to operator/ but numerator can be linear sum of multiple elements

Definition at line 897 of file bigfield_impl.hpp.

◆ div_without_denominator_check() [1/2]

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::div_without_denominator_check ( const bigfield< Builder, T > &  denominator)

Definition at line 886 of file bigfield_impl.hpp.

◆ div_without_denominator_check() [2/2]

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::div_without_denominator_check ( const std::vector< bigfield< Builder, T > > &  numerators,
const bigfield< Builder, T > &  denominator 
)
static

Div method without constraining denominator!=0.

Similar to operator/ but numerator can be linear sum of multiple elements

Definition at line 879 of file bigfield_impl.hpp.

◆ dual_madd()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::dual_madd ( const bigfield< Builder, T > &  left_a,
const bigfield< Builder, T > &  right_a,
const bigfield< Builder, T > &  left_b,
const bigfield< Builder, T > &  right_b,
const std::vector< bigfield< Builder, T > > &  to_add 
)
static

Compute (left_a * right_a) + (left_b * right_b) + ...to_add = c mod p

This is cheaper than two multiplication operations, as the above only requires one quotient/remainder

Definition at line 1453 of file bigfield_impl.hpp.

◆ fix_witness()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::fix_witness ( )
inline

Fix a witness. The value of the witness is constrained with a selector

Definition at line 689 of file bigfield.hpp.

◆ from_witness()

template<typename Builder , typename T >
static bigfield bb::stdlib::bigfield< Builder, T >::from_witness ( Builder ctx,
const bb::field< T > &  input 
)
inlinestatic

Definition at line 294 of file bigfield.hpp.

◆ get_binary_basis_limb_maximums()

template<typename Builder , typename T >
std::array< uint256_t, NUM_LIMBS > bb::stdlib::bigfield< Builder, T >::get_binary_basis_limb_maximums ( )
inlineprivate

Get the maximum values of the binary basis limbs.

Returns
std::array<field_t<Builder>, NUM_LIMBS> An array containing the maximum values of the binary basis limbs.

Definition at line 1100 of file bigfield.hpp.

◆ get_binary_basis_limb_witness_indices()

template<typename Builder , typename T >
std::array< uint32_t, NUM_LIMBS > bb::stdlib::bigfield< Builder, T >::get_binary_basis_limb_witness_indices ( ) const
inlineprivate

Get the witness indices of the (normalized) binary basis limbs.

Returns
Witness indices of the binary basis limbs

Definition at line 948 of file bigfield.hpp.

◆ get_context()

template<typename Builder , typename T >
Builder * bb::stdlib::bigfield< Builder, T >::get_context ( ) const
inline

Definition at line 701 of file bigfield.hpp.

◆ get_maximum_crt_product()

template<typename Builder , typename T >
static constexpr uint1024_t bb::stdlib::bigfield< Builder, T >::get_maximum_crt_product ( )
inlinestaticconstexpr

Compute the maximum product of two bigfield elements in CRT: M = 2^t * n.

When we multiply two bigfield elements a and b, we need to check that: a * b = q * p + r, where q is the quotient, r is the remainder, and p is the size of the non-native field. With the CRT, we should have both sizes less than the maximum product M = 2^t * n.

Returns
uint1024_t Maximum product of two bigfield elements in CRT form

Definition at line 811 of file bigfield.hpp.

◆ get_maximum_unreduced_limb_value()

template<typename Builder , typename T >
static constexpr uint256_t bb::stdlib::bigfield< Builder, T >::get_maximum_unreduced_limb_value ( )
inlinestaticconstexpr

Definition at line 935 of file bigfield.hpp.

◆ get_maximum_unreduced_value()

template<typename Builder , typename T >
static constexpr uint512_t bb::stdlib::bigfield< Builder, T >::get_maximum_unreduced_value ( )
inlinestaticconstexpr

Definition at line 764 of file bigfield.hpp.

◆ get_maximum_value()

template<typename Builder , typename T >
uint512_t bb::stdlib::bigfield< Builder, T >::get_maximum_value ( ) const

Definition at line 305 of file bigfield_impl.hpp.

◆ get_origin_tag()

template<typename Builder , typename T >
bb::OriginTag bb::stdlib::bigfield< Builder, T >::get_origin_tag ( ) const
inline

Definition at line 711 of file bigfield.hpp.

◆ get_prohibited_limb_value()

template<typename Builder , typename T >
static constexpr uint256_t bb::stdlib::bigfield< Builder, T >::get_prohibited_limb_value ( )
inlinestaticconstexpr

Definition at line 938 of file bigfield.hpp.

◆ get_prohibited_value()

template<typename Builder , typename T >
static constexpr uint512_t bb::stdlib::bigfield< Builder, T >::get_prohibited_value ( )
inlinestaticconstexpr

Definition at line 794 of file bigfield.hpp.

◆ get_quotient_max_bits()

template<typename Builder , typename T >
static size_t bb::stdlib::bigfield< Builder, T >::get_quotient_max_bits ( const std::vector< uint1024_t > &  remainders_max)
inlinestatic

Compute the maximum number of bits for quotient range proof to protect against CRT underflow.

When multiplying a and b, we need to check that: a * b = q * p + r, and each side of the equation is less than the maximum product M = 2^t * n. The quotient q is a size of the non-native field, and we need to ensure it fits within the available bits. q * p + r < M ==> q < (M - r_max) / p

Parameters
remainders_maxMaximum sizes of resulting remainders
Returns
Desired length of range proof

Definition at line 827 of file bigfield.hpp.

◆ get_quotient_reduction_info()

template<typename Builder , typename T >
std::pair< bool, size_t > bb::stdlib::bigfield< Builder, T >::get_quotient_reduction_info ( const std::vector< uint512_t > &  as_max,
const std::vector< uint512_t > &  bs_max,
const std::vector< bigfield< Builder, T > > &  to_add,
const std::vector< uint1024_t > &  remainders_max = DEFAULT_MAXIMUM_REMAINDER } 
)
staticprivate

Check for 2 conditions (CRT modulus is overflown or the maximum quotient doesn't fit into range proof). Also returns the length of quotient's range proof if there is no need to reduce.

Parameters
as_maxVector of left multiplicands' maximum values
bs_maxVector of right multiplicands' maximum values
to_addVector of added bigfield values
Returns
<true, 0> if we need to reduce the product; <false, The length of quotient range proof> if there is no need to reduce the product.

Definition at line 2613 of file bigfield_impl.hpp.

◆ get_value()

template<typename Builder , typename T >
uint512_t bb::stdlib::bigfield< Builder, T >::get_value ( ) const

Definition at line 296 of file bigfield_impl.hpp.

◆ internal_div()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::internal_div ( const std::vector< bigfield< Builder, T > > &  numerators,
const bigfield< Builder, T > &  denominator,
bool  check_for_zero 
)
static

Division of a sum with an optional check if divisor is zero. Should not be used outside of class.

Parameters
numeratorsVector of numerators
denominatorDenominator
check_for_zeroIf the zero check should be enabled
Returns
The result of division

Definition at line 794 of file bigfield_impl.hpp.

◆ invert()

template<typename Builder , typename T >
bigfield bb::stdlib::bigfield< Builder, T >::invert ( ) const
inline

Inverting function with the assumption that the bigfield element we are calling invert on is not zero.

Returns
bigfield

Definition at line 634 of file bigfield.hpp.

◆ is_constant()

template<typename Builder , typename T >
bool bb::stdlib::bigfield< Builder, T >::is_constant ( ) const
inline

Check if the bigfield is constant, i.e. its prime limb is constant.

Returns
true if the bigfield is constant, false otherwise.

We use assertions to ensure that all limbs are consistent in their constant status.

Definition at line 615 of file bigfield.hpp.

◆ madd()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::madd ( const bigfield< Builder, T > &  to_mul,
const std::vector< bigfield< Builder, T > > &  to_add 
) const

Compute a * b + ...to_add = c mod p.

Parameters
to_mulBigfield element to multiply by
to_addVector of elements to add
Returns
New bigfield element c

Definition at line 1058 of file bigfield_impl.hpp.

◆ msub_div()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::msub_div ( const std::vector< bigfield< Builder, T > > &  mul_left,
const std::vector< bigfield< Builder, T > > &  mul_right,
const bigfield< Builder, T > &  divisor,
const std::vector< bigfield< Builder, T > > &  to_sub,
bool  enable_divisor_nz_check = false 
)
static

multiply, subtract, divide. This method computes:

result = -(\sum{mul_left[i] * mul_right[i]} + ...to_add) / divisor

Algorithm is constructed in this way to ensure that all computed terms are positive

i.e. we evaluate: result * divisor + (\sum{mul_left[i] * mul_right[i]) + ...to_add) = 0

It is critical that ALL the terms on the LHS are positive to eliminate the possiblity of underflows when calling evaluate_multiple_multiply_add

only requires one quotient and remainder + overflow limbs

We proxy this to mult_madd, so it only requires one quotient and remainder + overflow limbs

Definition at line 1490 of file bigfield_impl.hpp.

◆ mul_product_overflows_crt_modulus() [1/2]

template<typename Builder , typename T >
static bool bb::stdlib::bigfield< Builder, T >::mul_product_overflows_crt_modulus ( const std::vector< uint512_t > &  as_max,
const std::vector< uint512_t > &  bs_max,
const std::vector< bigfield< Builder, T > > &  to_add 
)
inlinestatic

Check that the maximum value of a sum of bigfield productc with added values overflows ctf modulus.

Parameters
as_maxVector of multiplicands' maximum values
b_maxVector of multipliers' maximum values
to_addVector of field elements to be added
Returns
true if there is an overflow, false otherwise

Definition at line 873 of file bigfield.hpp.

◆ mul_product_overflows_crt_modulus() [2/2]

template<typename Builder , typename T >
static bool bb::stdlib::bigfield< Builder, T >::mul_product_overflows_crt_modulus ( const uint1024_t a_max,
const uint1024_t b_max,
const std::vector< bigfield< Builder, T > > &  to_add 
)
inlinestatic

Check that the maximum value of a bigfield product with added values overflows crt modulus.

Parameters
a_maxmultiplicand maximum value
b_maxmultiplier maximum value
to_addvector of field elements to be added
Returns
true if there is an overflow, false otherwise

Definition at line 847 of file bigfield.hpp.

◆ mult_madd()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::mult_madd ( const std::vector< bigfield< Builder, T > > &  mul_left,
const std::vector< bigfield< Builder, T > > &  mul_right,
const std::vector< bigfield< Builder, T > > &  to_add,
bool  fix_remainder_to_zero = false 
)
static

Evaluate the sum of products and additional values safely.

Parameters
mul_leftVector of bigfield multiplicands
mul_rightVector of bigfield multipliers
to_addVector of bigfield elements to add to the sum of products
Returns
A reduced value that is the sum of all products and to_add values

Definition at line 1264 of file bigfield_impl.hpp.

◆ one()

template<typename Builder , typename T >
static bigfield bb::stdlib::bigfield< Builder, T >::one ( )
inlinestatic

Create a public one constant

Definition at line 639 of file bigfield.hpp.

◆ operator*()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::operator* ( const bigfield< Builder, T > &  other) const

Evaluate a non-native field multiplication: (a * b = c mod p) where p == target_basis.modulus.

Parameters
other
Returns
bigfield

We compute quotient term q and remainder c and evaluate that: a * b - q * p - c = 0 mod modulus_u512 (binary basis modulus, currently 2**272) a * b - q * p - c = 0 mod circuit modulus We also check that: a * b < M and q * p - c < M, where M = (2^t * n) is CRT modulus.

Definition at line 708 of file bigfield_impl.hpp.

◆ operator*=()

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

Definition at line 495 of file bigfield.hpp.

◆ operator+()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::operator+ ( const bigfield< Builder, T > &  other) const

Adds two bigfield elements. Inputs are reduced to the modulus if necessary. Requires 4 gates if both elements are witnesses.

Naive addition of two bigfield elements would require 5 gates: 4 gates to add the binary basis limbs and 1 gate to add the prime basis limbs. However, if both elements are witnesses, we can use an optimized addition trick that uses 4 gates instead of 5. In this case, we add the prime basis limbs and one of the binary basis limbs in a single gate.

Template Parameters
Builder
T
Parameters
other
Returns
bigfield<Builder, T>

Definition at line 355 of file bigfield_impl.hpp.

◆ operator+=()

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

Definition at line 485 of file bigfield.hpp.

◆ operator-() [1/2]

template<typename Builder , typename T >
bigfield bb::stdlib::bigfield< Builder, T >::operator- ( ) const
inline

Negation operator, works by subtracting this from zero.

Returns
bigfield

Definition at line 483 of file bigfield.hpp.

◆ operator-() [2/2]

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::operator- ( const bigfield< Builder, T > &  other) const

Subtraction operator.

Parameters
other
Returns
bigfield

Uses lazy reduction techniques (similar to operator+) to save on field reductions. Instead of computing *this - other, we compute a constant X = s * p and compute: *this + X - other to ensure we do not underflow. Note that NOT enough to ensure that the integer value of *this + X - other does not underflow. We must ALSO ensure that each LIMB of the result does not underflow. Based on this condition, we compute the minimum value of s such that, for each limb i, the following result is positive: *this.limb[i] + X.limb[i] - other.limb[i] ≥ 0.

Plookup bigfield subtractoin

We have a special addition gate we can toggle, that will compute: (w_1 + w_4 - w_4_omega + q_arith = 0) This is in addition to the regular addition gate

We can arrange our wires in memory like this:

| 1 | 2 | 3 | 4 | |--—|--—|--—|--—| | b.p | a.0 | b.0 | c.p | (b.p + c.p - a.p = 0) AND (a.0 - b.0 - c.0 = 0) | a.p | a.1 | b.1 | c.0 | (a.1 - b.1 - c.1 = 0) | a.2 | b.2 | c.2 | c.1 | (a.2 - b.2 - c.2 = 0) | a.3 | b.3 | c.3 | — | (a.3 - b.3 - c.3 = 0)

Step 1: For each limb compute the MAXIMUM value we will have to borrow from the next significant limb

i.e. if we assume that *this = 0 and other = other.maximum_value, how many bits do we need to borrow from the next significant limb to ensure each limb value is positive?

N.B. for this segment maximum_value really refers to maximum NEGATIVE value of the result

Step 2: Compute the constant value X = m * p we must add to the result to ensure EVERY limb is >= 0

We need to find a value X where X.limb[3] > limb_3_maximum_value. As long as the above holds, we can borrow bits from X.limb[3] to ensure less significant limbs are positive

Start by setting constant_to_add = p

Step 3: Compute offset terms t0, t1, t2, t3 that we add to our result to ensure each limb is positive

t3 represents the value we are BORROWING from constant_to_add.limb[3] t2, t1, t0 are the terms we will ADD to constant_to_add.limb[2], constant_to_add.limb[1], constant_to_add.limb[0]

Borrow propagation table: ┌───────┬─────────────────────────────────┬──────────────────────────────────┐ │ Limb │ Value received FROM next limb │ Value given TO previous limb │ ├───────┼─────────────────────────────────┼──────────────────────────────────┤ │ 0 │ 2^limb_0_borrow_shift │ 0 │ │ 1 │ 2^limb_1_borrow_shift │ 2^(limb_0_borrow_shift - L) │ │ 2 │ 2^limb_2_borrow_shift │ 2^(limb_1_borrow_shift - L) │ │ 3 │ 0 │ 2^(limb_2_borrow_shift - L) │ └───────┴─────────────────────────────────┴──────────────────────────────────┘

i.e. The net value we add to constant_to_add is 0. We must ensure that: t3 = t0 + (t1 << NUM_LIMB_BITS) + (t2 << NUM_LIMB_BITS * 2)

e.g. the value we borrow to produce t0 is subtracted from t1, the value we borrow from t1 is subtracted from t2 the value we borrow from t2 is equal to t3

Compute the limbs of constant_to_add, including our offset terms t0, t1, t2, t3 that ensure each result limb is positive

Update the maximum possible value of the result. We assume here that (*this.value) = 0

Compute the binary basis limbs of our result

Compute the prime basis limb of the result

Definition at line 501 of file bigfield_impl.hpp.

◆ operator-=()

template<typename Builder , typename T >
bigfield bb::stdlib::bigfield< Builder, T >::operator-= ( const bigfield< Builder, T > &  other)
inline

Definition at line 490 of file bigfield.hpp.

◆ operator/()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::operator/ ( const bigfield< Builder, T > &  other) const

Division operator: a / b. Creates constraints for division in the circuit and checks b != 0. If you need a variant without the zero check, use div_without_denominator_check.

Parameters
otherThe denominator.
Returns
The result of the division c = (a / b) mod p.

To evaluate (a / b = c mod p), we instead evaluate (c * b = a mod p), where p is the target modulus. We also check that b != 0.

Definition at line 752 of file bigfield_impl.hpp.

◆ operator/=()

template<typename Builder , typename T >
bigfield bb::stdlib::bigfield< Builder, T >::operator/= ( const bigfield< Builder, T > &  other)
inline

Definition at line 500 of file bigfield.hpp.

◆ operator=() [1/2]

template<typename Builder , typename T >
bigfield< Builder, T > & bb::stdlib::bigfield< Builder, T >::operator= ( bigfield< Builder, T > &&  other)
noexcept

Definition at line 285 of file bigfield_impl.hpp.

◆ operator=() [2/2]

template<typename Builder , typename T >
bigfield< Builder, T > & bb::stdlib::bigfield< Builder, T >::operator= ( const bigfield< Builder, T > &  other)

Definition at line 271 of file bigfield_impl.hpp.

◆ operator==()

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

Validate whether two bigfield elements are equal to each other.

To evaluate whether (a == b), we use result boolean r to evaluate the following logic: (n.b all algebra involving bigfield elements is done in the bigfield)

  1. If r == 1 , a - b == 0
  2. If r == 0, a - b posesses an inverse I i.e. (a - b) * I - 1 == 0 We efficiently evaluate this logic by evaluating a single expression (a - b)*X = Y We use conditional assignment logic to define X, Y to be the following: If r == 1 then X = 1, Y = 0 If r == 0 then X = I, Y = 1 This allows us to evaluate operator== using only 1 bigfield multiplication operation. We can check the product equals 0 or 1 by directly evaluating the binary basis/prime basis limbs of Y. i.e. if r == 1 then (a - b)*X should have 0 for all limb values if r == 0 then (a - b)*X should have 1 in the least significant binary basis limb and 0 elsewhere
    Template Parameters
    Builder
    T
    Parameters
    other
    Returns
    bool_t<Builder>

Definition at line 1718 of file bigfield_impl.hpp.

◆ perform_reductions_for_mult_madd()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::perform_reductions_for_mult_madd ( std::vector< bigfield< Builder, T > > &  mul_left,
std::vector< bigfield< Builder, T > > &  mul_right,
const std::vector< bigfield< Builder, T > > &  to_add 
)
static

Performs individual reductions on the supplied elements as well as more complex reductions to prevent CRT modulus overflow and to fit the quotient inside the range proof.

Template Parameters
Builderbuilder
Tbasefield
Parameters
mul_left
mul_right
to_add

Definition at line 1133 of file bigfield_impl.hpp.

◆ pow()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::pow ( const uint32_t  exponent) const

Raise the bigfield element to the power of (out-of-circuit) exponent.

Parameters
exponentThe exponent to raise the bigfield element to.
Returns
this ** (exponent)

Uses the square-and-multiply algorithm to compute a^exponent mod p.

NOTE(https://github.com/AztecProtocol/barretenberg/issues/1014) Improve the efficiency of this function using sliding window method.

Definition at line 1005 of file bigfield_impl.hpp.

◆ reconstruct_from_public()

template<typename Builder , typename T >
static bigfield bb::stdlib::bigfield< Builder, T >::reconstruct_from_public ( const std::span< const field_ct, PUBLIC_INPUTS_SIZE > &  limbs)
inlinestatic

Reconstruct a bigfield from limbs (generally stored in the public inputs)

Definition at line 759 of file bigfield.hpp.

◆ reduction_check()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::reduction_check ( ) const
private

Check if the bigfield element needs to be reduced.

This function checks if the bigfield element is within the valid range and reduces it if necessary. When multiplying bigfield elements a and b, we need to ensure that each side of the equation: a * b = q * p + r (a) holds modulo the size of the native field n, (b) holds modulo the size of the bigger ring 2^t. (c) is less than the maximum value: M = 2^t * n. This implies a, b must always be less than √M, and their limbs must be less than the maximum limb value.

Given a bigfield element c, this function applies these checks: (i) c < √M (see note below). (ii) each limb (binary basis) of c is less than the maximum limb value.

These checks prevent our field arithmetic from overflowing the native modulus boundary, whilst ensuring we can still use the chinese remainder theorem to validate field multiplications with a reduced number of range checks.

Note: We actually apply a stricter bound, see get_maximum_unreduced_value for an explanation.

Definition at line 1759 of file bigfield_impl.hpp.

◆ sanity_check()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::sanity_check ( ) const
private

Perform a sanity check on a value that is about to interact with another value.

ASSERTs that the value of all limbs is less than or equal to the prohibited maximum value. Checks that the maximum value of the whole element is also less than a prohibited maximum value.

Definition at line 1798 of file bigfield_impl.hpp.

◆ self_reduce()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::self_reduce ( ) const

Definition at line 1990 of file bigfield_impl.hpp.

◆ set_free_witness_tag()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::set_free_witness_tag ( )
inline

Set the free witness flag for the bigfield.

Definition at line 723 of file bigfield.hpp.

◆ set_origin_tag()

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

Definition at line 703 of file bigfield.hpp.

◆ set_public()

template<typename Builder , typename T >
uint32_t bb::stdlib::bigfield< Builder, T >::set_public ( ) const
inline

Set the witness indices of the binary basis limbs to public.

Returns
uint32_t The public input index at which the representation of the bigfield starts

Definition at line 746 of file bigfield.hpp.

◆ sqr()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::sqr ( ) const

Square operator, computes a * a = c mod p.

Returns
bigfield

Costs the same as operator* as it just sets a = b. NOTE(https://github.com/AztecProtocol/aztec-packages/issues/15089): Can optimize this further to save a gate.

Definition at line 903 of file bigfield_impl.hpp.

◆ sqradd()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::sqradd ( const std::vector< bigfield< Builder, T > > &  to_add) const

Square and add operator, computes a * a + ...to_add = c mod p.

Parameters
to_addThe bigfield element to add to the square.
Returns
bigfield

We can chain multiple additions to a square/multiply with a single quotient/remainder. Chaining the additions here is cheaper than calling operator+ because we can combine some gates in evaluate_multiply_add

Definition at line 934 of file bigfield_impl.hpp.

◆ sum()

template<typename Builder , typename T >
bigfield< Builder, T > bb::stdlib::bigfield< Builder, T >::sum ( const std::vector< bigfield< Builder, T > > &  terms)
static

Create constraints for summing these terms.

Template Parameters
Builder
T
Parameters
terms
Returns
The sum of terms

Definition at line 766 of file bigfield_impl.hpp.

◆ to_byte_array()

template<typename Builder , typename T >
byte_array< Builder > bb::stdlib::bigfield< Builder, T >::to_byte_array ( ) const
inline

Convert the bigfield element to a byte array. Concatenates byte arrays of the high (2L bits) and low (2L bits) parts of the bigfield element.

Assumes that 2L is divisible by 8, i.e. (NUM_LIMB_BITS * 2) % 8 == 0. Also we check that the bigfield element is in the target field.

Returns
byte_array<Builder>

Definition at line 363 of file bigfield.hpp.

◆ unreduced_zero()

template<typename Builder , typename T >
static constexpr bigfield bb::stdlib::bigfield< Builder, T >::unreduced_zero ( )
inlinestaticconstexpr

Create an unreduced 0 ~ p*k, where p*k is the minimal multiple of modulus that should be reduced.

We need it for division. If we always add this element during division, then we never run into the formula-breaking situation

Definition at line 660 of file bigfield.hpp.

◆ unsafe_construct_from_limbs() [1/2]

template<typename Builder , typename T >
static bigfield bb::stdlib::bigfield< Builder, T >::unsafe_construct_from_limbs ( const field_t< Builder > &  a,
const field_t< Builder > &  b,
const field_t< Builder > &  c,
const field_t< Builder > &  d,
const bool  can_overflow = false 
)
inlinestatic

Construct a bigfield element from binary limbs that are already reduced.

This API should only be used by bigfield and other stdlib members for efficiency and with extreme care. We need it in cases where we precompute and reduce the elements, for example, and then put them in a table

Definition at line 157 of file bigfield.hpp.

◆ unsafe_construct_from_limbs() [2/2]

template<typename Builder , typename T >
static bigfield bb::stdlib::bigfield< Builder, T >::unsafe_construct_from_limbs ( const field_t< Builder > &  a,
const field_t< Builder > &  b,
const field_t< Builder > &  c,
const field_t< Builder > &  d,
const field_t< Builder > &  prime_limb,
const bool  can_overflow = false 
)
inlinestatic

Construct a bigfield element from binary limbs and a prime basis limb that are already reduced.

This API should only be used by bigfield and other stdlib members for efficiency and with extreme care. We need it in cases where we precompute and reduce the elements, for example, and then put them in a table

Definition at line 230 of file bigfield.hpp.

◆ unsafe_evaluate_multiple_multiply_add()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::unsafe_evaluate_multiple_multiply_add ( const std::vector< bigfield< Builder, T > > &  input_left,
const std::vector< bigfield< Builder, T > > &  input_right,
const std::vector< bigfield< Builder, T > > &  to_add,
const bigfield< Builder, T > &  input_quotient,
const std::vector< bigfield< Builder, T > > &  input_remainders 
)
staticprivate

Evaluate a relation involving multiple multiplications and additions.

Parameters
input_leftVector of left multiplication operands.
input_rightVector of right multiplication operands.
to_addVector of elements to add to the product.
input_quotientQuotient term.
input_remaindersVector of remainders.

This function evaluates the relationship: (a[0] * b[0] + ... + (a[-1] * b[-1])) + (to_add[0] + .. + to_add[-1]) - q * p - (r[0] + .. + r[-1]) = 0 mod 2^t (a[0] * b[0] + ... + (a[-1] * b[-1])) + (to_add[0] + .. + to_add[-1]) - q * p - (r[0] + .. + r[-1]) = 0 mod n

Note
This method supports multiple "remainders" because, when evaluating division of the form: (n[0] * n[1] + ... + n[-1]) / d, the numerator (which becomes the remainder term) can be a sum of multiple elements. See msub_div for more details on how this is used.
Warning
THIS FUNCTION IS UNSAFE TO USE IN CIRCUITS AS IT DOES NOT PROTECT AGAINST CRT OVERFLOWS.

Step 1: Compute the maximum potential value of our product limbs

max_lo = maximum value of limb products that span the range 0 - 2^{3t} max_hi = maximum value of limb products that span the range 2^{2t} - 2^{5t} (t = NUM_LIMB_BITS)

Definition at line 2245 of file bigfield_impl.hpp.

◆ unsafe_evaluate_multiply_add()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::unsafe_evaluate_multiply_add ( const bigfield< Builder, T > &  input_left,
const bigfield< Builder, T > &  input_to_mul,
const std::vector< bigfield< Builder, T > > &  to_add,
const bigfield< Builder, T > &  input_quotient,
const std::vector< bigfield< Builder, T > > &  input_remainders 
)
staticprivate

Evaluate a multiply add identity with several added elements and several remainders.

Parameters
leftLeft multiplicand
rightRight multiplicand
to_addVector of elements to add
quotientQuotient term
remaindersVector of remainders

This function evaluates the relationship: a * b + (to_add[0] + .. + to_add[-1]) - q * p - (r[0] + .. + r[-1]) = 0 mod 2^t (binary basis modulus) a * b + (to_add[0] + .. + to_add[-1]) - q * p - (r[0] + .. + r[-1]) = 0 mod n (circuit modulus)

Note
This method supports multiple "remainders" because, when evaluating division of the form: (n[0] * n[1] + ... + n[-1]) / d, the numerator (which becomes the remainder term) can be a sum of multiple elements.
Warning
THIS FUNCTION IS UNSAFE TO USE IN CIRCUITS AS IT DOES NOT PROTECT AGAINST CRT OVERFLOWS.

Definition at line 2037 of file bigfield_impl.hpp.

◆ unsafe_evaluate_square_add()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::unsafe_evaluate_square_add ( const bigfield< Builder, T > &  left,
const std::vector< bigfield< Builder, T > > &  to_add,
const bigfield< Builder, T > &  quotient,
const bigfield< Builder, T > &  remainder 
)
staticprivate

Evaluate a square with several additions.

Parameters
leftLeft multiplicand
to_addVector of elements to add
quotientQuotient term
remainderRemainder term

This function evaluates the relationship: (a * a) + (to_add[0] + .. + to_add[-1]) - q * p - r = 0 mod 2^t (binary basis modulus) (a * a) + (to_add[0] + .. + to_add[-1]) - q * p - r = 0 mod n (circuit modulus)

Warning
THIS FUNCTION IS UNSAFE TO USE IN CIRCUITS AS IT DOES NOT PROTECT AGAINST CRT OVERFLOWS.

Definition at line 2544 of file bigfield_impl.hpp.

◆ unset_free_witness_tag()

template<typename Builder , typename T >
void bb::stdlib::bigfield< Builder, T >::unset_free_witness_tag ( )
inline

Unset the free witness flag for the bigfield.

Definition at line 734 of file bigfield.hpp.

◆ zero()

template<typename Builder , typename T >
static bigfield bb::stdlib::bigfield< Builder, T >::zero ( )
inlinestatic

Create a public zero constant

Definition at line 648 of file bigfield.hpp.

Member Data Documentation

◆ binary_basis

template<typename Builder , typename T >
constexpr Basis bb::stdlib::bigfield< Builder, T >::binary_basis { uint512_t(1) << LOG2_BINARY_MODULUS, LOG2_BINARY_MODULUS }
staticconstexpr

Definition at line 332 of file bigfield.hpp.

◆ binary_basis_limbs

template<typename Builder , typename T >
std::array<Limb, NUM_LIMBS> bb::stdlib::bigfield< Builder, T >::binary_basis_limbs
mutable

Represents a bigfield element in the binary basis. A bigfield element is represented as a combination of 4 binary basis limbs: a = a[0] + a[1] * 2^L + a[2] * 2^2L + a[3] * 2^3L.

Definition at line 80 of file bigfield.hpp.

◆ context

template<typename Builder , typename T >
Builder* bb::stdlib::bigfield< Builder, T >::context

Definition at line 74 of file bigfield.hpp.

◆ DEFAULT_MAXIMUM_LIMB

template<typename Builder , typename T >
constexpr uint256_t bb::stdlib::bigfield< Builder, T >::DEFAULT_MAXIMUM_LIMB = (uint256_t(1) << NUM_LIMB_BITS) - uint256_t(1)
staticconstexpr

Definition at line 319 of file bigfield.hpp.

◆ DEFAULT_MAXIMUM_MOST_SIGNIFICANT_LIMB

template<typename Builder , typename T >
constexpr uint256_t bb::stdlib::bigfield< Builder, T >::DEFAULT_MAXIMUM_MOST_SIGNIFICANT_LIMB
staticconstexpr
Initial value:
=
(uint256_t(1) << NUM_LAST_LIMB_BITS) - uint256_t(1)
static constexpr uint64_t NUM_LAST_LIMB_BITS
Definition bigfield.hpp:311

Definition at line 320 of file bigfield.hpp.

◆ DEFAULT_MAXIMUM_REMAINDER

template<typename Builder , typename T >
const uint1024_t bb::stdlib::bigfield< Builder, T >::DEFAULT_MAXIMUM_REMAINDER
inlinestatic
Initial value:
=
static constexpr uint64_t NUM_LIMB_BITS
Definition bigfield.hpp:310
uintx< uint512_t > uint1024_t
Definition uintx.hpp:309

Definition at line 317 of file bigfield.hpp.

◆ is_composite

template<typename Builder , typename T >
constexpr bool bb::stdlib::bigfield< Builder, T >::is_composite = true
staticconstexpr

Definition at line 323 of file bigfield.hpp.

◆ LOG2_BINARY_MODULUS

template<typename Builder , typename T >
constexpr uint64_t bb::stdlib::bigfield< Builder, T >::LOG2_BINARY_MODULUS = NUM_LIMB_BITS * NUM_LIMBS
staticconstexpr

Definition at line 322 of file bigfield.hpp.

◆ MAX_ADDITION_LOG

template<typename Builder , typename T >
constexpr uint64_t bb::stdlib::bigfield< Builder, T >::MAX_ADDITION_LOG = 10
staticconstexpr

Definition at line 897 of file bigfield.hpp.

◆ MAX_UNREDUCED_LIMB_BITS

template<typename Builder , typename T >
constexpr uint64_t bb::stdlib::bigfield< Builder, T >::MAX_UNREDUCED_LIMB_BITS = NUM_LIMB_BITS + 10
staticconstexpr

Definition at line 928 of file bigfield.hpp.

◆ MAXIMUM_LIMB_SIZE_THAT_WOULDNT_OVERFLOW

template<typename Builder , typename T >
constexpr uint64_t bb::stdlib::bigfield< Builder, T >::MAXIMUM_LIMB_SIZE_THAT_WOULDNT_OVERFLOW
staticconstexpr
Initial value:
=
constexpr uint64_t get_msb() const
static constexpr uint64_t MAX_ADDITION_LOG
Definition bigfield.hpp:897
static constexpr uint256_t modulus

Definition at line 922 of file bigfield.hpp.

◆ MAXIMUM_SUMMAND_COUNT

template<typename Builder , typename T >
constexpr size_t bb::stdlib::bigfield< Builder, T >::MAXIMUM_SUMMAND_COUNT = 1 << MAXIMUM_SUMMAND_COUNT_LOG
staticconstexpr

Definition at line 327 of file bigfield.hpp.

◆ MAXIMUM_SUMMAND_COUNT_LOG

template<typename Builder , typename T >
constexpr size_t bb::stdlib::bigfield< Builder, T >::MAXIMUM_SUMMAND_COUNT_LOG = 4
staticconstexpr

Definition at line 326 of file bigfield.hpp.

◆ modulus

template<typename Builder , typename T >
constexpr uint256_t bb::stdlib::bigfield< Builder, T >::modulus = (uint256_t(T::modulus_0, T::modulus_1, T::modulus_2, T::modulus_3))
staticconstexpr

Definition at line 308 of file bigfield.hpp.

◆ modulus_u512

template<typename Builder , typename T >
constexpr uint512_t bb::stdlib::bigfield< Builder, T >::modulus_u512 = static_cast<uint512_t>(modulus)
staticconstexpr

Definition at line 309 of file bigfield.hpp.

◆ neg_modulus_limbs

template<typename Builder , typename T >
constexpr std::array<bb::fr, NUM_LIMBS> bb::stdlib::bigfield< Builder, T >::neg_modulus_limbs
staticconstexpr
Initial value:
{
}
constexpr uintx slice(const uint64_t start, const uint64_t end) const
Definition uintx.hpp:82
static constexpr uint512_t negative_prime_modulus
Definition bigfield.hpp:340
field< Bn254FrParams > fr
Definition fr.hpp:174

Definition at line 347 of file bigfield.hpp.

◆ neg_modulus_limbs_u256

template<typename Builder , typename T >
constexpr std::array<uint256_t, NUM_LIMBS> bb::stdlib::bigfield< Builder, T >::neg_modulus_limbs_u256
staticconstexpr

◆ negative_prime_modulus

template<typename Builder , typename T >
constexpr uint512_t bb::stdlib::bigfield< Builder, T >::negative_prime_modulus = binary_basis.modulus - target_basis.modulus
staticconstexpr

Definition at line 340 of file bigfield.hpp.

◆ negative_prime_modulus_mod_binary_basis

template<typename Builder , typename T >
constexpr bb::fr bb::stdlib::bigfield< Builder, T >::negative_prime_modulus_mod_binary_basis = -bb::fr(uint256_t(modulus_u512))
staticconstexpr

Definition at line 339 of file bigfield.hpp.

◆ NUM_LAST_LIMB_BITS

template<typename Builder , typename T >
constexpr uint64_t bb::stdlib::bigfield< Builder, T >::NUM_LAST_LIMB_BITS = modulus_u512.get_msb() + 1 - (NUM_LIMB_BITS * 3)
staticconstexpr

Definition at line 311 of file bigfield.hpp.

◆ NUM_LIMB_BITS

template<typename Builder , typename T >
constexpr uint64_t bb::stdlib::bigfield< Builder, T >::NUM_LIMB_BITS = NUM_LIMB_BITS_IN_FIELD_SIMULATION
staticconstexpr

Definition at line 310 of file bigfield.hpp.

◆ NUM_LIMBS

template<typename Builder , typename T >
constexpr size_t bb::stdlib::bigfield< Builder, T >::NUM_LIMBS = 4
staticconstexpr

Definition at line 72 of file bigfield.hpp.

◆ prime_basis

template<typename Builder , typename T >
constexpr Basis bb::stdlib::bigfield< Builder, T >::prime_basis { uint512_t(bb::fr::modulus), bb::fr::modulus.get_msb() + 1 }
staticconstexpr

Definition at line 331 of file bigfield.hpp.

◆ prime_basis_limb

template<typename Builder , typename T >
field_t<Builder> bb::stdlib::bigfield< Builder, T >::prime_basis_limb
mutable

Represents a bigfield element in the prime basis: (a mod n) where n is the native modulus.

Definition at line 85 of file bigfield.hpp.

◆ prime_basis_maximum_limb

template<typename Builder , typename T >
constexpr uint256_t bb::stdlib::bigfield< Builder, T >::prime_basis_maximum_limb
staticconstexpr
Initial value:
=
static constexpr size_t NUM_LIMBS
Definition bigfield.hpp:72
static constexpr uint512_t modulus_u512
Definition bigfield.hpp:309

Definition at line 329 of file bigfield.hpp.

◆ PROHIBITED_LIMB_BITS

template<typename Builder , typename T >
constexpr uint64_t bb::stdlib::bigfield< Builder, T >::PROHIBITED_LIMB_BITS = MAX_UNREDUCED_LIMB_BITS + 5
staticconstexpr

Definition at line 932 of file bigfield.hpp.

◆ PUBLIC_INPUTS_SIZE

template<typename Builder , typename T >
constexpr size_t bb::stdlib::bigfield< Builder, T >::PUBLIC_INPUTS_SIZE = BIGFIELD_PUBLIC_INPUTS_SIZE
staticconstexpr

Definition at line 31 of file bigfield.hpp.

◆ shift_1

template<typename Builder , typename T >
constexpr bb::fr bb::stdlib::bigfield< Builder, T >::shift_1 = bb::fr(uint256_t(1) << NUM_LIMB_BITS)
staticconstexpr

Definition at line 334 of file bigfield.hpp.

◆ shift_2

template<typename Builder , typename T >
constexpr bb::fr bb::stdlib::bigfield< Builder, T >::shift_2 = bb::fr(uint256_t(1) << (NUM_LIMB_BITS * 2))
staticconstexpr

Definition at line 335 of file bigfield.hpp.

◆ shift_3

template<typename Builder , typename T >
constexpr bb::fr bb::stdlib::bigfield< Builder, T >::shift_3 = bb::fr(uint256_t(1) << (NUM_LIMB_BITS * 3))
staticconstexpr

Definition at line 336 of file bigfield.hpp.

◆ shift_right_1

template<typename Builder , typename T >
constexpr bb::fr bb::stdlib::bigfield< Builder, T >::shift_right_1 = bb::fr(1) / shift_1
staticconstexpr

Definition at line 337 of file bigfield.hpp.

◆ shift_right_2

template<typename Builder , typename T >
constexpr bb::fr bb::stdlib::bigfield< Builder, T >::shift_right_2 = bb::fr(1) / shift_2
staticconstexpr

Definition at line 338 of file bigfield.hpp.

◆ target_basis

template<typename Builder , typename T >
constexpr Basis bb::stdlib::bigfield< Builder, T >::target_basis { modulus_u512, static_cast<size_t>(modulus_u512.get_msb() + 1) }
staticconstexpr

Definition at line 333 of file bigfield.hpp.


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