forked from bristolcrypto/SPDZ-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fake-Offline.cpp
555 lines (515 loc) · 15.1 KB
/
Fake-Offline.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// (C) 2018 University of Bristol. See License.txt
#include "Math/gf2n.h"
#include "Math/gfp.h"
#include "Math/Share.h"
#include "Math/Setup.h"
#include "Auth/fake-stuff.h"
#include "Exceptions/Exceptions.h"
#include "Math/Setup.h"
#include "Processor/Data_Files.h"
#include "Tools/mkpath.h"
#include "Tools/ezOptionParser.h"
#include "Tools/benchmarking.h"
#include <sstream>
#include <fstream>
using namespace std;
string prep_data_prefix;
/* N = Number players
* ntrip = Number triples needed
* str = "2" or "p"
*/
template<class T>
void make_mult_triples(const T& key,int N,int ntrip,const string& str,bool zero)
{
PRNG G;
G.ReSeed();
ofstream* outf=new ofstream[N];
T a,b,c;
vector<Share<T> > Sa(N),Sb(N),Sc(N);
/* Generate Triples */
for (int i=0; i<N; i++)
{ stringstream filename;
filename << prep_data_prefix << "Triples-" << str << "-P" << i;
cout << "Opening " << filename.str() << endl;
outf[i].open(filename.str().c_str(),ios::out | ios::binary);
if (outf[i].fail()) { throw file_error(filename.str().c_str()); }
}
for (int i=0; i<ntrip; i++)
{
if (!zero)
a.randomize(G);
make_share(Sa,a,N,key,G);
if (!zero)
b.randomize(G);
make_share(Sb,b,N,key,G);
c.mul(a,b);
make_share(Sc,c,N,key,G);
for (int j=0; j<N; j++)
{ Sa[j].output(outf[j],false);
Sb[j].output(outf[j],false);
Sc[j].output(outf[j],false);
}
}
for (int i=0; i<N; i++)
{ outf[i].close(); }
delete[] outf;
}
void make_bit_triples(const gf2n& key,int N,int ntrip,Dtype dtype,bool zero)
{
PRNG G;
G.ReSeed();
ofstream* outf=new ofstream[N];
gf2n a,b,c, one;
one.assign_one();
vector<Share<gf2n> > Sa(N),Sb(N),Sc(N);
/* Generate Triples */
for (int i=0; i<N; i++)
{ stringstream filename;
filename << prep_data_prefix << Data_Files::dtype_names[dtype] << "-2-P" << i;
cout << "Opening " << filename.str() << endl;
outf[i].open(filename.str().c_str(),ios::out | ios::binary);
if (outf[i].fail()) { throw file_error(filename.str().c_str()); }
}
for (int i=0; i<ntrip; i++)
{
if (!zero)
a.randomize(G);
a.AND(a, one);
make_share(Sa,a,N,key,G);
if (!zero)
b.randomize(G);
if (dtype == DATA_BITTRIPLE)
b.AND(b, one);
make_share(Sb,b,N,key,G);
c.mul(a,b);
make_share(Sc,c,N,key,G);
for (int j=0; j<N; j++)
{ Sa[j].output(outf[j],false);
Sb[j].output(outf[j],false);
Sc[j].output(outf[j],false);
}
}
for (int i=0; i<N; i++)
{ outf[i].close(); }
delete[] outf;
}
/* N = Number players
* ntrip = Number tuples needed
* str = "2" or "p"
*/
template<class T>
void make_square_tuples(const T& key,int N,int ntrip,const string& str,bool zero)
{
PRNG G;
G.ReSeed();
ofstream* outf=new ofstream[N];
T a,c;
vector<Share<T> > Sa(N),Sc(N);
/* Generate Squares */
for (int i=0; i<N; i++)
{ stringstream filename;
filename << prep_data_prefix << "Squares-" << str << "-P" << i;
cout << "Opening " << filename.str() << endl;
outf[i].open(filename.str().c_str(),ios::out | ios::binary);
if (outf[i].fail()) { throw file_error(filename.str().c_str()); }
}
for (int i=0; i<ntrip; i++)
{
if (!zero)
a.randomize(G);
make_share(Sa,a,N,key,G);
c.mul(a,a);
make_share(Sc,c,N,key,G);
for (int j=0; j<N; j++)
{ Sa[j].output(outf[j],false);
Sc[j].output(outf[j],false);
}
}
for (int i=0; i<N; i++)
{ outf[i].close(); }
delete[] outf;
}
/* N = Number players
* ntrip = Number bits needed
* str = "2" or "p"
*/
template<class T>
void make_bits(const T& key,int N,int ntrip,const string& str,bool zero)
{
PRNG G;
G.ReSeed();
ofstream* outf=new ofstream[N];
T a;
vector<Share<T> > Sa(N);
/* Generate Bits */
for (int i=0; i<N; i++)
{ stringstream filename;
filename << prep_data_prefix << "Bits-" << str << "-P" << i;
cout << "Opening " << filename.str() << endl;
outf[i].open(filename.str().c_str(),ios::out | ios::binary);
if (outf[i].fail()) { throw file_error(filename.str().c_str()); }
}
for (int i=0; i<ntrip; i++)
{ if ((G.get_uchar()&1)==0 || zero) { a.assign_zero(); }
else { a.assign_one(); }
make_share(Sa,a,N,key,G);
for (int j=0; j<N; j++)
{ Sa[j].output(outf[j],false); }
}
for (int i=0; i<N; i++)
{ outf[i].close(); }
delete[] outf;
}
/* N = Number players
* ntrip = Number inputs needed
* str = "2" or "p"
*
*/
template<class T>
void make_inputs(const T& key,int N,int ntrip,const string& str,bool zero)
{
PRNG G;
G.ReSeed();
ofstream* outf=new ofstream[N];
T a;
vector<Share<T> > Sa(N);
/* Generate Inputs */
for (int player=0; player<N; player++)
{ for (int i=0; i<N; i++)
{ stringstream filename;
filename << prep_data_prefix << "Inputs-" << str << "-P" << i << "-" << player;
cout << "Opening " << filename.str() << endl;
outf[i].open(filename.str().c_str(),ios::out | ios::binary);
if (outf[i].fail()) { throw file_error(filename.str().c_str()); }
}
for (int i=0; i<ntrip; i++)
{
if (!zero)
a.randomize(G);
make_share(Sa,a,N,key,G);
for (int j=0; j<N; j++)
{ Sa[j].output(outf[j],false);
if (j==player)
{ a.output(outf[j],false); }
}
}
for (int i=0; i<N; i++)
{ outf[i].close(); }
}
delete[] outf;
}
/* N = Number players
* ntrip = Number inverses needed
* str = "2" or "p"
*/
template<class T>
void make_inverse(const T& key,int N,int ntrip,bool zero)
{
PRNG G;
G.ReSeed();
ofstream* outf=new ofstream[N];
T a,b;
vector<Share<T> > Sa(N),Sb(N);
/* Generate Triples */
for (int i=0; i<N; i++)
{ stringstream filename;
filename << prep_data_prefix << "Inverses-" << T::type_char() << "-P" << i;
cout << "Opening " << filename.str() << endl;
outf[i].open(filename.str().c_str(),ios::out | ios::binary);
if (outf[i].fail()) { throw file_error(filename.str().c_str()); }
}
for (int i=0; i<ntrip; i++)
{
if (zero)
// ironic?
a.assign_one();
else
do
a.randomize(G);
while (a.is_zero());
make_share(Sa,a,N,key,G);
b=a; b.invert();
make_share(Sb,b,N,key,G);
for (int j=0; j<N; j++)
{ Sa[j].output(outf[j],false);
Sb[j].output(outf[j],false);
}
}
for (int i=0; i<N; i++)
{ outf[i].close(); }
delete[] outf;
}
template<class T>
void make_PreMulC(const T& key, int N, int ntrip, bool zero)
{
stringstream ss;
ss << prep_data_prefix << "PreMulC-" << T::type_char();
Files<T> files(N, key, ss.str());
PRNG G;
G.ReSeed();
T a, b, c;
c = 1;
for (int i=0; i<ntrip; i++)
{
// close the circle
if (i == ntrip - 1 || zero)
a.assign_one();
else
do
a.randomize(G);
while (a.is_zero());
files.output_shares(a);
b = a;
b.invert();
files.output_shares(b);
files.output_shares(a * c);
c = b;
}
}
int main(int argc, const char** argv)
{
insecure("preprocessing");
ez::ezOptionParser opt;
opt.syntax = "./Fake-Offline.x <nplayers> [OPTIONS]\n\nOptions with 2 arguments take the form '-X <#gf2n tuples>,<#modp tuples>'";
opt.example = "./Fake-Offline.x 2 -lgp 128 -lg2 128 --default 10000\n./Fake-Offline.x 3 -trip 50000,10000 -btrip 100000\n";
opt.add(
"128", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Bit length of GF(p) field (default: 128)", // Help description.
"-lgp", // Flag token.
"--lgp" // Flag token.
);
opt.add(
"40", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Bit length of GF(2^n) field (default: 40)", // Help description.
"-lg2", // Flag token.
"--lg2" // Flag token.
);
opt.add(
"1000", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Default number of tuples to generate for ALL data types (default: 1000)", // Help description.
"-d", // Flag token.
"--default" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
2, // Number of args expected.
',', // Delimiter if expecting multiple args.
"Number of triples, for gf2n / modp types", // Help description.
"-trip", // Flag token.
"--ntriples" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
2, // Number of args expected.
',', // Delimiter if expecting multiple args.
"Number of random bits, for gf2n / modp types", // Help description.
"-bit", // Flag token.
"--nbits" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
2, // Number of args expected.
',', // Delimiter if expecting multiple args.
"Number of input tuples, for gf2n / modp types", // Help description.
"-inp", // Flag token.
"--ninputs" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
2, // Number of args expected.
',', // Delimiter if expecting multiple args.
"Number of square tuples, for gf2n / modp types", // Help description.
"-sq", // Flag token.
"--nsquares" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Number of inverse tuples (modp only)", // Help description.
"-inv", // Flag token.
"--ninverses" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Number of GF(2) triples", // Help description.
"-btrip", // Flag token.
"--nbittriples" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Number of GF(2) x GF(2^n) triples", // Help description.
"-mixed", // Flag token.
"--nbitgf2ntriples" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
0, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Set all values to zero, but not the shares", // Help description.
"-z", // Flag token.
"--zero" // Flag token.
);
opt.parse(argc, argv);
vector<string> badOptions;
string usage;
unsigned int i;
if(!opt.gotRequired(badOptions))
{
for (i=0; i < badOptions.size(); ++i)
cerr << "ERROR: Missing required option " << badOptions[i] << ".";
opt.getUsage(usage);
cout << usage;
return 1;
}
if(!opt.gotExpected(badOptions))
{
for(i=0; i < badOptions.size(); ++i)
cerr << "ERROR: Got unexpected number of arguments for option " << badOptions[i] << ".";
opt.getUsage(usage);
cout << usage;
return 1;
}
int nplayers;
if (opt.firstArgs.size() == 2)
{
nplayers = atoi(opt.firstArgs[1]->c_str());
}
else if (opt.lastArgs.size() == 1)
{
nplayers = atoi(opt.lastArgs[0]->c_str());
}
else
{
cerr << "ERROR: invalid number of arguments\n";
opt.getUsage(usage);
cout << usage;
return 1;
}
int default_num = 0;
int ntrip2=0, ntripp=0, nbits2=0,nbitsp=0,nsqr2=0,nsqrp=0,ninp2=0,ninpp=0,ninv=0, nbittrip=0, nbitgf2ntrip=0;
vector<int> list_options;
int lg2, lgp;
opt.get("--lgp")->getInt(lgp);
opt.get("--lg2")->getInt(lg2);
opt.get("--default")->getInt(default_num);
ntrip2 = ntripp = nbits2 = nbitsp = nsqr2 = nsqrp = ninp2 = ninpp = ninv =
nbittrip = nbitgf2ntrip = default_num;
if (opt.isSet("--ntriples"))
{
opt.get("--ntriples")->getInts(list_options);
ntrip2 = list_options[0];
ntripp = list_options[1];
}
if (opt.isSet("--nbits"))
{
opt.get("--nbits")->getInts(list_options);
nbits2 = list_options[0];
nbitsp = list_options[1];
}
if (opt.isSet("--ninputs"))
{
opt.get("--ninputs")->getInts(list_options);
ninp2 = list_options[0];
ninpp = list_options[1];
}
if (opt.isSet("--nsquares"))
{
opt.get("--nsquares")->getInts(list_options);
nsqr2 = list_options[0];
nsqrp = list_options[1];
}
if (opt.isSet("--ninverses"))
opt.get("--ninverses")->getInt(ninv);
if (opt.isSet("--nbittriples"))
opt.get("--nbittriples")->getInt(nbittrip);
if (opt.isSet("--nbitgf2ntriples"))
opt.get("--nbitgf2ntriples")->getInt(nbitgf2ntrip);
bool zero = opt.isSet("--zero");
if (zero)
cout << "Set all values to zero" << endl;
PRNG G;
G.ReSeed();
prep_data_prefix = get_prep_dir(nplayers, lgp, lg2);
// Set up the fields
ofstream outf;
bigint p;
generate_online_setup(outf, prep_data_prefix, p, lgp, lg2);
/* Find number players and MAC keys etc*/
gfp keyp,pp; keyp.assign_zero();
gf2n key2,p2; key2.assign_zero();
int tmpN = 0;
ifstream inpf;
// create PREP_DIR if not there
if (mkdir_p(PREP_DIR) == -1)
{
cerr << "mkdir_p(" PREP_DIR ") failed\n";
throw file_error();
}
for (i = 0; i < (unsigned int)nplayers; i++)
{
stringstream filename;
filename << prep_data_prefix << "Player-MAC-Keys-P" << i;
inpf.open(filename.str().c_str());
if (inpf.fail())
{
inpf.close();
cout << "No MAC key share for player " << i << ", generating a fresh one\n";
pp.randomize(G);
p2.randomize(G);
ofstream outf(filename.str().c_str());
if (outf.fail())
throw file_error(filename.str().c_str());
outf << nplayers << " " << pp << " " << p2;
outf.close();
cout << "Written new MAC key share to " << filename.str() << endl;
}
else
{
inpf >> tmpN; // not needed here
pp.input(inpf,true);
p2.input(inpf,true);
inpf.close();
}
cout << " Key " << i << "\t p: " << pp << "\n\t 2: " << p2 << endl;
keyp.add(pp);
key2.add(p2);
}
cout << "--------------\n";
cout << "Final Keys :\t p: " << keyp << "\n\t\t 2: " << key2 << endl;
make_mult_triples(key2,nplayers,ntrip2,"2",zero);
make_mult_triples(keyp,nplayers,ntripp,"p",zero);
make_bits(key2,nplayers,nbits2,"2",zero);
make_bits(keyp,nplayers,nbitsp,"p",zero);
make_square_tuples(key2,nplayers,nsqr2,"2",zero);
make_square_tuples(keyp,nplayers,nsqrp,"p",zero);
make_inputs(key2,nplayers,ninp2,"2",zero);
make_inputs(keyp,nplayers,ninpp,"p",zero);
make_inverse(key2,nplayers,ninv,zero);
make_inverse(keyp,nplayers,ninv,zero);
make_bit_triples(key2,nplayers,nbittrip,DATA_BITTRIPLE,zero);
make_bit_triples(key2,nplayers,nbitgf2ntrip,DATA_BITGF2NTRIPLE,zero);
make_PreMulC(key2,nplayers,ninv,zero);
make_PreMulC(keyp,nplayers,ninv,zero);
}