Repository navigation
Expand file tree
/
Copy pathProtocolClient.cpp
More file actions
executable file
·347 lines (275 loc) · 8.12 KB
/
Copy pathProtocolClient.cpp
File metadata and controls
executable file
·347 lines (275 loc) · 8.12 KB
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
#include <iostream>
#include <sstream>
#include <math.h>
#include <chrono>
#include "protocol.h"
#include "util.h"
using namespace std;
void RunClientProtocol(CSocket *socket, string pArgsFile, string clientDataDir)
{
ClientLog("starting client protocol");
#if !OPTIMAL
ClientLog("Reading in protocol arguments");
#endif
ifstream in;
in.open(pArgsFile, ios::in | ios::binary);
struct ProtocolArgs pArgs(in);
in.close();
#if !OPTIMAL
ClientLog("Reading in receiver OTs");
#endif
ifstream rOTFile1;
rOTFile1.open(clientDataDir + "rOT.txt", ios::in | ios::binary);
ReceiverOTCorrelation **FSM1_RC = ReadInReceiverOTsFSM1(rOTFile1, pArgs);
ReceiverOTCorrelation **FSM2_RC = ReadInReceiverOTsFSM2(rOTFile1, pArgs);
rOTFile1.close();
#if !OPTIMAL
ClientLog("Finished Reading in OTs");
#endif
uint32_t N = pArgs.N;
uint32_t C = pArgs.C;
uint8_t* bits_per_component = pArgs.bits_per_component;
#if !OPTIMAL
cout << "N: " << N << " C: " << C << endl;
#endif
ifstream queryFile;
queryFile.open(clientDataDir + "query.txt");
uint32_t *query = ReadInEntry(queryFile, C);
queryFile.close();
#if VERBOSE
cout << "Query: ";
for (int i = 0; i < C; i++)
{
cout << query[i] << " ";
}
cout << endl;
#endif
#if !OPTIMAL
ClientLog("Creating clients states");
#endif
uint8_t **client_states = new uint8_t *[N];
for (int i = 0; i < N; i++)
{
client_states[i] = new uint8_t[C];
for (int j = 0; j < C; j++)
{
client_states[i][j] = 0; //Default state is 0
}
}
#if !OPTIMAL
ClientLog("Starting first FSM");
cout << pArgs.FSM1_rounds << endl;
for (int i = 0; i < pArgs.FSM1_rounds; i++)
{
cout << i << " : " << (uint32_t)pArgs.FSM1_steps[i] << endl;
}
#endif
int l = 0;
int smallest_number_of_bits = 0;
for (int round = 0; round < pArgs.FSM1_rounds; round++)
{
uint8_t concurrent_iterations = pArgs.FSM1_steps[round];
uint8_t all_ones_string = (uint8_t)((1 << concurrent_iterations) - 1);
uint8_t pow_iterations = (uint8_t)((1 << concurrent_iterations));
uint8_t pos_states = 2 * pow_iterations;
if (l >= bits_per_component[smallest_number_of_bits])
{
while(l >= bits_per_component[smallest_number_of_bits])
smallest_number_of_bits ++;
}
uint8_t number_of_higher_bit_entries = C - smallest_number_of_bits;
uint8_t *choices = new uint8_t[N * number_of_higher_bit_entries * concurrent_iterations];
for (int i = 0; i < N; i++)
{
for (int j = 0; j < number_of_higher_bit_entries; j++)
{
uint8_t postImageJ = j + C - number_of_higher_bit_entries;
uint8_t query_bits = (query[postImageJ] >> l) & all_ones_string;
choices[number_of_higher_bit_entries * i + j] = pow_iterations * client_states[i][postImageJ] + query_bits;
}
}
#if !OPTIMAL
ClientLog("Created Choice bits");
#endif
uint8_t *rMsg = new uint8_t[N * number_of_higher_bit_entries];
ConstructOTReceiverMsg(rMsg, *(FSM1_RC[round]), choices, N * number_of_higher_bit_entries);
SendLargePacked(socket, 1 + concurrent_iterations, N * number_of_higher_bit_entries, rMsg);
uint8_t *sMsg = new uint8_t[N * number_of_higher_bit_entries * pos_states];
ReceiveLargePacked(socket, 1, N * number_of_higher_bit_entries * pos_states, sMsg);
uint8_t *msgs2 = new uint8_t[N * number_of_higher_bit_entries];
RecoverReceiverMsg(msgs2, *(FSM1_RC[round]), choices, sMsg, N * number_of_higher_bit_entries, 1, bits_FSM1);
#if !OPTIMAL
ClientLog("Finished One Round");
#endif
for (int i = 0; i < N; i++)
{
for (int j = 0; j < number_of_higher_bit_entries; j++)
{
client_states[i][j + C - number_of_higher_bit_entries] = msgs2[number_of_higher_bit_entries * i + j];
}
}
delete[] choices;
delete[] msgs2;
delete[] rMsg;
delete[] sMsg;
l += concurrent_iterations;
}
#if !OPTIMAL
ClientLog("Finished first FSM");
#endif
#if VERBOSE
for (int i = 0; i < N; i++)
{
cout << "Database entry: " << i << endl;
cout << "DB : ";
for (int j = 0; j < C; j++)
{
cout << 0 << ", ";
}
cout << endl;
cout << "Query: ";
for (int j = 0; j < C; j++)
{
cout << query[j] << ", ";
}
cout << endl;
cout << "Equal: ";
for (int j = 0; j < C; j++)
{
cout << (uint32_t)client_states[i][j] << ", ";
}
cout << endl;
cout << "Actual: ";
for (int j = 0; j < C; j++)
{
uint32_t value = (uint32_t)client_states[i][j];
uint8_t accept_state = 0;
if (accept_state == 1)
{
cout << value << ", ";
}
if (accept_state == 0)
{
cout << !value << ", ";
}
}
cout << endl;
}
#endif
#if !OPTIMAL
ClientLog("Starting Second FSM");
cout << "Finished with 1st part of OT correlation calculations" << endl;
cout << "Setting up second FSM to compute k matches" << endl;
#endif
uint8_t *client_k_state = new uint8_t[N];
for (int i = 0; i < N; i++)
{
client_k_state[i] = 2; // default state
}
uint32_t c = 0;
for (int round = 0; round < pArgs.FSM2_rounds; round++)
{
uint8_t round_step = pArgs.FSM2_steps[round];
uint8_t round_factor = 1 << round_step;
uint8_t *choices = new uint8_t[N];
for (int i = 0; i < N; i++)
{
// (client state, components equal)
uint8_t client_state_value = 0;
for (int j = 0; j < round_step; j++)
{
client_state_value += client_states[i][c + j] * (1 << j);
}
choices[i] = round_factor * client_k_state[i] + client_state_value;
}
#if !OPTIMAL
ClientLog("One one rounds");
#endif
uint8_t *rMsg = new uint8_t[N];
ConstructOTReceiverMsg(rMsg, *(FSM2_RC[round]), choices, N);
SendLargePacked(socket, 2 + round_step, N, rMsg);
#if !OPTIMAL
cout << "Sending: " << N << endl;
#endif
uint8_t *sMsg = new uint8_t[N * 3 * round_factor];
ReceiveLargePacked(socket, 2, N * 3 * round_factor, sMsg);
uint8_t *msgs2 = new uint8_t[N];
RecoverReceiverMsg(msgs2, *(FSM2_RC[round]), choices, sMsg, N, 2, bits_FSM2);
for (int i = 0; i < N; i++)
{
client_k_state[i] = msgs2[i];
}
c += round_step;
delete[] choices;
delete[] msgs2;
delete[] rMsg;
delete[] sMsg;
}
#if !OPTIMAL
ClientLog("Finished second FSM");
#endif
for (int i = 0; i < N; i++)
{
if (client_k_state[i] > 0)
ClientLog("found a match at index " + to_string(i));
}
#if !OPTIMAL
cout << "Memory clean up" << endl;
#endif
delete[] query;
for (int i = 0; i < N; i++)
{
delete[] client_states[i];
}
delete[] client_states;
delete[] client_k_state;
for (int i = 0; i < pArgs.FSM1_rounds; i++)
{
delete FSM1_RC[i];
}
delete[] FSM1_RC;
for (int i = 0; i < pArgs.FSM2_rounds; i++)
{
delete FSM2_RC[i];
}
delete[] FSM2_RC;
}
void StartClient(const char *address, int port, string pArgsFile, string clientDataDir)
{
CSocket *socket = new CSocket();
if (Connect(socket, address, port))
{
ClientLog("successfully connected to server");
}
else
{
stringstream ss;
ss << "unable to connect to port " << port << " on address " << address;
ClientLog(ss.str());
return;
}
auto t_start = chrono::high_resolution_clock::now();
RunClientProtocol(socket, pArgsFile, clientDataDir);
auto t_end = chrono::high_resolution_clock::now();
chrono::duration<float> duration = t_end - t_start;
ClientLog("protocol execution completed");
cout << "protocol execution time: " << duration.count() << " seconds" << endl;
cout << "total bytes sent: " << socket->GetBytesSent() << endl;
cout << "total bytes received: " << socket->GetBytesReceived() << endl;
cout << "total bytes communicated (received + sent): " << socket->GetBytesSent() + socket->GetBytesReceived() << endl;
socket->Close();
delete socket;
}
int main(int argc, const char **argv)
{
if (argc < 5)
{
cout << "usage: ./ProtocolClient ProtocolArgsFile clientDataDir addr port" << endl;
return 1;
}
srand(time(NULL));
int port = atoi(argv[4]);
string pArgsFile(argv[1]);
string clientDataDir(argv[2]);
StartClient(argv[3], port, pArgsFile, clientDataDir);
}