-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.cpp
More file actions
276 lines (222 loc) · 7.84 KB
/
Copy pathhelper.cpp
File metadata and controls
276 lines (222 loc) · 7.84 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
#include "helper.hpp"
StrMat Helper::read_tbl(const std::string& filename, int row){
// Create the result table.
StrMat table;
// Read the file.
std::ifstream file(filename);
// Some holders for values.
str line, value;
for (int i = 0; i < row; i++){
std::getline(file, line);
std::stringstream ss(line);
StrVec temp;
while (std::getline(ss, value, '|')){
temp.push_back(value);
}
table.push_back(temp);
}
return table;
}
int Helper::rand_int(){
// Seed for the random number engine.
static std::random_device rd;
// Mersenne Twister engine.
static std::mt19937 gen(rd());
// Set the distribution from 1 to largest int.
std::uniform_int_distribution dist(1, std::numeric_limits<int>::max());
return dist(gen);
}
CharVec Helper::int_to_char_vec(const int& x){
// Declare the return object, a vector of unsigned characters.
CharVec r;
// Extract bytes in big-endian order.
for (size_t i = sizeof(int); i > 0; --i) r.emplace_back(static_cast<unsigned char>(x >> (i - 1) * 8) & 0xFF);
return r;
}
int Helper::char_vec_to_int(const CharVec& x){
// Declare the return object, an integer.
int r = 0;
// Ensure the vector size does not exceed the size of int (typically 4 bytes)
const size_t num_bytes = std::min(x.size(), sizeof(int));
// Combine bytes into an integer (big-endian order).
for (size_t i = 0; i < num_bytes; ++i) r |= x[i] << (num_bytes - 1 - i) * 8;
return r;
}
CharVec Helper::str_to_char_vec(const str& x){
return {x.begin(), x.end()};
}
str Helper::char_vec_to_str(const CharVec& x){
return {x.begin(), x.end()};
}
Fp Helper::char_vec_to_fp(const CharVec& x){
return Fp(x);
}
CharVec Helper::xor_char_vec(const CharMat& x){
// Create a copy of x.
CharVec r = x[0];
// Perform XOR.
for (size_t i = 1; i < x.size(); ++i){
std::transform(
r.begin(), r.end(), x[i].begin(), r.begin(),
[](const unsigned char a, const unsigned char b){ return a ^ b; }
);
}
return r;
}
IntVec Helper::rand_int_vec(const int& length, const int& min_v, const int& max_v){
IntVec r(length);
// Initialize random number generator with a seed based on current time
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution dis(min_v, max_v);
for (int i = 0; i < length; ++i) r[i] = dis(gen);
return r;
}
IntMat Helper::rand_int_mat(const int& row, const int& col, const int& min_v, const int& max_v){
IntMat r(row, IntVec(col));
// Initialize random number generator with a seed based on current time
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution dis(min_v, max_v);
// Fill the matrix with random integers
for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) r[i][j] = dis(gen);
return r;
}
FpVec Helper::power_poly(const int d, const BP& pairing_group, const FpVec& x){
FpVec r, power;
// Fill the power Fp vector from 1 to degree.
for (int i = 1; i <= d; ++i) power.emplace_back(i);
// Compute x_i^1, x_i^2..., x_n^d.
for (const auto& i : x)
for (const auto& j : power) r.emplace_back(pairing_group.Zp->exp(i, j));
// Finally attach a one to the end.
r.emplace_back(1);
// Return the output vector.
return r;
}
FpVec Helper::coeff_poly(const int d, const BP& pairing_group, const FpMat& x){
// Create holder for the returned vector.
FpVec r;
// Create holder for the aggregated constants.
Fp constant;
for (const auto& i : x){
// Interpolate the polynomial on this row.
FpVec temp = pairing_group.Zp->poly_interpolate(d, i);
// Add the constant together.
constant = pairing_group.Zp->add(constant, temp[0]);
// Pop the constant; the first value.
temp.erase(temp.begin());
// Join the vector to the result.
r = Field::vec_join(r, temp);
}
// Add the aggregated constant to the returned vector.
r.push_back(constant);
return r;
}
FpVec Helper::split_poly(const BP& pairing_group, const FpVec& x){
// Create holder for the returned vector.
FpVec r;
// Sample the x1.
for (const auto& i : x){
// Sample a random point.
Fp temp = pairing_group.Zp->rand();
// If the random point is the same as i, sample another one.
while (Field::cmp(i, temp)) temp = pairing_group.Zp->rand();
// Attach this value to r.
r.push_back(temp);
}
// Find x2 such that x1 + x2 = x.
for (auto i = 0; i < x.size(); ++i) r.push_back(pairing_group.Zp->sub(x[i], r[i]));
return r;
}
FpVec Helper::vec_to_fp(const BP& pairing_group, const Vec& x, const IntVec& sel){
// Create holder for the Fp result.
FpVec r;
// Depending on the input type, hash the input x vector.
std::visit([&pairing_group, &sel, &r](auto&& vec_x){
// Get the type of the input.
using T = std::decay_t<decltype(vec_x)>;
// For integer vectors.
if constexpr (std::is_same_v<T, IntVec>){
if (sel.empty()){
r = pairing_group.Zp->from_int(vec_x);
}
else{
for (const auto& i : sel){
r.push_back(pairing_group.Zp->from_int(vec_x[i]));
}
}
}
// For string vectors.
else if constexpr (std::is_same_v<T, StrVec>){
if (sel.empty()){
for (const auto& each_str : vec_x){
r.push_back(char_vec_to_fp(str_to_char_vec(each_str)));
}
}
else{
for (const auto& i : sel){
r.push_back(char_vec_to_fp(str_to_char_vec(vec_x[i])));
}
}
}
// Otherwise the type is not supported.
else{
throw std::runtime_error("Unsupported fp conversion type");
}
}, x);
return r;
}
FpMat Helper::mat_to_fp(const BP& pairing_group, const Mat& x, const IntVec& sel){
// Create holder for the Fp result.
FpMat r;
// Depending on the input type, hash the input x vector.
std::visit([&pairing_group, &sel, &r](auto&& mat_x){
// Get the type of the input.
using T = std::decay_t<decltype(mat_x)>;
// For integer matrices.
if constexpr (std::is_same_v<T, IntMat>){
if (sel.empty()){
for (const auto& i : mat_x){
r.push_back(pairing_group.Zp->from_int(i));
}
}
else{
for (const auto& i : sel){
r.push_back(pairing_group.Zp->from_int(mat_x[i]));
}
}
}
// For string matrices.
else if constexpr (std::is_same_v<T, StrMat>){
if (sel.empty()){
for (const auto& i : mat_x){
FpVec temp;
for (const auto& j : i) temp.push_back(char_vec_to_fp(str_to_char_vec(j)));
r.push_back(temp);
}
}
else{
for (const auto& i : mat_x){
FpVec temp;
for (const auto& j : sel) temp.push_back(char_vec_to_fp(str_to_char_vec(i[j])));
r.push_back(temp);
}
}
}
// Otherwise the type is not supported.
else{
throw std::runtime_error("Unsupported fp conversion type");
}
}, x);
return r;
}
IntVec Helper::get_sel_index(const int degree, const int length, const IntVec& sel){
// Create holder for the returned vector.
IntVec r;
// Add the selected index.
for (const auto i : sel) for (int j = 0; j < degree; ++j) r.emplace_back(i * degree + j);
// Add the last index.
r.push_back(degree * length);
return r;
}