Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
field6.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
9
10namespace bb {
11template <typename base_field, typename Fq6Params> class field6 {
12 public:
13 constexpr field6(const base_field& a = base_field::zero(),
14 const base_field& b = base_field::zero(),
15 const base_field& c = base_field::zero())
16 : c0(a)
17 , c1(b)
18 , c2(c)
19 {}
20
21 constexpr field6(const field6& other)
22 : c0(other.c0)
23 , c1(other.c1)
24 , c2(other.c2)
25 {}
26
27 constexpr field6(field6&& other) noexcept
28 : c0(other.c0)
29 , c1(other.c1)
30 , c2(other.c2)
31 {}
32
33 constexpr field6& operator=(const field6& other) noexcept
34 {
35 if (this == &other) {
36 return *this;
37 }
38 c0 = other.c0;
39 c1 = other.c1;
40 c2 = other.c2;
41 return *this;
42 }
43
44 constexpr field6& operator=(field6&& other) noexcept
45 {
46 c0 = other.c0;
47 c1 = other.c1;
48 c2 = other.c2;
49 return *this;
50 }
51
52 constexpr ~field6() noexcept = default;
53
54 base_field c0;
55 base_field c1;
56 base_field c2;
57
58 static constexpr field6 zero() { return { base_field::zero(), base_field::zero(), base_field::zero() }; };
59 static constexpr field6 one() { return { base_field::one(), base_field::zero(), base_field::zero() }; };
60
61 static constexpr base_field mul_by_non_residue(const base_field& a) { return Fq6Params::mul_by_non_residue(a); }
62
63 constexpr field6 operator+(const field6& other) const
64 {
65 return {
66 c0 + other.c0,
67 c1 + other.c1,
68 c2 + other.c2,
69 };
70 }
71
72 constexpr field6 operator-(const field6& other) const
73 {
74 return {
75 c0 - other.c0,
76 c1 - other.c1,
77 c2 - other.c2,
78 };
79 }
80
81 constexpr field6 operator-() const
82 {
83 return {
84 -c0,
85 -c1,
86 -c2,
87 };
88 }
89
90 constexpr field6 operator*(const field6& other) const
91 {
92 // /* Devegili OhEig Scott Dahab --- Multiplication and Squaring on Pairing-Friendly Fields.pdf; Section 4
93 // * (Karatsuba) */
94
95 base_field T0 = c0 * other.c0;
96 base_field T1 = c1 * other.c1;
97 base_field T2 = c2 * other.c2;
98
99 base_field T3 = (c0 + c2) * (other.c0 + other.c2);
100 base_field T4 = (c0 + c1) * (other.c0 + other.c1);
101 base_field T5 = (c1 + c2) * (other.c1 + other.c2);
102
103 return {
104 T0 + mul_by_non_residue(T5 - (T1 + T2)),
105 T4 - (T0 + T1) + mul_by_non_residue(T2),
106 T3 + T1 - (T0 + T2),
107 };
108 }
109
110 constexpr field6 operator/(const field6& other) const { return operator*(other.invert()); }
111
112 constexpr field6 sqr() const
113 {
114 /* Devegili OhEig Scott Dahab --- Multiplication and Squaring on Pairing-Friendly Fields.pdf; Section 4
115 * (CH-SQR2) */
116 base_field S0 = c0.sqr();
117 base_field S1 = c0 * c1;
118 S1 += S1;
119 base_field S2 = (c0 + c2 - c1).sqr();
120 base_field S3 = c1 * c2;
121 S3 += S3;
122 base_field S4 = c2.sqr();
123 return {
124 mul_by_non_residue(S3) + S0,
125 mul_by_non_residue(S4) + S1,
126 S1 + S2 + S3 - S0 - S4,
127 };
128 }
129
130 constexpr field6 operator+=(const field6& other)
131 {
132 c0 += other.c0;
133 c1 += other.c1;
134 c2 += other.c2;
135 return *this;
136 }
137
138 constexpr field6 operator-=(const field6& other)
139 {
140 c0 -= other.c0;
141 c1 -= other.c1;
142 c2 -= other.c2;
143 return *this;
144 }
145
146 constexpr field6 operator*=(const field6& other)
147 {
148 *this = operator*(other);
149 return *this;
150 }
151
152 constexpr field6 operator/=(const field6& other)
153 {
154 *this = operator/(other);
155 return *this;
156 }
157
158 constexpr field6 invert() const
159 {
160 /* From "High-Speed Software Implementation of the Optimal Ate Pairing over Barreto-Naehrig Curves"; Algorithm
161 * 17 */
162 base_field C0 = c0.sqr() - mul_by_non_residue(c1 * c2);
163 base_field C1 = mul_by_non_residue(c2.sqr()) - (c0 * c1);
164 base_field C2 = c1.sqr() - (c0 * c2);
165 base_field T0 = ((c0 * C0) + mul_by_non_residue((c2 * C1) + (c1 * C2))).invert();
166
167 return {
168 T0 * C0,
169 T0 * C1,
170 T0 * C2,
171 };
172 }
173
174 constexpr field6 mul_by_fq2(const base_field& other) const { return { other * c0, other * c1, other * c2 }; }
175
176 constexpr field6 frobenius_map_three() const
177 {
178 return {
179 c0.frobenius_map(),
180 Fq6Params::frobenius_coeffs_c1_3 * c1.frobenius_map(),
181 Fq6Params::frobenius_coeffs_c2_3 * c2.frobenius_map(),
182 };
183 }
184
185 constexpr field6 frobenius_map_two() const
186 {
187 return { c0, Fq6Params::frobenius_coeffs_c1_2 * c1, Fq6Params::frobenius_coeffs_c2_2 * c2 };
188 }
189
190 constexpr field6 frobenius_map_one() const
191 {
192 return {
193 c0.frobenius_map(),
194 Fq6Params::frobenius_coeffs_c1_1 * c1.frobenius_map(),
195 Fq6Params::frobenius_coeffs_c2_1 * c2.frobenius_map(),
196 };
197 }
198
199 static constexpr field6 random_element(numeric::RNG* engine = nullptr)
200 {
201 return {
202 base_field::random_element(engine),
203 base_field::random_element(engine),
204 base_field::random_element(engine),
205 };
206 }
207
208 constexpr field6 to_montgomery_form() const
209 {
210 return {
214 };
215 }
216
217 constexpr field6 from_montgomery_form() const
218 {
219 return {
223 };
224 }
225
226 [[nodiscard]] constexpr bool is_zero() const { return c0.is_zero() && c1.is_zero() && c2.is_zero(); }
227
228 constexpr bool operator==(const field6& other) const { return c0 == other.c0 && c1 == other.c1 && c2 == other.c2; }
229};
230} // namespace bb
constexpr bool operator==(const field6 &other) const
Definition field6.hpp:228
base_field c2
Definition field6.hpp:56
constexpr field6 frobenius_map_one() const
Definition field6.hpp:190
base_field c1
Definition field6.hpp:55
constexpr field6 operator-() const
Definition field6.hpp:81
constexpr field6 mul_by_fq2(const base_field &other) const
Definition field6.hpp:174
base_field c0
Definition field6.hpp:54
constexpr field6 sqr() const
Definition field6.hpp:112
static constexpr base_field mul_by_non_residue(const base_field &a)
Definition field6.hpp:61
constexpr field6 operator-(const field6 &other) const
Definition field6.hpp:72
constexpr field6 frobenius_map_two() const
Definition field6.hpp:185
constexpr field6 & operator=(field6 &&other) noexcept
Definition field6.hpp:44
constexpr field6(const field6 &other)
Definition field6.hpp:21
constexpr field6 to_montgomery_form() const
Definition field6.hpp:208
static constexpr field6 one()
Definition field6.hpp:59
constexpr field6 operator-=(const field6 &other)
Definition field6.hpp:138
constexpr field6 operator*(const field6 &other) const
Definition field6.hpp:90
constexpr bool is_zero() const
Definition field6.hpp:226
constexpr field6 frobenius_map_three() const
Definition field6.hpp:176
constexpr field6 operator+(const field6 &other) const
Definition field6.hpp:63
constexpr field6 operator/(const field6 &other) const
Definition field6.hpp:110
static constexpr field6 random_element(numeric::RNG *engine=nullptr)
Definition field6.hpp:199
constexpr field6 operator/=(const field6 &other)
Definition field6.hpp:152
constexpr field6 operator+=(const field6 &other)
Definition field6.hpp:130
constexpr field6 invert() const
Definition field6.hpp:158
constexpr field6 & operator=(const field6 &other) noexcept
Definition field6.hpp:33
constexpr field6(field6 &&other) noexcept
Definition field6.hpp:27
static constexpr field6 zero()
Definition field6.hpp:58
constexpr ~field6() noexcept=default
constexpr field6 from_montgomery_form() const
Definition field6.hpp:217
constexpr field6(const base_field &a=base_field::zero(), const base_field &b=base_field::zero(), const base_field &c=base_field::zero())
Definition field6.hpp:13
constexpr field6 operator*=(const field6 &other)
Definition field6.hpp:146
FF a
FF b
numeric::RNG & engine
Entry point for Barretenberg command-line interface.