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
88 // If you end up implementing this, congrats, you've solved the DL problem!
89 // P.S. This is a joke, don't even attempt! 😂
90 // constexpr Fr operator/(const element& other) noexcept {}
91
92 constexpr element normalize() const noexcept;
93 static element infinity();
94 BB_INLINE constexpr element set_infinity() const noexcept;
95 BB_INLINE constexpr void self_set_infinity() noexcept;
96 [[nodiscard]] BB_INLINE constexpr bool is_point_at_infinity() const noexcept;
97 [[nodiscard]] BB_INLINE constexpr bool on_curve() const noexcept;
98 BB_INLINE constexpr bool operator==(const element& other) const noexcept;
99
100 static void batch_normalize(element* elements, size_t num_elements) noexcept;
101 static void batch_affine_add(const std::span<affine_element<Fq, Fr, Params>>& first_group,
102 const std::span<affine_element<Fq, Fr, Params>>& second_group,
103 const std::span<affine_element<Fq, Fr, Params>>& results) noexcept;
105 const std::span<const affine_element<Fq, Fr, Params>>& points, const Fr& scalar) noexcept;
106
111 static affine_element<Fq, Fr, Params> batch_mul(std::span<const affine_element<Fq, Fr, Params>> points,
112 std::span<Fr> scalars,
113 size_t max_num_bits = 0,
114 bool with_edgecases = true,
115 const Fr& masking_scalar = Fr(1)) noexcept
116 {
117 return affine_element<Fq, Fr, Params>::batch_mul(points, scalars, max_num_bits, with_edgecases, masking_scalar);
118 }
119
123
124 private:
125 // For test access to mul_without_endomorphism
126 friend class TestElementPrivate;
127 element mul_without_endomorphism(const Fr& scalar) const noexcept;
128 element mul_with_endomorphism(const Fr& scalar) const noexcept;
129
130 template <typename = typename std::enable_if<Params::can_hash_to_curve>>
132
133 friend std::ostream& operator<<(std::ostream& os, const element& a)
134 {
135 os << "{ " << a.x << ", " << a.y << ", " << a.z << " }";
136 return os;
137 }
138};
139
140template <class Fq, class Fr, class Params> std::ostream& operator<<(std::ostream& os, element<Fq, Fr, Params> const& e)
141{
142 return os << "x:" << e.x << " y:" << e.y << " z:" << e.z;
143}
144
145} // namespace bb::group_elements
146
147#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.
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:111
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:140
AffineElement const size_t Fq *scratch_space noexcept
STL namespace.
static constexpr field one()
curve::BN254::BaseField Fq