-
Notifications
You must be signed in to change notification settings - Fork 365
/
client-setup.cpp
180 lines (159 loc) · 6.27 KB
/
client-setup.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
// (C) 2018 University of Bristol. See License.txt
// Preprocessing stage to:
// Create the public/private key pairs for each client
// Create the public/private key pairs for each spdz engine
// For each client store the client keys + all spdz engine public keys
// in a file named Client-Keys-C<client id>
// For each spdz engine store the spdz engine keys + all client public keys
// in a file named Player-SPDZ-Keys-P<player id>
//
#include <sodium.h>
#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/Config.h"
#include <sstream>
#include <fstream>
using namespace std;
static void output(const vector<octet> &vec, ofstream &of)
{
copy(vec.begin(), vec.end(), ostreambuf_iterator<char>(of));
}
int main(int argc, const char** argv)
{
ez::ezOptionParser opt;
opt.syntax = "./client-setup.x <nplayers> [OPTIONS]\n";
opt.add(
"0", // Default.
0, // Required?
1, // Number of args expected.
0, // Delimiter if expecting multiple args.
"Number of external clients (default: nplayers)", // Help description.
"-nc", // Flag token.
"--numclients" // Flag token.
);
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.parse(argc, argv);
string prep_data_prefix;
string usage;
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 lg2, lgp, nclients;
opt.get("--numclients")->getInt(nclients);
if (nclients <= 0)
nclients = nplayers;
opt.get("--lgp")->getInt(lgp);
opt.get("--lg2")->getInt(lg2);
cout << "nplayers = " << nplayers << endl;
cout << "nclients = " << nclients << endl;
cout << "lgp = " << lgp << endl;
cout << "lgp2 = " << lg2 << endl;
prep_data_prefix = get_prep_dir(nplayers, lgp, lg2);
cout << "prep dir = " << prep_data_prefix << endl;
vector<Config::public_key> client_publickeys;
vector<Config::secret_key> client_secretkeys;
client_publickeys.resize(nclients);
client_secretkeys.resize(nclients);
for (int i = 0; i < nclients; i++) {
client_secretkeys[i].resize(crypto_box_SECRETKEYBYTES);
client_publickeys[i].resize(crypto_box_PUBLICKEYBYTES);
randombytes_buf(&client_secretkeys[i][0], client_secretkeys[i].size());
crypto_scalarmult_base(&client_publickeys[i][0], &client_secretkeys[i][0]);
}
vector<Config::public_signing_key> client_signing_publickeys;
vector<Config::secret_signing_key> client_signing_secretkeys;
client_signing_publickeys.resize(nclients);
client_signing_secretkeys.resize(nclients);
for (int i = 0; i < nclients; i++) {
client_signing_publickeys[i].resize(crypto_sign_PUBLICKEYBYTES);
client_signing_secretkeys[i].resize(crypto_sign_SECRETKEYBYTES);
crypto_sign_keypair(&client_signing_publickeys[i][0], &client_signing_secretkeys[i][0]);
}
vector<Config::public_key> server_publickeys;
vector<Config::secret_key> server_secretkeys;
server_publickeys.resize(nplayers);
server_secretkeys.resize(nplayers);
for (int i = 0; i < nplayers; i++) {
server_publickeys[i].resize(crypto_box_PUBLICKEYBYTES);
server_secretkeys[i].resize(crypto_box_SECRETKEYBYTES);
randombytes_buf(&server_secretkeys[i][0], server_secretkeys[i].size());
crypto_scalarmult_base(&server_publickeys[i][0], &server_secretkeys[i][0]);
}
vector<Config::public_signing_key> server_signing_publickeys;
vector<Config::secret_signing_key> server_signing_secretkeys;
server_signing_publickeys.resize(nplayers);
server_signing_secretkeys.resize(nplayers);
for (int i = 0; i < nplayers; i++) {
server_signing_publickeys[i].resize(crypto_sign_PUBLICKEYBYTES);
server_signing_secretkeys[i].resize(crypto_sign_SECRETKEYBYTES);
crypto_sign_keypair(&server_signing_publickeys[i][0], &server_signing_secretkeys[i][0]);
}
/* Write client files */
for (int i = 0; i < nclients; i++) {
stringstream filename;
filename << prep_data_prefix << "Client-Keys-C" << i;
ofstream outf(filename.str().c_str());
if (outf.fail())
throw file_error(filename.str().c_str());
// Write public key and secret key
output(client_publickeys[i],outf);
output(client_secretkeys[i],outf);
output(client_signing_publickeys[i],outf);
output(client_signing_secretkeys[i],outf);
int keycount = 2;
// Write all spdz engine public keys
for (int j = 0; j < nplayers; j++) {
output(server_publickeys[j], outf);
output(server_signing_publickeys[j], outf);
keycount++;
}
outf.close();
cout << "Wrote " << keycount << " keys to " << filename.str() << endl;
}
/* Write spdz engine files */
for (int i = 0; i < nplayers; i++) {
Config::write_player_config_file( prep_data_prefix, i
, server_publickeys[i], server_secretkeys[i]
, server_signing_publickeys[i], server_signing_secretkeys[i]
, client_publickeys, client_signing_publickeys
, server_publickeys, server_signing_publickeys);
}
}