preprocessing.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <fcntl.h>
  22. #include <cstdlib>
  23. #include "block.h"
  24. #include <chrono>
  25. #include <sys/mman.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <fstream>
  29. #include <future>
  30. #include <boost/asio.hpp>
  31. using boost::asio::ip::tcp;
  32. #include <mutex>
  33. #include <boost/lexical_cast.hpp>
  34. using socket_t = boost::asio::ip::tcp::socket;
  35. typedef unsigned char byte_t;
  36. typedef __m128i node_t;
  37. block<__m128i> seed_for_blinds;
  38. constexpr size_t leaf_size = 1;
  39. typedef __m128i leaf_type;
  40. typedef std::array<leaf_type, leaf_size> leaf_t;
  41. using namespace dpf;
  42. #include "dpfgen.h"
  43. #include "share-conversion.h"
  44. void accept_conncections_from_Pb(boost::asio::io_context&io_context, std::vector<socket_t>& socketsPb, int port, size_t j)
  45. {
  46. tcp::acceptor acceptor_a(io_context, tcp::endpoint(tcp::v4(), port));
  47. tcp::socket sb_a(acceptor_a.accept());
  48. socketsPb[j] = std::move(sb_a);
  49. }
  50. int main(int argc, char * argv[])
  51. {
  52. boost::asio::io_context io_context;
  53. tcp::resolver resolver(io_context);
  54. std::string addr = "127.0.0.1";
  55. const std::string host1 = (argc < 2) ? "127.0.0.1" : argv[1];
  56. const std::string host2 = (argc < 3) ? "127.0.0.1" : argv[2];
  57. const size_t n_threads = atoi(argv[3]);
  58. const size_t number_of_sockets = 5 * n_threads;
  59. std::vector<socket_t> socketsPb;
  60. for(size_t j = 0; j < number_of_sockets + 1; ++j)
  61. {
  62. tcp::socket emptysocket(io_context);
  63. socketsPb.emplace_back(std::move(emptysocket));
  64. }
  65. socketsPb.reserve(number_of_sockets + 1);
  66. std::vector<socket_t> socketsP2;
  67. std::vector<int> ports;
  68. for(size_t j = 0; j < number_of_sockets; ++j)
  69. {
  70. int port = 6000;
  71. ports.push_back(port + j);
  72. }
  73. std::vector<int> ports2_0;
  74. for(size_t j = 0; j < number_of_sockets; ++j)
  75. {
  76. int port = 20000;
  77. ports2_0.push_back(port + j);
  78. }
  79. std::vector<int> ports2_1;
  80. for(size_t j = 0; j < number_of_sockets; ++j)
  81. {
  82. int port = 40000;
  83. ports2_1.push_back(port + j);
  84. }
  85. bool party;
  86. #if (PARTY == 0)
  87. party = false;
  88. for(size_t j = 0; j < number_of_sockets; ++j)
  89. {
  90. tcp::socket sb_a(io_context);
  91. boost::asio::connect(sb_a, resolver.resolve({host2, std::to_string(ports2_0[j])}));
  92. socketsP2.emplace_back(std::move(sb_a));
  93. }
  94. for(size_t j = 0; j < number_of_sockets; ++j)
  95. {
  96. tcp::socket sb_a(io_context);
  97. boost::asio::connect(sb_a, resolver.resolve({host1, std::to_string(ports[j])}));
  98. socketsPb[j] = std::move(sb_a);
  99. }
  100. #else
  101. party = true;
  102. for(size_t j = 0; j < number_of_sockets; ++j)
  103. {
  104. tcp::socket sb_a(io_context);
  105. boost::asio::connect(sb_a, resolver.resolve({host2, std::to_string(ports2_1[j])}));
  106. socketsP2.emplace_back(std::move(sb_a));
  107. }
  108. boost::asio::thread_pool pool2(number_of_sockets);
  109. for(size_t j = 0; j < number_of_sockets; ++j)
  110. {
  111. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(socketsPb), ports[j], j));
  112. }
  113. pool2.join();
  114. #endif
  115. const size_t db_nitems = 1ULL << atoi(argv[4]);
  116. __m128i * final_correction_word = (__m128i *) std::aligned_alloc(sizeof(__m256i), n_threads * sizeof(__m128i));
  117. uint8_t target_share[64];
  118. for(size_t j = 0; j < 64; ++j)
  119. {
  120. //srand(1);
  121. target_share[j] = rand();
  122. arc4random_buf(&target_share[j], sizeof(uint8_t));
  123. target_share[j] = target_share[j] % 2;
  124. }
  125. AES_KEY aeskey;
  126. __m128i ** output = (__m128i ** ) malloc(sizeof(__m128i *) * n_threads);
  127. int8_t ** flags = (int8_t ** ) malloc(sizeof(uint8_t *) * n_threads);
  128. for(size_t j = 0; j < n_threads; ++j)
  129. {
  130. output[j] = (__m128i *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(__m128i));
  131. flags[j] = (int8_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(uint8_t));
  132. }
  133. const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  134. const bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  135. const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
  136. const size_t depth = std::ceil(std::log2(db_nitems));
  137. const size_t nbits = std::ceil(std::log2(db_nitems));
  138. const size_t nodes_in_interval = db_nitems-1;
  139. boost::asio::thread_pool pool(n_threads);
  140. printf("n_threads = %zu\n\n", n_threads);
  141. auto start = std::chrono::steady_clock::now();
  142. for(size_t j = 0; j < n_threads; ++j)
  143. {
  144. boost::asio::post(pool, std::bind(evalfull_mpc, std::ref(nodes_per_leaf), std::ref(depth), std::ref(nbits), std::ref(nodes_in_interval),
  145. std::ref(aeskey), target_share, std::ref(socketsPb), std::ref(socketsP2), 0, db_nitems-1, output[j],
  146. flags[j], std::ref(final_correction_word[j]), party, 5 * j));
  147. }
  148. pool.join();
  149. for(size_t i = 0; i < n_threads; ++i)
  150. {
  151. for(size_t j = 0; j < db_nitems; ++j)
  152. {
  153. if(party)
  154. {
  155. output[i][j][0] = -output[i][j][0];
  156. output[i][j][1] = -output[i][j][1];
  157. }
  158. boost::asio::write(socketsPb[0], boost::asio::buffer(&output[i][j][0], sizeof(output[i][j][0])));
  159. uint64_t output_reconstruction;
  160. boost::asio::read(socketsPb[0], boost::asio::buffer(&output_reconstruction, sizeof(output_reconstruction)));
  161. output_reconstruction += output[i][j][0];
  162. boost::asio::write(socketsPb[0], boost::asio::buffer(&output[i][j][1], sizeof(output[i][j][1])));
  163. uint64_t output_reconstruction2;
  164. boost::asio::read(socketsPb[0], boost::asio::buffer(&output_reconstruction2, sizeof(output_reconstruction2)));
  165. output_reconstruction2 += output[i][j][1];
  166. std::cout << j << ":----> " << output_reconstruction << ", " << output_reconstruction2 << std::endl;
  167. }
  168. std::cout << "final_correction_word[i] = " << (uint64_t) final_correction_word[i][0] << " " << (uint64_t) final_correction_word[i][1] << std::endl;
  169. std::cout << std::endl << " -------------------------------------- " << std::endl << std::endl;
  170. }
  171. auto end = std::chrono::steady_clock::now();
  172. std::chrono::duration<double> elapsed_seconds = end-start;
  173. std::cout << "time to generate and evaluate " << n_threads << " dpfs of size 2^" << atoi(argv[4]) << " is: " << elapsed_seconds.count() << "s\n";
  174. convert_shares(output, flags, n_threads, db_nitems ,final_correction_word, socketsPb[0], party);
  175. if(!party)
  176. {
  177. char const * p0_filename0;
  178. p0_filename0 = "party0_read_flags_b";
  179. int w0 = open( p0_filename0, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  180. int written = write(w0, flags[0], db_nitems * sizeof(flags[0][0]));
  181. if(written<0) {
  182. perror("Write error");
  183. }
  184. close(w0);
  185. }
  186. else
  187. {
  188. char const * p0_filename0;
  189. p0_filename0 = "party1_read_flags_b";
  190. int w0 = open( p0_filename0, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  191. int written = write(w0, flags[0], db_nitems * sizeof(flags[0][0]));
  192. if(written<0) {
  193. perror("Write error");
  194. }
  195. close(w0);
  196. }
  197. return 0;
  198. }