-
Notifications
You must be signed in to change notification settings - Fork 3
/
protocol_executer.hpp
414 lines (368 loc) · 12.9 KB
/
protocol_executer.hpp
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
#pragma once
#include "core/init.hpp"
#include "protocols/Protocols.h"
#if FUNCTION_IDENTIFIER < 8
#include "programs/benchmarks/bench_basic_primitives.hpp"
#elif FUNCTION_IDENTIFIER < 13
#include "programs/benchmarks/bench_rounds.hpp"
#elif FUNCTION_IDENTIFIER < 24
#include "programs/benchmarks/bench_statistics.hpp"
#elif FUNCTION_IDENTIFIER < 33
#include "programs/benchmarks/bench_use_cases.hpp"
#elif FUNCTION_IDENTIFIER < 48
#include "programs/benchmarks/bench_nn.hpp"
#elif FUNCTION_IDENTIFIER < 54
#include "programs/benchmarks/bench_conv_alt.hpp"
#elif FUNCTION_IDENTIFIER == 54
#include "programs/tests/test_basic_primitives.hpp"
#elif FUNCTION_IDENTIFIER == 55
#include "programs/tests/test_fixed_point_arithmetic.hpp"
#elif FUNCTION_IDENTIFIER == 56
#include "programs/tests/test_truncation.hpp"
#elif FUNCTION_IDENTIFIER == 57
#include "programs/tests/test_mat_mul.hpp"
#elif FUNCTION_IDENTIFIER == 58
#include "programs/tests/test_multi_input.hpp"
#elif FUNCTION_IDENTIFIER == 59
#include "programs/tests/test_comparisons.hpp"
#elif FUNCTION_IDENTIFIER == 60
#include "programs/tests/test_all.hpp"
#elif FUNCTION_IDENTIFIER == 61
#include "programs/tutorials/basic_tutorial.hpp"
#elif FUNCTION_IDENTIFIER == 62
#include "programs/tutorials/fixed_point_tutorial.hpp"
#elif FUNCTION_IDENTIFIER == 63
#include "programs/tutorials/mixed_circuits_tutorial.hpp"
#elif FUNCTION_IDENTIFIER == 64
#include "programs/tutorials/matrix_operations_tutorial.hpp"
#elif FUNCTION_IDENTIFIER == 65
#include "programs/tutorials/YourFirstProgram.hpp"
#elif FUNCTION_IDENTIFIER < 400
#include "programs/NN.hpp"
#elif FUNCTION_IDENTIFIER >= 500 && FUNCTION_IDENTIFIER <= 534
#include "programs/functions/mpspdz.hpp"
#endif
void init_circuit(std::string ips[]) {
#if PRINT == 1
print("Initializing circuit ...\n");
#endif
sockets_received.push_back(0);
for (int t = 0; t < (num_players - 1); t++) {
#if LIVE == 1
receiving_args[t].elements_to_rec.push_back(0);
sending_args[t].elements_to_send.push_back(0);
#endif
#if PRE == 1
receiving_args_pre[t].elements_to_rec.push_back(0);
sending_args_pre[t].elements_to_send.push_back(0);
receiving_args_pre[t].rec_rounds = 1;
sending_args_pre[t].send_rounds = 1;
#endif
}
#if INIT == 1 && NO_INI == 0
auto garbage = new RESULTTYPE;
FUNCTION<PROTOCOL_INIT<DATATYPE>>(garbage);
#if MAL == 1
compare_views_init();
#endif
#if PRE == 1
PROTOCOL_INIT<DATATYPE>::finalize(ips, receiving_args_pre, sending_args_pre);
#else
PROTOCOL_INIT<DATATYPE>::finalize(ips); // TODO change to new version
#endif
#endif
#if LIVE == 1 && INIT == 0 && NO_INI == 0
init_from_file();
finalize(ips);
#endif
#if TRUNC_DELAYED == 1
delayed = false;
#endif
}
#if PRE == 1
void preprocess_circuit(std::string ips[]) {
pthread_t sending_Threads_pre[num_players - 1];
pthread_t receiving_threads_pre[num_players - 1];
int ret_pre;
for (int t = 0; t < (num_players - 1); t++) {
ret_pre = pthread_create(&receiving_threads_pre[t], NULL, receiver,
&receiving_args_pre[t]);
if (ret_pre) {
print("ERROR; return code from pthread_create() is %d\n", ret_pre);
exit(-1);
}
}
/// Creating sending threads
for (int t = 0; t < (num_players - 1); t++) {
ret_pre = pthread_create(&sending_Threads_pre[t], NULL, sender,
&sending_args_pre[t]);
if (ret_pre) {
print("ERROR; return code from pthread_create() is %d\n", ret_pre);
exit(-1);
}
}
// waiting until all threads connected
// #endif
pthread_mutex_lock(&mtx_connection_established);
while (num_successful_connections < 2 * (num_players - 1)) {
pthread_cond_wait(&cond_successful_connection, &mtx_connection_established);
}
num_successful_connections = -1;
pthread_cond_broadcast(
&cond_start_signal); // signal all threads to start receiving
pthread_mutex_unlock(&mtx_connection_established);
print("All parties connected sucessfully, starting protocol and timer! \n");
#if PRINT == 1
print("Preprocessing phase ...\n");
#endif
clock_t time_pre_function_start = clock();
clock_gettime(CLOCK_REALTIME, &p1);
std::chrono::high_resolution_clock::time_point p =
std::chrono::high_resolution_clock::now();
#if PROTOCOL_PRE == -1
// receive only
#else
/* auto p_pre = PROTOCOL_PRE(); */
auto garbage_PRE = new RESULTTYPE;
FUNCTION<PROTOCOL_PRE<DATATYPE>>(garbage_PRE);
#endif
// manual send
sb = 0;
pthread_mutex_lock(&mtx_send_next);
sending_rounds += 1;
pthread_cond_broadcast(&cond_send_next); // signal all threads that sending
// buffer contains next data
pthread_mutex_unlock(&mtx_send_next);
// manual receive
rounds += 1;
// receive_data
// wait until all sockets have finished received their last data
pthread_mutex_lock(&mtx_receive_next);
while (rounds > receiving_rounds) // wait until all threads received their
// data
pthread_cond_wait(&cond_receive_next, &mtx_receive_next);
pthread_mutex_unlock(&mtx_receive_next);
rb = 0;
// Join threads to avoid address rebind
for (int t = 0; t < (num_players - 1); t++) {
pthread_join(receiving_threads_pre[t], NULL);
pthread_join(sending_Threads_pre[t], NULL);
}
double time_pre = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - p)
.count();
/* searchComm__<Sharemind,DATATYPE>(protocol,*found); */
clock_gettime(CLOCK_REALTIME, &p2);
double accum_pre = (p2.tv_sec - p1.tv_sec) +
(double)(p2.tv_nsec - p1.tv_nsec) / (double)1000000000L;
clock_t time_pre_function_finished = clock();
print("Time measured to perform preprocessing clock: %fs \n",
double((time_pre_function_finished - time_pre_function_start)) /
CLOCKS_PER_SEC);
print("Time measured to perform preprocessing getTime: %fs \n", accum_pre);
print("Time measured to perform preprocessing chrono: %fs \n",
time_pre / 1000000);
#if LIVE == 1
// reset all variables
num_successful_connections = 0;
std::fill(sockets_received.begin(), sockets_received.end(), 0);
share_buffer[0] = 0;
share_buffer[1] = 0;
send_count[0] = 0;
send_count[1] = 0;
rb = 0;
sb = 0;
rounds = 0;
sending_rounds = 0;
receiving_rounds = 0;
#if INIT == 0 && NO_INI == 0
init_from_file();
finalize(ips);
#else
auto p_init = PROTOCOL_INIT<DATATYPE>();
p_init.finalize(ips);
#endif
#endif
#if TRUNC_DELAYED == 1
delayed = false;
#endif
}
#endif
#if LIVE == 1
void live_circuit() {
pthread_t sending_Threads[num_players - 1];
pthread_t receiving_threads[num_players - 1];
int ret;
// TODO check, recently commented
for (int t = 0; t < (num_players - 1); t++) {
ret = pthread_create(&receiving_threads[t], NULL, receiver,
&receiving_args[t]);
if (ret) {
print("ERROR; return code from pthread_create() is %d\n", ret);
exit(-1);
}
}
/// Creating sending threads
for (int t = 0; t < (num_players - 1); t++) {
ret = pthread_create(&sending_Threads[t], NULL, sender, &sending_args[t]);
if (ret) {
print("ERROR; return code from pthread_create() is %d\n", ret);
exit(-1);
}
}
// waiting until all threads connected
/* printf("m: locking conn \n"); */
print("Initialized circuit, waiting for all parties to connect ... \n");
pthread_mutex_lock(&mtx_connection_established);
/* printf("m: locked conn \n"); */
while (num_successful_connections < 2 * (num_players - 1)) {
/* printf("m: unlocking conn and waiting \n"); */
pthread_cond_wait(&cond_successful_connection, &mtx_connection_established);
}
/* printf("m: done waiting, modifying conn \n"); */
num_successful_connections = -1;
pthread_cond_broadcast(
&cond_start_signal); // signal all threads to start receiving
pthread_mutex_unlock(&mtx_connection_established);
/* printf("m: unlocked conn \n"); */
print("All parties connected sucessfully, starting protocol and timer! \n");
clock_gettime(CLOCK_REALTIME, &l1);
/* clock_gettime(CLOCK_REALTIME, &i3); */
/// Processing Inputs ///
/* Sharemind protocol = Sharemind(); */
clock_t time_function_start = clock();
std::chrono::high_resolution_clock::time_point c1 =
std::chrono::high_resolution_clock::now();
/* auto p_live = PROTOCOL_LIVE(); */
auto result = new RESULTTYPE;
FUNCTION<PROTOCOL_LIVE<DATATYPE>>(result);
#if MAL == 1
compare_views();
/* p_live.communicate(); */
#endif
for (int t = 0; t < (num_players - 1); t++) {
pthread_join(receiving_threads[t], NULL);
pthread_join(sending_Threads[t], NULL);
/* sending_args[t].elements_to_send.clear(); */
}
double time = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - c1)
.count();
/* searchComm__<Sharemind,DATATYPE>(protocol,*found); */
clock_gettime(CLOCK_REALTIME, &l2);
double accum = (l2.tv_sec - l1.tv_sec) +
(double)(l2.tv_nsec - l1.tv_nsec) / (double)1000000000L;
#if PRINT == 1
print_result(result); // different for other functions
#endif
clock_t time_function_finished = clock();
double init_time = (l1.tv_sec - i1.tv_sec) +
(double)(l1.tv_nsec - i1.tv_nsec) / (double)1000000000L;
#if PRE == 1
double accum_pre = (p2.tv_sec - p1.tv_sec) +
(double)(p2.tv_nsec - p1.tv_nsec) / (double)1000000000L;
init_time = init_time - accum_pre;
#endif
print("Time measured to initialize program: %fs \n", init_time);
print("Time measured to perform computation clock: %fs \n",
double((time_function_finished - time_function_start)) /
CLOCKS_PER_SEC);
print("Time measured to perform computation getTime: %fs \n", accum);
print("Time measured to perform computation chrono: %fs \n", time / 1000000);
// Join threads to ensure closing of sockets
#if FUNCTION_IDENTIFIER >= 70
print_layer_stats();
#endif
}
#endif
#if PROTOCOL != 13
void executeProgram(int argc, char *argv[], int process_id, int process_num) {
clock_gettime(CLOCK_REALTIME, &i1);
player_id = PARTY;
#if num_players == 2
init_srng(PPREV, SRNG_SEED);
init_srng(PSELF, PARTY+1+SRNG_SEED);
#elif num_players == 3
/* init_srng(PPREV,SRNG_SEED); */
/* init_srng(PNEXT,SRNG_SEED); */
/* init_srng(PSELF,PARTY+1+SRNG_SEED); */
#if PROTOCOL == 6 || PROTOCOL == 7 //TTP
init_srng(0, SRNG_SEED);
init_srng(1, SRNG_SEED);
init_srng(2, SRNG_SEED);
#else
init_srng(PPREV,
modulo((player_id - 1), num_players) * 101011 + 5000 + SRNG_SEED);
init_srng(PNEXT, player_id * 101011 + 5000 + SRNG_SEED);
init_srng(num_players - 1,
player_id * 291932 + 6000 + SRNG_SEED); // used for sharing inputs
#endif
#elif num_players == 4
init_srng(0, SRNG_SEED);
init_srng(1, SRNG_SEED);
init_srng(2, SRNG_SEED);
init_srng(3, SRNG_SEED);
init_srng(4, SRNG_SEED);
init_srng(5, SRNG_SEED);
init_srng(6, SRNG_SEED);
init_srng(7, SRNG_SEED);
#endif
/// Connecting to other Players
std::string ips[num_players - 1];
// char* hostnames[num_players-1];
for (int i = 0; i < num_players - 1; i++) {
if (i < argc - 1)
ips[i] = std::string(argv[i + 1]);
else {
ips[i] = "127.0.0.1";
}
}
init_muetexes();
init_circuit(ips);
#if PRE == 1
preprocess_circuit(ips);
#endif
#if PRE == 1 && LIVE == 0
double dummy_time = 0.00;
print("Time measured to initialize program: %fs \n", dummy_time);
print("Time measured to perform computation clock: %fs \n", dummy_time);
print("Time measured to perform computation getTime: %fs \n", dummy_time);
print("Time measured to perform computation chrono: %fs \n", dummy_time);
#endif
#if LIVE == 1
live_circuit();
#endif
}
#else
void simulate_live() {
current_phase = PHASE_LIVE;
clock_t time_function_start = clock();
clock_gettime(CLOCK_REALTIME, &l1);
std::chrono::high_resolution_clock::time_point c1 =
std::chrono::high_resolution_clock::now();
auto result = new RESULTTYPE;
FUNCTION<PROTOCOL_LIVE<DATATYPE>>(result);
double time = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now() - c1)
.count();
/* searchComm__<Sharemind,DATATYPE>(protocol,*found); */
clock_gettime(CLOCK_REALTIME, &l2);
double accum = (l2.tv_sec - l1.tv_sec) +
(double)(l2.tv_nsec - l1.tv_nsec) / (double)1000000000L;
#if PRINT == 1
print_result(result); // different for other functions
#endif
clock_t time_function_finished = clock();
print("Time measured to perform computation clock: %fs \n",
double((time_function_finished - time_function_start)) /
CLOCKS_PER_SEC);
print("Time measured to perform computation getTime: %fs \n", accum);
print("Time measured to perform computation chrono: %fs \n", time / 1000000);
// Join threads to ensure closing of sockets
}
void executeProgram(int argc, char *argv[], int process_id, int process_num) {
current_phase = PHASE_LIVE;
init_srng(0, 0);
simulate_live();
}
#endif