1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 // Tool to generate dummy data for the client and server in Private Join and
17 // Compute.
18 
19 #include <iostream>
20 #include <ostream>
21 #include <utility>
22 
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "private_join_and_compute/data_util.h"
26 
27 // Flags defining the size of data to generate for the client and server, bounds
28 // on the associated values, and where the write the outputs.
29 ABSL_FLAG(int64_t, server_data_size, 100,
30           "Number of dummy identifiers in server database.");
31 ABSL_FLAG(
32     int64_t, client_data_size, 100,
33     "Number of dummy identifiers and associated values in client database.");
34 ABSL_FLAG(int64_t, intersection_size, 50,
35           "Number of items in the intersection. Must be less than the "
36           "server and client data sizes.");
37 ABSL_FLAG(int64_t, max_associated_value, 100,
38           "Dummy associated values for the client will be between 0 and "
39           "this. Must be nonnegative.");
40 ABSL_FLAG(std::string, server_data_file, "",
41           "The file to which to write the server database.");
42 ABSL_FLAG(std::string, client_data_file, "",
43           "The file to which to write the client database.");
44 
main(int argc,char ** argv)45 int main(int argc, char** argv) {
46   absl::ParseCommandLine(argc, argv);
47 
48   auto maybe_dummy_data = private_join_and_compute::GenerateRandomDatabases(
49       absl::GetFlag(FLAGS_server_data_size),
50       absl::GetFlag(FLAGS_client_data_size),
51       absl::GetFlag(FLAGS_intersection_size),
52       absl::GetFlag(FLAGS_max_associated_value));
53 
54   if (!maybe_dummy_data.ok()) {
55     std::cerr << "GenerateDummyData: Error generating the dummy data: "
56               << maybe_dummy_data.status() << std::endl;
57     return 1;
58   }
59 
60   auto dummy_data = std::move(maybe_dummy_data.value());
61   auto& server_identifiers = std::get<0>(dummy_data);
62   auto& client_identifiers_and_associated_values = std::get<1>(dummy_data);
63   int64_t intersection_sum = std::get<2>(dummy_data);
64 
65   auto server_write_status = private_join_and_compute::WriteServerDatasetToFile(
66       server_identifiers, absl::GetFlag(FLAGS_server_data_file));
67   if (!server_write_status.ok()) {
68     std::cerr << "GenerateDummyData: Error writing server dataset: "
69               << server_write_status << std::endl;
70     return 1;
71   }
72 
73   auto client_write_status = private_join_and_compute::WriteClientDatasetToFile(
74       client_identifiers_and_associated_values.first,
75       client_identifiers_and_associated_values.second,
76       absl::GetFlag(FLAGS_client_data_file));
77   if (!client_write_status.ok()) {
78     std::cerr << "GenerateDummyData: Error writing client dataset: "
79               << client_write_status << std::endl;
80     return 1;
81   }
82 
83   std::cout << "Generated Server dataset of size "
84             << absl::GetFlag(FLAGS_client_data_size)
85             << ", Client dataset of size "
86             << absl::GetFlag(FLAGS_client_data_size) << std::endl;
87   std::cout << "Intersection size = " << absl::GetFlag(FLAGS_intersection_size)
88             << std::endl;
89   std::cout << "Intersection sum = " << intersection_sum << std::endl;
90 
91   return 0;
92 }
93