-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathristretto_vector.cpp
More file actions
292 lines (256 loc) · 7.61 KB
/
ristretto_vector.cpp
File metadata and controls
292 lines (256 loc) · 7.61 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
////
//// Created by yizheng on 14/3/23.
////
#include <cassert>
#include "include/ristretto_vector.h"
std::vector<unsigned char> bytes_from_rist_elem_vec(const RistElemVec &gg) {
std::vector<unsigned char> gg_bytes(gg.size() * RISTBYTES);
for (int i = 0; i < gg.size(); i++) {
memcpy(gg_bytes.data() + i * RISTBYTES, gg[i].element.data(),
RISTBYTES);
}
return gg_bytes;
}
std::vector<unsigned char> bytes_from_rist_scal_vec(const RistScalVec &gg) {
std::vector<unsigned char> gg_bytes(gg.size() * RISTSCALBYTES);
for (int i = 0; i < gg.size(); i++) {
memcpy(gg_bytes.data() + i * RISTSCALBYTES, gg[i].scalar.data(),
RISTSCALBYTES);
}
return gg_bytes;
}
RistElemVec rist_elem_vec_from_bytes(const std::vector<unsigned char> &vv) {
RistElemVec gg(vv.size() / RISTBYTES);
for (int i = 0; i < gg.size(); i++) {
memcpy(gg[i].element.data(), vv.data() + i * RISTBYTES, RISTBYTES);
}
return gg;
}
RistScalVec rist_scal_vec_from_bytes(const std::vector<unsigned char> &vv) {
RistScalVec gg(vv.size() / RISTSCALBYTES);
for (int i = 0; i < gg.size(); i++) {
memcpy(gg[i].scalar.data(), vv.data() + i * RISTSCALBYTES,
RISTSCALBYTES);
}
return gg;
}
RistScalVec rist_scal_vec_from_int_vec(const std::vector<int> &s) {
RistScalVec ret(s.size());
for (int i = 0; i < s.size(); i++) {
ret[i] = RistScal(s[i]);
}
return ret;
}
RistScalVec rist_scal_vec_from_long_vec(const std::vector<long> &s) {
RistScalVec ret(s.size());
for (int i = 0; i < s.size(); i++) {
ret[i] = RistScal(s[i]);
}
return ret;
}
void export_to_bytestream(const RistScalVec &rr, std::vector<unsigned char>::iterator &it) {
for (auto &&r: rr)
r.export_to_bytestream(it);
}
void import_from_bytestream(RistScalVec &rr, std::vector<unsigned char>::const_iterator &it) {
for (auto &&r: rr)
r.import_from_bytestream(it);
}
void export_to_bytestream(const RistScalMat &rr, std::vector<unsigned char>::iterator &it) {
for (auto &&r: rr)
export_to_bytestream(r, it);
}
void import_from_bytestream(RistScalMat &rr, std::vector<unsigned char>::const_iterator &it) {
for (auto &&r: rr)
import_from_bytestream(r, it);
}
void rand_init(RistElemVec &pp) {
std::for_each(
std::execution::par_unseq,
pp.begin(),
pp.end(),
[](auto &&p) {
rand_init(p);
}
);
}
void hash_init(RistElemVec &pp, const RistHashbytesVec &rr) {
std::for_each(
std::execution::par_unseq,
pp.begin(),
pp.end(),
[&pp, &rr](auto &&p) {
auto i = &p - pp.data();
hash_init(p, rr[i]);
}
);
}
void add(RistElemVec &rr, const RistElemVec &pp, const RistElemVec &qq) {
std::for_each(
std::execution::par_unseq,
rr.begin(),
rr.end(),
[&rr, &pp, &qq](auto &&r) {
auto i = &r - rr.data();
add(rr[i], pp[i], qq[i]);
}
);
}
void scalar_mult(RistElemVec &qq, const RistScalVec &nn, const RistElemVec pp) {
assert(nn.size() == pp.size());
std::for_each(
std::execution::par_unseq,
qq.begin(),
qq.end(),
[&qq, &nn, &pp](auto &&q) {
auto i = &q - qq.data();
scalar_mult(qq[i], nn[i], pp[i]);
}
);
}
RistElemVec operator*(const RistScalVec &nn, const RistElemVec pp) {
RistElemVec qq(nn.size());
scalar_mult(qq, nn, pp);
return qq;
}
void scalar_mult_base(RistElemVec &qq, const RistScalVec &nn) {
std::for_each(
std::execution::par_unseq,
qq.begin(),
qq.end(),
[&qq, &nn](auto &&q) {
auto i = &q - qq.data();
scalar_mult_base(qq[i], nn[i]);
}
);
}
void rand_init(RistScalVec &pp) {
std::for_each(
std::execution::par_unseq,
pp.begin(),
pp.end(),
[](auto &&p) {
rand_init(p);
}
);
}
void scalar_reduce(RistScalVec &rr, const RistNonreducedScalarVec &ss) {
std::for_each(
std::execution::par_unseq,
rr.begin(),
rr.end(),
[&rr, &ss](auto &&r) {
auto i = &r - rr.data();
scalar_reduce(rr[i], ss[i]);
}
);
}
void scalar_reduce(RistScalVec &rr, const RistHashbytesVec &ss) {
scalar_reduce(rr, *reinterpret_cast<const RistNonreducedScalarVec *>(&ss));
}
void scalar_add(RistScalVec &zz, const RistScalVec &xx, const RistScalVec &yy) {
assert(xx.size() == yy.size());
std::for_each(
std::execution::par_unseq,
zz.begin(),
zz.end(),
[&zz, &xx, &yy](auto &&z) {
auto i = &z - zz.data();
scalar_add(zz[i], xx[i], yy[i]);
}
);
}
RistScalVec operator+(const RistScalVec &xx, const RistScalVec &yy) {
RistScalVec zz(xx.size());
scalar_add(zz, xx, yy);
return zz;
}
void scalar_sub(RistScalVec &zz, const RistScalVec &xx, const RistScalVec &yy) {
std::for_each(
std::execution::par_unseq,
zz.begin(),
zz.end(),
[&zz, &xx, &yy](auto &&z) {
auto i = &z - zz.data();
scalar_sub(zz[i], xx[i], yy[i]);
}
);
}
RistScalVec operator-(const RistScalVec &xx, const RistScalVec &yy) {
RistScalVec zz(xx.size());
scalar_sub(zz, xx, yy);
return zz;
}
RistScalVec operator-(const RistScalVec &xx) {
RistScalVec zz(xx.size());
for (int i = 0; i < xx.size(); i++) {
zz[i] = -xx[i];
}
return zz;
}
void scalar_mul(RistScalVec &zz, const RistScalVec &xx, const RistScalVec &yy) {
assert(xx.size() == yy.size());
std::for_each(
std::execution::par_unseq,
zz.begin(),
zz.end(),
[&zz, &xx, &yy](auto &&z) {
auto i = &z - zz.data();
scalar_mul(zz[i], xx[i], yy[i]);
}
);
}
RistScalVec operator*(const RistScalVec &xx, const RistScalVec &yy) {
RistScalVec zz{xx.size()};
scalar_mul(zz, xx, yy);
return zz;
}
RistScalVec operator*(const RistScal &x, const RistScalVec &yy) {
RistScalVec zz{yy.size()};
std::for_each(
std::execution::par_unseq,
zz.begin(),
zz.end(),
[&zz, &x, &yy](auto &&z) {
auto i = &z - zz.data();
scalar_mul(zz[i], x, yy[i]);
}
);
return zz;
}
RistElem sum(const RistElemVec &pp) {
return std::reduce(std::execution::par_unseq,
pp.begin(), pp.end());
}
RistScal sum(const RistScalVec &pp) {
return std::reduce(std::execution::par_unseq,
pp.begin(), pp.end());
}
RistElem linear_comb(const RistScalVec &nn, const RistElemVec &pp) {
return sum(nn * pp);
}
RistScalMat operator*(const RistScal &r, const RistScalMat &B) {
RistScalMat ret;
ret.reserve(B.size());
for (int i = 0; i < B.size(); i++) {
ret.emplace_back(r * B[i]);
}
return ret;
}
RistScalMat operator+(const RistScalMat &A, const RistScalMat &B) {
assert(A.size() == B.size());
RistScalMat ret;
ret.reserve(B.size());
for (int i = 0; i < B.size(); i++) {
ret.emplace_back(A[i] + B[i]);
}
return ret;
}
RistScalMat operator-(const RistScalMat &A) {
RistScalMat ret;
ret.reserve(A.size());
for (int i = 0; i < A.size(); i++) {
ret.emplace_back(-A[i]);
}
return ret;
}