-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathot.c
More file actions
235 lines (201 loc) · 5.62 KB
/
ot.c
File metadata and controls
235 lines (201 loc) · 5.62 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
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>
#include <sys/wait.h>
#include "libot/ot.h"
#include "codes.h"
#include "otext.h"
static size_t nOTs = 1 << 10;
bool active_security = false;
uint8_t codewordsm = 1;
size_t codewordsn = 2;
code_t *code = NULL;
#define START_TIMEIT() \
struct timeval __start, __end; gettimeofday(&__start, NULL)
#define END_TIMEIT() \
gettimeofday(&__end, NULL); \
double __sdiff = (__end.tv_sec - __start.tv_sec), __udiff = (__end.tv_usec - __start.tv_usec)
#define GET_TIMEIT() \
__sdiff + __udiff * 1e-6
#define TIMEIT_FORMAT "%lf"
static int sender_main(int port) {
int sockfd;
int newsockfd;
int rcvbuf = BUFSIZE;
bitmatrix_t V;
sockfd = server_listen(port);
newsockfd = server_accept(sockfd);
if (setsockopt(newsockfd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) !=
0) {
perror("ERROR setsockopt");
exit(-1);
}
START_TIMEIT();
V = kk_sender(newsockfd, nOTs);
END_TIMEIT();
#ifndef NDEBUG
for (size_t j = 0; j < nOTs; ++j) {
for (size_t i = 0; i < codewordsn; i++) {
Bprint(row(V, j*codewordsn+i), octs(KAPPA));
printf("\t");
}
printf("\n");
}
#endif
free_bitmatrix(V);
printf("%ld OTs sender: " TIMEIT_FORMAT " seconds\n", nOTs, GET_TIMEIT());
uint8_t end = 0x42;
writing(newsockfd, &end, 1);
shutdown(newsockfd, 2);
shutdown(sockfd, 2);
return 0;
}
static int receiver_main(const char *host, const int port) {
int sockfd;
int sndbuf = BUFSIZE;
bitmatrix_t V;
client_connect(&sockfd, host, port);
if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(int)) != 0) {
perror("ERROR setsockopt");
exit(-1);
}
START_TIMEIT();
V = kk_receiver(sockfd, nOTs);
END_TIMEIT();
#ifndef NDEBUG
for (size_t j = 0; j < nOTs; ++j) {
Bprint(row(V, j), octs(KAPPA));
printf("\n");
}
#endif
free_bitmatrix(V);
printf("%ld OTs receiver: " TIMEIT_FORMAT " seconds\n", nOTs, GET_TIMEIT());
uint8_t end;
reading(sockfd, &end, 1);
assert(end == 0x42);
shutdown(sockfd, 2);
return 0;
}
static int both_main(const char *host, const int port) {
int spid;
if ((spid = fork()) == 0) {
sender_main(port);
return 0;
}
int rpid;
if ((rpid = fork()) == 0) {
receiver_main(host, port);
return 0;
}
waitpid(spid, NULL, 0);
waitpid(rpid, NULL, 0);
return 0;
}
static const char* short_options = "hH:p:m:n:aC:";
static const struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"host", required_argument, 0, 'H'},
{"port", required_argument, 0, 'p'},
{"nots", required_argument, 0, 'm'},
{"out-of", required_argument, 0, 'n'},
{"active", no_argument, 0, 'a'},
{"code", required_argument, 0, 'C'},
{0, 0, 0, 0}
};
const char help_message[] =
"Usage:\n"
" ot <role> [options]\n"
" ot -h | --help\n"
"\n"
"Options:\n"
"-h --help Shows this screen.\n"
"-m INT log2 number of OTs [default: 7].\n"
"-H HOST, --host HOST IP-address [default: localhost].\n"
"-p INT, --port INT IP-port [default: 1337].\n"
"-n INT, --out-of INT Do 1-out-of-n oblivious transfer\n"
"-a, --active Perform active-security checks.\n"
"-C, --code Linear code to be used [default: WH]\n"
"";
const char usage_pattern[] =
"Usage:\n"
" ot <role> [options]\n"
" ot -h | --help\n"
"";
void usage()
{
fputs(usage_pattern, stderr);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
char *host = "localhost";
int port = 1337;
if (argc < 2) {
usage();
}
char *role = argv[1];
char opt;
int option_index;
while ((opt=getopt_long(argc, argv,
short_options, long_options,
&option_index)) != -1) {
/* XXX. we are not really sanitizing the input */
switch (opt) {
case 'h':
fputs(help_message, stdout);
exit(EXIT_SUCCESS);
break;
case 'H':
host = optarg;
break;
case 'p':
port = atoi(optarg);
break;
case 'm':
nOTs = 1 << atol(optarg);
break;
case 'n':
/* XXX. here we assume the input will be of the form 2^{optarg} */
codewordsn = atoi(optarg);
break;
case 'a':
active_security = true;
break;
case 'C':
code = load_codestr(optarg);
if (!code) usage();
break;
case '?':
default:
usage();
}
}
// If no code was provided, use Walsh-Hadamard.
if (!code) code = load_codestr(DEFAULT_CODE);
// XXX.
// There's a bug in reaceiver.c that produces a segmentation fault when the number
// of OTs is not divisible by 128. Right now I don't have enough time to investigate.
// Remove the following line to reproduce.
// (Note: tests have always been done on multiples of 128 in the past.)
if (active_security) nOTs -= (nOTs + SSEC) % 128;
// XXX.
// There's a bug in receiver.c that produces a malloc() corruption when the number of OTs
// is less than 64. Right now I don't have enough time to investigate.
if (nOTs < 64) usage();
if (!strcmp("sender", role)) {
sender_main(port);
} else if (!strcmp("receiver", role)) {
receiver_main(host, port);
} else if (!strcmp("both", role)) {
both_main(host, port);
} else {
usage();
}
unload_code(code);
return 0;
}