preprocessing.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_aes_impl.h"
  19. #include <iostream>
  20. #include <fcntl.h>
  21. #include <cstdlib>
  22. #include "block.h"
  23. #include <chrono>
  24. #include <sys/mman.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <fstream>
  28. #include <future>
  29. #include <boost/asio.hpp>
  30. using boost::asio::ip::tcp;
  31. #include <mutex>
  32. #include <boost/lexical_cast.hpp>
  33. using socket_t = boost::asio::ip::tcp::socket;
  34. typedef unsigned char byte_t;
  35. typedef __m128i node_t;
  36. constexpr size_t leaf_size = 1;
  37. typedef __m128i leaf_type;
  38. typedef std::array<leaf_type, leaf_size> leaf_t;
  39. using namespace dpf; // The namespace is found in bitutils.h
  40. #include "mpc.h"
  41. #include "network.h"
  42. #include "dpfgen.h"
  43. #include "share-conversion.h"
  44. int main(int argc, char * argv[])
  45. {
  46. boost::asio::io_context io_context;
  47. std::string addr = "127.0.0.1";
  48. const std::string host1 = (argc < 2) ? "127.0.0.1" : argv[1];
  49. const std::string host2 = (argc < 3) ? "127.0.0.1" : argv[2];
  50. const size_t n_threads = atoi(argv[3]);
  51. const size_t expo = atoi(argv[4]);
  52. const size_t number_of_sockets = 5 * n_threads;
  53. std::vector<socket_t> socketsPb, socketsP2;
  54. std::vector<int> ports, ports2_1, ports2_0;
  55. bool party;
  56. make_connections(party, host1, host2, io_context, socketsPb, socketsP2, ports, ports2_1, ports2_0, number_of_sockets);
  57. const size_t db_nitems = 1ULL << atoi(argv[4]);
  58. auto start = std::chrono::steady_clock::now();
  59. uint8_t ** target_share_read = new uint8_t*[n_threads];
  60. generate_random_targets(target_share_read, n_threads, party, expo);
  61. AES_KEY aeskey;
  62. __m128i * final_correction_word = (__m128i *) std::aligned_alloc(sizeof(__m256i), n_threads * sizeof(__m128i));
  63. __m128i ** output = (__m128i ** ) malloc(sizeof(__m128i *) * n_threads);
  64. int8_t ** flags = (int8_t ** ) malloc(sizeof(uint8_t *) * n_threads);
  65. for(size_t j = 0; j < n_threads; ++j)
  66. {
  67. output[j] = (__m128i *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(__m128i));
  68. flags[j] = (int8_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(uint8_t));
  69. }
  70. boost::asio::thread_pool pool(n_threads);
  71. boost::asio::thread_pool pool_share_conversion(n_threads);
  72. int64_t ** leaves = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
  73. int64_t ** leafbits = (int64_t ** ) malloc(sizeof(int64_t *) * n_threads);
  74. for(size_t j = 0; j < n_threads; ++j)
  75. {
  76. leaves[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  77. leafbits[j] = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  78. }
  79. // The following function call creates and evaluates DPFs at target_share_read[j] for j \in \{0, \ldots, n_threads}
  80. // the flag vectors are stored in flags
  81. // the leaves are stored in output
  82. // the final correctionword is stored in final_correction_word
  83. for(size_t j = 0; j < n_threads; ++j)
  84. {
  85. boost::asio::post(pool, std::bind(create_dpfs, db_nitems, std::ref(aeskey), target_share_read[j], std::ref(socketsPb), std::ref(socketsP2), 0, db_nitems-1,
  86. output[j], flags[j], std::ref(final_correction_word[j]), party, 5 * j, j));
  87. }
  88. pool.join();
  89. //#ifdef DEBUG
  90. for(size_t j = 0; j < n_threads; ++j)
  91. {
  92. for(size_t i = 0; i < db_nitems; ++i)
  93. {
  94. int8_t flags_reconstruction;
  95. boost::asio::write(socketsPb[0], boost::asio::buffer(&flags[j][i], sizeof(flags[j][i])));
  96. boost::asio::read(socketsPb[0], boost::asio::buffer(&flags_reconstruction, sizeof(flags_reconstruction)));
  97. flags_reconstruction -= flags[j][i];
  98. if(flags_reconstruction != 0) std::cout << i << " ---> " << (int) flags_reconstruction << std::endl;
  99. int64_t output_reconstruction;
  100. boost::asio::write(socketsPb[0], boost::asio::buffer(&output[j][i][0], sizeof(output[j][i][0])));
  101. boost::asio::read(socketsPb[0], boost::asio::buffer(&output_reconstruction, sizeof(output_reconstruction)));
  102. output_reconstruction -= output[j][i][0];
  103. if(output_reconstruction != 0) std::cout << i << "---> " << output_reconstruction << std::endl;
  104. }
  105. int64_t final_correction_word_reconstruction = 0;
  106. boost::asio::write(socketsPb[0], boost::asio::buffer(&final_correction_word[j][0], sizeof(final_correction_word[j][0])));
  107. boost::asio::read(socketsPb[0], boost::asio::buffer(&final_correction_word_reconstruction, sizeof(final_correction_word_reconstruction)));
  108. final_correction_word_reconstruction = final_correction_word_reconstruction + final_correction_word[j][0];
  109. std::cout << "final_correction_word_reconstruction = " << final_correction_word_reconstruction << std::endl << std::endl;
  110. }
  111. //#endif
  112. for(size_t j = 0; j < n_threads; ++j)
  113. {
  114. boost::asio::post(pool_share_conversion, std::bind(convert_shares, j, output, flags, n_threads, db_nitems, final_correction_word, leaves, leafbits,
  115. std::ref(socketsPb[j]), std::ref(socketsP2[j]), party ));
  116. }
  117. pool_share_conversion.join();
  118. xor_to_additive( party, target_share_read, socketsPb, socketsP2);
  119. auto end = std::chrono::steady_clock::now();
  120. std::chrono::duration<double> elapsed_seconds = end-start;
  121. std::cout << "time to generate and evaluate " << n_threads << " dpfs of size 2^" << atoi(argv[4]) << " is: " << elapsed_seconds.count() << "s\n";
  122. return 0;
  123. }