forked from ockie1729/paillier-secret-computation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
172 lines (142 loc) · 3.28 KB
/
client.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
/* Copyright (c) 2016 Kana Shimizu and Yusuke Okimoto */
#include "sumall.h"
#include "comm.h"
#include<sys/time.h>
#include <unistd.h>
//#define DEBUG
#ifdef DEBUG
#define DBG_PRT(...) printf(__VA_ARGS__)
#else
#define DBG_PRT(...)
#endif
double
get_wall_time ()
{
struct timeval time;
if (gettimeofday (&time, NULL))
{
return 0;
}
return (double) time.tv_sec + (double) time.tv_usec * .000001;
}
char *host_m; // master server
char *host_c; // compute server
int port_m;
int port_c;
std::string tmp_dir_path;
std::string key_dir_path;
long long
val[VECTOR_DIMENSION];
int
ID;
int
setParam (int argc, char **argv)
{
int
opt;
int
nopt = 0;
extern int optind;
tmp_dir_path = "";
key_dir_path = "";
while ((opt = getopt (argc, argv, "m:c:p:q:d:k:i:")) != -1)
{
switch (opt)
{
case 'm':
nopt++;
host_m = optarg;
break;
case 'c':
nopt++;
host_c = optarg;
break;
case 'p':
nopt++;
port_m = atoi (optarg);
break;
case 'q':
nopt++;
port_c = atoi (optarg);
break;
case 'd':
tmp_dir_path = optarg;
break;
case 'k':
key_dir_path = optarg;
break;
case 'i':
nopt++;
ID = atoi (optarg);
break;
default:
fprintf (stderr,
"Usage: %s [-d tmpfile_dir_path] [-k key_dir_path] -m master_server -c compute_server -p port_m -q port_c -i ID -- val1 val2 ...\n",
argv[0]);
exit (EXIT_FAILURE);
}
}
if (nopt != 5)
{
fprintf (stderr,
"Usage: %s [-d tmpfile_dir_path] [-k key_dir_path] -m master_server -c compute_server -p port_m -q port_c -i ID -- val1 val2 ...\n",
argv[0]);
exit (1);
}
argc -= optind;
argv += optind;
if (argc != VECTOR_DIMENSION)
{
fprintf (stderr,
"Usage: %s [-d tmpfile_dir_path] [-k key_dir_path] -m master_server -c compute_server -p port_m -q port_c -i ID -- val1 val2 ...",
argv[0]);
exit (1);
}
for (int i=0; i<VECTOR_DIMENSION; i++)
{
val[i] = atoll(argv[i]);
}
return (0);
}
int
main (int argc, char **argv)
{
int
recvSize = 0;
int
sentSize = 0;
double
wts,
wte;
PublicKey
pub;
wts = get_wall_time ();
setParam (argc, argv);
int
sock = prepCSock (host_m, port_m);
std::string publicKeyFile = key_dir_path + "pubkey";
recvSize += recvFile (sock, (char *) publicKeyFile.c_str ()); //receive pubkey from server
pub.load (publicKeyFile);
std::string file = tmp_dir_path + "u2m_ack";
encrypt (file, pub, ID);
sentSize += sendFile (sock, (char *) file.c_str ());
closeSock (sock);
int
sock2 = prepCSock (host_c, port_c);
file = tmp_dir_path + "id";
std::ofstream ofs ((char *) file.c_str ());
ofs << ID << std::endl;
sendFile (sock2, (char *) file.c_str ()); //send round and positions to server
pub.load (publicKeyFile);
file = tmp_dir_path + "c2s_ran";
encrypt (file, pub, val, VECTOR_DIMENSION);
sentSize += sendFile (sock, (char *) file.c_str ());
closeSock (sock2);
wte = get_wall_time ();
std::cerr << "client(Wall): " << wte - wts << "\n";
std::cerr.precision (3);
std::cerr << "c2s, s2c(Mbyte): " << (double) sentSize / (1024 *
1024) << ", " <<
(double) recvSize / (1024 * 1024) << "\n";
return (0);
}