-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathShareThread.hpp
More file actions
403 lines (352 loc) · 10.4 KB
/
ShareThread.hpp
File metadata and controls
403 lines (352 loc) · 10.4 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
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
/*
* MalicousRepParty.cpp
*
*/
#ifndef GC_SHARETHREAD_HPP_
#define GC_SHARETHREAD_HPP_
#include <GC/ShareThread.h>
#include "GC/ShareParty.h"
#include "BitPrepFiles.h"
#include "Math/Setup.h"
#include "Tools/DoubleRange.h"
#include "Processor/Data_Files.hpp"
namespace GC
{
template<class T>
StandaloneShareThread<T>::StandaloneShareThread(int i, ThreadMaster<T>& master) :
ShareThread<T>(*Preprocessing<T>::get_new(master.opts.live_prep,
master.N, usage)),
Thread<T>(i, master)
{
}
template<class T>
StandaloneShareThread<T>::~StandaloneShareThread()
{
delete &this->DataF;
}
template<class T>
ShareThread<T>::ShareThread(Preprocessing<T>& prep) :
P(0), MC(0), protocol(0), DataF(prep)
{
}
template<class T>
ShareThread<T>::ShareThread(Preprocessing<T>& prep, Player& P,
typename T::mac_key_type mac_key) :
ShareThread(prep)
{
pre_run(P, mac_key);
}
template<class T>
ShareThread<T>::~ShareThread()
{
if (MC)
delete MC;
if (protocol)
delete protocol;
}
template<class T>
void ShareThread<T>::pre_run(Player& P, typename T::mac_key_type mac_key)
{
this->P = &P;
if (singleton)
throw runtime_error("there can only be one");
singleton = this;
protocol = new typename T::Protocol(*this->P);
MC = this->new_mc(mac_key);
DataF.set_protocol(*this->protocol);
this->protocol->init(DataF, *MC);
}
template<class T>
void StandaloneShareThread<T>::pre_run()
{
ShareThread<T>::pre_run(*Thread<T>::P, ShareParty<T>::s().mac_key);
usage.set_num_players(Thread<T>::P->num_players());
}
template<class T>
void ShareThread<T>::post_run()
{
check();
}
template<class T>
void ShareThread<T>::check()
{
protocol->check();
MC->Check(*this->P);
}
template<class T>
class BitOpTuple
{
size_t n_bits, dest, left, right;
public:
static const int n = 4;
BitOpTuple(vector<int>::const_iterator it) :
n_bits(*it++), dest(*it++), left(*it++), right(*it++)
{
}
size_t n_blocks()
{
return DIV_CEIL(n_bits, T::default_length);
}
size_t n_full_blocks()
{
return n_bits / T::default_length;
}
DoubleRange<T> input_range(StackedVector<T>& S)
{
return {S, left, right, n_blocks()};
}
DoubleRange<T> full_block_input_range(StackedVector<T>& S)
{
return {S, left, right, n_full_blocks()};
}
Range<StackedVector<T>> full_block_left_range(StackedVector<T>& S)
{
return {S, left, n_full_blocks()};
}
DoubleIterator<T> partial_block(StackedVector<T>& S)
{
assert(n_blocks() != n_full_blocks());
return {S.iterator_for_size(left + n_full_blocks(), 1),
S.iterator_for_size(right + n_full_blocks(), 1)};
}
typename CheckVector<T>::iterator partial_left_block(StackedVector<T>& S)
{
assert(n_blocks() != n_full_blocks());
return S.iterator_for_size(left + n_full_blocks(), 1);
}
T& get_right_base(StackedVector<T>& S)
{
return S[right];
}
Range<StackedVector<T>> full_block_output_range(StackedVector<T>& S)
{
return {S, dest, n_full_blocks()};
}
T& partial_output(StackedVector<T>& S)
{
assert(n_blocks() != n_full_blocks());
return S[dest + n_full_blocks()];
}
int last_length()
{
if (n_blocks() == n_full_blocks())
return 0;
else
return n_bits % T::default_length;
}
};
template<class T>
void ShareThread<T>::and_(Processor<T>& processor,
const vector<int>& args, bool repeat)
{
vector<int> active_args;
int active_limit = -1;
long prefix = processor.get_arg().get();
if (prefix < 0)
active_limit = int(-prefix);
if (active_limit >= 0)
{
active_args.reserve(args.size());
for (auto it = args.begin(); it < args.end(); it += 4)
{
int active_bits = min(*it, active_limit);
if (active_bits > 0)
{
active_args.push_back(active_bits);
active_args.push_back(*(it + 1));
active_args.push_back(*(it + 2));
active_args.push_back(*(it + 3));
}
}
}
else
{
active_args = args;
}
auto& protocol = this->protocol;
auto& S = processor.S;
processor.check_args(active_args, 4);
protocol->init_mul();
T x_ext, y_ext;
size_t total_bits = 0;
for (auto it = active_args.begin(); it < active_args.end(); it += 4)
total_bits += *it;
// accept 10 % waste
bool fast_mode = 0.1 * total_bits > active_args.size() / 4 * T::default_length;
if (fast_mode)
{
protocol->set_fast_mode(true);
}
ArgList<BitOpTuple<T>> infos(active_args);
if (repeat)
for (auto info : infos)
{
int n = T::default_length;
auto& y = info.get_right_base(S);
for (auto x : info.full_block_left_range(S))
{
y.extend_bit(y_ext, n);
protocol->prepare_mult(x, y_ext, n, true);
}
n = info.last_length();
if (n)
{
info.partial_left_block(S)->mask(x_ext, n);
y.extend_bit(y_ext, n);
protocol->prepare_mult(x_ext, y_ext, n, true);
}
}
else
for (auto info : infos)
{
if (fast_mode)
for (auto x : info.full_block_input_range(S))
protocol->prepare_mul_fast(x.first, x.second);
else if (info.n_full_blocks())
for (auto x : info.full_block_input_range(S))
protocol->prepare_mul(x.first, x.second);
int n = info.last_length();
if (n)
{
info.partial_block(S).left->mask(x_ext, n);
info.partial_block(S).right->mask(y_ext, n);
protocol->prepare_mult(x_ext, y_ext, n, false);
}
}
if (OnlineOptions::singleton.has_option("verbose_and"))
fprintf(stderr, "%zu%s ANDs\n", total_bits, repeat ? " repeat" : "");
protocol->exchange();
if (repeat)
for (auto info : infos)
{
int n = T::default_length;
for (auto& res : info.full_block_output_range(S))
protocol->finalize_mult(res, n);
n = info.last_length();
if (n)
{
protocol->finalize_mult(info.partial_output(S), n);
}
}
else
for (auto info : infos)
{
if (fast_mode)
for (auto& res : info.full_block_output_range(S))
res = protocol->finalize_mul_fast();
else if (info.n_full_blocks())
for (auto& res : info.full_block_output_range(S))
res = protocol->finalize_mul();
int n = info.last_length();
if (n)
{
protocol->finalize_mult(info.partial_output(S), n);
}
}
if (OnlineOptions::singleton.has_option("always_check"))
protocol->check();
protocol->set_fast_mode(false);
}
template<class T>
void ShareThread<T>::andrsvec(Processor<T>& processor, const vector<int>& args)
{
int N_BITS = T::default_length;
auto& protocol = this->protocol;
assert(protocol);
vector<int> active_args;
int active_limit = -1;
long prefix = processor.get_arg().get();
if (prefix < 0)
active_limit = int(-prefix);
if (active_limit >= 0)
{
active_args.reserve(args.size());
auto it = args.begin();
while (it < args.end())
{
int n_args = (*it - 3) / 2;
int size = *(it + 1);
int active_size = min(size, active_limit);
if (active_size > 0)
{
int group_size = 2 * n_args + 3;
active_args.push_back(*it);
active_args.push_back(active_size);
for (int i = 0; i < group_size - 2; i++)
active_args.push_back(*(it + 2 + i));
}
it += 2 * n_args + 3;
}
}
else
{
active_args = args;
}
protocol->init_mul();
auto it = active_args.begin();
T x_ext, y_ext;
int total_bits = 0;
while (it < active_args.end())
{
int n_args = (*it++ - 3) / 2;
int size = *it++;
total_bits += size * n_args;
it += n_args;
int base = *it++;
for (int i = 0; i < size; i += N_BITS)
{
int n_ops = min(N_BITS, size - i);
for (int j = 0; j < n_args; j++)
{
processor.S.at(*(it + j) + i / N_BITS).mask(x_ext, n_ops);
processor.S.at(base + i / N_BITS).mask(y_ext, n_ops);
protocol->prepare_mul(x_ext, y_ext, n_ops);
}
}
it += n_args;
}
if (OnlineOptions::singleton.has_option("verbose_and"))
fprintf(stderr, "%d repeat ANDs\n", total_bits);
protocol->exchange();
it = active_args.begin();
while (it < active_args.end())
{
int n_args = (*it++ - 3) / 2;
int size = *it++;
for (int i = 0; i < size; i += N_BITS)
{
int n_ops = min(N_BITS, size - i);
for (int j = 0; j < n_args; j++)
protocol->finalize_mul(n_ops).mask(
processor.S.at(*(it + j) + i / N_BITS), n_ops);
}
it += 2 * n_args + 1;
}
if (OnlineOptions::singleton.has_option("always_check"))
protocol->check();
}
template<class T>
void ShareThread<T>::xors(Processor<T>& processor, const vector<int>& args)
{
processor.check_args(args, 4);
auto it = args.begin();
while (it < args.end())
{
int n_bits = *it++;
if (n_bits == 1)
processor.S[*it++].xor_(1, processor.S[*it++], processor.S[*it++]);
else
{
int size = DIV_CEIL(n_bits, T::default_length);
auto out = processor.S.iterator_for_size(*it++, size);
auto left = processor.S.iterator_for_size(*it++, size);
auto right = processor.S.iterator_for_size(*it++, size);
for (int j = 0; j < size - 1; j++)
(*out++).xor_(T::default_length, *left++, *right++);
int n_bits_left = n_bits - (size - 1) * T::default_length;
(*out++).xor_(n_bits_left, *left++, *right++);
}
}
}
} /* namespace GC */
#endif