duoram.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include <type_traits> // std::is_same<>
  2. #include <limits> // std::numeric_limits<>
  3. #include <climits> // CHAR_BIT
  4. #include <cmath> // std::log2, std::ceil, std::floor
  5. #include <stdexcept> // std::runtime_error
  6. #include <array> // std::array<>
  7. #include <iostream> // std::istream and std::ostream
  8. #include <vector> // std::vector<>
  9. #include <memory> // std::shared_ptr<>
  10. #include <utility> // std::move
  11. #include <algorithm> // std::copy
  12. #include <cstring> // std::memcpy
  13. #include <bsd/stdlib.h> // arc4random_buf
  14. #include <x86intrin.h> // SSE and AVX intrinsics
  15. #include <chrono>
  16. #include <thread>
  17. #include <deque>
  18. #include <../boost/asio.hpp>
  19. using boost::asio::ip::tcp;
  20. using namespace std::chrono;
  21. using namespace std;
  22. using socket_t = boost::asio::ip::tcp::socket;
  23. size_t communication_cost_dep_read = 0;
  24. size_t communication_cost_write = 0;
  25. size_t communication_cost_ind_read = 0;
  26. #include "prg.h"
  27. #include "prg_aes_impl.h"
  28. #include "block.h"
  29. #include "duoram-utils.h"
  30. #include "readvectors.h"
  31. #include "duoram-read.h"
  32. #include "duoram-refresh.h"
  33. #include "duoram-write.h"
  34. double dependent_read_time = 0.0;
  35. double write_time = 0.0;
  36. double interleaved_time = 0.0;
  37. void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& sockets_, int port, size_t j)
  38. {
  39. tcp::acceptor acceptor_a(io_context, tcp::endpoint(tcp::v4(), port));
  40. tcp::socket sb_a(acceptor_a.accept());
  41. sockets_[j] = std::move(sb_a);
  42. }
  43. int main(const int argc, const char * argv[])
  44. {
  45. size_t expo = atoi(argv[3]);
  46. size_t db_nitems = 1ULL << expo;
  47. size_t number_of_writes = atoi(argv[4]);
  48. size_t number_of_ind_reads = atoi(argv[5]);
  49. size_t number_of_dep_reads = atoi(argv[6]);
  50. size_t number_of_accesses = atoi(argv[7]);
  51. reading_temp = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  52. DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  53. updated_DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  54. blinded_DB = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  55. blinded_DB_recv = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  56. updated_blinded_DB_recv = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  57. b = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  58. c = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  59. d = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  60. reading_b = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  61. reading_c = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  62. reading_d = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  63. writing_b = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  64. writing_c = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  65. writing_d = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  66. size_t * rotate_by_ = new size_t[number_of_writes];
  67. boost::asio::io_context io_context;
  68. tcp::resolver resolver(io_context);
  69. std::string addr = "127.0.0.1";
  70. const std::string host1 = (argc < 2) ? "127.0.0.1" : argv[1];
  71. const std::string host2 = (argc < 3) ? "127.0.0.1" : argv[2];
  72. bool party;
  73. const size_t number_of_sockets = 40;
  74. std::vector<socket_t> sockets_;
  75. for(size_t j = 0; j < number_of_sockets + 1; ++j)
  76. {
  77. tcp::socket emptysocket(io_context);
  78. sockets_.emplace_back(std::move(emptysocket));
  79. }
  80. sockets_.reserve(number_of_sockets + 1);
  81. //printf("number_of_sockets = %zu\n", number_of_sockets);
  82. std::vector<socket_t> sockets_2;
  83. std::vector<int> ports;
  84. for(size_t j = 0; j < number_of_sockets; ++j)
  85. {
  86. int port = 6000;
  87. ports.push_back(port + j);
  88. }
  89. std::vector<int> ports2_0;
  90. for(size_t j = 0; j < number_of_sockets; ++j)
  91. {
  92. int port = 8000;
  93. ports2_0.push_back(port + j);
  94. }
  95. std::vector<int> ports2_1;
  96. for(size_t j = 0; j < number_of_sockets; ++j)
  97. {
  98. int port = 9000;
  99. ports2_1.push_back(port + j);
  100. }
  101. #if (PARTY == 0)
  102. party = false;
  103. #ifdef ThreeParty
  104. for(size_t j = 0; j < number_of_sockets; ++j)
  105. {
  106. tcp::socket sb_a(io_context);
  107. boost::asio::connect(sb_a, resolver.resolve({host2, std::to_string(ports2_0[j])}));
  108. sockets_2.emplace_back(std::move(sb_a));
  109. }
  110. #endif
  111. for(size_t j = 0; j < number_of_sockets; ++j)
  112. {
  113. tcp::socket sb_a(io_context);
  114. boost::asio::connect(sb_a, resolver.resolve({host1, std::to_string(ports[j])}));
  115. sockets_[j] = std::move(sb_a);
  116. }
  117. #else
  118. party = true;
  119. #ifdef ThreeParty
  120. for(size_t j = 0; j < number_of_sockets; ++j)
  121. {
  122. tcp::socket sb_a(io_context);
  123. boost::asio::connect(sb_a, resolver.resolve({host2, std::to_string(ports2_1[j])}));
  124. sockets_2.emplace_back(std::move(sb_a));
  125. }
  126. #endif
  127. boost::asio::thread_pool pool2(number_of_sockets);
  128. for(size_t j = 0; j < number_of_sockets; ++j)
  129. {
  130. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(sockets_), ports[j], j));
  131. }
  132. pool2.join();
  133. #endif
  134. generate_random_distinguished_points(party);
  135. AES_KEY aeskey;
  136. blinds = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  137. updated_blinds = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  138. size_t * where_to_write = new size_t[number_of_writes];
  139. size_t * where_to_read_dependent = new size_t[number_of_dep_reads];
  140. size_t * where_to_read_independent = new size_t[number_of_ind_reads];
  141. auto start_total = std::chrono::steady_clock::now();
  142. setup(DB, updated_DB, blinded_DB_recv, blinds, updated_blinds, updated_blinded_DB_recv, db_nitems, party);
  143. for(size_t i = 0; i < number_of_accesses; ++i)
  144. {
  145. for(size_t j = 0; j < number_of_writes; ++j)
  146. {
  147. where_to_write[j] = j + 4;
  148. }
  149. for(size_t j = 0; j < number_of_dep_reads; ++j)
  150. {
  151. where_to_read_dependent[j] = j + 4;
  152. }
  153. for(size_t j = 0; j < number_of_ind_reads; ++j)
  154. {
  155. where_to_read_independent[j] = j + 4;
  156. }
  157. for(size_t j = 0; j < db_nitems; ++j)
  158. {
  159. blinds[j] = 0;
  160. updated_blinds[j] = blinds[j];
  161. updated_blinded_DB_recv[j] = blinded_DB_recv[j];
  162. }
  163. // arc4random_buf(&ri, sizeof(ri));
  164. // ri = ri % db_nitems;
  165. // boost::asio::write(sockets_[0], boost::asio::buffer(&ri, sizeof(ri)));
  166. // boost::asio::read(sockets_[0], boost::asio::buffer(&ri_recv, sizeof(ri_recv)));
  167. // if(party) ri = 2 - ri_recv;
  168. int64_t ri;
  169. read_rand_indx(party, ri);
  170. #ifdef DEBUG
  171. int64_t ri_recv;
  172. boost::asio::write(sockets_[0], boost::asio::buffer(&ri, sizeof(ri)));
  173. boost::asio::read(sockets_[0], boost::asio::buffer(&ri_recv, sizeof(ri_recv)));
  174. ri_recv = ri_recv + ri;
  175. std::cout << "ri_recv = " << ri_recv % db_nitems << std::endl;
  176. #endif
  177. #ifdef VERBOSE
  178. boost::asio::write(sockets_[0], boost::asio::buffer(&ri, sizeof(ri)));
  179. boost::asio::read(sockets_[0], boost::asio::buffer(&ri_recv, sizeof(ri_recv)));
  180. int64_t ri_reconstruction = ri + ri_recv;
  181. std::cout << "ri_reconstruction = " << ri_reconstruction << std::endl;
  182. #endif
  183. #ifdef ThreeParty
  184. DuORAM_Write * WritePb_ = new DuORAM_Write[number_of_writes];
  185. DuORAM_Write * WritePb_recv = new DuORAM_Write[number_of_writes];
  186. DB_t * read_out = new DB_t[number_of_writes];
  187. DB_t * Gamma = new DB_t[number_of_writes];
  188. #endif
  189. DB_t * CW = new DB_t[number_of_writes];
  190. DB_t * update_message = new DB_t[number_of_writes];
  191. auto start_writes = std::chrono::steady_clock::now();
  192. #ifdef ThreeParty
  193. for(size_t w = 0; w < number_of_writes; ++w)
  194. {
  195. DB_t FCW_read = 0;
  196. /*The definition of read_final_correction_word appears in duoram-utils.h*/
  197. read_final_correction_word(party, FCW_read);
  198. #ifdef VERBOSE
  199. std::cout << "FCW_read (from) = " << FCW_read << std::endl;
  200. #endif
  201. DB_t alpha0 = -FCW_read;
  202. WritePb_[w].shift = where_to_write[w] -ri;
  203. WritePb_[w].CW = distinguised_value[0];
  204. boost::asio::write(sockets_2[0], boost::asio::buffer(&WritePb_[w], sizeof(DuORAM_Write)));
  205. read(sockets_2[1], boost::asio::buffer(&Gamma[w], sizeof(DB_t)));
  206. boost::asio::write(sockets_[0], boost::asio::buffer(&WritePb_[w], sizeof(DuORAM_Write)));
  207. boost::asio::read(sockets_[0], boost::asio::buffer(&WritePb_recv[w], sizeof(DuORAM_Write)));
  208. /*The definition of read_flags_for_writing appears in duoram-utils.h*/
  209. read_flags_for_writing(party, db_nitems);
  210. rotate_by_[w] = WritePb_[w].shift + WritePb_recv[w].shift;
  211. #ifdef VERBOSE
  212. std::cout << "print database (prints the non-zero database entries): " << std::endl;
  213. /* The definition of reconstruct_database appears in duoram-utils.h */
  214. reconstruct_database(sockets_[0], DB, db_nitems);
  215. #endif
  216. for(size_t j = 0; j < db_nitems; ++j) reading_temp[j] = DB[j] + updated_blinded_DB_recv[j];
  217. /*The definition of dot_product_with_bool appears in duoram-write.h*/
  218. if(!party) read_out[w] = dot_product_with_bool(reading_temp, writing_b, db_nitems, rotate_by_[w]) +
  219. dot_product_with_bool(updated_blinds, writing_b, db_nitems, rotate_by_[w]) -
  220. dot_product_with_bool(updated_blinds, writing_c, db_nitems, rotate_by_[w]) + Gamma[w];
  221. if(party) read_out[w] = dot_product_with_bool(reading_temp, writing_c, db_nitems, rotate_by_[w]) +
  222. dot_product_with_bool(updated_blinds, writing_c, db_nitems, rotate_by_[w]) -
  223. dot_product_with_bool(updated_blinds, writing_d, db_nitems, rotate_by_[w]) + Gamma[w];
  224. #ifdef VERBOSE
  225. std::cout << "read_out[" << w << "] = " << read_out[w] << std::endl;
  226. #endif
  227. #ifdef VERBOSE
  228. std::cout << "reconstructing the output: " << print_reconstruction(sockets_[0], read_out[w]) << "\n";
  229. #endif
  230. distinguised_value[0] = 80 * (1 + w);
  231. update_message[w] = distinguised_value[0] - read_out[w] + alpha0;
  232. #ifdef VERBOSE
  233. std::cout << "The updated message shares is = " << update_message[w] << std::endl;
  234. #endif
  235. boost::asio::write(sockets_2[2], boost::asio::buffer(&update_message[w], sizeof(DB_t)));
  236. boost::asio::write(sockets_[2], boost::asio::buffer(&update_message[w], sizeof(DB_t)));
  237. boost::asio::read(sockets_[2], boost::asio::buffer(&CW[w], sizeof(DB_t)));
  238. CW[w] = CW[w] + update_message[w];
  239. #ifdef VERBOSE
  240. std::cout << "cw = " << CW[w] << std::endl;
  241. #endif
  242. }
  243. #endif
  244. for(size_t w = 0; w < number_of_writes; ++w)
  245. {
  246. /*The definition of DuoramUpdate appears in duoram-write.h*/
  247. DuoramUpdate(party, db_nitems, rotate_by_[w], DB, updated_DB, writing_b, b, CW[w], update_message[w], writing_c, writing_d, c, d);
  248. communication_cost_write += 9 * sizeof(DB_t);
  249. #ifdef DEBUG
  250. #ifdef ThreeParty
  251. /*The definition debug_ appears in duoram-utils.h*/
  252. debug_(sockets_2[0], sockets_[0], db_nitems);
  253. #endif
  254. #endif
  255. }
  256. auto end_writes = std::chrono::steady_clock::now();
  257. std::chrono::duration<double> elapsed_seconds_writes = end_writes - start_writes;
  258. write_time = elapsed_seconds_writes.count();
  259. // printf("elapsed_seconds_writes = %f\n",elapsed_seconds_writes.count());
  260. // std::cout << "communication_cost_writes = " << communication_cost_write << std::endl;
  261. #ifdef VERBOSE
  262. std::cout << "Reconstructing the database after doing " << number_of_writes << " writes " << std::endl;
  263. reconstruct_database(sockets_[0], DB, db_nitems);
  264. #endif
  265. // WRITES END.
  266. #ifdef ThreeParty
  267. #ifdef VERBOSE
  268. std::cout << std::endl << std::endl << "============== WRITES END ==============" << std::endl << std::endl;
  269. #endif
  270. auto start_ind_reads = std::chrono::steady_clock::now();
  271. size_t * WritePb_ind_reads = new size_t[number_of_ind_reads];
  272. size_t * WritePb_ind_reads_recv = new size_t[number_of_ind_reads];
  273. size_t * rotate = new size_t[number_of_ind_reads];
  274. for(size_t r = 0; r < number_of_ind_reads; ++r) WritePb_ind_reads[r] = where_to_read_independent[r] -ri;
  275. boost::asio::write(sockets_2[3], boost::asio::buffer(WritePb_ind_reads, number_of_ind_reads * sizeof(size_t)));
  276. boost::asio::write(sockets_[3], boost::asio::buffer(WritePb_ind_reads, number_of_ind_reads * sizeof(size_t)));
  277. boost::asio::read(sockets_[3], boost::asio::buffer(WritePb_ind_reads_recv, number_of_ind_reads * sizeof(size_t)));
  278. DB_t * Gamma_reads = new DB_t[number_of_ind_reads];
  279. boost::asio::read(sockets_2[4], boost::asio::buffer(Gamma_reads, number_of_ind_reads * sizeof(DB_t)));
  280. for(size_t j = 0; j < number_of_ind_reads; ++j)
  281. {
  282. rotate[j] = WritePb_ind_reads[j] + WritePb_ind_reads_recv[j];
  283. }
  284. DB_t * read_out_independent_reads = new DB_t[number_of_ind_reads];
  285. for(size_t r = 0; r < number_of_ind_reads; ++r)
  286. {
  287. #ifdef VERBOSE
  288. std::cout << "rotate[r]" << rotate[r] << std::endl;
  289. std::cout << "Gamma_reads[r] = " << Gamma_reads[r] << std::endl;
  290. #endif
  291. read_out_independent_reads[r] = DuoramIndependentRead(party, db_nitems, ri, Gamma_reads, rotate, r);
  292. #ifdef VERBOSE
  293. std::cout << "---> [duoram independent reads] " << print_reconstruction(sockets_[0], read_out_independent_reads[r]) << std::endl;
  294. #endif
  295. }
  296. auto end_ind_reads = std::chrono::steady_clock::now();
  297. std::chrono::duration<double> elapsed_seconds_ind_reads = end_ind_reads - start_ind_reads;
  298. //printf("elapsed_seconds_ind_reads = %f\n",elapsed_seconds_ind_reads.count());
  299. #ifdef VERBOSE
  300. std::cout << std::endl << std::endl << "============== INDEPENDENT READS END ==============" << std::endl << std::endl;
  301. #endif
  302. auto start_dep_reads = std::chrono::steady_clock::now();
  303. #ifdef VERBOSE
  304. std::cout << std::endl << std::endl << "============== DEPENDENT READS START ==============" << std::endl << std::endl;
  305. #endif
  306. DB_t * read_out_dependent_reads = new DB_t[number_of_dep_reads];
  307. for(size_t r = 0; r < number_of_dep_reads; ++r)
  308. {
  309. read_out_dependent_reads[r] = DuoramRead(party, db_nitems, ri, where_to_read_dependent[r], sockets_2[5], sockets_2[6], sockets_[5]);
  310. communication_cost_dep_read += sizeof(DB_t);
  311. communication_cost_dep_read += sizeof(DB_t);
  312. #ifdef VERBOSE
  313. std::cout << "dependent read (share) " << r << " -> " << read_out_dependent_reads[r] << std::endl;
  314. std::cout << print_reconstruction(sockets_[0], read_out_dependent_reads[r]) << std::endl;
  315. #endif
  316. }
  317. auto end_dep_reads = std::chrono::steady_clock::now();
  318. std::chrono::duration<double> elapsed_seconds_dep_reads = end_dep_reads - start_dep_reads;
  319. dependent_read_time = elapsed_seconds_dep_reads.count();
  320. // printf("elapsed_seconds_dep_reads = %f\n",elapsed_seconds_dep_reads.count());
  321. // std::cout << "communication_cost_dep_read = " << communication_cost_dep_read << std::endl;
  322. #ifdef VERBOSE
  323. std::cout << std::endl << std::endl << "============== DEPENDENT READS END ==============" << std::endl << std::endl;
  324. #endif
  325. #endif
  326. }
  327. auto end_total = std::chrono::steady_clock::now();
  328. std::chrono::duration<double> elapsed_seconds_total = end_total - start_total;
  329. //printf("elapsed_seconds_total = %f\n",elapsed_seconds_total.count());
  330. std::cout << "write_time = " << write_time << std::endl;
  331. std::cout << "communication_cost_writes = " << communication_cost_write/1024 << " KiB" << std::endl;
  332. #ifdef ThreeParty
  333. std::cout << "dependent_read_time = " << dependent_read_time << std::endl;
  334. std::cout << "communication_cost_dep_read = " << communication_cost_dep_read/1024 << " KiB" << std::endl;
  335. std::cout << "interleaved_read_time = " << dependent_read_time + write_time << std::endl;
  336. std::cout << "communication_cost_interleaved = " << (communication_cost_dep_read + communication_cost_write)/1024 << " KiB" << std::endl;
  337. #endif
  338. free(reading_temp);
  339. return 0;
  340. }