Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
bb::group_elements::element< Fq, Fr, Params > Class Template Reference

element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l More...

#include <element.hpp>

Public Member Functions

 element () noexcept=default
 
constexpr element (const Fq &a, const Fq &b, const Fq &c) noexcept
 
constexpr element (const element &other) noexcept
 
constexpr element (element &&other) noexcept
 
constexpr element (const affine_element< Fq, Fr, Params > &other) noexcept
 
 ~element () noexcept=default
 
constexpr elementoperator= (const element &other) noexcept
 
constexpr elementoperator= (element &&other) noexcept
 
constexpr operator affine_element< Fq, Fr, Params > () const noexcept
 
constexpr element dbl () const noexcept
 
constexpr void self_dbl () noexcept
 
constexpr element operator+ (const element &other) const noexcept
 
constexpr element operator+ (const affine_element< Fq, Fr, Params > &other) const noexcept
 
constexpr element operator+= (const element &other) noexcept
 
constexpr element operator+= (const affine_element< Fq, Fr, Params > &other) noexcept
 
constexpr element operator- (const element &other) const noexcept
 
constexpr element operator- (const affine_element< Fq, Fr, Params > &other) const noexcept
 
constexpr element operator- () const noexcept
 
constexpr element operator-= (const element &other) noexcept
 
constexpr element operator-= (const affine_element< Fq, Fr, Params > &other) noexcept
 
element operator* (const Fr &exponent) const noexcept
 
element operator*= (const Fr &exponent) noexcept
 
element mul_const_time (const Fr &scalar, numeric::RNG *engine=nullptr) const noexcept
 Constant-time scalar multiplication intended for secret scalars (e.g. ECDSA / Schnorr nonces).
 
constexpr element normalize () const noexcept
 
constexpr element normalize_const_time () const noexcept
 
constexpr affine_element< Fq, Fr, Paramsto_affine_const_time () const noexcept
 
BB_INLINE constexpr element set_infinity () const noexcept
 
BB_INLINE constexpr void self_set_infinity () noexcept
 
BB_INLINE constexpr bool is_point_at_infinity () const noexcept
 
BB_INLINE constexpr bool on_curve () const noexcept
 
BB_INLINE constexpr bool operator== (const element &other) const noexcept
 
template<typename >
element< Fq, Fr, T > random_coordinates_on_curve (numeric::RNG *engine) noexcept
 

Static Public Member Functions

static constexpr element one () noexcept
 
static constexpr element zero () noexcept
 
static element random_element (numeric::RNG *engine=nullptr) noexcept
 
static element infinity ()
 
static void batch_normalize (element *elements, size_t num_elements) noexcept
 
static void batch_affine_add (const std::span< affine_element< Fq, Fr, Params > > &first_group, const std::span< affine_element< Fq, Fr, Params > > &second_group, const std::span< affine_element< Fq, Fr, Params > > &results) noexcept
 Pairwise affine add points in first and second group.
 
static element straus_msm (std::span< const affine_element< Fq, Fr, Params > > points, std::span< const Fr > scalars) noexcept
 Straus-style multi-scalar multiplication.
 
static std::vector< affine_element< Fq, Fr, Params > > batch_mul_with_endomorphism (const std::span< const affine_element< Fq, Fr, Params > > &points, const Fr &scalar) noexcept
 Multiply each point by the same scalar.
 
static affine_element< Fq, Fr, Paramsbatch_mul (std::span< const affine_element< Fq, Fr, Params > > points, std::span< Fr > scalars, size_t max_num_bits=0, bool with_edgecases=true, const Fr &masking_scalar=Fr(1)) noexcept
 Multi-scalar multiplication: compute sum_i(scalars[i] * points[i])
 

Public Attributes

Fq x
 
Fq y
 
Fq z
 

Static Public Attributes

static constexpr Fq curve_b = Params::b
 

Private Member Functions

element mul_without_endomorphism (const Fr &scalar) const noexcept
 
element mul_with_endomorphism (const Fr &scalar) const noexcept
 

Static Private Member Functions

template<typename = typename std::enable_if<Params::can_hash_to_curve>>
static element random_coordinates_on_curve (numeric::RNG *engine=nullptr) noexcept
 

Friends

class TestElementPrivate
 
constexpr element operator+ (const affine_element< Fq, Fr, Params > &left, const element &right) noexcept
 
constexpr element operator- (const affine_element< Fq, Fr, Params > &left, const element &right) noexcept
 
std::ostream & operator<< (std::ostream &os, const element &a)
 

Detailed Description

template<class Fq, class Fr, class Params>
class bb::group_elements::element< Fq, Fr, Params >

element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-0.html#doubling-dbl-2009-l

Note: BN254 / Grumpkin G1 have cofactor 1, so on-curve membership coincides with prime-order subgroup membership. BN254 G2 has a non-trivial cofactor; an explicit subgroup check is provided by affine_element::is_in_prime_subgroup() and must be applied to externally-supplied G2 bytes (see bbapi). The arithmetic in this file does not rederive subgroup membership and assumes the caller already ensured operands are valid prime-order subgroup elements.

Template Parameters
Fqprime field the curve is defined over
Frprime field whose characteristic equals the size of the prime-order elliptic curve subgroup
Paramscurve parameters

Definition at line 35 of file element.hpp.

Constructor & Destructor Documentation

◆ element() [1/5]

template<class Fq , class Fr , class Params >
bb::group_elements::element< Fq, Fr, Params >::element ( )
defaultnoexcept

◆ element() [2/5]

template<class Fq , class Fr , class T >
constexpr bb::group_elements::element< Fq, Fr, T >::element ( const Fq a,
const Fq b,
const Fq c 
)
constexprnoexcept

Definition at line 19 of file element_impl.hpp.

◆ element() [3/5]

template<class Fq , class Fr , class T >
constexpr bb::group_elements::element< Fq, Fr, T >::element ( const element< Fq, Fr, Params > &  other)
constexprnoexcept

Definition at line 26 of file element_impl.hpp.

◆ element() [4/5]

template<class Fq , class Fr , class T >
constexpr bb::group_elements::element< Fq, Fr, T >::element ( element< Fq, Fr, Params > &&  other)
constexprnoexcept

Definition at line 33 of file element_impl.hpp.

◆ element() [5/5]

template<class Fq , class Fr , class Params >
constexpr bb::group_elements::element< Fq, Fr, Params >::element ( const affine_element< Fq, Fr, Params > &  other)
constexprnoexcept

◆ ~element()

template<class Fq , class Fr , class Params >
bb::group_elements::element< Fq, Fr, Params >::~element ( )
defaultnoexcept

Member Function Documentation

◆ batch_affine_add()

template<class Fq , class Fr , class Params >
void bb::group_elements::element< Fq, Fr, T >::batch_affine_add ( const std::span< affine_element< Fq, Fr, Params > > &  first_group,
const std::span< affine_element< Fq, Fr, Params > > &  second_group,
const std::span< affine_element< Fq, Fr, Params > > &  results 
)
staticnoexcept

Pairwise affine add points in first and second group.

Parameters
first_groupLeft-hand points
second_groupRight-hand points
resultsOutput array for results[i] = first_group[i] + second_group[i]
Warning
This function does NOT handle edge cases (point at infinity, point doubling, etc.). For generic point addition with edge case handling, use Jacobian coordinates instead. Only use this when you know points are in generic position (e.g., in Pippenger/MSM).

Definition at line 1260 of file element_impl.hpp.

◆ batch_mul()

template<class Fq , class Fr , class Params >
static affine_element< Fq, Fr, Params > bb::group_elements::element< Fq, Fr, Params >::batch_mul ( std::span< const affine_element< Fq, Fr, Params > >  points,
std::span< Fr scalars,
size_t  max_num_bits = 0,
bool  with_edgecases = true,
const Fr masking_scalar = Fr(1) 
)
inlinestaticnoexcept

Multi-scalar multiplication: compute sum_i(scalars[i] * points[i])

Delegates to affine_element::batch_mul. Provided for interface compatibility with stdlib.

Definition at line 143 of file element.hpp.

◆ batch_mul_with_endomorphism()

template<class Fq , class Fr , class Params >
std::vector< affine_element< Fq, Fr, T > > bb::group_elements::element< Fq, Fr, T >::batch_mul_with_endomorphism ( const std::span< const affine_element< Fq, Fr, Params > > &  points,
const Fr scalar 
)
staticnoexcept

Multiply each point by the same scalar.

We use the fact that all points are being multiplied by the same scalar to batch the operations (perform batch affine additions and doublings with batch inversion trick)

Parameters
pointsThe span of individual points that need to be scaled
scalarThe scalar we multiply all the points by
Returns
std::vector<affine_element<Fq, Fr, T>> Vector of new points where each point is exponent⋅points[i]

Definition at line 1295 of file element_impl.hpp.

◆ batch_normalize()

template<typename Fq , typename Fr , typename T >
void bb::group_elements::element< Fq, Fr, T >::batch_normalize ( element< Fq, Fr, Params > *  elements,
size_t  num_elements 
)
staticnoexcept

We now proceed to iterate back down the array of points. At each iteration we update the accumulator to contain the z-coordinate of the currently worked-upon z-coordinate. We can then multiply this accumulator with temporaries, to get a scalar that is equal to the inverse of the z-coordinate of the point at the next iteration cycle e.g. Imagine we have 4 points, such that:

accumulator = 1 / z.data[0]*z.data[1]*z.data[2]*z.data[3] temporaries[3] = z.data[0]*z.data[1]*z.data[2] temporaries[2] = z.data[0]*z.data[1] temporaries[1] = z.data[0] temporaries[0] = 1

At the first iteration, accumulator * temporaries[3] = z.data[0]*z.data[1]*z.data[2] / z.data[0]*z.data[1]*z.data[2]*z.data[3] = (1 / z.data[3]) We then update accumulator, such that:

accumulator = accumulator * z.data[3] = 1 / z.data[0]*z.data[1]*z.data[2]

At the second iteration, accumulator * temporaries[2] = z.data[0]*z.data[1] / z.data[0]*z.data[1]*z.data[2] = (1 z.data[2]) And so on, until we have computed every z-inverse!

We can then convert out of Jacobian form (x = X / Z^2, y = Y / Z^3) with 4 muls and 1 square.

Definition at line 1731 of file element_impl.hpp.

◆ dbl()

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::dbl ( ) const
constexprnoexcept

Definition at line 173 of file element_impl.hpp.

◆ infinity()

template<class Fq , class Fr , class T >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::infinity ( )
static

Definition at line 500 of file element_impl.hpp.

◆ is_point_at_infinity()

template<class Fq , class Fr , class T >
constexpr bool bb::group_elements::element< Fq, Fr, T >::is_point_at_infinity ( ) const
constexprnoexcept

Definition at line 534 of file element_impl.hpp.

◆ mul_const_time()

template<class Fq , class Fr , class T >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::mul_const_time ( const Fr scalar,
numeric::RNG engine = nullptr 
) const
noexcept

Constant-time scalar multiplication intended for secret scalars (e.g. ECDSA / Schnorr nonces).

Implementation: Montgomery ladder (Montgomery 1987 [1]; SCA-regular form: Joye & Yen, CHES 2002 [2]) over a fixed iteration count, with Coron's first DPA countermeasure (CHES 1999 [3]) applied to the scalar: k' = k + r * n for a fresh random 64-bit r sampled per call. Since n * P = O in the prime-order subgroup, k' * P = k * P; the randomization decorrelates the per-bit timing trace across signings with the same k.

[1] P. L. Montgomery, "Speeding the Pollard and Elliptic Curve Methods of Factorization", Mathematics of Computation 48 (1987), pp. 243-264. [2] M. Joye and S.-M. Yen, "The Montgomery Powering Ladder", CHES 2002, LNCS 2523, pp. 291-302. [3] J.-S. Coron, "Resistance against Differential Power Analysis for Elliptic Curve Cryptosystems", CHES 1999, LNCS 1717, pp. 292-302.

Parameters
engineOptional RNG for the blinding factor. If nullptr, uses the global RNG.
Warning
Slower than operator*. Use only when the scalar is secret. For public scalars (MSM, public arithmetic), prefer operator*.

Definition at line 425 of file element_impl.hpp.

◆ mul_with_endomorphism()

template<class Fq , class Fr , class T >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::mul_with_endomorphism ( const Fr scalar) const
privatenoexcept

Definition at line 669 of file element_impl.hpp.

◆ mul_without_endomorphism()

template<class Fq , class Fr , class T >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::mul_without_endomorphism ( const Fr scalar) const
privatenoexcept

Definition at line 610 of file element_impl.hpp.

◆ normalize()

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::normalize ( ) const
constexprnoexcept

Definition at line 488 of file element_impl.hpp.

◆ normalize_const_time()

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::normalize_const_time ( ) const
constexprnoexcept

Definition at line 495 of file element_impl.hpp.

◆ on_curve()

template<class Fq , class Fr , class T >
constexpr bool bb::group_elements::element< Fq, Fr, T >::on_curve ( ) const
constexprnoexcept

Definition at line 545 of file element_impl.hpp.

◆ one()

template<class Fq , class Fr , class Params >
static constexpr element bb::group_elements::element< Fq, Fr, Params >::one ( )
inlinestaticconstexprnoexcept

Definition at line 47 of file element.hpp.

◆ operator affine_element< Fq, Fr, Params >()

template<class Fq , class Fr , class Params >
constexpr bb::group_elements::element< Fq, Fr, Params >::operator affine_element< Fq, Fr, Params > ( ) const
constexprnoexcept

◆ operator*()

template<class Fq , class Fr , class T >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::operator* ( const Fr exponent) const
noexcept

Definition at line 410 of file element_impl.hpp.

◆ operator*=()

template<class Fq , class Fr , class T >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::operator*= ( const Fr exponent)
noexcept

Definition at line 418 of file element_impl.hpp.

◆ operator+() [1/2]

template<class Fq , class Fr , class Params >
constexpr element bb::group_elements::element< Fq, Fr, Params >::operator+ ( const affine_element< Fq, Fr, Params > &  other) const
constexprnoexcept

◆ operator+() [2/2]

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::operator+ ( const element< Fq, Fr, Params > &  other) const
constexprnoexcept

Definition at line 384 of file element_impl.hpp.

◆ operator+=() [1/2]

template<class Fq , class Fr , class Params >
constexpr element bb::group_elements::element< Fq, Fr, Params >::operator+= ( const affine_element< Fq, Fr, Params > &  other)
constexprnoexcept

◆ operator+=() [2/2]

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::operator+= ( const element< Fq, Fr, Params > &  other)
constexprnoexcept

Definition at line 295 of file element_impl.hpp.

◆ operator-() [1/3]

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::operator- ( ) const
constexprnoexcept

Definition at line 404 of file element_impl.hpp.

◆ operator-() [2/3]

template<class Fq , class Fr , class Params >
constexpr element bb::group_elements::element< Fq, Fr, Params >::operator- ( const affine_element< Fq, Fr, Params > &  other) const
constexprnoexcept

◆ operator-() [3/3]

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::operator- ( const element< Fq, Fr, Params > &  other) const
constexprnoexcept

Definition at line 398 of file element_impl.hpp.

◆ operator-=() [1/2]

template<class Fq , class Fr , class Params >
constexpr element bb::group_elements::element< Fq, Fr, Params >::operator-= ( const affine_element< Fq, Fr, Params > &  other)
constexprnoexcept

◆ operator-=() [2/2]

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::operator-= ( const element< Fq, Fr, Params > &  other)
constexprnoexcept

Definition at line 391 of file element_impl.hpp.

◆ operator=() [1/2]

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > & bb::group_elements::element< Fq, Fr, T >::operator= ( const element< Fq, Fr, Params > &  other)
constexprnoexcept

Definition at line 47 of file element_impl.hpp.

◆ operator=() [2/2]

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > & bb::group_elements::element< Fq, Fr, T >::operator= ( element< Fq, Fr, Params > &&  other)
constexprnoexcept

Definition at line 59 of file element_impl.hpp.

◆ operator==()

template<class Fq , class Fr , class T >
constexpr bool bb::group_elements::element< Fq, Fr, T >::operator== ( const element< Fq, Fr, Params > &  other) const
constexprnoexcept

Definition at line 566 of file element_impl.hpp.

◆ random_coordinates_on_curve() [1/2]

template<class Fq , class Fr , class Params >
template<typename >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, Params >::random_coordinates_on_curve ( numeric::RNG engine)
noexcept

Definition at line 1785 of file element_impl.hpp.

◆ random_coordinates_on_curve() [2/2]

template<class Fq , class Fr , class Params >
template<typename = typename std::enable_if<Params::can_hash_to_curve>>
static element bb::group_elements::element< Fq, Fr, Params >::random_coordinates_on_curve ( numeric::RNG engine = nullptr)
staticprivatenoexcept

◆ random_element()

template<class Fq , class Fr , class T >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::random_element ( numeric::RNG engine = nullptr)
staticnoexcept

Definition at line 593 of file element_impl.hpp.

◆ self_dbl()

template<class Fq , class Fr , class T >
constexpr void bb::group_elements::element< Fq, Fr, T >::self_dbl ( )
constexprnoexcept

Definition at line 104 of file element_impl.hpp.

◆ self_set_infinity()

template<class Fq , class Fr , class T >
constexpr void bb::group_elements::element< Fq, Fr, T >::self_set_infinity ( )
constexprnoexcept

Definition at line 514 of file element_impl.hpp.

◆ set_infinity()

template<class Fq , class Fr , class T >
constexpr element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::set_infinity ( ) const
constexprnoexcept

Definition at line 507 of file element_impl.hpp.

◆ straus_msm()

template<class Fq , class Fr , class Params >
element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::straus_msm ( std::span< const affine_element< Fq, Fr, Params > >  points,
std::span< const Fr scalars 
)
staticnoexcept

Straus-style multi-scalar multiplication.

Computes Σ_i scalars[i] * points[i], efficient when num points is small (~64 or less)

Definition at line 730 of file element_impl.hpp.

◆ to_affine_const_time()

template<class Fq , class Fr , class T >
constexpr affine_element< Fq, Fr, T > bb::group_elements::element< Fq, Fr, T >::to_affine_const_time ( ) const
constexprnoexcept

Definition at line 88 of file element_impl.hpp.

◆ zero()

template<class Fq , class Fr , class Params >
static constexpr element bb::group_elements::element< Fq, Fr, Params >::zero ( )
inlinestaticconstexprnoexcept

Definition at line 48 of file element.hpp.

Friends And Related Symbol Documentation

◆ operator+

template<class Fq , class Fr , class Params >
constexpr element operator+ ( const affine_element< Fq, Fr, Params > &  left,
const element< Fq, Fr, Params > &  right 
)
friend

Definition at line 76 of file element.hpp.

◆ operator-

template<class Fq , class Fr , class Params >
constexpr element operator- ( const affine_element< Fq, Fr, Params > &  left,
const element< Fq, Fr, Params > &  right 
)
friend

Definition at line 80 of file element.hpp.

◆ operator<<

template<class Fq , class Fr , class Params >
std::ostream & operator<< ( std::ostream &  os,
const element< Fq, Fr, Params > &  a 
)
friend

Definition at line 165 of file element.hpp.

◆ TestElementPrivate

template<class Fq , class Fr , class Params >
friend class TestElementPrivate
friend

Definition at line 158 of file element.hpp.

Member Data Documentation

◆ curve_b

template<class Fq , class Fr , class Params >
constexpr Fq bb::group_elements::element< Fq, Fr, Params >::curve_b = Params::b
staticconstexpr

Definition at line 37 of file element.hpp.

◆ x

template<class Fq , class Fr , class Params >
Fq bb::group_elements::element< Fq, Fr, Params >::x

Definition at line 152 of file element.hpp.

◆ y

template<class Fq , class Fr , class Params >
Fq bb::group_elements::element< Fq, Fr, Params >::y

Definition at line 153 of file element.hpp.

◆ z

template<class Fq , class Fr , class Params >
Fq bb::group_elements::element< Fq, Fr, Params >::z

Definition at line 154 of file element.hpp.


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