-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.cpp
439 lines (327 loc) · 10.4 KB
/
field.cpp
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
#include "field.hpp"
Field::Field(const Fp& order){
prime = order;
}
Field::Field(const int& order){
bn_set_dig(prime.value, order);
}
bool Field::cmp(const Fp& x, const Fp& y){
return bn_cmp(x.value, y.value) == RLC_EQ;
}
bool Field::cmp(const Fp& x, const int& y){
return bn_cmp_dig(x.value, y) == RLC_EQ;
}
Fp Field::one(){
Fp r;
bn_set_dig(r.value, 1);
return r;
}
Fp Field::zero(){
Fp r;
bn_set_dig(r.value, 0);
return r;
}
void Field::mod(Fp& x) const{
bn_mod(x.value, x.value, prime.value);
}
Fp Field::from_int(const int& x) const{
Fp r;
// Check if the input integer is negative.
if (x >= 0){
bn_set_dig(r.value, x);
mod(r);
} else{
bn_set_dig(r.value, -x);
mod(r);
r = neg(r);
}
return r;
}
Fp Field::rand() const{
Fp r;
bn_rand_mod(r.value, prime.value);
return r;
}
Fp Field::add(const Fp& x, const Fp& y) const{
Fp r;
bn_add(r.value, x.value, y.value);
mod(r);
return r;
}
Fp Field::sub(const Fp& x, const Fp& y) const{
Fp r;
bn_sub(r.value, x.value, y.value);
mod(r);
return r;
}
Fp Field::mul(const Fp& x, const Fp& y) const{
Fp r;
bn_mul(r.value, x.value, y.value);
mod(r);
return r;
}
Fp Field::exp(const Fp& x, const Fp& y) const{
Fp r;
bn_mxp(r.value, x.value, y.value, prime.value);
return r;
}
Fp Field::neg(const Fp& x) const{
Fp r;
bn_neg(r.value, x.value);
mod(r);
return r;
}
Fp Field::inv(const Fp& x) const{
Fp r;
bn_mod_inv(r.value, x.value, prime.value);
return r;
}
void Field::mod(FpVec& x) const{
for (auto i : x) mod(i);
}
FpVec Field::vec_join(const FpVec& x, const FpVec& y){
FpVec r;
for (const auto& i : x) r.push_back(i);
for (const auto& i : y) r.push_back(i);
return r;
}
FpVec Field::from_int(const IntVec& x) const{
FpVec r;
for (int i : x) r.push_back(from_int(i));
return r;
}
FpVec Field::rand_vec(const int& size) const{
FpVec r;
for (int i = 0; i < size; ++i) r.push_back(rand());
return r;
}
FpVec Field::vec_add(const FpVec& x, const FpVec& y) const{
if (x.size() != y.size())
throw std::runtime_error("The input vectors are of different lengths.");
FpVec r;
for (int i = 0; i < x.size(); ++i) r.push_back(add(x[i], y[i]));
return r;
}
FpVec Field::vec_sub(const FpVec& x, const FpVec& y) const{
if (x.size() != y.size())
throw std::runtime_error("The input vectors are of different lengths.");
FpVec r;
for (int i = 0; i < x.size(); ++i) r.push_back(sub(x[i], y[i]));
return r;
}
FpVec Field::vec_mul(const FpVec& x, const FpVec& y) const{
if (x.size() != y.size())
throw std::runtime_error("The input vectors are of different lengths.");
FpVec r;
for (int i = 0; i < x.size(); ++i) r.push_back(mul(x[i], y[i]));
return r;
}
FpVec Field::vec_mul(const FpVec& x, const Fp& y) const{
FpVec r;
for (const auto& i : x) r.push_back(mul(i, y));
return r;
}
FpVec Field::vec_inv(const FpVec& x) const{
FpVec r;
for (const auto& i : x) r.push_back(inv(i));
return r;
}
Fp Field::vec_ip(const FpVec& x, const FpVec& y) const{
if (x.size() != y.size())
throw std::runtime_error("The input vectors are of different lengths.");
Fp ip = zero();
FpVec r = vec_mul(x, y);
for (const auto& i : r) ip = add(i, ip);
return ip;
}
FpVec Field::poly_interpolate(const int& degree, const FpVec& roots) const{
if (roots.size() > degree)
throw std::runtime_error("Their are too many roots provided.");
// Declare a temp zp variable and the result zp vector.
FpVec coeff(roots.size() + 1);
// Initialize with leading coefficient 1 for x^0.
coeff[0] = one();
// Compute the coefficients.
for (int i = 0; i < roots.size(); ++i){
for (int j = i; j >= 0; --j){
coeff[j + 1] = add(coeff[j + 1], neg(mul(coeff[j], roots[i])));
}
}
// Invert the coefficients to return c + a0 + a1 + ...
reverse(coeff.begin(), coeff.end());
// Attach additional zeros if needed.
if (degree > roots.size())
coeff = vec_join(coeff, FpVec(degree - roots.size()));
return coeff;
}
bool Field::mat_is_id(const FpMat& x){
const auto size = x.size();
for (int i = 0; i < size; i++){
if (x[i].size() != size) return false;
for (int j = 0; j < size; j++){
if (i == j && !cmp(x[i][j], 1)) return false;
if (i != j && !cmp(x[i][j], 0)) return false;
}
}
return true;
}
FpMat Field::mat_id(const int& size){
FpMat r(size, FpVec(size));
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
if (i == j) r[i][i] = one();
// else r[i][j] = zero();
}
}
return r;
}
FpMat Field::mat_transpose(const FpMat& x){
const auto row = x.size();
const auto col = x[0].size();
FpMat r(col, FpVec(row));
for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j)r[j][i] = x[i][j];
return r;
}
FpMat Field::mat_join(const FpMat& x, const FpMat& y){
// Check that inputs have to have equal length.
if (x.size() != y.size())
throw std::runtime_error("The input matrices are of different sizes.");
FpMat r(x.size());
for (int i = 0; i < x.size(); i++) r[i] = vec_join(x[i], y[i]);
return r;
}
FpMat Field::from_int(const IntMat& x) const{
FpMat r;
for (const auto& i : x) r.push_back(from_int(i));
return r;
}
FpMat Field::rand_mat(const int& row, const int& col) const{
FpMat r;
for (int i = 0; i < row; ++i) r.push_back(rand_vec(col));
return r;
}
FpVec Field::mat_mul(const FpMat& x, const FpVec& y) const{
// Check that inputs have to have equal length.
if (x[0].size() != y.size())
throw std::runtime_error("The input matrix col size is different from the input vector size.");
FpVec r;
for (const auto& i : x) r.push_back(vec_ip(i, y));
return r;
}
FpVec Field::mat_mul(const FpVec& x, const FpMat& y) const{
// Check that inputs have to have equal length.
if (x.size() != y.size())
throw std::runtime_error("The input vector size is different from the input matrix row size.");
FpVec r(y[0].size());
for (int i = 0; i < y[0].size(); ++i){
for (int j = 0; j < y.size(); ++j){
r[i] = add(r[i], mul(x[j], y[j][i]));
}
}
return r;
}
FpMat Field::mat_mul(const FpMat& x, const FpMat& y) const{
// Check that inputs have to have equal length.
if (x[0].size() != y.size())
throw std::runtime_error("The input matrix x's col size is different from y's row size.");
const auto rowx = x.size();
const auto colx = y.size();
const auto coly = y[0].size();
FpMat r(rowx, FpVec(coly));
for (int i = 0; i < rowx; ++i){
for (int j = 0; j < coly; ++j){
for (int k = 0; k < colx; ++k){
r[i][j] = add(r[i][j], mul(x[i][k], y[k][j]));
}
}
}
return r;
}
FpMat Field::mat_mul(const FpMat& x, const Fp& y) const{
FpMat r;
for (const auto& i : x) r.push_back(vec_mul(i, y));
return r;
}
FpMat Field::mat_inv(const FpMat& x) const{
// Check that input matrix is a square matrix.
if (x.size() != x[0].size()){
throw std::runtime_error("The input matrix is not a square matrix.");
}
// This cast should be safe as we won't be handling really large matrix.
const int size = static_cast<int>(x.size());
// Declare a value that will be reused.
Fp temp_mul;
// Generate the row echelon matrix.
FpMat row_echelon = mat_join(x, mat_id(size));
// Bottom left half to all zeros.
for (int i = 0; i < size; i++){
for (int j = i; j < size; j++){
if (i == j && !cmp(row_echelon[i][j], 1)){
temp_mul = inv(row_echelon[i][i]);
for (int k = i; k < size * 2; k++)
row_echelon[j][k] = mul(row_echelon[j][k], temp_mul);
}
if (i == j && cmp(row_echelon[i][j], 0)) break;
if (i != j){
temp_mul = row_echelon[j][i];
for (int k = i; k < size * 2; k++)
row_echelon[j][k] = sub(row_echelon[j][k], mul(temp_mul, row_echelon[i][k]));
}
}
}
// Top right half to all zeros.
for (int i = size - 1; i > 0; i--){
for (int j = i - 1; j >= 0; j--){
temp_mul = row_echelon[j][i];
for (int k = i; k < size * 2; k++)
row_echelon[j][k] = sub(row_echelon[j][k], mul(temp_mul, row_echelon[i][k]));
}
}
// Copy over the output.
FpMat xi;
for (const auto& i : row_echelon) xi.emplace_back(i.begin() + size, i.end());
return xi;
}
FpMat Field::mat_inv_with_det(const FpMat& x, Fp& det) const{
// Check that input matrix is a square matrix.
if (x.size() != x[0].size()){
throw std::runtime_error("The input matrix is not a square matrix.");
}
// This cast should be safe as we won't be handling really large matrix.
const int size = static_cast<int>(x.size());
// Declare a value that will be reused.
Fp temp_mul;
// Generate the row echelon matrix.
FpMat row_echelon = mat_join(x, mat_id(size));
// Set the starting determinant to 1.
det = one();
// Bottom left half to all zeros.
for (int i = 0; i < size; i++){
for (int j = i; j < size; j++){
if (i == j && !cmp(row_echelon[i][j], 1)){
det = mul(det, row_echelon[i][j]);
temp_mul = inv(row_echelon[i][i]);
for (int k = i; k < size * 2; k++)
row_echelon[j][k] = mul(row_echelon[j][k], temp_mul);
}
if (i == j && cmp(row_echelon[i][j], 0)) break;
if (i != j){
temp_mul = row_echelon[j][i];
for (int k = i; k < size * 2; k++)
row_echelon[j][k] = sub(row_echelon[j][k], mul(temp_mul, row_echelon[i][k]));
}
}
}
// Top right half to all zeros.
for (int i = size - 1; i > 0; i--){
for (int j = i - 1; j >= 0; j--){
temp_mul = row_echelon[j][i];
for (int k = i; k < size * 2; k++)
row_echelon[j][k] = sub(row_echelon[j][k], mul(temp_mul, row_echelon[i][k]));
}
}
// Copy over the output.
FpMat xi;
for (const auto& i : row_echelon) xi.emplace_back(i.begin() + size, i.end());
return xi;
}