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
33template <class Fq, class Fr, class Params> class alignas(32) element {
34 public:
35 static constexpr Fq curve_b = Params::b;
36
37 element() noexcept = default;
38
39 constexpr element(const Fq& a, const Fq& b, const Fq& c) noexcept;
40 constexpr element(const element& other) noexcept;
41 constexpr element(element&& other) noexcept;
42 constexpr element(const affine_element<Fq, Fr, Params>& other) noexcept;
43 ~element() noexcept = default;
44
45 static constexpr element one() noexcept { return { Params::one_x, Params::one_y, Fq::one() }; };
46 static constexpr element zero() noexcept
47 {
50 return zero;
51 };
52
53 constexpr element& operator=(const element& other) noexcept;
54 constexpr element& operator=(element&& other) noexcept;
55
56 constexpr operator affine_element<Fq, Fr, Params>() const noexcept;
57
58 static element random_element(numeric::RNG* engine = nullptr) noexcept;
59
60 constexpr element dbl() const noexcept;
61 constexpr void self_dbl() noexcept;
62
63 constexpr element operator+(const element& other) const noexcept;
64 constexpr element operator+(const affine_element<Fq, Fr, Params>& other) const noexcept;
65 constexpr element operator+=(const element& other) noexcept;
66 constexpr element operator+=(const affine_element<Fq, Fr, Params>& other) noexcept;
67
68 constexpr element operator-(const element& other) const noexcept;
69 constexpr element operator-(const affine_element<Fq, Fr, Params>& other) const noexcept;
70 constexpr element operator-() const noexcept;
71 constexpr element operator-=(const element& other) noexcept;
72 constexpr element operator-=(const affine_element<Fq, Fr, Params>& other) noexcept;
73
74 friend constexpr element operator+(const affine_element<Fq, Fr, Params>& left, const element& right) noexcept
75 {
76 return right + left;
77 }
78 friend constexpr element operator-(const affine_element<Fq, Fr, Params>& left, const element& right) noexcept
79 {
80 return -right + left;
81 }
82
83 element operator*(const Fr& exponent) const noexcept;
84 element operator*=(const Fr& exponent) noexcept;
85
86 // If you end up implementing this, congrats, you've solved the DL problem!
87 // P.S. This is a joke, don't even attempt! 😂
88 // constexpr Fr operator/(const element& other) noexcept {}
89
90 constexpr element normalize() const noexcept;
91 static element infinity();
92 BB_INLINE constexpr element set_infinity() const noexcept;
93 BB_INLINE constexpr void self_set_infinity() noexcept;
94 [[nodiscard]] BB_INLINE constexpr bool is_point_at_infinity() const noexcept;
95 [[nodiscard]] BB_INLINE constexpr bool on_curve() const noexcept;
96 BB_INLINE constexpr bool operator==(const element& other) const noexcept;
97
98 static void batch_normalize(element* elements, size_t num_elements) noexcept;
99 static void batch_affine_add(const std::span<affine_element<Fq, Fr, Params>>& first_group,
100 const std::span<affine_element<Fq, Fr, Params>>& second_group,
101 const std::span<affine_element<Fq, Fr, Params>>& results) noexcept;
103 const std::span<const affine_element<Fq, Fr, Params>>& points, const Fr& scalar) noexcept;
104
109 static affine_element<Fq, Fr, Params> batch_mul(std::span<const affine_element<Fq, Fr, Params>> points,
110 std::span<const Fr> scalars,
111 size_t max_num_bits = 0,
112 bool with_edgecases = true,
113 const Fr& masking_scalar = Fr(1)) noexcept
114 {
115 return affine_element<Fq, Fr, Params>::batch_mul(points, scalars, max_num_bits, with_edgecases, masking_scalar);
116 }
117
121
122 private:
123 // For test access to mul_without_endomorphism
124 friend class TestElementPrivate;
125 element mul_without_endomorphism(const Fr& scalar) const noexcept;
126 element mul_with_endomorphism(const Fr& scalar) const noexcept;
127
128 template <typename = typename std::enable_if<Params::can_hash_to_curve>>
130
131 friend std::ostream& operator<<(std::ostream& os, const element& a)
132 {
133 os << "{ " << a.x << ", " << a.y << ", " << a.z << " }";
134 return os;
135 }
136};
137
138template <class Fq, class Fr, class Params> std::ostream& operator<<(std::ostream& os, element<Fq, Fr, Params> const& e)
139{
140 return os << "x:" << e.x << " y:" << e.y << " z:" << e.z;
141}
142
143} // namespace bb::group_elements
144
145#include "./element_impl.hpp"
static affine_element batch_mul(std::span< const affine_element > points, std::span< const 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:33
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:46
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:78
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:45
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.
BB_INLINE constexpr bool on_curve() const noexcept
element operator*(const Fr &exponent) const noexcept
static constexpr Fq curve_b
Definition element.hpp:35
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< const 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:109
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:138
AffineElement const size_t Fq *scratch_space noexcept
STL namespace.
static constexpr field one()
curve::BN254::BaseField Fq