duoram.cpp 16 KB

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