p2preprocessing.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "bitutils.h"
  17. #include "block.h"
  18. #include "prg.h"
  19. #include "prg_aes_impl.h"
  20. #include <iostream>
  21. #include "block.h"
  22. #include <chrono>
  23. #include <sys/mman.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <fstream>
  27. #include <boost/asio.hpp>
  28. #include <mutex>
  29. #include <boost/lexical_cast.hpp>
  30. using boost::asio::ip::tcp;
  31. using namespace dpf;
  32. typedef __m128i leaf_type;
  33. typedef __m128i leaf_t;
  34. typedef __m128i node_t;
  35. using socket_t = boost::asio::ip::tcp::socket;
  36. size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  37. bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  38. size_t leaves_per_node = is_packed ? sizeof(node_t) * CHAR_BIT / bits_per_leaf : 1;
  39. size_t __depth(const size_t nitems) { return std::ceil(std::log2(std::ceil(static_cast<double>(nitems) / leaves_per_node))); }
  40. void mpc_gen(const size_t depth, std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1, size_t socket_no = 0)
  41. {
  42. for(size_t j = 0; j < depth; ++j)
  43. {
  44. __m128i rand0, rand1, gamma0, gamma1;
  45. arc4random_buf(&rand0, sizeof(__m128i));
  46. arc4random_buf(&rand1, sizeof(__m128i));
  47. uint8_t bit0, bit1;
  48. bit0 = rand();
  49. bit0 = bit0 % 2;
  50. bit1 = rand();
  51. bit1 = bit1 %2;
  52. if(bit1 == 1)
  53. {
  54. gamma0 = rand0;
  55. }
  56. else
  57. {
  58. gamma0 = _mm_setzero_si128();
  59. }
  60. if(bit0 == 1)
  61. {
  62. gamma1 = rand1;
  63. }
  64. else
  65. {
  66. gamma1 = _mm_setzero_si128();
  67. }
  68. struct cw_construction
  69. {
  70. __m128i rand_b, gamma_b;
  71. uint8_t bit_b;
  72. };
  73. cw_construction computecw0, computecw1;
  74. computecw0.rand_b = rand0;
  75. computecw0.gamma_b = gamma0;
  76. computecw0.bit_b = bit0;
  77. computecw1.rand_b = rand1;
  78. computecw1.gamma_b = gamma1;
  79. computecw1.bit_b = bit1;
  80. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&computecw0, sizeof(computecw0)));
  81. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&computecw1, sizeof(computecw1)));
  82. #ifdef DEBUG
  83. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&rand0, sizeof(rand0)));
  84. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&gamma0, sizeof(gamma0)));
  85. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&bit0, sizeof(bit0)));
  86. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&rand1, sizeof(rand1)));
  87. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&gamma1, sizeof(gamma1)));
  88. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&bit1, sizeof(bit1)));
  89. #endif
  90. }
  91. }
  92. void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& sockets0, int port, size_t j)
  93. {
  94. tcp::acceptor acceptor2_(io_context, tcp::endpoint(tcp::v4(), port));
  95. tcp::socket s2(acceptor2_.accept());
  96. sockets0[j] = std::move(s2);
  97. }
  98. int main(int argc, char* argv[])
  99. {
  100. AES_KEY aeskey;
  101. boost::asio::io_context io_context;
  102. tcp::resolver resolver(io_context);
  103. const std::string host0 = (argc < 2) ? "127.0.0.1" : argv[1];
  104. const std::string host1 = (argc < 3) ? "127.0.0.1" : argv[2];
  105. const size_t n_threads = atoi(argv[3]);
  106. const size_t number_of_sockets = 5 * n_threads;
  107. const size_t nitems2 = 1ULL << atoi(argv[4]);
  108. const size_t depth = std::ceil(std::log2(nitems2));
  109. std::vector<int> ports2_0;
  110. for(size_t j = 0; j < number_of_sockets; ++j)
  111. {
  112. int port = 20000;
  113. ports2_0.push_back(port + j);
  114. }
  115. std::vector<int> ports2_1;
  116. for(size_t j = 0; j < number_of_sockets; ++j)
  117. {
  118. int port = 40000;
  119. ports2_1.push_back(port + j);
  120. }
  121. std::vector<socket_t> sockets0;
  122. std::vector<socket_t> sockets1;
  123. sockets0.reserve(number_of_sockets + 1);
  124. sockets1.reserve(number_of_sockets + 1);
  125. boost::asio::thread_pool pool2(number_of_sockets * 2);
  126. for(size_t j = 0; j < number_of_sockets; ++j)
  127. {
  128. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(sockets1), ports2_1[j], j));
  129. }
  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(sockets0), ports2_0[j], j));
  133. }
  134. pool2.join();
  135. boost::asio::thread_pool pool(n_threads);
  136. for(size_t j = 0; j < n_threads; ++j)
  137. {
  138. boost::asio::post(pool, std::bind(mpc_gen, std::ref(depth), std::ref(sockets0), std::ref(sockets1), 5 * j));
  139. }
  140. pool.join();
  141. return 0;
  142. }