forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrustedParty.cpp
335 lines (301 loc) · 7.08 KB
/
TrustedParty.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
/*
* TrustedParty.cpp
*
*/
#include "TrustedParty.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <string.h>
#include <iostream>
#include "proto_utils.h"
#include "msg_types.h"
#include "SpdzWire.h"
#include "Protocols/fake-stuff.h"
#include "Register_inline.h"
#include "CommonParty.hpp"
#include "Protocols/fake-stuff.hpp"
#include "BMR/Register.hpp"
#include "GC/Machine.hpp"
#include "GC/Processor.hpp"
#include "GC/Secret.hpp"
#include "GC/Thread.hpp"
#include "GC/ThreadMaster.hpp"
#include "GC/Program.hpp"
#include "GC/ShareSecret.hpp"
#include "Processor/Instruction.hpp"
#include "Protocols/Share.hpp"
TrustedProgramParty* TrustedProgramParty::singleton = 0;
BaseTrustedParty::BaseTrustedParty()
{
_num_prf_received = 0;
_received_gc_received = 0;
n_received = 0;
randomfd = open("/dev/urandom", O_RDONLY);
done_filling = false;
}
BaseTrustedParty::~BaseTrustedParty()
{
close(randomfd);
}
TrustedProgramParty::TrustedProgramParty(int argc, char** argv) :
processor(machine),
random_processor(random_machine)
{
if (argc < 2)
{
cerr << "Usage: " << argv[0] << " <program> [netmap]" << endl;
exit(1);
}
program.parse(string(argv[1]) + "-0");
processor.reset(program);
machine.reset(program);
random_processor.reset(program);
random_machine.reset(program);
if (singleton)
throw runtime_error("there can only be one");
singleton = this;
if (argc == 3)
init(argv[2], 0);
else
init("LOOPBACK", 0);
deltas.resize(_N);
for (size_t i = 0; i < _N; i++)
{
deltas[i] = prng.get_doubleword();
#ifdef DEBUG
deltas[i] = Key(i + 1, 0);
#endif
if (deltas[i].get_signal() == 0)
deltas[i] ^= Key(1);
cout << "Delta " << i << ": " << deltas[i] << endl;
}
}
TrustedProgramParty::~TrustedProgramParty()
{
cout << "Random timer: " << random_timer.elapsed() << endl;
}
void BaseTrustedParty::NodeReady()
{
#ifdef DEBUG_STEPS
printf("\n\nNode ready \n\n");
#endif
//sleep(1);
prepare_randomness();
send_randomness();
prf_outputs.resize(get_n_parties());
}
void BaseTrustedParty::prepare_randomness()
{
msg_keys.resize(_N);
for (size_t i = 0; i < msg_keys.size(); i++)
{
msg_keys[i].clear();
fill_message_type(msg_keys[i], TYPE_KEYS);
msg_keys[i].resize(MSG_KEYS_HEADER_SZ);
}
done_filling = _fill_keys();
}
void BaseTrustedParty::send_randomness()
{
for(party_id_t pid=1; pid<=_N; pid++)
{
// printf("all keys\n");
// phex(all_keys, size_of_keys);
_node->Send(pid, msg_keys[pid - 1]);
// printf("msg keys\n");
// phex(msg_keys, msg_keys_size);
send_input_masks(pid);
}
send_output_masks();
}
void TrustedProgramParty::send_input_masks(party_id_t pid)
{
SendBuffer& buffer = msg_input_masks[pid-1];
#ifdef DEBUG_ROUNDS
cout << "sending " << buffer.size() << " input masks to " << pid << endl;
#endif
_node->Send(pid, buffer);
}
void TrustedProgramParty::send_output_masks()
{
#ifdef DEBUG_OUTPUT_MASKS
cout << "sending " << msg_output_masks.size() - 4 << " output masks" << endl;
#endif
_node->Broadcast(msg_output_masks);
}
void BaseTrustedParty::NewMessage(int from, ReceivedMsg& msg)
{
char* message = msg.data();
int len = msg.size();
MSG_TYPE message_type;
msg.unserialize(message_type);
unique_lock<mutex> locker(global_lock);
switch(message_type) {
case TYPE_PRF_OUTPUTS:
{
#ifdef DEBUG
cout << "TYPE_PRF_OUTPUTS" << endl;
#endif
_print_mx.lock();
#ifdef DEBUG2
printf("got message of len %u from %d\n", len, from);
phex(message, len);
cout << "garbled table size " << get_garbled_tbl_size() << endl;
#endif
#ifdef DEBUG_STEPS
printf("\n Got prfs from %d\n",from);
#endif
prf_outputs[from-1] = msg;
_print_mx.unlock();
if(++_num_prf_received == _N) {
_num_prf_received = 0;
_compute_send_garbled_circuit();
}
break;
}
case TYPE_RECEIVED_GC:
{
if(++_received_gc_received == _N) {
_received_gc_received = 0;
if (done_filling)
_launch_online();
else
NodeReady();
}
break;
}
case TYPE_NEXT:
if (++n_received == _N)
{
n_received = 0;
send_randomness();
}
break;
case TYPE_DONE:
if (++n_received == _N)
_node->Stop();
break;
default:
{
_print_mx.lock();
printf("got message of len %u from %d\n", len, from);
printf("UNDEFINED\n");
printf("got undefined message\n");
phex(message, len);
_print_mx.unlock();
}
break;
}
}
void TrustedProgramParty::_launch_online()
{
_node->Broadcast(get_buffer(TYPE_LAUNCH_ONLINE));
}
void BaseTrustedParty::_compute_send_garbled_circuit()
{
SendBuffer& buffer = get_buffer(TYPE_GARBLED_CIRCUIT );
buffer.allocate(get_garbled_tbl_size() * 4 * get_n_parties() * sizeof(Key));
garble();
//sending to parties:
#ifdef DEBUG
cout << "sending garbled circuit" << endl;
#endif
#ifdef DEBUG2
phex(buffer);
#endif
_node->Broadcast(buffer);
//prepare_randomness();
}
void BaseTrustedParty::Start()
{
_node->Start();
}
void TrustedProgramParty::NodeReady()
{
for (int i = 0; i < get_n_parties(); i++)
{
SendBuffer& buffer = get_buffer(TYPE_DELTA);
buffer.serialize(deltas[i]);
_node->Send(i + 1, buffer);
}
this->BaseTrustedParty::NodeReady();
}
bool TrustedProgramParty::_fill_keys()
{
for (int i = 0; i < SPDZ_OP_N; i++)
{
spdz_wires[i].clear();
spdz_wires[i].resize(get_n_parties());
}
msg_output_masks = get_buffer(TYPE_MASK_OUTPUT);
msg_input_masks.resize(get_n_parties());
for (auto& buffer : msg_input_masks)
{
buffer.clear();
fill_message_type(buffer, TYPE_MASK_INPUTS);
}
return GC::DONE_BREAK == first_phase(program, random_processor, random_machine);
}
void TrustedProgramParty::garble()
{
NoMemory dynamic_memory;
second_phase(program, processor, machine, dynamic_memory);
vector< Share<gf2n_long> > tmp;
make_share(tmp, 1, get_n_parties(), mac_key, prng);
for (int i = 0; i < get_n_parties(); i++)
tmp[i].get_mac().pack(spdz_wires[SPDZ_MAC][i]);
for (int i = 0; i < get_n_parties(); i++)
{
for (int j = 0; j < SPDZ_OP_N; j++)
{
SendBuffer buffer;
fill_message_type(buffer, TYPE_SPDZ_WIRES);
buffer.serialize(j);
buffer.serialize(spdz_wires[j][i].get_data(), spdz_wires[j][i].get_length());
#ifdef DEBUG_SPDZ_WIRE
cout << "send " << spdz_wires[j][i].get_length() << "/" << buffer.size()
<< " bytes for type " << j << " to " << i << endl;
#endif
_node->Send(i + 1, buffer);
}
}
}
void TrustedProgramParty::store_spdz_wire(SpdzOp op, const Register& reg)
{
make_share(mask_shares, gf2n_long(reg.get_mask()), get_n_parties(),
gf2n_long(get_mac_key()), prng);
for (int i = 0; i < get_n_parties(); i++)
{
SpdzWire wire;
wire.mask = mask_shares[i];
for (int j = 0; j < 2; j++)
{
wire.my_keys[j] = reg.keys[j][i];
}
wire.pack(spdz_wires[op][i]);
}
#ifdef DEBUG_SPDZ_WIRE
cout << "stored SPDZ wire of type " << op << ":" << endl;
reg.keys.print(reg.get_id());
#endif
}
void TrustedProgramParty::store_wire(const Register& reg)
{
wires.serialize(reg.mask);
reg.keys.serialize(wires);
#ifdef DEBUG
cout << "storing wire" << endl;
reg.print();
#endif
}
void TrustedProgramParty::load_wire(Register& reg)
{
wires.unserialize(reg.mask);
reg.keys.unserialize(wires);
#ifdef DEBUG
cout << "loading wire" << endl;
reg.print();
#endif
}