forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFpModel.hpp
589 lines (482 loc) · 15.4 KB
/
FpModel.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#ifndef _SNARKLIB_FP_MODEL_HPP_
#define _SNARKLIB_FP_MODEL_HPP_
#include <array>
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <gmp.h>
#include <istream>
#include <memory>
#include <ostream>
#include <string>
#include "BigInt.hpp"
#include "Field.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// F[p]
//
template <mp_size_t N, const BigInt<N>& MODULUS>
class FpModel
{
template <mp_size_t N2, const BigInt<N2>& MODULUS2>
friend
std::ostream& operator<< (std::ostream&, const FpModel<N2, MODULUS2>&);
template <mp_size_t N2, const BigInt<N2>& MODULUS2>
friend
std::istream& operator>> (std::istream&, FpModel<N2, MODULUS2>&);
// lowest dimension field holds parameters used by the FpModel
typedef Field<FpModel> Fp;
public:
static constexpr mp_size_t numberLimbs() { return N; }
static constexpr const BigInt<N>& modulus() { return MODULUS; }
// required by Field<> template
typedef FpModel<N, MODULUS> BaseType;
static constexpr std::size_t dimension() { return 1; }
static constexpr std::size_t depth() { return 0; }
static constexpr std::size_t bottom() { return 0; }
static std::size_t sizeInBits() { return Fp::params.num_bits(); }
// The inner parameter template is also required by the Field<>
// template. Every finite prime field has associated parameters as
// a static data member. Note the parameter type T is the field
// holding it. (There is a recurrence pattern.)
template <typename T>
class Params
{
public:
// used by: Fp, Fp2, Fp3
// num_bits
std::size_t num_bits() const {
return m_num_bits;
}
void num_bits(const std::size_t a) {
m_num_bits = a;
}
// s
std::size_t s() const {
return m_s;
}
void s(const std::size_t a) {
m_s = a;
}
// t_minus_1_over_2
const BigInt<T::dimension() * N>& t_minus_1_over_2() const {
return m_t_minus_1_over_2;
}
void t_minus_1_over_2(const char* a) {
m_t_minus_1_over_2 = a;
}
// nqr_to_t
const T& nqr_to_t() const {
return m_nqr_to_t;
}
void nqr_to_t(const T& a) {
m_nqr_to_t = a;
}
void nqr_to_t(const char* a) {
nqr_to_t(T(a));
}
void nqr_to_t(const char* a, const char* b) {
nqr_to_t(T(a, b));
}
void nqr_to_t(const char* a, const char* b, const char* c) {
nqr_to_t(T(a, b, c));
}
// used by: FpModel
// Rsquared
const BigInt<N>& Rsquared() const {
return m_Rsquared;
}
void Rsquared(const char* a) {
m_Rsquared = a;
}
// Rcubed
const BigInt<N>& Rcubed() const {
return m_Rcubed;
}
void Rcubed(const char* a) {
m_Rcubed = a;
}
// inv
mp_limb_t inv() const {
return m_inv;
}
void inv(const mp_limb_t a) {
m_inv = a;
}
// non_residue and Frobenius coefficients dimension changes
//
// field Frobenius coefficients depth bottom
// Fp none 1 1
// Fp2 Fp 1 2
// Fp3 Fp 1 3
// Fp23 Fp2 2 2
// Fp32 Fp 2 3
// Fp232 Fp2 3 2
typedef Field<BaseType,
(1 < T::depth() && 2 == T::bottom()) ? 2 : 1>
FNRF;
// used by: Fp2, Fp3, Fp23, Fp32, Fp232
// non_residue
const FNRF& non_residue() const {
return m_non_residue;
}
void non_residue(const FNRF& a) {
m_non_residue = a;
}
void non_residue(const char* a) {
m_non_residue = FNRF(a);
}
void non_residue(const char* a, const char* b) {
m_non_residue = FNRF(a, b);
}
// Frobenius_coeffs_c1
const FNRF& Frobenius_coeffs_c1(const std::size_t i) const {
return m_Frobenius_coeffs_c1[i];
}
void Frobenius_coeffs_c1(const std::size_t i, const char* a) {
m_Frobenius_coeffs_c1[i] = FNRF(a);
}
void Frobenius_coeffs_c1(const std::size_t i, const char* a, const char* b) {
m_Frobenius_coeffs_c1[i] = FNRF(a, b);
}
// Frobenius_coeffs_c2
const FNRF& Frobenius_coeffs_c2(const std::size_t i) const {
return m_Frobenius_coeffs_c2[i];
}
void Frobenius_coeffs_c2(const std::size_t i, const char* a) {
m_Frobenius_coeffs_c2[i] = FNRF(a);
}
void Frobenius_coeffs_c2(const std::size_t i, const char* a, const char* b) {
m_Frobenius_coeffs_c2[i] = FNRF(a, b);
}
// used by: QAP
// multiplicative_generator
const T& multiplicative_generator() const {
return m_multiplicative_generator;
}
void multiplicative_generator(const T& a) {
m_multiplicative_generator = a;
}
void multiplicative_generator(const char* a) {
multiplicative_generator(T(a));
}
void multiplicative_generator(const char* a, const char* b) {
multiplicative_generator(T(a, b));
}
void multiplicative_generator(const char* a, const char* b, const char* c) {
multiplicative_generator(T(a, b, c));
}
// root_of_unity
const T& root_of_unity() const {
return m_root_of_unity;
}
void root_of_unity(const T& a) {
m_root_of_unity = a;
}
void root_of_unity(const char* a) {
root_of_unity(T(a));
}
void root_of_unity(const char* a, const char* b) {
root_of_unity(T(a, b));
}
void root_of_unity(const char* a, const char* b, const char* c) {
root_of_unity(T(a, b, c));
}
private:
// used by: Fp, Fp2, Fp3
std::size_t m_num_bits, m_s;
BigInt<T::dimension() * N> m_t_minus_1_over_2; // used for sqrt
static T m_nqr_to_t;
// used by: FpModel
BigInt<N> m_Rsquared, m_Rcubed;
mp_limb_t m_inv;
// used by: Fp2, Fp3, Fp23, Fp32, Fp232
static
FNRF
m_non_residue,
m_Frobenius_coeffs_c1[T::dimension()], // pairing
m_Frobenius_coeffs_c2[T::dimension()]; // pairing: only Fp3, Fp23
// used by: QAP
static T m_multiplicative_generator;
static T m_root_of_unity;
};
FpModel() = default;
explicit FpModel(const BigInt<N>& a) {
*this = a;
}
explicit FpModel(const long a) {
*this = a;
}
explicit FpModel(const unsigned long a) {
*this = a;
}
explicit FpModel(const std::string& a)
: FpModel{BigInt<N>(a)}
{}
explicit FpModel(const char* a)
: FpModel{BigInt<N>(a)}
{}
// assignment with unsigned long
FpModel& operator= (const unsigned long a) {
m_monty = a;
mulReduce(Fp::params.Rsquared()); // asm
return *this;
}
// assignment with signed long
FpModel& operator= (const long a) {
if (a >= 0) {
return *this = static_cast<unsigned long>(a);
} else {
const mp_limb_t borrow
= mpn_sub_1(m_monty.data(), MODULUS.data(), N, -a);
#ifdef USE_ASSERT
assert(0 == borrow);
#endif
}
mulReduce(Fp::params.Rsquared()); // asm
return *this;
}
// assignment with big integer
FpModel& operator= (const BigInt<N>& a) {
mpn_copyi(m_monty.data(),
Fp::params.Rsquared().data(),
N);
mulReduce(a); // asm
return *this;
}
// assignment with C string
FpModel& operator= (const char* a) {
return *this = BigInt<N>(a);
}
void clear() {
m_monty.clear();
}
// conversion to big integer
BigInt<N> asBigInt() const {
FpModel res(*this);
res.mulReduce(BigInt<N>(1ul)); // asm
return res.m_monty;
}
// conversion to unsigned long
unsigned long asUnsignedLong() const {
return asBigInt().asUnsignedLong();
}
bool operator== (const FpModel& other) const {
return m_monty == other.m_monty;
}
bool operator!= (const FpModel& other) const {
return m_monty != other.m_monty;
}
bool isZero() const {
return m_monty.isZero();
}
// multiplication in-place is optimized with assembler code
FpModel& operator*= (const FpModel& other) {
mulReduce(other.m_monty); // asm
return *this;
}
// addition and subtraction are optimized with assembler code
FpModel& operator+= (const FpModel& other); // asm
FpModel& operator-= (const FpModel& other); // asm
// negation
FpModel operator- () const {
if (isZero()) {
return *this;
} else {
FpModel r;
mpn_sub_n(r.m_monty.data(), MODULUS.data(), m_monty.data(), N);
return r;
}
}
// squaring is optimized with assembler code
FpModel squared() const; // asm
// inversion in-place
FpModel& invert() {
#ifdef USE_ASSERT
assert(! isZero());
#endif
BigInt<N> g, v = MODULUS;
std::array<mp_limb_t, N+1> s;
mp_size_t sn;
const mp_size_t gn = mpn_gcdext(g.data(),
s.data(),
std::addressof(sn),
m_monty.data(),
N,
v.data(),
N);
#ifdef USE_ASSERT
assert(1 == gn && 1 == g.data()[0]);
#endif
mp_limb_t q;
if (std::abs(sn) >= N) {
mpn_tdiv_qr(std::addressof(q),
m_monty.data(),
0,
s.data(),
std::abs(sn),
MODULUS.data(),
N);
} else {
mpn_zero(m_monty.data(), N);
mpn_copyi(m_monty.data(), s.data(), std::abs(sn));
}
if (sn < 0) {
const mp_limb_t borrow
= mpn_sub_n(m_monty.data(), MODULUS.data(), m_monty.data(), N);
#ifdef USE_ASSERT
assert(0 == borrow);
#endif
}
mulReduce(Fp::params.Rcubed()); // asm
return *this;
}
static FpModel zero() {
return FpModel();
}
static FpModel one() {
return FpModel(1ul);
}
static FpModel random() {
FpModel a;
do
{
a.m_monty.randomize();
std::size_t bitno = BigInt<N>::maxBits();
while (! MODULUS.testBit(bitno)) {
a.m_monty.clearBit(bitno);
--bitno;
}
}
while (mpn_cmp(a.m_monty.data(), MODULUS.data(), N) >= 0);
return a;
}
void marshal_out(std::ostream& os) const {
m_monty.marshal_out(os);
}
bool marshal_in(std::istream& is) {
return m_monty.marshal_in(is);
}
private:
void mulReduce(const BigInt<N>& other); // asm
BigInt<N> m_monty;
};
////////////////////////////////////////////////////////////////////////////////
// static member data (inner parameters template)
//
template <mp_size_t N, const BigInt<N>& MODULUS>
template <typename T>
T FpModel<N, MODULUS>::Params<T>::m_nqr_to_t;
template <mp_size_t N, const BigInt<N>& MODULUS>
template <typename T>
typename FpModel<N, MODULUS>::template Params<T>::FNRF
FpModel<N, MODULUS>::Params<T>::m_non_residue;
template <mp_size_t N, const BigInt<N>& MODULUS>
template <typename T>
typename FpModel<N, MODULUS>::template Params<T>::FNRF
FpModel<N, MODULUS>::Params<T>::m_Frobenius_coeffs_c1[T::dimension()];
template <mp_size_t N, const BigInt<N>& MODULUS>
template <typename T>
typename FpModel<N, MODULUS>::template Params<T>::FNRF
FpModel<N, MODULUS>::Params<T>::m_Frobenius_coeffs_c2[T::dimension()];
template <mp_size_t N, const BigInt<N>& MODULUS>
template <typename T>
T FpModel<N, MODULUS>::Params<T>::m_multiplicative_generator;
template <mp_size_t N, const BigInt<N>& MODULUS>
template <typename T>
T FpModel<N, MODULUS>::Params<T>::m_root_of_unity;
////////////////////////////////////////////////////////////////////////////////
// Operator functions for F[p]
//
// printing to stream
template <mp_size_t N, const BigInt<N>& MODULUS>
std::ostream& operator<< (std::ostream& out, const FpModel<N, MODULUS>& a) {
return out << a.m_monty;
}
// extracting from stream
template <mp_size_t N, const BigInt<N>& MODULUS>
std::istream& operator>> (std::istream& in, FpModel<N, MODULUS>& a) {
return in >> a.m_monty;
}
// multiplication
template <mp_size_t N, const BigInt<N>& MODULUS>
FpModel<N, MODULUS> operator* (const FpModel<N, MODULUS>& x,
const FpModel<N, MODULUS>& y) {
auto a = x;
return a *= y;
}
// addition
template <mp_size_t N, const BigInt<N>& MODULUS>
FpModel<N, MODULUS> operator+ (const FpModel<N, MODULUS>& x,
const FpModel<N, MODULUS>& y) {
auto a = x;
return a += y;
}
// subtraction
template <mp_size_t N, const BigInt<N>& MODULUS>
FpModel<N, MODULUS> operator- (const FpModel<N, MODULUS>& x,
const FpModel<N, MODULUS>& y) {
auto a = x;
return a -= y;
}
// exponentiation
template <mp_size_t N, const BigInt<N>& MODULUS, typename X>
FpModel<N, MODULUS> operator^ (const FpModel<N, MODULUS>& a,
const X& pow) {
return power(a, pow); // field version: power follows base
}
////////////////////////////////////////////////////////////////////////////////
// Operator functions for the field template specialization
//
// multiplication in-place: F[p] *= F[p]
template <mp_size_t N, const BigInt<N>& MODULUS>
Field<FpModel<N, MODULUS>>& operator*= (Field<FpModel<N, MODULUS>>& x,
const Field<FpModel<N, MODULUS>>& y) {
x[0] *= y[0];
return x;
}
// inverse
template <mp_size_t N, const BigInt<N>& MODULUS>
Field<FpModel<N, MODULUS>> inverse(const Field<FpModel<N, MODULUS>>& x) {
auto a = x[0];
return { a.invert() };
}
// squaring
template <mp_size_t N, const BigInt<N>& MODULUS>
Field<FpModel<N, MODULUS>> squared(const Field<FpModel<N, MODULUS>>& x) {
return { x[0].squared() };
}
// square root for: F[p], F[p^2], F[p^3]
template <mp_size_t N, const BigInt<N>& MODULUS, std::size_t A>
Field<FpModel<N, MODULUS>, A> sqrt(const Field<FpModel<N, MODULUS>, A>& a)
{
typedef Field<FpModel<N, MODULUS>, A> FpA;
auto z = FpA::params.nqr_to_t();
auto w = a ^ FpA::params.t_minus_1_over_2();
auto x = a * w;
auto b = x * w;
const auto ONE = FpA::one();
std::size_t v = FpA::params.s();
while (ONE != b) {
auto b2m = b;
std::size_t m = 0;
while (ONE != b2m) {
b2m = squared(b2m);
++m;
}
int j = v - m - 1;
w = z;
while (j > 0) {
w = squared(w);
--j;
}
z = squared(w);
b = b * z;
x = x * w;
v = m;
}
return x;
}
} // namespace snarklib
#include "FpModel.tcc" // asm
#endif