Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
element.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
9#include "affine_element.hpp"
14#include "wnaf.hpp"
15#include <array>
16#include <random>
17#include <vector>
18
19namespace bb::group_elements {
20
35template <class Fq, class Fr, class Params> class alignas(32) element {
36 public:
37 static constexpr Fq curve_b = Params::b;
38
39 element() noexcept = default;
40
41 constexpr element(const Fq& a, const Fq& b, const Fq& c) noexcept;
42 constexpr element(const element& other) noexcept;
43 constexpr element(element&& other) noexcept;
44 constexpr element(const affine_element<Fq, Fr, Params>& other) noexcept;
45 ~element() noexcept = default;
46
47 static constexpr element one() noexcept { return { Params::one_x, Params::one_y, Fq::one() }; };
48 static constexpr element zero() noexcept
49 {
52 return zero;
53 };
54
55 constexpr element& operator=(const element& other) noexcept;
56 constexpr element& operator=(element&& other) noexcept;
57
58 constexpr operator affine_element<Fq, Fr, Params>() const noexcept;
59
60 static element random_element(numeric::RNG* engine = nullptr) noexcept;
61
62 constexpr element dbl() const noexcept;
63 constexpr void self_dbl() noexcept;
64
65 constexpr element operator+(const element& other) const noexcept;
66 constexpr element operator+(const affine_element<Fq, Fr, Params>& other) const noexcept;
67 constexpr element operator+=(const element& other) noexcept;
68 constexpr element operator+=(const affine_element<Fq, Fr, Params>& other) noexcept;
69
70 constexpr element operator-(const element& other) const noexcept;
71 constexpr element operator-(const affine_element<Fq, Fr, Params>& other) const noexcept;
72 constexpr element operator-() const noexcept;
73 constexpr element operator-=(const element& other) noexcept;
74 constexpr element operator-=(const affine_element<Fq, Fr, Params>& other) noexcept;
75
76 friend constexpr element operator+(const affine_element<Fq, Fr, Params>& left, const element& right) noexcept
77 {
78 return right + left;
79 }
80 friend constexpr element operator-(const affine_element<Fq, Fr, Params>& left, const element& right) noexcept
81 {
82 return -right + left;
83 }
84
85 element operator*(const Fr& exponent) const noexcept;
86 element operator*=(const Fr& exponent) noexcept;
87
109 element mul_const_time(const Fr& scalar, numeric::RNG* engine = nullptr) const noexcept;
110
111 // If you end up implementing this, congrats, you've solved the DL problem!
112 // P.S. This is a joke, don't even attempt! 😂
113 // constexpr Fr operator/(const element& other) noexcept {}
114
115 constexpr element normalize() const noexcept;
116 static element infinity();
117 BB_INLINE constexpr element set_infinity() const noexcept;
118 BB_INLINE constexpr void self_set_infinity() noexcept;
119 [[nodiscard]] BB_INLINE constexpr bool is_point_at_infinity() const noexcept;
120 [[nodiscard]] BB_INLINE constexpr bool on_curve() const noexcept;
121 BB_INLINE constexpr bool operator==(const element& other) const noexcept;
122
123 static void batch_normalize(element* elements, size_t num_elements) noexcept;
124 static void batch_affine_add(const std::span<affine_element<Fq, Fr, Params>>& first_group,
125 const std::span<affine_element<Fq, Fr, Params>>& second_group,
126 const std::span<affine_element<Fq, Fr, Params>>& results) noexcept;
128 const std::span<const affine_element<Fq, Fr, Params>>& points, const Fr& scalar) noexcept;
129
134 static affine_element<Fq, Fr, Params> batch_mul(std::span<const affine_element<Fq, Fr, Params>> points,
135 std::span<Fr> scalars,
136 size_t max_num_bits = 0,
137 bool with_edgecases = true,
138 const Fr& masking_scalar = Fr(1)) noexcept
139 {
140 return affine_element<Fq, Fr, Params>::batch_mul(points, scalars, max_num_bits, with_edgecases, masking_scalar);
141 }
142
146
147 private:
148 // For test access to mul_without_endomorphism
149 friend class TestElementPrivate;
150 element mul_without_endomorphism(const Fr& scalar) const noexcept;
151 element mul_with_endomorphism(const Fr& scalar) const noexcept;
152
153 template <typename = typename std::enable_if<Params::can_hash_to_curve>>
155
156 friend std::ostream& operator<<(std::ostream& os, const element& a)
157 {
158 os << "{ " << a.x << ", " << a.y << ", " << a.z << " }";
159 return os;
160 }
161};
162
163template <class Fq, class Fr, class Params> std::ostream& operator<<(std::ostream& os, element<Fq, Fr, Params> const& e)
164{
165 return os << "x:" << e.x << " y:" << e.y << " z:" << e.z;
166}
167
168} // namespace bb::group_elements
169
170#include "./element_impl.hpp"
static affine_element batch_mul(std::span< const affine_element > 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])
element class. Implements ecc group arithmetic using Jacobian coordinates See https://hyperelliptic....
Definition element.hpp:35
element operator*=(const Fr &exponent) noexcept
BB_INLINE constexpr element set_infinity() const noexcept
element mul_with_endomorphism(const Fr &scalar) const noexcept
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 constexpr element zero() noexcept
Definition element.hpp:48
constexpr element dbl() const noexcept
constexpr element normalize() const noexcept
friend constexpr element operator-(const affine_element< Fq, Fr, Params > &left, const element &right) noexcept
Definition element.hpp:80
constexpr void self_dbl() noexcept
static element random_element(numeric::RNG *engine=nullptr) noexcept
static void batch_normalize(element *elements, size_t num_elements) noexcept
static constexpr element one() noexcept
Definition element.hpp:47
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.
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).
BB_INLINE constexpr bool on_curve() const noexcept
element operator*(const Fr &exponent) const noexcept
static constexpr Fq curve_b
Definition element.hpp:37
element() noexcept=default
static element random_coordinates_on_curve(numeric::RNG *engine=nullptr) noexcept
static affine_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)) noexcept
Multi-scalar multiplication: compute sum_i(scalars[i] * points[i])
Definition element.hpp:134
element mul_without_endomorphism(const Fr &scalar) const noexcept
constexpr element & operator=(const element &other) noexcept
BB_INLINE constexpr void self_set_infinity() noexcept
BB_INLINE constexpr bool is_point_at_infinity() const noexcept
#define BB_INLINE
FF a
FF b
numeric::RNG & engine
crypto::Poseidon2Bn254ScalarFieldParams Params
std::ostream & operator<<(std::ostream &os, element< Fq, Fr, Params > const &e)
Definition element.hpp:163
AffineElement const size_t Fq *scratch_space noexcept
STL namespace.
grumpkin::fq Fq
static constexpr field one()