-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpoly_interpolate.cpp
186 lines (178 loc) · 5.6 KB
/
poly_interpolate.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
#include <NTL/GF2E.h>
#include <NTL/GF2X.h>
#include <NTL/GF2EX.h>
#include <NTL/vec_GF2E.h>
#include <NTL/ZZ.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace NTL;
GF2E ZZ_to_GF128(ZZ a) {
GF2X Poly;
Poly.SetLength(128);
for (int i = 0; i < 128; i++) {
if (bit(a, i) == 1) {
SetCoeff(Poly, i);
}
}
return to_GF2E(Poly);
}
void interpolate_polynomial(char* input_path, char* output_path, long maxPoints = 1<<16) {
// read the input file
std::ifstream input_file(input_path);
if (!input_file.is_open()) {
std::cerr << "Error: cannot open the input file" << std::endl;
return;
}
std::cout << "Reading input file: " << input_path << std::endl;
// the first line is the modulus for GF2E
std::string modulus_str;
if (!std::getline(input_file, modulus_str)) {
std::cerr << "Error: invalid input format" << std::endl;
return;
}
ZZ ModZZ = to_ZZ(modulus_str.c_str());
int modbits = NumBits(ModZZ);
GF2X ModPoly;
ModPoly.SetLength(modbits);
ModPoly.HexOutput = 1;
for (int i = 0; i < modbits; i++) {
if (bit(ModZZ, i) == 1) {
// std::cout << "Setting coeff: " << i << std::endl;
SetCoeff(ModPoly, i);
}
}
GF2E::init(ModPoly);
// std::cout << "GF2E modulus: " << GF2E::modulus() << std::endl;
// lines
vec_GF2E x, y;
x.SetMaxLength(maxPoints);
y.SetMaxLength(maxPoints);
long numPoints = 0;
std::string line;
while (std::getline(input_file, line)) {
std::istringstream iss(line);
std::string x_str, y_str;
if (!(iss >> x_str >> y_str)) {
std::cerr << "Error: invalid input format" << std::endl;
return;
}
x[numPoints] = ZZ_to_GF128(to_ZZ(x_str.c_str()));
y[numPoints] = ZZ_to_GF128(to_ZZ(y_str.c_str()));
// std::cout << "x: " << x[numPoints] << ", y: " << y[numPoints] << std::endl;
numPoints++;
}
input_file.close();
// interpolate the polynomial
GF2EX L;
x.SetLength(numPoints);
y.SetLength(numPoints);
L.SetLength(numPoints);
interpolate(L, x, y);
// write the output file
std::ofstream output_file(output_path);
if (!output_file.is_open()) {
std::cerr << "Error: cannot open the output file" << std::endl;
return;
}
std::cout << "Writing output file: " << output_path << std::endl;
output_file << L << std::endl;
output_file.close();
}
void local_test(long numPoints) {
GF2X ModPoly;
ModPoly.SetLength(129);
ModPoly.HexOutput = 1;
// modulus=x ** 128 + x ** 7 + x ** 2 + x + 1
SetCoeff(ModPoly, 128);
SetCoeff(ModPoly, 7);
SetCoeff(ModPoly, 2);
SetCoeff(ModPoly, 1);
SetCoeff(ModPoly, 0);
GF2E::init(ModPoly);
std::cout << "GF2E modulus: " << GF2E::modulus() << std::endl;
// define points
vec_GF2E x, y;
x.SetLength(numPoints);
y.SetLength(numPoints);
// generate ranom 128 points from urandom
// start clock time
double st = GetWallTime();
for (int i = 0; i < numPoints; i++) {
x[i] = ZZ_to_GF128(RandomBits_ZZ(128));
y[i] = ZZ_to_GF128(RandomBits_ZZ(128));
}
double et = GetWallTime();
std::cout << "Data generation time: " << et - st << " s" << std::endl;
GF2EX L;
L.SetLength(numPoints);
st = GetWallTime();
interpolate(L, x, y);
// std::cout << "Interpolated polynomial: " << L << std::endl;
et = GetWallTime();
std::cout << "Interpolation time: " << et - st << " s" << std::endl;
// check the interpolation
for (int i = 0; i < numPoints; i++) {
GF2E yi;
eval(yi, L, x[i]);
if (yi != y[i]) {
std::cerr << "Error: interpolation failed" << std::endl;
return;
}
}
std::cout << "Interpolation test passed" << std::endl;
}
void data_test() {
GF2X ModPoly;
ModPoly.SetLength(129);
ModPoly.HexOutput = 1;
// modulus=x ** 128 + x ** 7 + x ** 2 + x + 1
SetCoeff(ModPoly, 128);
SetCoeff(ModPoly, 7);
SetCoeff(ModPoly, 2);
SetCoeff(ModPoly, 1);
SetCoeff(ModPoly, 0);
GF2E::init(ModPoly);
std::cout << "GF2E modulus: " << GF2E::modulus() << std::endl;
// define points
vec_GF2E x, y;
long numPoints = 2;
x.SetLength(numPoints);
y.SetLength(numPoints);
x[0] = ZZ_to_GF128(to_ZZ("64594005364537620550212150841531907650"));
x[1] = ZZ_to_GF128(to_ZZ("157509865643958166095935242667131453667"));
y[0] = ZZ_to_GF128(to_ZZ("316434540707694254530007623262490325507"));
y[1] = ZZ_to_GF128(to_ZZ("201191051208099574286740631118810974441"));
std::cout << "x0: " << x[0] << ", y0: " << y[0] << std::endl;
std::cout << "x1: " << x[1] << ", y1: " << y[1] << std::endl;
GF2EX L;
L.SetLength(numPoints);
interpolate(L, x, y);
// check the interpolation
for (int i = 0; i < numPoints; i++) {
GF2E yi;
eval(yi, L, x[i]);
if (yi != y[i]) {
std::cerr << "Error: interpolation failed" << std::endl;
return;
}
}
std::cout << "Interpolation test passed" << std::endl;
std::cout << "Interpolated polynomial: " << L << std::endl;
}
int main(int argc, char *argv[]) {
// local_test(1<<10);
// data_test();
// return 0;
long maxPoints = 1<<16;
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <input_path> <output_path> [<maxPoints>]" << std::endl;
return 1;
}
else if (argc == 4) {
maxPoints = std::stoi(argv[3]);
}
interpolate_polynomial(argv[1], argv[2], maxPoints);
return 0;
}