forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_input_fp.cpp
143 lines (130 loc) · 3.73 KB
/
gen_input_fp.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
#include <iostream>
#include <fstream>
#include "Math/gfp.hpp"
#include "Tools/Buffer.h"
#include "Tools/ezOptionParser.h"
#include "Math/Setup.h"
using namespace std;
int main(int argc, const char** argv) {
ez::ezOptionParser opt;
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(
to_string(gf2n::default_degree()).c_str(), // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
("Bit length of GF(2^n) field (default: " + to_string(gf2n::default_degree()) + ")").c_str(), // Help description.
"-lg2", // Flag token.
"--lg2" // Flag token.
);
opt.add(
"2", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Number of parties (default: 2).", // Help description.
"-N", // Flag token.
"--nparties" // Flag token.
);
opt.add(
"gfp_vals.in", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Input file (default: ./gfp_vals.in). Use \"-\" for STDIN.", // Help description.
"-i", // Flag token.
"--input" // Flag token.
);
opt.add(
"gfp_vals.out", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Output file (default: ./gfp_vals.out). Use \"-\" for STDOUT.", // Help description.
"-o", // Flag token.
"--output" // Flag token.
);
opt.add(
"", // Default.
0, // Required?
0, // Number of args expected.
0, // Delimiter if expecting multiple args.
"This message.", // Help description.
"-h", // Flag token.
"--help" // Flag token.
);
opt.parse(argc, argv);
if (opt.get("-h")->isSet)
{
string help;
opt.getUsage(help);
cerr << help;
exit(0);
}
int nparties, lgp, lg2;
opt.get("-N")->getInt(nparties);
opt.get("-lgp")->getInt(lgp);
opt.get("-lg2")->getInt(lg2);
gfp::init_default(lgp);
std::string input_name, output_name;
bool use_stdin = false;
bool use_stdout = false;
istream* in;
ostream* out;
opt.get("-i")->getString(input_name);
opt.get("-o")->getString(output_name);
// Input Stream
if (input_name == "-"){ // Open file or STDIN
use_stdin = true;
in = &cin;
} else {
in = new ifstream(input_name.c_str());
}
// Output Stream
if (output_name == "-"){ // Open file or STDOUT
use_stdout = true;
out = &cout;
} else {
out = new ofstream(output_name.c_str());
}
int n; *in >> n;
for (int i = 0; i < n; ++i) {
bigint a;
*in >> a;
gfp b = a;
b.output(*out, false);
}
if (in->fail())
{
if(!use_stdout) {
((ofstream*)out)->close();
delete out;
}
unlink(output_name.c_str());
throw runtime_error("Failed to read input \"" + string(input_name)+"\"");
}
n = -(n % BUFFER_SIZE) + BUFFER_SIZE;
cerr << "Adding " << n << " zeros to match buffer size" << endl;
for (int i = 0; i < n; i++)
gfp(0).output(*out, false);
cerr << "Output written to " << output_name
<< ", copy to Player-Data/Private-Input-<playerno>" << endl;
// Clean up file streams.
if(!use_stdin) {
((ifstream*)in)->close();
delete in;
}
if(!use_stdout) {
((ofstream*)out)->close();
delete out;
}
return 0;
}