p2preprocessing.cpp 7.6 KB

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