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: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
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 constexpr void self_mixed_add_or_sub(const affine_element<Fq, Fr, Params>& other, uint64_t predicate) noexcept;
63
64 constexpr element operator+(const element& other) const noexcept;
65 constexpr element operator+(const affine_element<Fq, Fr, Params>& other) const noexcept;
66 constexpr element operator+=(const element& other) noexcept;
67 constexpr element operator+=(const affine_element<Fq, Fr, Params>& other) noexcept;
68
69 constexpr element operator-(const element& other) const noexcept;
70 constexpr element operator-(const affine_element<Fq, Fr, Params>& other) const noexcept;
71 constexpr element operator-() const noexcept;
72 constexpr element operator-=(const element& other) noexcept;
73 constexpr element operator-=(const affine_element<Fq, Fr, Params>& other) noexcept;
74
75 friend constexpr element operator+(const affine_element<Fq, Fr, Params>& left, const element& right) noexcept
76 {
77 return right + left;
78 }
79 friend constexpr element operator-(const affine_element<Fq, Fr, Params>& left, const element& right) noexcept
80 {
81 return -right + left;
82 }
83
84 element operator*(const Fr& exponent) const noexcept;
85 element operator*=(const Fr& exponent) noexcept;
86
87 // If you end up implementing this, congrats, you've solved the DL problem!
88 // P.S. This is a joke, don't even attempt! 😂
89 // constexpr Fr operator/(const element& other) noexcept {}
90
91 constexpr element normalize() const noexcept;
92 static element infinity();
93 BB_INLINE constexpr element set_infinity() const noexcept;
94 BB_INLINE constexpr void self_set_infinity() noexcept;
95 [[nodiscard]] BB_INLINE constexpr bool is_point_at_infinity() const noexcept;
96 [[nodiscard]] BB_INLINE constexpr bool on_curve() const noexcept;
97 BB_INLINE constexpr bool operator==(const element& other) const noexcept;
98
99 static void batch_normalize(element* elements, size_t num_elements) noexcept;
100 static void batch_affine_add(const std::span<affine_element<Fq, Fr, Params>>& first_group,
101 const std::span<affine_element<Fq, Fr, Params>>& second_group,
102 const std::span<affine_element<Fq, Fr, Params>>& results) noexcept;
104 const std::span<const affine_element<Fq, Fr, Params>>& points, const Fr& scalar) noexcept;
105
109
110 private:
111 // For test access to mul_without_endomorphism
112 friend class TestElementPrivate;
113 element mul_without_endomorphism(const Fr& scalar) const noexcept;
114 element mul_with_endomorphism(const Fr& scalar) const noexcept;
115
116 template <typename = typename std::enable_if<Params::can_hash_to_curve>>
117 static element random_coordinates_on_curve(numeric::RNG* engine = nullptr) noexcept;
118 // {
119 // bool found_one = false;
120 // Fq yy;
121 // Fq x;
122 // Fq y;
123 // Fq t0;
124 // while (!found_one) {
125 // x = Fq::random_element(engine);
126 // yy = x.sqr() * x + Params::b;
127 // if constexpr (Params::has_a) {
128 // yy += (x * Params::a);
129 // }
130 // y = yy.sqrt();
131 // t0 = y.sqr();
132 // found_one = (yy == t0);
133 // }
134 // return { x, y, Fq::one() };
135 // }
136 // TODO(https://github.com/AztecProtocol/barretenberg/issues/908) point at inifinty isn't handled
137 // To reenable this do NOT do use MSGPACK_FIELDS macro below, instead follow the logic in affine_element
138 // MSGPACK_FIELDS(x, y, z);
139
140 static void conditional_negate_affine(const affine_element<Fq, Fr, Params>& in,
141 affine_element<Fq, Fr, Params>& out,
142 uint64_t predicate) noexcept;
143
144 friend std::ostream& operator<<(std::ostream& os, const element& a)
145 {
146 os << "{ " << a.x << ", " << a.y << ", " << a.z << " }";
147 return os;
148 }
149};
150
151template <class Fq, class Fr, class Params> std::ostream& operator<<(std::ostream& os, element<Fq, Fr, Params> const& e)
152{
153 return os << "x:" << e.x << " y:" << e.y << " z:" << e.z;
154}
155
156// constexpr element<Fq, Fr, Params>::one = element<Fq, Fr, Params>{ Params::one_x, Params::one_y, Fq::one() };
157// constexpr element<Fq, Fr, Params>::point_at_infinity = one.set_infinity();
158// constexpr element<Fq, Fr, Params>::curve_b = Params::b;
159} // namespace bb::group_elements
160
161#include "./element_impl.hpp"
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:79
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
constexpr void self_mixed_add_or_sub(const affine_element< Fq, Fr, Params > &other, uint64_t predicate) noexcept
static constexpr Fq curve_b
Definition element.hpp:35
element() noexcept=default
static void conditional_negate_affine(const affine_element< Fq, Fr, Params > &in, affine_element< Fq, Fr, Params > &out, uint64_t predicate) noexcept
static element random_coordinates_on_curve(numeric::RNG *engine=nullptr) noexcept
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:151
STL namespace.
static constexpr field one()