forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAC_Check.h
365 lines (298 loc) · 9.63 KB
/
MAC_Check.h
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
#ifndef _MAC_Check
#define _MAC_Check
/* Class for storing MAC Check data and doing the Check */
#include <vector>
#include <deque>
using namespace std;
#include "Protocols/Share.h"
#include "Networking/Player.h"
#include "Protocols/MAC_Check_Base.h"
#include "Tools/time-func.h"
#include "Tools/Coordinator.h"
/* The MAX number of things we will partially open before running
* a MAC Check
*
* Keep this at much less than 1MB of data to be able to cope with
* multi-threaded players
*
*/
#define POPEN_MAX 1000000
/**
* Sum and broadcast values via a tree of players
*/
template<class T>
class TreeSum
{
static const char* mc_timer_names[];
void start(vector<T>& values, const Player& P);
void finish(vector<T>& values, const Player& P);
protected:
int base_player;
int opening_sum;
int max_broadcast;
octetStream os;
void ReceiveValues(vector<T>& values, const Player& P, int sender);
virtual void AddToValues(vector<T>& values) { (void)values; }
public:
vector<octetStream> oss;
vector<Timer> timers;
vector<Timer> player_timers;
TreeSum(int opening_sum = 10, int max_broadcast = 10, int base_player = 0);
virtual ~TreeSum();
void run(vector<T>& values, const Player& P);
T run(const T& value, const Player& P);
octetStream& get_buffer() { return os; }
size_t report_size(ReportType type);
};
template<class U>
class Tree_MAC_Check : public TreeSum<typename U::open_type>, public MAC_Check_Base<U>
{
typedef typename U::open_type T;
template<class V> friend class Tree_MAC_Check;
protected:
static Coordinator* coordinator;
/* POpen Data */
int popen_cnt;
vector<typename U::mac_type> macs;
vector<T> vals;
void AddToValues(vector<T>& values);
void CheckIfNeeded(const Player& P);
int WaitingForCheck()
{ return max(macs.size(), vals.size()); }
public:
static void setup(Player& P);
static void teardown();
Tree_MAC_Check(const typename U::mac_key_type::Scalar& ai, int opening_sum = 10,
int max_broadcast = 10, int send_player = 0);
virtual ~Tree_MAC_Check();
virtual void init_open(const Player& P, int n = 0);
virtual void prepare_open(const U& secret, int = -1);
virtual void exchange(const Player& P);
virtual void AddToCheck(const U& share, const T& value, const Player& P);
virtual void Check(const Player& P) = 0;
// compatibility
void set_random_element(const U& random_element) { (void) random_element; }
};
template<class U>
Coordinator* Tree_MAC_Check<U>::coordinator = 0;
/**
* SPDZ opening protocol with MAC check (indirect communication)
*/
template<class U>
class MAC_Check_ : public Tree_MAC_Check<U>
{
public:
MAC_Check_(const typename U::mac_key_type::Scalar& ai, int opening_sum = 10,
int max_broadcast = 10, int send_player = 0);
virtual ~MAC_Check_() {}
virtual void Check(const Player& P);
};
template<class T>
using MAC_Check = MAC_Check_<Share<T>>;
template<int K, int S> class Spdz2kShare;
template<class T> class Spdz2kPrep;
template<class T> class MascotPrep;
/**
* SPDZ2k opening protocol with MAC check
*/
template<class T, class U, class V, class W>
class MAC_Check_Z2k : public Tree_MAC_Check<W>
{
protected:
Preprocessing<W>* prep;
W get_random_element();
public:
vector<W> random_elements;
MAC_Check_Z2k(const T& ai, int opening_sum=10, int max_broadcast=10, int send_player=0);
MAC_Check_Z2k(const T& ai, Names& Nms, int thread_num);
void prepare_open(const W& secret, int = -1);
void prepare_open_no_mask(const W& secret);
virtual void Check(const Player& P);
void set_random_element(const W& random_element);
void set_prep(Preprocessing<W>& prep);
virtual ~MAC_Check_Z2k() {};
};
template<class W>
using MAC_Check_Z2k_ = MAC_Check_Z2k<typename W::open_type,
typename W::mac_key_type, typename W::open_type, W>;
template<class T>
void add_openings(vector<T>& values, const Player& P, int sum_players, int last_sum_players, int send_player, TreeSum<T>& MC);
/**
* SPDZ opening protocol with MAC check (pairwise communication)
*/
template<class T>
class Direct_MAC_Check: public MAC_Check_<T>
{
typedef MAC_Check_<T> super;
typedef typename T::open_type open_type;
int open_counter;
vector<octetStream> oss;
protected:
void pre_exchange(const Player& P);
public:
// legacy interface
Direct_MAC_Check(const typename T::mac_key_type::Scalar& ai, Names& Nms, int thread_num);
Direct_MAC_Check(const typename T::mac_key_type::Scalar& ai);
~Direct_MAC_Check();
void init_open(const Player& P, int n = 0);
void prepare_open(const T& secret, int = -1);
void exchange(const Player& P);
};
enum mc_timer { SEND, RECV_ADD, BCAST, RECV_SUM, SEED, COMMIT, WAIT_SUMMER, RECV, SUM, SELECT, MAX_TIMER };
template<class T>
TreeSum<T>::TreeSum(int opening_sum, int max_broadcast, int base_player) :
base_player(base_player), opening_sum(opening_sum), max_broadcast(max_broadcast)
{
timers.resize(MAX_TIMER);
}
template<class T>
TreeSum<T>::~TreeSum()
{
#ifdef TREESUM_TIMINGS
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;
#endif
}
template<class T>
void TreeSum<T>::run(vector<T>& values, const Player& P)
{
start(values, P);
finish(values, P);
}
template<class T>
T TreeSum<T>::run(const T& value, const Player& P)
{
vector<T> values = {value};
run(values, P);
return values[0];
}
template<class T>
size_t TreeSum<T>::report_size(ReportType type)
{
if (type == CAPACITY)
return os.get_max_length();
else
return os.get_length();
}
template<class T>
void add_openings(vector<T>& values, const Player& P, int sum_players, int last_sum_players, int send_player, TreeSum<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]);
MC.player_timers[sender].stop();
MC.timers[SUM].start();
for (unsigned int i=0; i<values.size(); i++)
{
values[i].add(oss[j]);
}
MC.timers[SUM].stop();
}
}
template<class T>
void TreeSum<T>::start(vector<T>& values, const Player& P)
{
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)
{
// summing phase
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)
{
// send to the player up the tree
for (unsigned int i=0; i<values.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);
timers[SEND].stop();
}
if (my_relative_num < sum_players)
{
// if receiving, add the values
timers[RECV_ADD].start();
add_openings<T>(values, P, sum_players, last_sum_players, base_player, *this);
timers[RECV_ADD].stop();
}
}
if (P.my_num() == base_player)
{
// send from the root player
os.reset_write_head();
size_t n = values.size();
for (unsigned int i=0; i<n; 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);
}
timers[BCAST].stop();
AddToValues(values);
}
else if (my_relative_num * max_broadcast < P.num_players())
{
// send if there are children
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);
}
}
timers[BCAST].stop();
}
}
template<class T>
void TreeSum<T>::finish(vector<T>& values, const Player& P)
{
int my_relative_num = positive_modulo(P.my_num() - base_player, P.num_players());
if (my_relative_num * max_broadcast >= P.num_players())
{
// receiving at the leafs
int sender = (base_player + my_relative_num / max_broadcast) % P.num_players();
ReceiveValues(values, P, sender);
}
}
template<class T>
void TreeSum<T>::ReceiveValues(vector<T>& values, const Player& P, int sender)
{
timers[RECV_SUM].start();
P.receive_player(sender, os);
timers[RECV_SUM].stop();
for (unsigned int i = 0; i < values.size(); i++)
values[i].unpack(os);
AddToValues(values);
}
#endif