forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAC_Check.cpp
510 lines (431 loc) · 14 KB
/
MAC_Check.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
// (C) 2017 University of Bristol. See License.txt
#include "Auth/MAC_Check.h"
#include "Auth/Subroutines.h"
#include "Exceptions/Exceptions.h"
#include "Tools/random.h"
#include "Tools/time-func.h"
#include "Tools/int.h"
#include "Math/gfp.h"
#include "Math/gf2n.h"
#include <algorithm>
enum mc_timer { SEND, RECV_ADD, BCAST, RECV_SUM, SEED, COMMIT, WAIT_SUMMER, RECV, SUM, SELECT, MAX_TIMER };
const char* mc_timer_names[] = {
"sending",
"receiving and adding",
"broadcasting",
"receiving summed values",
"random seed",
"commit and open",
"wait for summer thread",
"receiving",
"summing",
"waiting for select()"
};
template<class T>
MAC_Check<T>::MAC_Check(const T& ai, int opening_sum, int max_broadcast, int send_player) :
base_player(send_player), opening_sum(opening_sum), max_broadcast(max_broadcast)
{
popen_cnt=0;
alphai=ai;
values_opened=0;
timers.resize(MAX_TIMER);
}
template<class T>
MAC_Check<T>::~MAC_Check()
{
for (unsigned int i = 0; i < timers.size(); i++)
if (timers[i].elapsed() > 0)
cerr << T::type_string() << " " << mc_timer_names[i] << ": "
<< timers[i].elapsed() << endl;
for (unsigned int i = 0; i < player_timers.size(); i++)
if (player_timers[i].elapsed() > 0)
cerr << T::type_string() << " waiting for " << i << ": "
<< player_timers[i].elapsed() << endl;
}
template<class T, int t>
void add_openings(vector<T>& values, const Player& P, int sum_players, int last_sum_players, int send_player, MAC_Check<T>& MC)
{
MC.player_timers.resize(P.num_players());
vector<octetStream>& oss = MC.oss;
oss.resize(P.num_players());
vector<int> senders;
senders.reserve(P.num_players());
for (int relative_sender = positive_modulo(P.my_num() - send_player, P.num_players()) + sum_players;
relative_sender < last_sum_players; relative_sender += sum_players)
{
int sender = positive_modulo(send_player + relative_sender, P.num_players());
senders.push_back(sender);
}
for (int j = 0; j < (int)senders.size(); j++)
P.request_receive(senders[j], oss[j]);
for (int j = 0; j < (int)senders.size(); j++)
{
int sender = senders[j];
MC.player_timers[sender].start();
P.wait_receive(sender, oss[j], true);
MC.player_timers[sender].stop();
if ((unsigned)oss[j].get_length() < values.size() * T::size())
{
stringstream ss;
ss << "Not enough information received, expected "
<< values.size() * T::size() << " bytes, got "
<< oss[j].get_length();
throw Processor_Error(ss.str());
}
MC.timers[SUM].start();
for (unsigned int i=0; i<values.size(); i++)
{
values[i].template add<t>(oss[j].consume(T::size()));
}
MC.timers[SUM].stop();
}
}
template<class T>
void MAC_Check<T>::POpen_Begin(vector<T>& values,const vector<Share<T> >& S,const Player& P)
{
AddToMacs(S);
for (unsigned int i=0; i<S.size(); i++)
{ values[i]=S[i].get_share(); }
os.reset_write_head();
int sum_players = P.num_players();
int my_relative_num = positive_modulo(P.my_num() - base_player, P.num_players());
while (true)
{
int last_sum_players = sum_players;
sum_players = (sum_players - 2 + opening_sum) / opening_sum;
if (sum_players == 0)
break;
if (my_relative_num >= sum_players && my_relative_num < last_sum_players)
{
for (unsigned int i=0; i<S.size(); i++)
{ values[i].pack(os); }
int receiver = positive_modulo(base_player + my_relative_num % sum_players, P.num_players());
timers[SEND].start();
P.send_to(receiver,os,true);
timers[SEND].stop();
}
if (my_relative_num < sum_players)
{
timers[RECV_ADD].start();
if (T::t() == 2)
add_openings<T,2>(values, P, sum_players, last_sum_players, base_player, *this);
else
add_openings<T,0>(values, P, sum_players, last_sum_players, base_player, *this);
timers[RECV_ADD].stop();
}
}
if (P.my_num() == base_player)
{
os.reset_write_head();
for (unsigned int i=0; i<S.size(); i++)
{ values[i].pack(os); }
timers[BCAST].start();
for (int i = 1; i < max_broadcast && i < P.num_players(); i++)
{
P.send_to((base_player + i) % P.num_players(), os, true);
}
timers[BCAST].stop();
AddToValues(values);
}
else if (my_relative_num * max_broadcast < P.num_players())
{
int sender = (base_player + my_relative_num / max_broadcast) % P.num_players();
ReceiveValues(values, P, sender);
timers[BCAST].start();
for (int i = 0; i < max_broadcast; i++)
{
int relative_receiver = (my_relative_num * max_broadcast + i);
if (relative_receiver < P.num_players())
{
int receiver = (base_player + relative_receiver) % P.num_players();
P.send_to(receiver, os, true);
}
}
timers[BCAST].stop();
}
values_opened += S.size();
}
template<class T>
void MAC_Check<T>::POpen_End(vector<T>& values,const vector<Share<T> >& S,const Player& P)
{
S.size();
int my_relative_num = positive_modulo(P.my_num() - base_player, P.num_players());
if (my_relative_num * max_broadcast >= P.num_players())
{
int sender = (base_player + my_relative_num / max_broadcast) % P.num_players();
ReceiveValues(values, P, sender);
}
else
GetValues(values);
popen_cnt += values.size();
CheckIfNeeded(P);
/* not compatible with continuous communication
send_player++;
if (send_player==P.num_players())
{ send_player=0; }
*/
}
template<class T>
void MAC_Check<T>::AddToMacs(const vector<Share<T> >& shares)
{
for (unsigned int i = 0; i < shares.size(); i++)
macs.push_back(shares[i].get_mac());
}
template<class T>
void MAC_Check<T>::AddToValues(vector<T>& values)
{
vals.insert(vals.end(), values.begin(), values.end());
}
template<class T>
void MAC_Check<T>::ReceiveValues(vector<T>& values, const Player& P, int sender)
{
timers[RECV_SUM].start();
P.receive_player(sender, os, true);
timers[RECV_SUM].stop();
for (unsigned int i = 0; i < values.size(); i++)
values[i].unpack(os);
AddToValues(values);
}
template<class T>
void MAC_Check<T>::GetValues(vector<T>& values)
{
int size = values.size();
if (popen_cnt + size > int(vals.size()))
{
stringstream ss;
ss << "wanted " << values.size() << " values from " << popen_cnt << ", only " << vals.size() << " in store";
throw out_of_range(ss.str());
}
values.clear();
typename vector<T>::iterator first = vals.begin() + popen_cnt;
values.insert(values.end(), first, first + size);
}
template<class T>
void MAC_Check<T>::CheckIfNeeded(const Player& P)
{
if (WaitingForCheck() >= POPEN_MAX)
Check(P);
}
template <class T>
void MAC_Check<T>::AddToCheck(const T& mac, const T& value, const Player& P)
{
macs.push_back(mac);
vals.push_back(value);
popen_cnt++;
CheckIfNeeded(P);
}
template<class T>
void MAC_Check<T>::Check(const Player& P)
{
if (WaitingForCheck() == 0)
return;
//cerr << "In MAC Check : " << popen_cnt << endl;
octet seed[SEED_SIZE];
timers[SEED].start();
Create_Random_Seed(seed,P,SEED_SIZE);
timers[SEED].stop();
PRNG G;
G.SetSeed(seed);
Share<T> sj;
T a,gami,h,temp;
a.assign_zero();
gami.assign_zero();
vector<T> tau(P.num_players());
for (int i=0; i<popen_cnt; i++)
{ h.almost_randomize(G);
temp.mul(h,vals[i]);
a.add(a,temp);
temp.mul(h,macs[i]);
gami.add(gami,temp);
}
vals.erase(vals.begin(), vals.begin() + popen_cnt);
macs.erase(macs.begin(), macs.begin() + popen_cnt);
temp.mul(alphai,a);
tau[P.my_num()].sub(gami,temp);
//cerr << "\tCommit and Open" << endl;
timers[COMMIT].start();
Commit_And_Open(tau,P);
timers[COMMIT].stop();
//cerr << "\tFinal Check" << endl;
T t;
t.assign_zero();
for (int i=0; i<P.num_players(); i++)
{ t.add(t,tau[i]); }
if (!t.is_zero()) { throw mac_fail(); }
popen_cnt=0;
}
template<class T>
int mc_base_id(int function_id, int thread_num)
{
return (function_id << 28) + ((T::field_type() + 1) << 24) + (thread_num << 16);
}
template<class T>
Separate_MAC_Check<T>::Separate_MAC_Check(const T& ai, Names& Nms,
int thread_num, int opening_sum, int max_broadcast, int send_player) :
MAC_Check<T>(ai, opening_sum, max_broadcast, send_player),
check_player(Nms, mc_base_id<T>(1, thread_num))
{
}
template<class T>
void Separate_MAC_Check<T>::Check(const Player& P)
{
P.my_num();
MAC_Check<T>::Check(check_player);
}
template<class T>
void* run_summer_thread(void* summer)
{
((Summer<T>*) summer)->run();
return 0;
}
template <class T>
Parallel_MAC_Check<T>::Parallel_MAC_Check(const T& ai, Names& Nms,
int thread_num, int opening_sum, int max_broadcast, int base_player) :
Separate_MAC_Check<T>(ai, Nms, thread_num, opening_sum, max_broadcast, base_player),
send_player(Nms, mc_base_id<T>(2, thread_num)),
send_base_player(base_player)
{
int sum_players = Nms.num_players();
Player* summer_send_player = &send_player;
for (int i = 0; ; i++)
{
int last_sum_players = sum_players;
sum_players = (sum_players - 2 + opening_sum) / opening_sum;
int next_sum_players = (sum_players - 2 + opening_sum) / opening_sum;
if (sum_players == 0)
break;
Player* summer_receive_player = summer_send_player;
summer_send_player = new Player(Nms, mc_base_id<T>(3, thread_num));
summers.push_back(new Summer<T>(sum_players, last_sum_players, next_sum_players,
summer_send_player, summer_receive_player, *this));
pthread_create(&(summers[i]->thread), 0, run_summer_thread<T>, summers[i]);
}
receive_player = summer_send_player;
}
template<class T>
Parallel_MAC_Check<T>::~Parallel_MAC_Check()
{
for (unsigned int i = 0; i < summers.size(); i++)
{
summers[i]->input_queue.stop();
pthread_join(summers[i]->thread, 0);
delete summers[i];
}
}
template<class T>
void Parallel_MAC_Check<T>::POpen_Begin(vector<T>& values,
const vector<Share<T> >& S, const Player& P)
{
values.size();
this->AddToMacs(S);
int my_relative_num = positive_modulo(P.my_num() - send_base_player, P.num_players());
int sum_players = (P.num_players() - 2 + this->opening_sum) / this->opening_sum;
int receiver = positive_modulo(send_base_player + my_relative_num % sum_players, P.num_players());
// use queue rather sending to myself
if (receiver == P.my_num())
{
for (unsigned int i = 0; i < S.size(); i++)
values[i] = S[i].get_share();
summers.front()->share_queue.push(values);
}
else
{
this->os.reset_write_head();
for (unsigned int i=0; i<S.size(); i++)
S[i].get_share().pack(this->os);
this->timers[SEND].start();
send_player.send_to(receiver,this->os,true);
this->timers[SEND].stop();
}
for (unsigned int i = 0; i < summers.size(); i++)
summers[i]->input_queue.push(S.size());
this->values_opened += S.size();
send_base_player = (send_base_player + 1) % send_player.num_players();
}
template<class T>
void Parallel_MAC_Check<T>::POpen_End(vector<T>& values,
const vector<Share<T> >& S, const Player& P)
{
int last_size = 0;
this->timers[WAIT_SUMMER].start();
summers.back()->output_queue.pop(last_size);
this->timers[WAIT_SUMMER].stop();
if (int(values.size()) != last_size)
{
stringstream ss;
ss << "stopopen wants " << values.size() << " values, but I have " << last_size << endl;
throw Processor_Error(ss.str().c_str());
}
if (this->base_player == P.my_num())
{
value_queue.pop(values);
if (int(values.size()) != last_size)
throw Processor_Error("wrong number of local values");
else
this->AddToValues(values);
}
this->MAC_Check<T>::POpen_End(values, S, *receive_player);
this->base_player = (this->base_player + 1) % send_player.num_players();
}
template<class T>
Direct_MAC_Check<T>::Direct_MAC_Check(const T& ai, Names& Nms, int num) : Separate_MAC_Check<T>(ai, Nms, num) {
open_counter = 0;
}
template<class T>
Direct_MAC_Check<T>::~Direct_MAC_Check() {
cerr << T::type_string() << " open counter: " << open_counter << endl;
}
template<class T>
void Direct_MAC_Check<T>::POpen_Begin(vector<T>& values,const vector<Share<T> >& S,const Player& P)
{
values.size();
this->os.reset_write_head();
for (unsigned int i=0; i<S.size(); i++)
S[i].get_share().pack(this->os);
this->timers[SEND].start();
P.send_all(this->os,true);
this->timers[SEND].stop();
this->AddToMacs(S);
for (unsigned int i=0; i<S.size(); i++)
this->vals.push_back(S[i].get_share());
}
template<class T, int t>
void direct_add_openings(vector<T>& values, const Player& P, vector<octetStream>& os)
{
for (unsigned int i=0; i<values.size(); i++)
for (int j=0; j<P.num_players(); j++)
if (j!=P.my_num())
values[i].template add<t>(os[j].consume(T::size()));
}
template<class T>
void Direct_MAC_Check<T>::POpen_End(vector<T>& values,const vector<Share<T> >& S,const Player& P)
{
S.size();
oss.resize(P.num_players());
this->GetValues(values);
this->timers[RECV].start();
for (int j=0; j<P.num_players(); j++)
if (j!=P.my_num())
P.receive_player(j,oss[j],true);
this->timers[RECV].stop();
open_counter++;
if (T::t() == 2)
direct_add_openings<T,2>(values, P, oss);
else
direct_add_openings<T,0>(values, P, oss);
for (unsigned int i = 0; i < values.size(); i++)
this->vals[this->popen_cnt+i] = values[i];
this->popen_cnt += values.size();
this->CheckIfNeeded(P);
}
template class MAC_Check<gfp>;
template class Direct_MAC_Check<gfp>;
template class Parallel_MAC_Check<gfp>;
template class MAC_Check<gf2n>;
template class Direct_MAC_Check<gf2n>;
template class Parallel_MAC_Check<gf2n>;
#ifdef USE_GF2N_LONG
template class MAC_Check<gf2n_short>;
template class Direct_MAC_Check<gf2n_short>;
template class Parallel_MAC_Check<gf2n_short>;
#endif