-
Notifications
You must be signed in to change notification settings - Fork 14
/
fp3.hpp
301 lines (243 loc) · 15.2 KB
/
fp3.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//---------------------------------------------------------------------------//
// Copyright (c) 2020-2021 Mikhail Komarov <nemo@nil.foundation>
// Copyright (c) 2020-2021 Nikita Kaskov <nbering@nil.foundation>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//
#ifndef CRYPTO3_ALGEBRA_FIELDS_ELEMENT_FP3_HPP
#define CRYPTO3_ALGEBRA_FIELDS_ELEMENT_FP3_HPP
#include <nil/crypto3/algebra/fields/detail/exponentiation.hpp>
#include <nil/crypto3/algebra/fields/detail/element/operations.hpp>
namespace nil {
namespace crypto3 {
namespace algebra {
namespace fields {
namespace detail {
template<typename FieldParams>
class element_fp3 {
public:
typedef FieldParams policy_type;
typedef typename policy_type::integral_type integral_type;
constexpr static const integral_type modulus = policy_type::modulus;
typedef typename policy_type::field_type field_type;
typedef typename policy_type::non_residue_type non_residue_type;
constexpr static non_residue_type non_residue = policy_type::non_residue;
typedef typename policy_type::underlying_type underlying_type;
using data_type = std::array<underlying_type, 3>;
data_type data;
constexpr element_fp3() = default;
template<typename Number1, typename Number2, typename Number3,
typename std::enable_if<std::is_integral<Number1>::value && std::is_integral<Number2>::value && std::is_integral<Number3>::value, bool>::type* = true>
constexpr element_fp3(const Number1 &in_data0,
const Number2 &in_data1,
const Number3 &in_data2)
: data({underlying_type(in_data0), underlying_type(in_data1), underlying_type(in_data2)})
{}
constexpr element_fp3(const data_type &in_data)
: data({in_data[0], in_data[1], in_data[2]}) {}
constexpr element_fp3(const underlying_type &in_data0,
const underlying_type &in_data1,
const underlying_type &in_data2)
: data({in_data0, in_data1, in_data2}) {}
constexpr element_fp3(const element_fp3 &B) : data(B.data) {};
constexpr element_fp3(const element_fp3 &&B) BOOST_NOEXCEPT : data(std::move(B.data)) {};
// Creating a zero is a fairly slow operation and is called very often, so we must return a
// reference to the same static object every time.
constexpr static const element_fp3& zero();
constexpr static const element_fp3& one();
constexpr bool is_zero() const {
return *this == zero();
}
constexpr bool is_one() const {
return *this == one();
}
constexpr bool operator==(const element_fp3 &B) const {
return (data[0] == B.data[0]) && (data[1] == B.data[1]) && (data[2] == B.data[2]);
}
constexpr bool operator!=(const element_fp3 &B) const {
return (data[0] != B.data[0]) || (data[1] != B.data[1]) || (data[2] != B.data[2]);
}
constexpr element_fp3 &operator=(const element_fp3 &B) {
data[0] = B.data[0];
data[1] = B.data[1];
data[2] = B.data[2];
return *this;
}
constexpr element_fp3 operator+(const element_fp3 &B) const {
return element_fp3(data[0] + B.data[0], data[1] + B.data[1], data[2] + B.data[2]);
}
constexpr element_fp3 doubled() const {
return element_fp3(data[0].doubled(), data[1].doubled(), data[2].doubled());
}
constexpr element_fp3 operator-(const element_fp3 &B) const {
return element_fp3(data[0] - B.data[0], data[1] - B.data[1], data[2] - B.data[2]);
}
constexpr element_fp3 &operator-=(const element_fp3 &B) {
data[0] -= B.data[0];
data[1] -= B.data[1];
data[2] -= B.data[2];
return *this;
}
constexpr element_fp3 &operator+=(const element_fp3 &B) {
data[0] += B.data[0];
data[1] += B.data[1];
data[2] += B.data[2];
return *this;
}
constexpr element_fp3 operator-() const {
return zero() - *this;
}
constexpr element_fp3 operator*(const element_fp3 &B) const {
const underlying_type A0B0 = data[0] * B.data[0], A1B1 = data[1] * B.data[1],
A2B2 = data[2] * B.data[2];
return element_fp3(
A0B0 + non_residue * ((data[1] + data[2]) * (B.data[1] + B.data[2]) - A1B1 - A2B2),
(data[0] + data[1]) * (B.data[0] + B.data[1]) - A0B0 - A1B1 + non_residue * A2B2,
(data[0] + data[2]) * (B.data[0] + B.data[2]) - A0B0 + A1B1 - A2B2);
}
constexpr element_fp3& operator*=(const element_fp3 &B) {
const underlying_type A0B0 = data[0] * B.data[0], A1B1 = data[1] * B.data[1],
A2B2 = data[2] * B.data[2];
const underlying_type
r0 = A0B0 + non_residue * ((data[1] + data[2]) * (B.data[1] + B.data[2]) - A1B1 - A2B2),
r1 = (data[0] + data[1]) * (B.data[0] + B.data[1]) - A0B0 - A1B1 + non_residue * A2B2,
r2 = (data[0] + data[2]) * (B.data[0] + B.data[2]) - A0B0 + A1B1 - A2B2;
data[0] = r0;
data[1] = r1;
data[2] = r2;
return *this;
}
constexpr element_fp3 sqrt() const {
std::size_t v = policy_type::s;
element_fp3 z(policy_type::nqr_to_t[0], policy_type::nqr_to_t[1], policy_type::nqr_to_t[2]);
element_fp3 w = this->pow(policy_type::t_minus_1_over_2);
element_fp3 x((*this) * w);
element_fp3 b = x * w; // b = (*this)^t
// compute square root with Tonelli--Shanks
// (does not terminate if not a square!)
while (!b.is_one()) {
std::size_t m = 0;
element_fp3 b2m = b;
while (!b2m.is_one()) {
/* invariant: b2m = b^(2^m) after entering this loop */
b2m = b2m.squared();
m += 1;
}
int j = v - m - 1;
w = z;
while (j > 0) {
w = w.squared();
--j;
} // w = z^2^(v-m-1)
z = w.squared();
b = b * z;
x = x * w;
v = m;
}
return x;
}
constexpr element_fp3 squared() const {
return (*this) * (*this); // maybe can be done more effective
}
constexpr bool is_square() const {
element_fp3 tmp = this->pow(field_type::extension_policy::group_order_minus_one_half);
return (tmp.is_one() || tmp.is_zero()); // maybe can be done more effective
}
template<typename PowerType>
constexpr element_fp3 pow(const PowerType &pwr) const {
return element_fp3(power(*this, pwr));
}
constexpr element_fp3 inversed() const {
/* From "High-Speed Software Implementation of the Optimal Ate Pairing over Barreto-Naehrig
* Curves"; Algorithm 17 */
const underlying_type &A0 = data[0], &A1 = data[1], &A2 = data[2];
const underlying_type t0 = A0.squared();
const underlying_type t1 = A1.squared();
const underlying_type t2 = A2.squared();
const underlying_type t3 = A0 * A1;
const underlying_type t4 = A0 * A2;
const underlying_type t5 = A1 * A2;
const underlying_type c0 = t0 - non_residue * t5;
const underlying_type c1 = non_residue * t2 - t3;
const underlying_type c2 =
t1 - t4; // typo in paper referenced above. should be "-" as per Scott, but is "*"
const underlying_type t6 = (A0 * c0 + non_residue * (A2 * c1 + A1 * c2)).inversed();
return element_fp3(t6 * c0, t6 * c1, t6 * c2);
}
template<typename PowerType>
constexpr element_fp3 Frobenius_map(const PowerType &pwr) const {
return element_fp3(
data[0],
typename policy_type::non_residue_type(policy_type::Frobenius_coeffs_c1[pwr % 3]) *
data[1],
typename policy_type::non_residue_type(policy_type::Frobenius_coeffs_c2[pwr % 3]) *
data[2]);
// return element_fp3(data[0],
// policy_type::Frobenius_coeffs_c1[pwr % 3] * data[1],
// policy_type::Frobenius_coeffs_c2[pwr % 3] * data[2]});
}
};
template<typename FieldParams>
constexpr element_fp3<FieldParams> operator*(const typename FieldParams::underlying_type &lhs,
const element_fp3<FieldParams> &rhs) {
return element_fp3<FieldParams>(lhs * rhs.data[0], lhs * rhs.data[1], lhs * rhs.data[2]);
}
template<typename FieldParams>
constexpr element_fp3<FieldParams> operator*(const element_fp3<FieldParams> &lhs,
const typename FieldParams::underlying_type &rhs) {
return rhs * lhs;
}
template<typename FieldParams>
constexpr const typename element_fp3<FieldParams>::non_residue_type
element_fp3<FieldParams>::non_residue;
namespace element_fp3_details {
// These constexpr static variables can not be members of element_fp2, because
// element_fp2 is incomplete type until the end of its declaration.
template<typename FieldParams>
constexpr static element_fp3<FieldParams> zero_instance(
FieldParams::underlying_type::zero(),
FieldParams::underlying_type::zero(),
FieldParams::underlying_type::zero());
template<typename FieldParams>
constexpr static element_fp3<FieldParams> one_instance(
FieldParams::underlying_type::one(),
FieldParams::underlying_type::zero(),
FieldParams::underlying_type::zero());
}
template<typename FieldParams>
constexpr const element_fp3<FieldParams>& element_fp3<FieldParams>::zero() {
return element_fp3_details::zero_instance<FieldParams>;
}
template<typename FieldParams>
constexpr const element_fp3<FieldParams>& element_fp3<FieldParams>::one() {
return element_fp3_details::one_instance<FieldParams>;
}
template<typename FieldParams>
std::ostream& operator<<(std::ostream& os, const element_fp3<FieldParams>& elem) {
os << "[" << elem.data[0] << "," << elem.data[1] << "," << elem.data[2] << "]";
return os;
}
} // namespace detail
} // namespace fields
} // namespace algebra
} // namespace crypto3
} // namespace nil
#endif // CRYPTO3_ALGEBRA_FIELDS_ELEMENT_FP3_HPP