p2.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <sys/mman.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <fstream>
  20. #include <mutex>
  21. #include <../boost/asio.hpp>
  22. #include <../boost/lexical_cast.hpp>
  23. using boost::asio::ip::tcp;
  24. #include "prg.h"
  25. #include "prg_aes_impl.h"
  26. #include "block.h"
  27. #include "duoram-utils.h"
  28. #include "readvectors.h"
  29. DB_t * X0;
  30. DB_t * X1;
  31. DB_t alpha;
  32. typedef __m128i leaf_t;
  33. typedef __m128i node_t;
  34. using socket_t = boost::asio::ip::tcp::socket;
  35. using namespace std;
  36. auto generate_cancelation_terms(int8_t c0[], int8_t d1[], size_t db_nitems, size_t rotate_by = 0)
  37. {
  38. DB_t Z0 = dot_product_with_bool(X0, d1, db_nitems, rotate_by);
  39. DB_t Z1 = dot_product_with_bool(X1, c0, db_nitems, rotate_by);
  40. DB_t cancelation_term0 = -Z0 ;
  41. DB_t cancelation_term1 = -Z1 ;
  42. return std::make_pair(cancelation_term0, cancelation_term1);
  43. }
  44. void refresh_blinds(int8_t writing_c[], int8_t writing_d[],
  45. DB_t c[], DB_t d[],
  46. DB_t finalcw0, DB_t finalcw1, size_t db_nitems, size_t rotate_by = 0)
  47. {
  48. for(size_t j = 0; j < db_nitems; ++j)
  49. {
  50. X0[j] = X0[j] - c[(j + rotate_by) % db_nitems] - ((writing_c[(j + rotate_by) % db_nitems]) * finalcw0);// tmp0; //c0
  51. X1[j] = X1[j] - d[(j + rotate_by) % db_nitems] - ((writing_d[(j + rotate_by) % db_nitems]) * finalcw1); //c2
  52. }
  53. }
  54. void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& sockets_0, int port, size_t j)
  55. {
  56. tcp::acceptor acceptor2_(io_context, tcp::endpoint(tcp::v4(), port));
  57. tcp::socket s2(acceptor2_.accept());
  58. sockets_0[j] = std::move(s2);
  59. }
  60. int main(int argc, char* argv[])
  61. {
  62. size_t expo = atoi(argv[3]);
  63. size_t db_nitems = 1ULL << expo;
  64. size_t number_of_ind_writes = atoi(argv[4]);;
  65. size_t number_of_ind_reads = atoi(argv[5]);;
  66. size_t number_of_dep_reads = atoi(argv[6]);;
  67. size_t number_of_accesses = atoi(argv[7]);
  68. b = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  69. c = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  70. d = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  71. reading_b = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  72. reading_c = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  73. reading_d = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  74. writing_b = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  75. writing_c = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  76. writing_d = (int8_t *) malloc(db_nitems * sizeof(int8_t));
  77. boost::asio::io_context io_context;
  78. AES_KEY aeskey;
  79. const size_t number_of_sockets = 40;
  80. std::vector<int> ports2_0;
  81. for(size_t j = 0; j < number_of_sockets; ++j)
  82. {
  83. int port = 8000;
  84. ports2_0.push_back(port + j);
  85. }
  86. std::vector<int> ports2_1;
  87. for(size_t j = 0; j < number_of_sockets; ++j)
  88. {
  89. int port = 9000;
  90. ports2_1.push_back(port + j);
  91. }
  92. std::vector<socket_t> sockets_0;
  93. std::vector<socket_t> sockets_1;
  94. sockets_0.reserve(number_of_sockets + 1);
  95. sockets_1.reserve(number_of_sockets + 1);
  96. boost::asio::thread_pool pool2(number_of_sockets * 2);
  97. for(size_t j = 0; j < number_of_sockets; ++j)
  98. {
  99. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(sockets_1), ports2_1[j], j));
  100. }
  101. for(size_t j = 0; j < number_of_sockets; ++j)
  102. {
  103. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(sockets_0), ports2_0[j], j));
  104. }
  105. pool2.join();
  106. X0 = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  107. X1 = (DB_t *) std::aligned_alloc(sizeof(__m256i), db_nitems * sizeof(DB_t));
  108. for(size_t j = 0; j < db_nitems; ++j)
  109. {
  110. X0[j] = 0;
  111. X1[j] = 0;
  112. }
  113. DuORAM_Write * WriteP0_recv = new DuORAM_Write[number_of_ind_writes];
  114. DuORAM_Write * WriteP1_recv = new DuORAM_Write[number_of_ind_writes];
  115. DB_t * Gamma0_ = new DB_t[number_of_ind_writes];
  116. DB_t * Gamma1_ = new DB_t[number_of_ind_writes];
  117. DB_t * update_message0 = new DB_t[number_of_ind_writes];
  118. DB_t * update_message1 = new DB_t[number_of_ind_writes];
  119. DB_t * FCW = new DB_t[number_of_ind_writes];
  120. for(size_t i = 0; i < number_of_accesses; ++i)
  121. {
  122. for(size_t w = 0; w < number_of_ind_writes; ++w)
  123. {
  124. read_flags_for_refreshing(db_nitems);
  125. size_t rotate_by_ = WriteP0_recv[w].shift + WriteP1_recv[w].shift;
  126. auto [Gamma0, Gamma1] = generate_cancelation_terms(writing_c, writing_d, db_nitems, rotate_by_);
  127. Gamma0_[w] = Gamma0;
  128. Gamma1_[w] = Gamma1;
  129. boost::asio::read(sockets_0[0], boost::asio::buffer(&WriteP0_recv[w], sizeof(DuORAM_Write)));
  130. boost::asio::read(sockets_1[0], boost::asio::buffer(&WriteP1_recv[w], sizeof(DuORAM_Write)));
  131. boost::asio::write(sockets_0[1], boost::asio::buffer(&Gamma0_[w], sizeof(DB_t)));
  132. boost::asio::write(sockets_1[1], boost::asio::buffer(&Gamma1_[w], sizeof(DB_t)));
  133. boost::asio::read(sockets_0[2], boost::asio::buffer(&update_message0[w], sizeof(DB_t)));
  134. boost::asio::read(sockets_1[2], boost::asio::buffer(&update_message1[w], sizeof(DB_t)));
  135. FCW[w] = update_message0[w] + update_message1[w];
  136. }
  137. for(size_t w = 0; w < number_of_ind_writes; ++w)
  138. {
  139. size_t rotate_by_ = WriteP0_recv[w].shift + WriteP1_recv[w].shift;
  140. refresh_blinds(writing_c, writing_d, c, d, FCW[w], FCW[w], db_nitems, -rotate_by_);
  141. #ifdef DEBUG
  142. for(size_t j = 0; j < db_nitems; ++j)
  143. {
  144. boost::asio::write(sockets_0[0], boost::asio::buffer(&X0[j], sizeof(X0[j]))); //DEBUG
  145. boost::asio::write(sockets_1[0], boost::asio::buffer(&X1[j], sizeof(X1[j]))); //DUBUG
  146. }
  147. #endif
  148. }
  149. DB_t * Gamma0_reads = new DB_t[number_of_ind_reads];
  150. DB_t * Gamma1_reads = new DB_t[number_of_ind_reads];
  151. size_t * reads_shift_from_P0 = new size_t[number_of_ind_reads];
  152. size_t * reads_shift_from_P1 = new size_t[number_of_ind_reads];
  153. size_t * rotate = new size_t[number_of_ind_reads];
  154. boost::asio::read(sockets_0[3], boost::asio::buffer(reads_shift_from_P0, number_of_ind_reads * sizeof(size_t)));
  155. boost::asio::read(sockets_1[3], boost::asio::buffer(reads_shift_from_P1, number_of_ind_reads * sizeof(size_t)));
  156. for(size_t r = 0; r < number_of_ind_reads; ++r)
  157. {
  158. #ifdef VERBOSE
  159. std::cout << "rotate[r] " << rotate[r] << std::endl;
  160. #endif
  161. rotate[r] = reads_shift_from_P0[r] + reads_shift_from_P1[r];
  162. read_flags_for_generating_cancellation_terms(db_nitems);
  163. auto [Gamma0, Gamma1] = generate_cancelation_terms(reading_c, reading_d, db_nitems, rotate[r]);
  164. Gamma0_reads[r] = Gamma0;
  165. Gamma1_reads[r] = Gamma1;
  166. }
  167. boost::asio::write(sockets_0[4], boost::asio::buffer(Gamma0_reads, number_of_ind_reads * sizeof(DB_t)));
  168. boost::asio::write(sockets_1[4], boost::asio::buffer(Gamma1_reads, number_of_ind_reads * sizeof(DB_t)));
  169. for(size_t d = 0; d < number_of_dep_reads; ++d)
  170. {
  171. size_t shifts0, shifts1, rotate;
  172. boost::asio::read(sockets_0[5], boost::asio::buffer(&shifts0, sizeof(shifts0)));
  173. boost::asio::read(sockets_1[5], boost::asio::buffer(&shifts1, sizeof(shifts1)));
  174. rotate = shifts0 + shifts1;
  175. read_flags_for_generating_cancellation_terms(db_nitems);
  176. auto [Gamma0, Gamma1] = generate_cancelation_terms(reading_c, reading_d, db_nitems, rotate);
  177. boost::asio::write(sockets_0[6], boost::asio::buffer(&Gamma0, sizeof(Gamma0)));
  178. boost::asio::write(sockets_1[6], boost::asio::buffer(&Gamma1, sizeof(Gamma1)));
  179. }
  180. }
  181. return 0;
  182. }