forked from google/private-join-and-compute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cc
154 lines (135 loc) · 5.93 KB
/
client.cc
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
/*
* Copyright 2019 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iostream>
#include <memory>
#include <string>
#include "gflags/gflags.h"
#include "include/grpc/grpc_security_constants.h"
#include "include/grpcpp/channel.h"
#include "include/grpcpp/client_context.h"
#include "include/grpcpp/create_channel.h"
#include "include/grpcpp/grpcpp.h"
#include "include/grpcpp/security/credentials.h"
#include "include/grpcpp/support/status.h"
#include "client_lib.h"
#include "data_util.h"
#include "match.grpc.pb.h"
#include "match.pb.h"
#include "absl/memory/memory.h"
DEFINE_string(port, "0.0.0.0:10501", "Port on which to contact server");
DEFINE_string(client_data_file, "",
"The file from which to read the client database.");
DEFINE_int32(
paillier_modulus_size, 1536,
"The bit-length of the modulus to use for Paillier encryption. The modulus "
"will be the product of two safe primes, each of size "
"paillier_modulus_size/2.");
using ::private_join_and_compute::PrivateJoinAndComputeRpc;
int ExecuteProtocol() {
::private_join_and_compute::Context context;
std::cout << "Client: Loading data..." << std::endl;
auto maybe_client_identifiers_and_associated_values =
::private_join_and_compute::ReadClientDatasetFromFile(FLAGS_client_data_file, &context);
if (!maybe_client_identifiers_and_associated_values.ok()) {
std::cerr << "Client::ExecuteProtocol: failed "
<< maybe_client_identifiers_and_associated_values.status()
<< std::endl;
return 1;
}
auto client_identifiers_and_associated_values =
std::move(maybe_client_identifiers_and_associated_values.ValueOrDie());
std::cout << "Client: Generating keys..." << std::endl;
std::unique_ptr<::private_join_and_compute::Client> client =
absl::make_unique<::private_join_and_compute::Client>(
&context, std::move(client_identifiers_and_associated_values.first),
std::move(client_identifiers_and_associated_values.second),
FLAGS_paillier_modulus_size);
// Consider grpc::SslServerCredentials if not running locally.
std::unique_ptr<PrivateJoinAndComputeRpc::Stub> stub =
PrivateJoinAndComputeRpc::NewStub(::grpc::CreateChannel(
FLAGS_port, ::grpc::experimental::LocalCredentials(
grpc_local_connect_type::LOCAL_TCP)));
// Execute StartProtocol.
std::cout
<< "Client: Starting the protocol." << std::endl
<< "Client: Waiting for response and encrypted set from the server..."
<< std::endl;
::private_join_and_compute::StartProtocolRequest start_protocol_request;
::private_join_and_compute::ServerRoundOne server_round_one;
::grpc::ClientContext start_protocol_client_context;
::grpc::Status status =
stub->StartProtocol(&start_protocol_client_context,
start_protocol_request, &server_round_one);
if (!status.ok()) {
std::cerr << "Client::ExecuteProtocol: failed to StartProtocol: "
<< status.error_message() << std::endl;
return 1;
}
// Execute ClientRoundOne.
std::cout
<< "Client: Received encrypted set from the server, double encrypting..."
<< std::endl;
auto maybe_client_round_one = client->ReEncryptSet(server_round_one);
if (!maybe_client_round_one.ok()) {
std::cerr << "Client::ExecuteProtocol: failed to ReEncryptSet: "
<< maybe_client_round_one.status() << std::endl;
return 1;
}
auto client_round_one = std::move(maybe_client_round_one.ValueOrDie());
// Execute ServerRoundTwo.
std::cout << "Client: Sending double encrypted server data and "
"single-encrypted client data to the server."
<< std::endl
<< "Client: Waiting for encrypted intersection sum..." << std::endl;
::private_join_and_compute::ServerRoundTwo server_round_two;
::grpc::ClientContext server_round_two_client_context;
status = stub->ExecuteServerRoundTwo(&server_round_two_client_context,
client_round_one, &server_round_two);
if (!status.ok()) {
std::cerr << "Client::ExecuteProtocol: failed to ExecuteServerRoundTwo: "
<< status.error_message() << std::endl;
return 1;
}
// Compute the intersection size and sum.
std::cout << "Client: Received response from the server. Decrypting the "
"intersection-sum."
<< std::endl;
auto maybe_intersection_size_and_sum = client->DecryptSum(server_round_two);
if (!maybe_intersection_size_and_sum.ok()) {
std::cerr << "Client::ExecuteProtocol: failed to DecryptSum: "
<< maybe_intersection_size_and_sum.status() << std::endl;
return 1;
}
auto intersection_size_and_sum =
std::move(maybe_intersection_size_and_sum.ValueOrDie());
// Output the result.
int64_t intersection_size = intersection_size_and_sum.first;
auto maybe_intersection_sum = intersection_size_and_sum.second.ToIntValue();
if (!maybe_intersection_sum.ok()) {
std::cerr
<< "Client::ExecuteProtocol: failed to recover the intersection sum: "
<< maybe_intersection_sum.status() << std::endl;
return 1;
}
std::cout << "Client: The intersection size is " << intersection_size
<< " and the intersection-sum is "
<< maybe_intersection_sum.ValueOrDie() << std::endl;
return 0;
}
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
return ExecuteProtocol();
}