forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OTMachine.cpp
399 lines (350 loc) · 11.1 KB
/
OTMachine.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
#include "Networking/Player.h"
#include "OT/OTExtension.h"
#include "OT/OTExtensionWithMatrix.h"
#include "Tools/Exceptions.h"
#include "Tools/time-func.h"
#include <stdlib.h>
#include <sstream>
#include <fstream>
#include <iostream>
#include <sys/time.h>
#include "OutputCheck.h"
#include "OTMachine.h"
//#define BASE_OT_DEBUG
class OT_thread_info
{
public:
int thread_num;
bool stop;
int other_player_num;
OTExtensionWithMatrix* ot_ext;
int nOTs, nbase;
BitVector receiverInput;
int nloops;
};
void* run_otext_thread(void* ptr)
{
OT_thread_info *tinfo = (OT_thread_info*) ptr;
//int num = tinfo->thread_num;
//int other_player_num = tinfo->other_player_num;
printf("\tI am in thread %d\n", tinfo->thread_num);
tinfo->ot_ext->transfer(tinfo->nOTs, tinfo->receiverInput, tinfo->nloops);
return NULL;
}
OTMachine::OTMachine(int argc, const char** argv)
{
opt.add(
"", // Default.
1, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"This player's number, 0/1 (required).", // Help description.
"-p", // Flag token.
"--player" // Flag token.
);
opt.add(
"5000", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Base port number (default: 5000).", // Help description.
"-pn", // Flag token.
"--portnum" // Flag token.
);
opt.add(
"localhost", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Host name(s) that player 0 is running on (default: localhost). Split with commas.", // Help description.
"-h", // Flag token.
"--hostname" // Flag token.
);
opt.add(
"1024",
0,
1,
0,
"Number of extended OTs to run (default: 1024).",
"-n",
"--nOTs"
);
opt.add(
"128", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Number of base OTs to run (default: 128).", // Help description.
"-b", // Flag token.
"--nbase" // Flag token.
);
opt.add(
"s",
0,
1,
0,
"Mode for OT. a (asymmetric) or s (symmetric, i.e. play both sender/receiver) (default: s).",
"-m",
"--mode"
);
opt.add(
"1",
0,
1,
0,
"Number of threads (default: 1).",
"-x",
"--nthreads"
);
opt.add(
"1",
0,
1,
0,
"Number of loops (default: 1).",
"-l",
"--nloops"
);
opt.add(
"1",
0,
1,
0,
"Number of subloops (default: 1).",
"-s",
"--nsubloops"
);
opt.add(
"", // Default.
0, // Required?
0, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Run in passive security mode.", // Help description.
"-pas", // Flag token.
"--passive" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
0, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Write results to files.", // Help description.
"-o", // Flag token.
"--output" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
0, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Real base OT.", // Help description.
"-r", // Flag token.
"--real" // Flag token.
);
opt.parse(argc, argv);
string hostname, ot_mode, usage;
passive = false;
opt.get("-p")->getInt(my_num);
opt.get("-pn")->getInt(portnum_base);
opt.get("-h")->getString(hostname);
opt.get("-n")->getLong(nOTs);
opt.get("-m")->getString(ot_mode);
opt.get("--nthreads")->getInt(nthreads);
opt.get("--nloops")->getInt(nloops);
opt.get("--nsubloops")->getInt(nsubloops);
opt.get("--nbase")->getInt(nbase);
if (opt.isSet("-pas"))
passive = true;
if (!opt.isSet("-p"))
{
opt.getUsage(usage);
cout << usage;
exit(0);
}
cout << "Player 0 host name = " << hostname << endl;
cout << "Creating " << nOTs << " extended OTs in " << nthreads << " threads\n";
cout << "Running in mode " << ot_mode << endl;
if (passive)
cout << "Running with PASSIVE security only\n";
if (nbase < 128)
cout << "WARNING: only using " << nbase << " seed OTs, using these for OT extensions is insecure.\n";
if (ot_mode.compare("s") == 0)
ot_role = BOTH;
else if (ot_mode.compare("a") == 0)
{
if (my_num == 0)
ot_role = SENDER;
else
ot_role = RECEIVER;
}
else
{
cerr << "Invalid OT mode argument: " << ot_mode << endl;
exit(1);
}
// Several names for multiplexing
unsigned int pos = 0;
while (pos < hostname.length())
{
string::size_type new_pos = hostname.find(',', pos);
if (new_pos == string::npos)
new_pos = hostname.length();
int len = new_pos - pos;
string name = hostname.substr(pos, len);
pos = new_pos + 1;
vector<string> names(2);
names[my_num] = "localhost";
names[1-my_num] = name;
N.push_back(new Names(my_num, portnum_base + 1000 * N.size(), names));
}
P = new RealTwoPartyPlayer(*N[0], 1 - my_num, "machine");
timeval baseOTstart, baseOTend;
gettimeofday(&baseOTstart, NULL);
// swap role for base OTs
if (opt.isSet("-r"))
bot_ = new BaseOT(nbase, 128, P, INV_ROLE(ot_role));
else
bot_ = new FakeOT(nbase, 128, P, INV_ROLE(ot_role));
cout << "real mode " << opt.isSet("-r") << endl;
BaseOT& bot = *bot_;
bot.exec_base();
gettimeofday(&baseOTend, NULL);
double basetime = timeval_diff(&baseOTstart, &baseOTend);
cout << "\t\tBaseTime (" << role_to_str(ot_role) << "): " << basetime/1000000 << endl << flush;
// Receiver send something to force synchronization
// (since Sender finishes baseOTs before Receiver)
int a = 3;
vector<octetStream> os(2);
os[0].store(a);
P->send_receive_player(os);
os[1].get(a);
cout << a << endl;
#ifdef BASE_OT_DEBUG
// check base OTs
bot.check();
// check after extending with PRG a few times
for (int i = 0; i < 8; i++)
{
bot.extend_length();
bot.check();
}
cout << "Verifying base OTs (debugging)\n";
#endif
// convert baseOT selection bits to BitVector
// (not already BitVector due to legacy PVW code)
baseReceiverInput = bot.receiver_inputs;
baseReceiverInput.resize(nbase);
}
OTMachine::~OTMachine()
{
for (auto names : N)
delete names;
delete bot_;
delete P;
}
void OTMachine::run()
{
// divide nOTs between threads and loops
nOTs = DIV_CEIL(nOTs, nthreads * nloops);
// round up to multiple of base OTs and subloops
// discount for discarded OTs
nOTs = DIV_CEIL(nOTs + 2 * 128, nbase * nsubloops) * nbase * nsubloops - 2 * 128;
cout << "Running " << nOTs << " OT extensions per thread and loop\n" << flush;
// PRG for generating inputs etc
PRNG G;
G.ReSeed();
BitVector receiverInput(nOTs);
receiverInput.randomize(G);
BaseOT& bot = *bot_;
cout << "Initialize OT Extension\n";
vector<OT_thread_info> tinfos(nthreads);
vector<pthread_t> threads(nthreads);
timeval OTextstart, OTextend;
gettimeofday(&OTextstart, NULL);
// copy base inputs/outputs for each thread
vector<BitVector> base_receiver_input_copy(nthreads);
vector<vector< array<BitVector, 2> > > base_sender_inputs_copy(nthreads, vector<array<BitVector, 2> >(nbase));
vector< vector<BitVector> > base_receiver_outputs_copy(nthreads, vector<BitVector>(nbase));
vector<TwoPartyPlayer*> players(nthreads);
for (int i = 0; i < nthreads; i++)
{
tinfos[i].receiverInput.assign(receiverInput);
base_receiver_input_copy[i].assign(baseReceiverInput);
for (int j = 0; j < nbase; j++)
{
base_sender_inputs_copy[i][j][0].assign(bot.sender_inputs[j][0]);
base_sender_inputs_copy[i][j][1].assign(bot.sender_inputs[j][1]);
base_receiver_outputs_copy[i][j].assign(bot.receiver_outputs[j]);
}
// now setup resources for each thread
// round robin with the names
players[i] = new RealTwoPartyPlayer(*N[i % N.size()], 1 - my_num,
"thread" + to_string(i));
tinfos[i].thread_num = i+1;
tinfos[i].other_player_num = 1 - my_num;
tinfos[i].nOTs = nOTs;
tinfos[i].ot_ext = new OTExtensionWithMatrix(
players[i],
ot_role,
passive,
nsubloops);
tinfos[i].ot_ext->init(base_receiver_input_copy[i],
base_sender_inputs_copy[i], base_receiver_outputs_copy[i]);
tinfos[i].nloops = nloops;
// create the thread
pthread_create(&threads[i], NULL, run_otext_thread, &tinfos[i]);
// extend base OTs with PRG for the next thread
bot.extend_length();
}
// wait for threads to finish
for (int i = 0; i < nthreads; i++)
{
pthread_join(threads[i],NULL);
cout << "thread " << i+1 << " finished\n" << flush;
}
map<string,long long>& times = tinfos[0].ot_ext->times;
for (map<string,long long>::iterator it = times.begin(); it != times.end(); it++)
{
long long sum = 0;
for (int i = 0; i < nthreads; i++)
sum += tinfos[i].ot_ext->times[it->first];
cout << it->first << " on average took time "
<< double(sum) / nthreads / 1e6 << endl;
}
gettimeofday(&OTextend, NULL);
double totaltime = timeval_diff(&OTextstart, &OTextend);
cout << "Time for OTExt threads (" << role_to_str(ot_role) << "): " << totaltime/1000000 << endl << flush;
if (opt.isSet("-o"))
{
BitVector receiver_output, sender_output;
char filename[1024];
sprintf(filename, RECEIVER_INPUT, my_num);
ofstream outf(filename);
receiverInput.output(outf, false);
outf.close();
sprintf(filename, RECEIVER_OUTPUT, my_num);
outf.open(filename);
for (unsigned int i = 0; i < nOTs; i++)
{
receiver_output.assign_bytes((char*) tinfos[0].ot_ext->get_receiver_output(i), sizeof(__m128i));
receiver_output.output(outf, false);
}
outf.close();
for (int i = 0; i < 2; i++)
{
sprintf(filename, SENDER_OUTPUT, my_num, i);
outf.open(filename);
for (int j = 0; j < nOTs; j++)
{
sender_output.assign_bytes((char*) tinfos[0].ot_ext->get_sender_output(i, j), sizeof(__m128i));
sender_output.output(outf, false);
}
outf.close();
}
}
for (int i = 0; i < nthreads; i++)
{
delete players[i];
delete tinfos[i].ot_ext;
}
}