p2preprocessing.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <../boost/asio/thread_pool.hpp>
  16. #include <../boost/asio.hpp>
  17. #include <../boost/lexical_cast.hpp>
  18. #include <iostream>
  19. #include <chrono>
  20. #include <sys/mman.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <fstream>
  24. #include <mutex>
  25. #include "bitutils.h"
  26. #include "block.h"
  27. #include "prg_aes_impl.h"
  28. #include "filesio.h"
  29. using boost::asio::ip::tcp;
  30. using namespace dpf;
  31. typedef __m128i leaf_type;
  32. typedef __m128i leaf_t;
  33. typedef __m128i node_t;
  34. using socket_t = boost::asio::ip::tcp::socket;
  35. size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  36. bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  37. size_t leaves_per_node = is_packed ? sizeof(node_t) * CHAR_BIT / bits_per_leaf : 1;
  38. size_t __depth(const size_t nitems) { return std::ceil(std::log2(std::ceil(static_cast<double>(nitems) / leaves_per_node))); }
  39. #include "mpc.h"
  40. #include "dpfgen.h"
  41. #include "share-conversion.h"
  42. void mpc_gen(const size_t depth, AES_KEY& prgkey, const size_t db_nitems, const size_t n_threads, std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1,
  43. __m128i** output0, int8_t ** flags0, __m128i** output1, int8_t ** flags1, size_t j,
  44. size_t socket_no = 0)
  45. {
  46. dpfP2 dpf_instance0, dpf_instance1;
  47. for(size_t j = 0; j < depth; ++j)
  48. {
  49. __m128i rand0, rand1, gamma0, gamma1;
  50. arc4random_buf(&rand0, sizeof(__m128i));
  51. arc4random_buf(&rand1, sizeof(__m128i));
  52. uint8_t bit0, bit1;
  53. bit0 = rand();
  54. bit0 = bit0 % 2;
  55. bit1 = rand();
  56. bit1 = bit1 %2;
  57. gamma0 = (bit1 == 1) ? rand0 : _mm_setzero_si128();
  58. gamma1 = (bit0 == 1) ? rand1 : _mm_setzero_si128();
  59. struct cw_construction
  60. {
  61. __m128i rand_b, gamma_b;
  62. uint8_t bit_b;
  63. };
  64. cw_construction computecw0, computecw1;
  65. computecw0.rand_b = rand0;
  66. computecw0.gamma_b = gamma0;
  67. computecw0.bit_b = bit0;
  68. computecw1.rand_b = rand1;
  69. computecw1.gamma_b = gamma1;
  70. computecw1.bit_b = bit1;
  71. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&computecw0, sizeof(computecw0)));
  72. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&computecw1, sizeof(computecw1)));
  73. #ifdef DEBUG
  74. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&rand0, sizeof(rand0)));
  75. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&gamma0, sizeof(gamma0)));
  76. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&bit0, sizeof(bit0)));
  77. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&rand1, sizeof(rand1)));
  78. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&gamma1, sizeof(gamma1)));
  79. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&bit1, sizeof(bit1)));
  80. #endif
  81. }
  82. boost::asio::read(sockets0[socket_no+1], boost::asio::buffer(&dpf_instance0, sizeof(dpfP2)));
  83. boost::asio::read(sockets1[socket_no+1], boost::asio::buffer(&dpf_instance1, sizeof(dpfP2)));
  84. evaluate_dpfs(db_nitems, dpf_instance0, prgkey, 0, db_nitems-1, output0[j], flags0[j], false, 0);
  85. evaluate_dpfs(db_nitems, dpf_instance1, prgkey, 0, db_nitems-1, output1[j], flags1[j], true , 0);
  86. // P2_write_evalfull_outs_into_a_file(false, 0, db_nitems, flags0[0], output0[0]);
  87. // P2_write_evalfull_outs_into_a_file(true, 0, db_nitems, flags1[0], output1[0]);
  88. #ifdef DEBUG
  89. for(size_t j = 0; j < db_nitems; ++j)
  90. {
  91. std::cout << j << "-> " << (int) flags0[0][j] << " <-> " << (int) flags1[0][j] << std::endl;
  92. std::cout << j << "-> " << output0[0][j][0] << " <-> " << output1[0][j][0] << std::endl << std::endl;
  93. }
  94. #endif
  95. }
  96. void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& sockets0, int port, size_t j)
  97. {
  98. tcp::acceptor acceptor2_(io_context, tcp::endpoint(tcp::v4(), port));
  99. tcp::socket s2(acceptor2_.accept());
  100. sockets0[j] = std::move(s2);
  101. }
  102. int main(int argc, char* argv[])
  103. {
  104. AES_KEY aeskey;
  105. boost::asio::io_context io_context;
  106. tcp::resolver resolver(io_context);
  107. const std::string host0 = (argc < 2) ? "127.0.0.1" : argv[1];
  108. const std::string host1 = (argc < 3) ? "127.0.0.1" : argv[2];
  109. const size_t n_threads = atoi(argv[3]);
  110. const size_t number_of_sockets = 5 * n_threads;
  111. const size_t db_nitems = 1ULL << atoi(argv[4]);
  112. const size_t depth = std::ceil(std::log2(db_nitems));
  113. std::vector<int> ports2_0;
  114. for(size_t j = 0; j < number_of_sockets; ++j)
  115. {
  116. int port = 20000;
  117. ports2_0.push_back(port + j);
  118. }
  119. std::vector<int> ports2_1;
  120. for(size_t j = 0; j < number_of_sockets; ++j)
  121. {
  122. int port = 40000;
  123. ports2_1.push_back(port + j);
  124. }
  125. std::vector<socket_t> sockets0;
  126. std::vector<socket_t> sockets1;
  127. sockets0.reserve(number_of_sockets + 1);
  128. sockets1.reserve(number_of_sockets + 1);
  129. boost::asio::thread_pool pool2(number_of_sockets * 2);
  130. for(size_t j = 0; j < number_of_sockets; ++j)
  131. {
  132. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(sockets1), ports2_1[j], j));
  133. }
  134. for(size_t j = 0; j < number_of_sockets; ++j)
  135. {
  136. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(sockets0), ports2_0[j], j));
  137. }
  138. pool2.join();
  139. boost::asio::thread_pool pool(n_threads);
  140. __m128i ** output0 = (__m128i ** ) malloc(sizeof(__m128i *) * n_threads);
  141. int8_t ** flags0 = (int8_t ** ) malloc(sizeof(uint8_t *) * n_threads);
  142. for(size_t j = 0; j < n_threads; ++j)
  143. {
  144. output0[j] = (__m128i *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(__m128i));
  145. flags0[j] = (int8_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(uint8_t));
  146. }
  147. __m128i ** output1 = (__m128i ** ) malloc(sizeof(__m128i *) * n_threads);
  148. int8_t ** flags1 = (int8_t ** ) malloc(sizeof(uint8_t *) * n_threads);
  149. for(size_t j = 0; j < n_threads; ++j)
  150. {
  151. output1[j] = (__m128i *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(__m128i));
  152. flags1[j] = (int8_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(uint8_t));
  153. }
  154. for(size_t j = 0; j < n_threads; ++j)
  155. {
  156. boost::asio::post(pool, std::bind(mpc_gen, std::ref(depth), std::ref(aeskey), db_nitems, n_threads, std::ref(sockets0), std::ref(sockets1),
  157. output0, flags0, output1, flags1, j, 5 * j));
  158. }
  159. pool.join();
  160. boost::asio::thread_pool pool3(n_threads);
  161. int64_t ** leaves0 = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
  162. int64_t ** leafbits0 = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
  163. int64_t ** leaves1 = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
  164. int64_t ** leafbits1 = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
  165. for(size_t j = 0; j < n_threads; ++j)
  166. {
  167. leaves0[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  168. leafbits0[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  169. leaves1[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  170. leafbits1[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  171. }
  172. /* The function convert_sharesP2 appears in share-conversion.h */
  173. for(size_t j = 0; j < n_threads; ++j)
  174. {
  175. boost::asio::post(pool3, std::bind(convert_sharesP2, db_nitems, output0, flags0, output1, flags1, leaves0, leafbits0, leaves1, leafbits1, std::ref(sockets0), std::ref(sockets1),j, j));
  176. }
  177. pool3.join();
  178. /* The function P2_xor_to_additive appears in share-conversion.h */
  179. boost::asio::thread_pool pool4(n_threads);
  180. for(size_t j = 0; j < n_threads; ++j)
  181. {
  182. boost::asio::post(pool4, std::bind(P2_xor_to_additive, std::ref(sockets0[j]), std::ref(sockets1[j]), j));
  183. }
  184. pool4.join();
  185. return 0;
  186. }