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 <array>
15#include <random>
16#include <span>
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 constexpr element normalize_const_time() const noexcept;
118 static element infinity();
119 BB_INLINE constexpr element set_infinity() const noexcept;
120 BB_INLINE constexpr void self_set_infinity() noexcept;
121 [[nodiscard]] BB_INLINE constexpr bool is_point_at_infinity() const noexcept;
122 [[nodiscard]] BB_INLINE constexpr bool on_curve() const noexcept;
123 BB_INLINE constexpr bool operator==(const element& other) const noexcept;
124
125 static void batch_normalize(element* elements, size_t num_elements) noexcept;
126 static void batch_affine_add(const std::span<affine_element<Fq, Fr, Params>>& first_group,
127 const std::span<affine_element<Fq, Fr, Params>>& second_group,
128 const std::span<affine_element<Fq, Fr, Params>>& results) noexcept;
129
134 static element straus_msm(std::span<const affine_element<Fq, Fr, Params>> points,
135 std::span<const Fr> scalars) noexcept;
137 const std::span<const affine_element<Fq, Fr, Params>>& points, const Fr& scalar) noexcept;
138
143 static affine_element<Fq, Fr, Params> batch_mul(std::span<const affine_element<Fq, Fr, Params>> points,
144 std::span<Fr> scalars,
145 size_t max_num_bits = 0,
146 bool with_edgecases = true,
147 const Fr& masking_scalar = Fr(1)) noexcept
148 {
149 return affine_element<Fq, Fr, Params>::batch_mul(points, scalars, max_num_bits, with_edgecases, masking_scalar);
150 }
151
155
156 private:
157 // For test access to mul_without_endomorphism
158 friend class TestElementPrivate;
159 element mul_without_endomorphism(const Fr& scalar) const noexcept;
160 element mul_with_endomorphism(const Fr& scalar) const noexcept;
161
162 template <typename = typename std::enable_if<Params::can_hash_to_curve>>
164
165 friend std::ostream& operator<<(std::ostream& os, const element& a)
166 {
167 os << "{ " << a.x << ", " << a.y << ", " << a.z << " }";
168 return os;
169 }
170};
171
172template <class Fq, class Fr, class Params> std::ostream& operator<<(std::ostream& os, element<Fq, Fr, Params> const& e)
173{
174 return os << "x:" << e.x << " y:" << e.y << " z:" << e.z;
175}
176
177} // namespace bb::group_elements
178
179#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.
constexpr affine_element< Fq, Fr, Params > to_affine_const_time() const noexcept
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
static element straus_msm(std::span< const affine_element< Fq, Fr, Params > > points, std::span< const Fr > scalars) noexcept
Straus-style multi-scalar multiplication.
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:143
element mul_without_endomorphism(const Fr &scalar) const noexcept
constexpr element & operator=(const element &other) noexcept
BB_INLINE constexpr void self_set_infinity() noexcept
constexpr element normalize_const_time() const noexcept
BB_INLINE constexpr bool is_point_at_infinity() const noexcept
#define BB_INLINE
FF a
FF b
numeric::RNG & engine
crypto::Poseidon2Bn254ScalarFieldParams Params
AffineElement const size_t Fq *scratch_space noexcept
std::ostream & operator<<(std::ostream &os, element< Fq, Fr, Params > const &e)
Definition element.hpp:172
STL namespace.
grumpkin::fq Fq
static constexpr field one()