spir_test.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <sys/random.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include <bsd/stdlib.h>
  7. #include <boost/asio.hpp>
  8. #include <string>
  9. using boost::asio::ip::tcp;
  10. #include "spir.hpp"
  11. #include "reading.h"
  12. using std::cout;
  13. using std::cerr;
  14. static inline size_t elapsed_us(const struct timeval *start)
  15. {
  16. struct timeval end;
  17. gettimeofday(&end, NULL);
  18. return (end.tv_sec-start->tv_sec)*1000000 + end.tv_usec - start->tv_usec;
  19. }
  20. using socket_t = boost::asio::ip::tcp::socket;
  21. void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& sockets_, int port, size_t j)
  22. {
  23. tcp::acceptor acceptor_a(io_context, tcp::endpoint(tcp::v4(), port));
  24. tcp::socket sb_a(acceptor_a.accept());
  25. sockets_[j] = std::move(sb_a);
  26. // sockets_.emplace_back(std::move(sb_a));
  27. }
  28. void write_pub_params(tcp::socket& sout, string pub_params)
  29. {
  30. auto * bytes_to_write = pub_params.data();
  31. auto bytes_remaining = pub_params.length();
  32. while (bytes_remaining )
  33. {
  34. auto bytes_written = sout.write_some(boost::asio::buffer(bytes_to_write, bytes_remaining));
  35. bytes_to_write += bytes_written;
  36. bytes_remaining -= bytes_written;
  37. }
  38. }
  39. void read_pub_params(tcp::socket& sin, string& pub_params_recv, size_t len)
  40. {
  41. pub_params_recv.resize(len);
  42. auto bytes_remaining = len;
  43. char * bytes_to_read = (char*)pub_params_recv.data();
  44. while (bytes_remaining )
  45. {
  46. auto bytes_read = sin.read_some(boost::asio::buffer(bytes_to_read, bytes_remaining));
  47. bytes_to_read += bytes_read;
  48. bytes_remaining -= bytes_read;
  49. }
  50. }
  51. int main(int argc, char **argv)
  52. {
  53. boost::asio::io_context io_context;
  54. tcp::resolver resolver(io_context);
  55. std::string addr = "127.0.0.1";
  56. const std::string host1 = (argc < 1) ? "127.0.0.1" : argv[1];
  57. const size_t number_of_sockets = 5;
  58. std::vector<socket_t> sockets_;
  59. for(size_t j = 0; j < number_of_sockets + 1; ++j)
  60. {
  61. tcp::socket emptysocket(io_context);
  62. sockets_.emplace_back(std::move(emptysocket));
  63. }
  64. sockets_.reserve(number_of_sockets + 1);
  65. printf("number_of_sockets = %zu\n", number_of_sockets);
  66. std::vector<socket_t> sockets_2;
  67. std::vector<int> ports;
  68. for(size_t j = 0; j < number_of_sockets; ++j)
  69. {
  70. int port = 6000;
  71. ports.push_back(port + j);
  72. }
  73. std::vector<int> ports2_0;
  74. for(size_t j = 0; j < number_of_sockets; ++j)
  75. {
  76. int port = 8000;
  77. ports2_0.push_back(port + j);
  78. }
  79. std::vector<int> ports2_1;
  80. for(size_t j = 0; j < number_of_sockets; ++j)
  81. {
  82. int port = 9000;
  83. ports2_1.push_back(port + j);
  84. }
  85. #if (PARTY == 0)
  86. for(size_t j = 0; j < number_of_sockets; ++j)
  87. {
  88. tcp::socket sb_a(io_context);
  89. boost::asio::connect(sb_a, resolver.resolve({host1, std::to_string(ports[j])}));
  90. sockets_[j] = std::move(sb_a);
  91. }
  92. #else
  93. boost::asio::thread_pool pool2(number_of_sockets);
  94. for(size_t j = 0; j < number_of_sockets; ++j)
  95. {
  96. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(sockets_), ports[j], j));
  97. }
  98. pool2.join();
  99. #endif
  100. #if (PARTY == 0)
  101. std::cout << "PARTY 0" << std::endl;
  102. #endif
  103. #if (PARTY == 1)
  104. std::cout << "PARTY 1" << std::endl;
  105. #endif
  106. // if (argc < 2 || argc > 5) {
  107. // cerr << "Usage: " << argv[0] << " r [num_threads [num_preproc [num_pirs]]]\n";
  108. // cerr << "r = log_2(num_records)\n";
  109. // exit(1);
  110. // }
  111. uint32_t r, num_threads = 1, num_preproc = 1, num_pirs = 1;
  112. r = strtoul(argv[2], NULL, 10);
  113. size_t num_records = ((size_t) 1)<<r;
  114. size_t num_records_mask = num_records - 1;
  115. if (argc > 4) {
  116. num_threads = strtoul(argv[3], NULL, 10);
  117. }
  118. if (argc > 5) {
  119. num_preproc = strtoul(argv[4], NULL, 10);
  120. }
  121. if (argc > 5) {
  122. num_pirs = strtoul(argv[5], NULL, 10);
  123. } else {
  124. num_pirs = num_preproc;
  125. }
  126. cout << "===== ONE-TIME SETUP =====\n\n";
  127. struct timeval otsetup_start;
  128. gettimeofday(&otsetup_start, NULL);
  129. SPIR::init(num_threads);
  130. string pub_params, pub_params_recv;
  131. SPIR_Client client(r, pub_params);
  132. std::thread writer(write_pub_params, std::ref(sockets_[0]), pub_params);
  133. std::thread reader(read_pub_params, std::ref(sockets_[0]), std::ref(pub_params_recv), pub_params.size());
  134. writer.join();
  135. reader.join();
  136. SPIR_Server server(r, pub_params_recv);
  137. size_t otsetup_us = elapsed_us(&otsetup_start);
  138. cout << "One-time setup: " << otsetup_us << " µs\n";
  139. cout << "pub_params len = " << pub_params_recv.length() << "\n";
  140. cout << "\n===== PREPROCESSING =====\n\n";
  141. cout << "num_preproc = " << num_preproc << "\n";
  142. struct timeval preproc_client_start;
  143. gettimeofday(&preproc_client_start, NULL);
  144. string preproc_msg = client.preproc(num_preproc);
  145. string preproc_msg_recv = preproc_msg;
  146. boost::asio::write(sockets_[0], boost::asio::buffer(preproc_msg));
  147. boost::asio::read(sockets_[0], boost::asio::buffer(preproc_msg_recv));
  148. size_t preproc_client_us = elapsed_us(&preproc_client_start);
  149. cout << "Preprocessing client: " << preproc_client_us << " µs\n";
  150. cout << "preproc_msg len = " << preproc_msg.length() << "\n";
  151. struct timeval preproc_server_start;
  152. gettimeofday(&preproc_server_start, NULL);
  153. string preproc_resp = server.preproc_process(preproc_msg_recv);
  154. string preproc_resp_recv = preproc_resp;
  155. boost::asio::write(sockets_[0], boost::asio::buffer(preproc_resp));
  156. boost::asio::read(sockets_[0], boost::asio::buffer(preproc_resp_recv));
  157. size_t preproc_server_us = elapsed_us(&preproc_server_start);
  158. cout << "Preprocessing server: " << preproc_server_us << " µs\n";
  159. cout << "preproc_resp len = " << preproc_resp.length() << "\n";
  160. struct timeval preproc_finish_start;
  161. gettimeofday(&preproc_finish_start, NULL);
  162. client.preproc_finish(preproc_resp_recv);
  163. size_t preproc_finish_us = elapsed_us(&preproc_finish_start);
  164. cout << "Preprocessing client finish: " << preproc_finish_us << " µs\n";
  165. // Create the database
  166. SPIR::DBEntry *db = new SPIR::DBEntry[num_records];
  167. for (size_t i=0; i<num_records; ++i) {
  168. db[i] = i;// * 10000001;
  169. #if(PARTY == 0)
  170. db[i] = 0;
  171. #endif
  172. }
  173. SPIR::DBEntry rand_blind = 1221030;
  174. //blind_the_database(db, rand_blind, num_records);
  175. for (size_t i=0; i<num_pirs; ++i) {
  176. cout << "\n===== SPIR QUERY " << i+1 << " =====\n\n";
  177. size_t idx;
  178. if (getrandom(&idx, sizeof(idx), 0) != sizeof(idx)) {
  179. cerr << "Failure in getrandom\n";
  180. exit(1);
  181. }
  182. idx &= num_records_mask;
  183. boost::asio::write(sockets_[0], boost::asio::buffer(&idx, sizeof(idx)));
  184. size_t idx_recv;
  185. boost::asio::read(sockets_[0], boost::asio::buffer(&idx_recv, sizeof(idx_recv)));
  186. idx_recv += idx;
  187. idx_recv = idx_recv % num_records;
  188. cout << "idx = " << idx << std::endl;
  189. cout << "idx_reconstructed = " << idx_recv << std::endl;
  190. // idx = 100;
  191. // #if(PARTY == 1)
  192. // idx = 40;
  193. // #endif
  194. struct timeval query_client_start;
  195. gettimeofday(&query_client_start, NULL);
  196. string query_msg = client.query(idx);
  197. boost::asio::write(sockets_[0], boost::asio::buffer(query_msg));
  198. string query_msg_recv = client.query(idx);
  199. boost::asio::read(sockets_[0], boost::asio::buffer(query_msg_recv));
  200. size_t query_client_us = elapsed_us(&query_client_start);
  201. cout << "Query client: " << query_client_us << " µs\n";
  202. cout << "query_msg len = " << query_msg.length() << "\n";
  203. struct timeval query_server_start;
  204. gettimeofday(&query_server_start, NULL);
  205. //string query_resp = server.query_process(query_msg_recv, db, 0, 0);
  206. string query_resp = server.query_process(query_msg_recv, db, idx, rand_blind);
  207. boost::asio::write(sockets_[0], boost::asio::buffer(query_resp));
  208. string query_resp_recv = query_resp;
  209. boost::asio::read(sockets_[0], boost::asio::buffer(query_resp_recv));
  210. size_t query_server_us = elapsed_us(&query_server_start);
  211. cout << "Query server: " << query_server_us << " µs\n";
  212. cout << "query_resp len = " << query_resp.length() << "\n";
  213. struct timeval query_finish_start;
  214. gettimeofday(&query_finish_start, NULL);
  215. SPIR::DBEntry entry = client.query_finish(query_resp_recv);
  216. boost::asio::write(sockets_[0], boost::asio::buffer(&entry, sizeof(entry)));
  217. SPIR::DBEntry entry_recv;
  218. boost::asio::read(sockets_[0], boost::asio::buffer(&entry_recv, sizeof(entry)));
  219. SPIR::DBEntry read_output = entry_recv - rand_blind;
  220. boost::asio::write(sockets_[0], boost::asio::buffer(&read_output, sizeof(entry)));
  221. SPIR::DBEntry read_output_recv;
  222. boost::asio::read(sockets_[0], boost::asio::buffer(&read_output_recv, sizeof(entry)));
  223. read_output_recv += read_output;
  224. cout << "read_output_recv = " << read_output_recv << std::endl;
  225. size_t query_finish_us = elapsed_us(&query_finish_start);
  226. cout << "Query client finish: " << query_finish_us << " µs\n";
  227. cout << "idx = " << idx << "; entry = " << entry << "\n";
  228. }
  229. delete[] db;
  230. return 0;
  231. }