-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield.h
More file actions
1384 lines (1205 loc) · 36.8 KB
/
Copy pathfield.h
File metadata and controls
1384 lines (1205 loc) · 36.8 KB
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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef FP_FIELD_H
#define FP_FIELD_H
#include "util.h"
#include <omp.h>
template <typename G = uint64_t>
inline G block_to_G(block &in)
{
//return reinterpret_cast<const uint64_t *>(&in)[0];
// NOTE: In SFSS, G only can be either uint64_t or __uint128_t
if constexpr (std::is_same<G, uint64_t>::value)
{
// if G = uint64_t
return reinterpret_cast<const uint64_t *>(&in)[0];
}
else if constexpr (std::is_same<G, uint32_t>::value)
{
// if G = uint64_t
return reinterpret_cast<const uint32_t *>(&in)[0];
}
else if constexpr (std::is_same<G, uint16_t>::value)
{
// if G = uint16_t
return reinterpret_cast<const uint16_t *>(&in)[0];
}
else if constexpr (std::is_same<G, __uint128_t>::value)
{
// G = __uint128_t
uint64_t low = reinterpret_cast<const uint64_t *>(&in)[0];
uint64_t high = reinterpret_cast<const uint64_t *>(&in)[1];
return (static_cast<__uint128_t>(high) << 64) | low;
}
else // other defiend group types, just call G(in)
{
return G(in);
}
}
template <typename G = uint64_t>
G F(const block &prf_key, uint64_t counter)
{
PRP prf(prf_key);
block data = makeBlock(0, counter);
prf.permute_block(&data, 1); // data <- prf(prf_key, data)
// std::cout << "F: prf_key = " << prf_key << ", counter = " << counter << ", data = " << data << std::endl;
G v = block_to_G<G>(data);
// std::cout << "F, v = " << v << std::endl;
return v;
}
__uint128_t sum_and_carry(const __uint128_t a, const __uint128_t b, __uint128_t &sum)
{
sum = a + b;
if (sum < a && sum < b)
return 1; // overflow occurred
else
return 0;
}
class FP127
{
public:
static constexpr __uint128_t MOD = (__uint128_t(1) << 127) - 1;
__uint128_t val;
static __uint128_t reduce(__uint128_t x)
{
__uint128_t r = (x & MOD) + (x >> 127);
// One more reduction may be needed if carry occurred
if (r >= MOD)
r -= MOD;
return r;
}
FP127() : val(0) {}
FP127(__uint128_t x)
{
val = reduce(x);
}
FP127(emp::block &x)
{
__uint128_t tmp = 0;
static_assert(sizeof(emp::block) >= sizeof(__uint128_t), "sizeof(emp::block) < sizeof(__uint128_t)");
std::memcpy(&tmp, &x, sizeof(__uint128_t)); // 按内存拷贝
val = reduce(tmp);
}
FP127 operator+(const FP127 &rhs) const
{
__uint128_t res = val + rhs.val;
if (res >= MOD)
res -= MOD;
return FP127(res);
}
FP127& operator+=(const FP127 &rhs)
{
val += rhs.val;
if (val >= MOD)
val -= MOD;
return *this;
}
FP127 operator-(const FP127 &rhs) const
{
__uint128_t res;
if (val >= rhs.val)
{
res = val - rhs.val;
}
else
{
res = MOD - (rhs.val - val); // to avoid any wrap-around over __uint128_t
}
return FP127(res);
}
FP127& operator-=(const FP127 &rhs){
if (val >= rhs.val)
{
val = val - rhs.val;
}
else
{
val = MOD - (rhs.val - val); // to avoid any wrap-around over __uint128_t
}
return *this;
}
FP127 operator*(const FP127 &rhs) const
{
uint64_t a0 = static_cast<uint64_t>(val);
uint64_t a1 = static_cast<uint64_t>(val >> 64);
uint64_t b0 = static_cast<uint64_t>(rhs.val);
uint64_t b1 = static_cast<uint64_t>(rhs.val >> 64);
// a = a0 + a1 * 2^64, b = b0 + b1 * 2^64
// a * b = (a0 * b0) + ((a0 * b1 + a1 * b0) << 64) + (a1 * b1 * 2^128)
__uint128_t z00 = (__uint128_t)a0 * (__uint128_t)b0;
__uint128_t z11 = (__uint128_t)a1 * (__uint128_t)b1;
__uint128_t z01 = (__uint128_t)a0 * (__uint128_t)b1;
__uint128_t z10 = (__uint128_t)a1 * (__uint128_t)b0;
__uint128_t z1, carry;
carry = sum_and_carry(z01, z10, z1);
// std::cout << "carry = " << carry << std::endl;
// result = low + high * 2^128
// low = z0 + (z1 << 64) over **integer**
// high = z2 + (z1 >> 64) over **integer**
// however, we need to handle the overflow over __uint128_t
__uint128_t z1_low = z1 << 64;
__uint128_t z1_high = (z1 >> 64) + (carry << 64);
__uint128_t low, high, carry_low, carry_high;
carry_low = sum_and_carry(z00, z1_low, low);
carry_high = sum_and_carry(z11, z1_high, high);
// std::cout << "carry_low = " << carry_low << std::endl;
// std::cout << "carry_high = " << carry_high << std::endl;
// NOTE: low = z0 + z1_low - carry_low * 2^128
// high = z2 + z1_high - carry_high * 2^128
// sum = low + carry_Low * 2^128 + (high + carry_high * 2^128) << 128 over **INTEGER**
//__uint128_t sum = low + carry_low << 1 + (high << 1) + (carry_high << 2) mod MOD; using the fact that 2^127 ≡ 1 (mod 2^127 - 1)
__uint128_t low_mod = reduce(reduce(low) + (carry_low << 1));
__uint128_t high_mod = reduce(reduce(reduce(high) << 1) + (carry_high << 2)); // high_mod = reduce(high << 1) + carry_high
//__uint128_t sum = low + (high << 1);
__uint128_t sum = reduce(low_mod + high_mod);
return FP127(sum);
}
bool operator==(const FP127 &rhs) const
{
return val == rhs.val;
}
bool operator!=(const FP127 &rhs) const
{
return val != rhs.val;
}
friend std::ostream &operator<<(std::ostream &os, const FP127 &x)
{
uint64_t low = static_cast<uint64_t>(x.val);
uint64_t high = static_cast<uint64_t>(x.val >> 64);
// uint16_t high = static_cast<uint16_t>(x.val >> 128); // Actually always 0 for 127 bits, but for completeness
os << "0x";
if (high != 0)
os << std::hex << high << std::setfill('0') << std::setw(16) << low << std::dec;
else
os << std::hex << low << std::dec;
return os;
}
FP127 pow(__uint128_t exp) const
{
FP127 base = *this, result(1);
while (exp)
{
if (exp & 1)
result = result * base;
base = base * base;
exp >>= 1;
}
return result;
}
FP127 inv() const
{
// Inverse using extended Euclidean algorithm
__uint128_t a = val, m = MOD;
__uint128_t m0 = m, t, q;
__int128_t x0 = 0, x1 = 1;
if (a == 0)
return FP127(0);
while (a > 1)
{
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - (__int128_t)q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return FP127((__uint128_t)x1);
}
};
class FP61
{
public:
static constexpr uint64_t MOD = (uint64_t(1) << 61) - 1;
uint64_t val;
FP61() : val(0) {}
FP61(uint64_t x)
{
val = reduce(x);
}
FP61(emp::block &x)
{
uint64_t tmp = 0;
static_assert(sizeof(emp::block) >= sizeof(uint64_t), "sizeof(emp::block) < sizeof(uint64_t)");
std::memcpy(&tmp, &x, sizeof(uint64_t)); // 按内存拷贝
val = reduce(tmp);
}
static uint64_t reduce(uint64_t x)
{
uint64_t r = (x & MOD) + (x >> 61);
if (r >= MOD)
r -= MOD;
return r;
}
FP61 operator+(const FP61 &rhs) const
{
uint64_t res = val + rhs.val;
if (res >= MOD)
res -= MOD;
return FP61(res);
}
FP61& operator+=(const FP61 &rhs)
{
val += rhs.val;
if (val >= MOD)
val -= MOD;
return *this;
}
FP61 operator-(const FP61 &rhs) const
{
uint64_t res = (val >= rhs.val) ? (val - rhs.val) : (MOD + val - rhs.val);
return FP61(res);
}
FP61& operator-=(const FP61 &rhs)
{
if (val >= rhs.val)
{
val -= rhs.val;
}
else
{
val = MOD - (rhs.val - val);
}
return *this;
}
FP61 operator*(const FP61 &rhs) const
{
__uint128_t prod = (__uint128_t)val * rhs.val;
uint64_t r = (uint64_t)((prod & MOD) + (prod >> 61));
if (r >= MOD)
r -= MOD;
return FP61(r);
}
bool operator==(const FP61 &rhs) const { return val == rhs.val; }
bool operator!=(const FP61 &rhs) const { return val != rhs.val; }
friend std::ostream &operator<<(std::ostream &os, const FP61 &x)
{
os << "0x" << std::hex << x.val << std::dec;
return os;
}
FP61 pow(uint64_t exp) const
{
FP61 base = *this, result(1);
while (exp)
{
if (exp & 1)
result = result * base;
base = base * base;
exp >>= 1;
}
return result;
}
FP61 inv() const
{
// Inverse using extended Euclidean algorithm
uint64_t a = val, m = MOD, m0 = m, t, q;
int64_t x0 = 0, x1 = 1;
if (a == 0)
return FP61(0);
while (a > 1)
{
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return FP61((uint64_t)x1);
}
};
class FP89
{
public:
static constexpr __uint128_t MOD = (__uint128_t(1) << 89) - 1;
__uint128_t val;
FP89() : val(0) {}
FP89(__uint128_t x)
{
val = reduce(x);
}
FP89(emp::block &x)
{
__uint128_t tmp = 0;
static_assert(sizeof(emp::block) >= sizeof(__uint128_t), "sizeof(emp::block) < sizeof(__uint128_t)");
std::memcpy(&tmp, &x, sizeof(__uint128_t)); // 按内存拷贝
val = reduce(tmp);
}
static __uint128_t reduce(__uint128_t x)
{
__uint128_t r = (x & MOD) + (x >> 89);
if (r >= MOD)
r -= MOD;
return r;
}
FP89 operator+(const FP89 &rhs) const
{
__uint128_t res = val + rhs.val;
if (res >= MOD)
res -= MOD;
return FP89(res);
}
FP89& operator+=(const FP89 &rhs)
{
val += rhs.val;
if (val >= MOD)
val -= MOD;
return *this;
}
FP89 operator-(const FP89 &rhs) const
{
__uint128_t res = (val >= rhs.val) ? (val - rhs.val) : (MOD + val - rhs.val);
return FP89(res);
}
FP89& operator-=(const FP89 &rhs)
{
if (val >= rhs.val)
{
val -= rhs.val;
}
else
{
val = MOD - (rhs.val - val);
}
return *this;
}
FP89 operator*(const FP89 &rhs) const
{
uint64_t a0 = static_cast<uint64_t>(val);
uint64_t a1 = static_cast<uint64_t>(val >> 64);
uint64_t b0 = static_cast<uint64_t>(rhs.val);
uint64_t b1 = static_cast<uint64_t>(rhs.val >> 64);
// a = a0 + a1 * 2^64, b = b0 + b1 * 2^64
// a * b = (a0 * b0) + (a0 * b1 + a1 * b0) * 2^64 + (a1 * b1 * 2^128)
__uint128_t z00 = (__uint128_t)a0 * (__uint128_t)b0; // <= 2^128 - 1
__uint128_t z11 = (__uint128_t)a1 * (__uint128_t)b1; // <= 2^50 - 1
__uint128_t z01 = (__uint128_t)a0 * (__uint128_t)b1; // <= 2^89 - 1
__uint128_t z10 = (__uint128_t)a1 * (__uint128_t)b0; // <= 2^89 - 1
__uint128_t z1 = z01 + z10; // z1 = z01 + z10 <= 2^89 - 1 + 2^89 - 1 = 2^90 - 2, no overflow
// result = low + high * 2^128
// low = z00 + (z1 << 64) over **integer**
// high = z11 + (z1 >> 64) over **integer**
// however, we need to handle the overflow over __uint128_t
__uint128_t z1_low = z1 << 64;
__uint128_t z1_high = z1 >> 64; //<= 2^26 -1
__uint128_t low, high, carry_low;
carry_low = sum_and_carry(z00, z1_low, low);
// carry_high = sum_and_carry(z11, z1_high, high);
high = z11 + z1_high; // high = z11 + (z1 >> 64) over **integer**. Note that z11 < 2^50, z1_high < 2^26, so high < 2^76, which is safe for __uint128_t
// std::cout << "carry_low = " << carry_low << std::endl;
// std::cout << "carry_high = " << carry_high << std::endl;
// NOTE: low = z00 + z1_low - carry_low * 2^128
// high = z11 + z1_high
// sum = low + (carry_Low * 2^128) + high * 2^128 over **INTEGER**.
// so sum = low + (carry_low << 39) + (high << 39) mod MOD; using the fact that 2^89 ≡ 1 (mod 2^89 - 1)
__uint128_t low_mod = reduce(low); /* reduce(low) < 2^89, no overflow.
*/
__uint128_t high_mod = reduce((high + carry_low) << 39); // high < 2^76 => (high << 39) < 2^115, (carry_low << 39) < 2^39, no overflow.
// low_mod < 2^89, high_mod < 2^115, so low_mod + high_mod < 2^128, no overflow.
__uint128_t sum = reduce(low_mod + high_mod);
return FP89(sum);
}
bool operator==(const FP89 &rhs) const { return val == rhs.val; }
bool operator!=(const FP89 &rhs) const { return val != rhs.val; }
friend std::ostream &operator<<(std::ostream &os, const FP89 &x)
{
uint64_t low = static_cast<uint64_t>(x.val);
uint32_t mid = static_cast<uint32_t>(x.val >> 64);
os << "0x";
if (mid != 0)
os << std::hex << mid << std::setfill('0') << std::setw(16) << low << std::dec;
else
os << std::hex << low << std::dec;
return os;
}
FP89 pow(__uint128_t exp) const
{
FP89 base = *this, result(1);
while (exp)
{
if (exp & 1)
result = result * base;
base = base * base;
exp >>= 1;
}
return result;
}
FP89 inv() const
{
__uint128_t a = val, m = MOD, m0 = m, t, q;
__int128_t x0 = 0, x1 = 1;
if (a == 0)
return FP89(0);
while (a > 1)
{
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - (__int128_t)q * x0;
x1 = t;
}
if (x1 < 0)
x1 += m0;
return FP89((__uint128_t)x1);
}
};
template <typename T, size_t BITS = 32>
class MyInteger
{
public:
static constexpr size_t bits = BITS;
static constexpr T MOD = (T(1) << BITS); // 模数
static_assert(BITS < sizeof(T) * 8, "BITS exceeds the size of T in bits");
MyInteger() : value(0) {}
MyInteger(const T &v) : value(v % MOD) {}
MyInteger(const emp::block &block)
{
T tmp = 0;
static_assert(sizeof(emp::block) >= sizeof(T), "sizeof(emp::block) < sizeof(T)");
std::memcpy(&tmp, &block, sizeof(T)); // 按内存拷贝
value = tmp % MOD;
}
static size_t get_BITS() { return bits; }
T get_mod() const { return MOD; }
T get_value() const { return value; }
MyInteger operator+(const MyInteger &other) const
{
return MyInteger((value + other.value) % MOD);
}
MyInteger& operator+=(const MyInteger &other)
{
value = (value + other.value) % MOD;
return *this;
}
MyInteger operator-(const MyInteger &other) const
{
return MyInteger((value - other.value + MOD) % MOD);
}
MyInteger& operator-=(const MyInteger &other)
{
value = (value - other.value + MOD) % MOD;
return *this;
}
MyInteger operator*(const MyInteger &other) const
{
return MyInteger((value * other.value) % MOD);
}
MyInteger& operator*=(const MyInteger &other)
{
value = (value * other.value) % MOD;
return *this;
}
MyInteger operator%(const MyInteger &other) const
{
return MyInteger(value % other.value);
}
MyInteger operator%(const T &other) const
{
return MyInteger(value % other);
}
MyInteger operator/(const MyInteger &other) const
{
return MyInteger(value / other.value);
}
MyInteger operator<<(const size_t shift) const
{
return MyInteger((value << shift) % MOD);
}
MyInteger operator>>(const size_t shift) const
{
return MyInteger((value >> shift));
}
bool operator==(const MyInteger &other) const
{
return value == other.value;
}
friend std::ostream &operator<<(std::ostream &os, const MyInteger &x)
{
return os << x.value;
}
// Serialization: write value to buffer
size_t serialize(char *buf) const
{
std::memcpy(buf, &value, sizeof(T));
return sizeof(T);
}
// Deserialization: read value from buffer
size_t deserialize(const char *buf)
{
std::memcpy(&value, buf, sizeof(T));
value = value % MOD; // ensure value is reduced
return sizeof(T); // return the size of the deserialized data
}
size_t get_serialized_size() const
{
return sizeof(T);
}
static size_t get_buf_size() {
return sizeof(T);
}
private:
T value;
};
// define a new class called MyBigInteger, but use mpz for the inner computation. Define +/-/*, /, % operators
template <size_t BITS>
class MyBigInteger
{
public:
static constexpr size_t bits = BITS; // 256 bits for MyBigInteger
inline static mpz_class MOD = (mpz_class(1) << BITS);
MyBigInteger() : value(0) {}
MyBigInteger(__uint128_t v)
{
mpz_class tmp;
mpz_import(tmp.get_mpz_t(), 1, 1, sizeof(v), 0, 0, &v);
value = tmp % MOD;
}
MyBigInteger(const mpz_class &v) : value(v % MOD) {}
MyBigInteger(const void *block_ptr, size_t block_size)
{
mpz_class tmp;
mpz_import(tmp.get_mpz_t(), block_size, 1, 1, 0, 0, block_ptr);
value = tmp % MOD;
}
MyBigInteger(const emp::block &in) : MyBigInteger(&in, sizeof(in)) {}
MyBigInteger(const MyBigInteger &other) : value(other.value) {}
MyBigInteger(MyBigInteger &&other) : value(std::move(other.value)) {}
static size_t get_BITS() { return bits; }
mpz_class get_mod() const { return MOD; }
mpz_class get_value() const { return value; }
MyBigInteger& operator+=(const MyBigInteger &other)
{
value = (value + other.value) % MOD;
return *this;
}
MyBigInteger operator+(const MyBigInteger &other) const
{
return MyBigInteger((value + other.value) % MOD);
}
MyBigInteger operator-(const MyBigInteger &other) const
{
return MyBigInteger((value - other.value + MOD) % MOD);
}
MyBigInteger& operator-=(const MyBigInteger &other)
{
value = (value - other.value + MOD) % MOD;
return *this;
}
MyBigInteger operator*(const MyBigInteger &other) const
{
return MyBigInteger((value * other.value) % MOD);
}
MyBigInteger operator%(const MyBigInteger &other) const
{
return MyBigInteger(value % other.value);
}
MyBigInteger operator%(const mpz_class &other) const
{
return MyBigInteger(value % other);
}
MyBigInteger operator/(const MyBigInteger &other) const
{
if (other.value == 0)
throw std::runtime_error("Division by zero in modular arithmetic");
return MyBigInteger(value / other.value);
}
MyBigInteger operator/(const mpz_class &other) const
{
if (other == 0)
throw std::runtime_error("Division by zero in modular arithmetic");
return MyBigInteger(value / other);
}
MyBigInteger operator<<(const size_t shift) const
{
return MyBigInteger((value << shift) % MOD);
}
MyBigInteger operator>>(const size_t shift) const
{
return MyBigInteger((value >> shift) % MOD);
}
MyBigInteger &operator=(const MyBigInteger &other)
{
if (this != &other)
{
value = other.value;
}
return *this;
}
MyBigInteger &operator=(MyBigInteger &&other)
{
if (this != &other)
{
value = std::move(other.value);
}
return *this;
}
bool operator==(const MyBigInteger &other) const
{
return value == other.value;
}
bool operator!=(const MyBigInteger &other) const
{
return value != other.value;
}
friend std::ostream &operator<<(std::ostream &os, const MyBigInteger &x)
{
return os << x.value;
}
// Serialization: write value to buffer (length + data)
size_t serialize(char *buf) const
{
size_t count = 0;
void *data = mpz_export(nullptr, &count, 1, 1, 0, 0, value.get_mpz_t());
std::memcpy(buf, &count, sizeof(count));
std::memcpy(buf + sizeof(count), data, count);
free(data);
return sizeof(count) + count;
}
// Deserialization: read value from buffer (length + data)
size_t deserialize(const char *buf)
{
size_t count = 0;
std::memcpy(&count, buf, sizeof(count));
mpz_import(value.get_mpz_t(), count, 1, 1, 0, 0, buf + sizeof(count));
value = value % MOD;
return sizeof(count) + count; // return the size of the deserialized data
}
// Get the number of bytes needed for serialization
size_t get_serialized_size() const
{
size_t count = 0;
void *data = mpz_export(nullptr, &count, 1, 1, 0, 0, MOD.get_mpz_t()); // NOTE: cannot use value.get_mpz_t() here, because it may be smaller if not setup
free(data);
return sizeof(count) + count;
}
private:
mpz_class value;
};
/// @brief A class representing a mersenne finite field element with a specified number of bits.
/// @tparam BITS The number of bits in the finite field.
/// @details This class provides basic arithmetic operations (addition, subtraction, multiplication, division)
/// and modular reduction for finite field elements.
template <size_t BITS>
class FP
{
public:
static constexpr size_t bits = BITS;
inline static mpz_class MOD = (mpz_class(1) << BITS) - 1;
FP() : value(0) {}
FP(const mpz_class &v) { set_value(v); }
FP(uint64_t v)
{
mpz_class tmp;
mpz_import(tmp.get_mpz_t(), 1, 1, sizeof(v), 0, 0, &v);
set_value(tmp);
}
FP(int v)
{
int abs_v = v;
mpz_class tmp;
if (v < 0)
{
abs_v = -v;
mpz_import(tmp.get_mpz_t(), 1, 1, sizeof(abs_v), 0, 0, &abs_v);
tmp = MOD - tmp; // convert to positive equivalent in the field
}
else
{
mpz_import(tmp.get_mpz_t(), 1, 1, sizeof(abs_v), 0, 0, &abs_v);
}
set_value(tmp);
}
explicit FP(__uint128_t v)
{
mpz_class tmp;
mpz_import(tmp.get_mpz_t(), 1, 1, sizeof(v), 0, 0, &v);
set_value(tmp);
}
FP(const void *block_ptr, size_t block_size)
{
// GMP 的 mpz_import 按字节导入大整数
mpz_class tmp;
mpz_import(tmp.get_mpz_t(), block_size, 1, 1, 0, 0, block_ptr);
set_value(tmp);
}
FP(const emp::block &in) : FP(&in, sizeof(in)) {}
FP(const FP &other) : value(other.value) {}
FP(FP &&other) : value(std::move(other.value)) {}
mpz_class get_mod() const
{
return MOD;
}
FP &operator=(const FP &other)
{
if (this != &other)
{
value = other.value;
}
return *this;
}
FP &operator=(FP &&other)
{
if (this != &other)
{
value = std::move(other.value);
}
return *this;
}
void set_value(const mpz_class &v)
{
// in case the value is larger than MOD
value = (v & ((mpz_class(1) << BITS) - 1)) + (v >> BITS);
if (value >= MOD)
value -= MOD;
}
mpz_class get_value() const
{
return value;
}
FP operator+(const FP &other) const
{
FP r;
r.value = value + other.value;
if (r.value >= MOD)
r.value -= MOD;
return r;
}
FP& operator+=(const FP &other)
{
value = value + other.value;
if (value >= MOD)
value -= MOD;
return *this;
}
FP operator-(const FP &other) const
{
FP r;
r.value = value - other.value;
if (r.value < 0)
r.value += MOD;
return r;
}
FP& operator-=(const FP &other)
{
value = value - other.value;
if (value < 0)
value += MOD;
return *this;
}
FP operator*(const FP &other) const
{
FP r;
r.set_value(value * other.value);
return r;
}
FP inverse() const
{
FP r;
if (mpz_invert(r.value.get_mpz_t(), value.get_mpz_t(), MOD.get_mpz_t()) == 0)
{
throw std::runtime_error("No modular inverse exists");
}
return r;
}
FP operator/(const FP &other) const
{
return *this * other.inverse();
}
FP operator%(const FP &other) const
{
if (other.value == 0)
throw std::runtime_error("Division by zero in modular arithmetic");
FP r;
r.value = value % other.value;
return r;
}
FP operator%(const mpz_class &other) const
{
if (other == 0)
throw std::runtime_error("Division by zero in modular arithmetic");
FP r;
r.value = value % other;
return r;
}
bool operator==(const FP &other) const
{
return value == other.value;
}
friend std::ostream &operator<<(std::ostream &os, const FP &x)
{
return os << x.value;
}
private:
mpz_class value;
// void reduce()
// {
// value = (value & ((mpz_class(1) << BITS) - 1)) + (value >> BITS);
// // if (r > MOD)
// // r -= MOD;
// // value = r;
// }
};
// // Initialize the static modulus
// template <size_t BITS>
// mpz_class FP<BITS>::MOD = (mpz_class(1) << BITS) - 1;
template <typename Ring, size_t D = 127>
class RingVec
{
public:
std::vector<Ring> data;
// RingVec(RingVec &&) noexcept = default;
// RingVec &operator=(RingVec &&) noexcept = default;
// 明确启用移动语义并标记为 noexcept,帮助容器优先使用移动而非拷贝
// RingVec(RingVec&&) noexcept = default;
// RingVec &operator=(RingVec&&) noexcept = default;
// // 显式默认的拷贝构造/赋值
// RingVec(const RingVec&) = default;
// RingVec &operator=(const RingVec&) = default;
// RingVec() = default;
RingVec() : data(D) {}
RingVec(const std::vector<Ring> &v) : data(D)
{
size_t n = std::min(D, v.size());
//#pragma omp parallel for
for (size_t i = 0; i < n; ++i)