-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallFields.h
338 lines (248 loc) · 7.58 KB
/
SmallFields.h
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
//
// Created by meital on 12/17/19.
//
#ifndef COMPARISON_SMALLFIELDS_H
#define COMPARISON_SMALLFIELDS_H
#include "NTL/ZZ_p.h"
#include "NTL/ZZ.h"
#ifdef __x86_64__
#include <x86intrin.h>
#elif __aarch64__
#include "../infra/SSE2NEON.h"
#endif
#include <gmp.h>
#include <libscapi/include/primitives/Prg.hpp>
#include <libscapi/include/primitives/Mersenne.hpp>
using namespace std;
using namespace NTL;
class ZpMersenne13 {
//private:
public: //TODO return to private after tesing
static const unsigned short int p = 8191;
unsigned short int elem;
public:
ZpMersenne13(){elem = 0;};
ZpMersenne13(long elem){ this->elem = elem %p;}//no need to optimize, just an int modulo
ZpMersenne13& operator=(const ZpMersenne13& other){elem = other.elem; return *this;};
bool operator!=(const ZpMersenne13& other){ return !(other.elem == elem); };
bool operator==(const ZpMersenne13& other){ return other.elem == elem; };
ZpMersenne13 operator+(const ZpMersenne13& f2)
{
ZpMersenne13 answer;
answer.elem = (elem + f2.elem);
if(answer.elem>=p)
answer.elem-=p;
return answer;
}
ZpMersenne13 operator-(const ZpMersenne13& f2)
{
ZpMersenne13 answer;
int temp = (int)elem - (int)f2.elem;
if(temp<0){
answer.elem = temp + p;
}
else{
answer.elem = temp;
}
return answer;
}
ZpMersenne13 operator/(const ZpMersenne13& f2)
{
//code taken from NTL for the function XGCD
int a = f2.elem;
int b = p;
long s;
int u, v, q, r;
long u0, v0, u1, v1, u2, v2;
int aneg = 0, bneg = 0;
if (a < 0) {
if (a < -NTL_MAX_LONG) Error("XGCD: integer overflow");
a = -a;
aneg = 1;
}
if (b < 0) {
if (b < -NTL_MAX_LONG) Error("XGCD: integer overflow");
b = -b;
bneg = 1;
}
u1=1; v1=0;
u2=0; v2=1;
u = a; v = b;
while (v != 0) {
q = u / v;
r = u % v;
u = v;
v = r;
u0 = u2;
v0 = v2;
u2 = u1 - q*u2;
v2 = v1- q*v2;
u1 = u0;
v1 = v0;
}
if (aneg)
u1 = -u1;
s = u1;
if (s < 0)
s = s + p;
ZpMersenne13 inverse(s);
return inverse* (*this);
}
ZpMersenne13 operator*(const ZpMersenne13& f2)
{
ZpMersenne13 answer;
//the multiplication can be put into an int, since it has no more than 26 bits.
answer.elem = (elem * f2.elem)%p;
return answer;
}
ZpMersenne13& operator+=(const ZpMersenne13& f2){
elem = (f2.elem + elem);
if(elem>=p)
elem-=p;
return *this;
};
ZpMersenne13& operator*=(const ZpMersenne13& f2)
{
elem = (elem * f2.elem) %p;
return *this;
}
ZpMersenne13 sqrt()
{
//The algorithm for checking the square root of a value is as follows:
//We know that 2^31 and 2^61 are both divisible by 4 (the results are 2^29 and 2^59 respectively). So 2^31-1=3 mod 4 and 2^61-1=3 mod 4.
//So if we have b=x^2 (over Mersenne61) then we can compute x by b^{2^59}.
//To do this, we can make about 58 field multiplications:
//Set b_1 = b, then
//For i=2...59:
//compute b_i = (b_{i-1})^2.
//So x1=b_59 and x2=-b_59 = 2^61-1-b_59
//Check that x1^2 = b, if it does then output it, otherwise, it means that a cheat is detected.
ZpMersenne13 answer = *this;
for (int i=2; i<=12; i++){
answer *= answer;
}
ZpMersenne13 check = answer*answer;
if (check != *this){
cout<<"CHEATING!!!"<<endl;
return ZpMersenne13(0);
}
return answer;
}
};
inline ::ostream& operator<<(::ostream& s, const ZpMersenne13& a){ return s << a.elem; };
class Zp16BitPrime {
//private:
public: //TODO return to private after tesing
static const unsigned int p = 65521;
unsigned int elem;
public:
Zp16BitPrime(){elem = 0;};
Zp16BitPrime(long elem){ this->elem = elem %p;}//no need to optimize, just an int modulo
Zp16BitPrime& operator=(const Zp16BitPrime& other){elem = other.elem; return *this;};
bool operator!=(const Zp16BitPrime& other){ return !(other.elem == elem); };
bool operator==(const Zp16BitPrime& other){ return other.elem == elem; };
Zp16BitPrime operator+(const Zp16BitPrime& f2)
{
Zp16BitPrime answer;
answer.elem = (elem + f2.elem);
if(answer.elem>=p)
answer.elem-=p;
return answer;
}
Zp16BitPrime operator-(const Zp16BitPrime& f2)
{
Zp16BitPrime answer;
int temp = (int)elem - (int)f2.elem;
if(temp<0){
answer.elem = temp + p;
}
else{
answer.elem = temp;
}
return answer;
}
Zp16BitPrime operator/(const Zp16BitPrime& f2)
{
//code taken from NTL for the function XGCD
int a = f2.elem;
int b = p;
long s;
int u, v, q, r;
long u0, v0, u1, v1, u2, v2;
int aneg = 0, bneg = 0;
if (a < 0) {
if (a < -NTL_MAX_LONG) Error("XGCD: integer overflow");
a = -a;
aneg = 1;
}
if (b < 0) {
if (b < -NTL_MAX_LONG) Error("XGCD: integer overflow");
b = -b;
bneg = 1;
}
u1=1; v1=0;
u2=0; v2=1;
u = a; v = b;
while (v != 0) {
q = u / v;
r = u % v;
u = v;
v = r;
u0 = u2;
v0 = v2;
u2 = u1 - q*u2;
v2 = v1- q*v2;
u1 = u0;
v1 = v0;
}
if (aneg)
u1 = -u1;
s = u1;
if (s < 0)
s = s + p;
Zp16BitPrime inverse(s);
return inverse* (*this);
}
Zp16BitPrime operator*(const Zp16BitPrime& f2)
{
Zp16BitPrime answer;
//the multiplication can be put into an int, since it has no more than 26 bits.
answer.elem = (elem * f2.elem)%p;
return answer;
}
Zp16BitPrime& operator+=(const Zp16BitPrime& f2){
elem = (f2.elem + elem);
if(elem>=p)
elem-=p;
return *this;
};
Zp16BitPrime& operator*=(const Zp16BitPrime& f2)
{
elem = (elem * f2.elem) %p;
return *this;
}
Zp16BitPrime sqrt()
{
//The algorithm for checking the square root of a value is as follows:
//We know that 2^31 and 2^61 are both divisible by 4 (the results are 2^29 and 2^59 respectively). So 2^31-1=3 mod 4 and 2^61-1=3 mod 4.
//So if we have b=x^2 (over Mersenne61) then we can compute x by b^{2^59}.
//To do this, we can make about 58 field multiplications:
//Set b_1 = b, then
//For i=2...59:
//compute b_i = (b_{i-1})^2.
//So x1=b_59 and x2=-b_59 = 2^61-1-b_59
//Check that x1^2 = b, if it does then output it, otherwise, it means that a cheat is detected.
Zp16BitPrime answer = *this;
for (int i=2; i<=12; i++){
answer *= answer;
}
Zp16BitPrime check = answer*answer;
if (check != *this){
cout<<"CHEATING!!!"<<endl;
return Zp16BitPrime(0);
}
return answer;
}
};
inline ::ostream& operator<<(::ostream& s, const Zp16BitPrime& a){ return s << a.elem; };
#endif //COMPARISON_SMALLFIELDS_H