preprocessing.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. size_t communication_cost = 0;
  17. #include "bitutils.h"
  18. #include "block.h"
  19. #include "prg.h"
  20. #include "prg_aes_impl.h"
  21. #include <iostream>
  22. #include <fcntl.h>
  23. #include <cstdlib>
  24. #include "block.h"
  25. #include <chrono>
  26. #include <sys/mman.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <fstream>
  30. #include <future>
  31. #include <boost/asio.hpp>
  32. using boost::asio::ip::tcp;
  33. #include <mutex>
  34. #include <boost/lexical_cast.hpp>
  35. using socket_t = boost::asio::ip::tcp::socket;
  36. typedef unsigned char byte_t;
  37. typedef __m128i node_t;
  38. block<__m128i> seed_for_blinds;
  39. constexpr size_t leaf_size = 1;
  40. typedef __m128i leaf_type;
  41. typedef std::array<leaf_type, leaf_size> leaf_t;
  42. size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  43. bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  44. size_t leaves_per_node = is_packed ? sizeof(node_t) * CHAR_BIT / bits_per_leaf : 1;
  45. size_t input_bits(const size_t nitems) {
  46. return std::ceil(std::log2(nitems));
  47. }
  48. leaf_t val;
  49. using namespace dpf;
  50. #include "mpc.h"
  51. void generate_random_targets(uint8_t **target_share_read, size_t n_threads, bool party, size_t expo)
  52. {
  53. for (size_t j = 0; j < 64; ++j)
  54. {
  55. for (size_t i = 0; i < n_threads; ++i)
  56. {
  57. uint8_t random_value;
  58. arc4random_buf(&random_value, sizeof(uint8_t));
  59. target_share_read[i][j] = random_value; // rand();
  60. }
  61. }
  62. }
  63. void compute_CW(bool party, tcp::socket &sout, __m128i L, __m128i R, uint8_t bit, __m128i &CW)
  64. {
  65. // struct cw_construction
  66. //{
  67. __m128i rand_b, gamma_b;
  68. uint8_t bit_b;
  69. //};
  70. __m128i *X, *Y;
  71. if (party)
  72. {
  73. std::string qfile = std::string("./gamma1");
  74. int qfd = open(qfile.c_str(), O_RDWR);
  75. X = (__m128i *)mmap(NULL, 8 * sizeof(__m128i),
  76. PROT_READ, MAP_PRIVATE, qfd, 0);
  77. qfile = std::string("./x1");
  78. qfd = open(qfile.c_str(), O_RDWR);
  79. Y = (__m128i *)mmap(NULL, 8 * sizeof(__m128i),
  80. PROT_READ, MAP_PRIVATE, qfd, 0);
  81. close(qfd);
  82. }
  83. if (!party)
  84. {
  85. std::string qfile = std::string("./gamma0");
  86. int qfd = open(qfile.c_str(), O_RDWR);
  87. X = (__m128i *)mmap(NULL, 8 * sizeof(__m128i),
  88. PROT_READ, MAP_PRIVATE, qfd, 0);
  89. qfile = std::string("./x0");
  90. qfd = open(qfile.c_str(), O_RDWR);
  91. Y = (__m128i *)mmap(NULL, 8 * sizeof(__m128i),
  92. PROT_READ, MAP_PRIVATE, qfd, 0);
  93. close(qfd);
  94. }
  95. // cw_construction computecw;
  96. // read(sin, boost::asio::buffer(&computecw, sizeof(computecw)));
  97. // computecw.rand_b;
  98. //__m128i gamma_b = computecw.gamma_b;
  99. if (party)
  100. {
  101. rand_b = Y[0]; //_mm_set_epi32(0x6fef9434, 0x6768121e, 0x20942286, 0x1b59f7a7);
  102. gamma_b = X[0]; // _mm_set_epi32(0x6a499109 , 0x803067dd , 0xd1e2281b , 0xe71b6262);
  103. bit_b = 1; // computecw.bit_b;
  104. }
  105. else
  106. {
  107. rand_b = Y[0]; // _mm_set_epi32(0xb29747df, 0xf7300f6d, 0x9476d971, 0xd5f75d98);
  108. gamma_b = X[0]; // _mm_set_epi32(0xb73142e2 , 0x10687aae , 0x06500d3ec , 0x29b5c85d);
  109. bit_b = 1; // computecw.bit_b;
  110. }
  111. uint8_t blinded_bit, blinded_bit_read;
  112. blinded_bit = bit ^ bit_b;
  113. __m128i blinded_L = L ^ R ^ rand_b;
  114. __m128i blinded_L_read;
  115. struct BlindsCW
  116. {
  117. __m128i blinded_message;
  118. uint8_t blinded_bit;
  119. };
  120. BlindsCW blinds_sent, blinds_recv;
  121. blinds_sent.blinded_bit = blinded_bit;
  122. blinds_sent.blinded_message = blinded_L;
  123. boost::asio::write(sout, boost::asio::buffer(&blinds_sent, sizeof(blinds_sent)));
  124. boost::asio::read(sout, boost::asio::buffer(&blinds_recv, sizeof(blinds_recv)));
  125. communication_cost += sizeof(blinds_recv);
  126. blinded_bit_read = blinds_recv.blinded_bit;
  127. blinded_L_read = blinds_recv.blinded_message;
  128. __m128i out_ = R ^ gamma_b; //_mm_setzero_si128;
  129. if (bit)
  130. {
  131. out_ ^= (L ^ R ^ blinded_L_read);
  132. }
  133. if (blinded_bit_read)
  134. {
  135. out_ ^= rand_b;
  136. }
  137. __m128i out_reconstruction;
  138. boost::asio::write(sout, boost::asio::buffer(&out_, sizeof(out_)));
  139. boost::asio::read(sout, boost::asio::buffer(&out_reconstruction, sizeof(out_reconstruction)));
  140. communication_cost += sizeof(out_reconstruction);
  141. out_reconstruction = out_ ^ out_reconstruction;
  142. CW = out_reconstruction;
  143. #ifdef DEBUG
  144. uint8_t bit_reconstruction;
  145. boost::asio::write(sout, boost::asio::buffer(&bit, sizeof(bit)));
  146. boost::asio::read(sout, boost::asio::buffer(&bit_reconstruction, sizeof(bit_reconstruction)));
  147. bit_reconstruction = bit ^ bit_reconstruction;
  148. __m128i L_reconstruction;
  149. boost::asio::write(sout, boost::asio::buffer(&L, sizeof(L)));
  150. boost::asio::read(sout, boost::asio::buffer(&L_reconstruction, sizeof(L_reconstruction)));
  151. L_reconstruction = L ^ L_reconstruction;
  152. __m128i R_reconstruction;
  153. boost::asio::write(sout, boost::asio::buffer(&R, sizeof(R)));
  154. boost::asio::read(sout, boost::asio::buffer(&R_reconstruction, sizeof(R_reconstruction)));
  155. R_reconstruction = R ^ R_reconstruction;
  156. __m128i CW_debug;
  157. if (bit_reconstruction != 0)
  158. {
  159. CW_debug = L_reconstruction;
  160. }
  161. else
  162. {
  163. CW_debug = R_reconstruction;
  164. }
  165. assert(CW_debug[0] == CW[0]);
  166. assert(CW_debug[1] == CW[1]);
  167. #endif
  168. munmap(X, 8 * sizeof(__m128i));
  169. munmap(Y, 8 * sizeof(__m128i));
  170. }
  171. __m128i bit_mask_avx2_msb(unsigned int n)
  172. {
  173. __m128i ones = _mm_set1_epi32(-1);
  174. __m128i cnst32_128 = _mm_set_epi32(32, 64, 96, 128);
  175. __m128i shift = _mm_set1_epi32(n);
  176. shift = _mm_subs_epu16(cnst32_128, shift);
  177. return _mm_sllv_epi32(ones, shift);
  178. }
  179. __m128i bit_mask_avx2_lsb(unsigned int n)
  180. {
  181. __m128i ones = _mm_set1_epi32(-1);
  182. __m128i cnst32_128 = _mm_set_epi32(128, 96, 64, 32);
  183. __m128i shift = _mm_set1_epi32(n);
  184. shift = _mm_subs_epu16(cnst32_128, shift);
  185. return _mm_srlv_epi32(ones, shift);
  186. }
  187. template <typename node_t, typename prgkey_t>
  188. static inline void traverse(const prgkey_t &prgkey, const node_t &seed, node_t s[2])
  189. {
  190. dpf::PRG(prgkey, clear_lsb(seed, 0b11), s, 2);
  191. } // dpf::expand
  192. inline void evalfull_mpc(const size_t &nodes_per_leaf, const size_t &depth, const size_t &nbits, const size_t &nodes_in_interval,
  193. const AES_KEY &prgkey, uint8_t target_share[64], std::vector<socket_t> &socketsPb,
  194. const size_t from, const size_t to, __m128i *output, int8_t *_t, __m128i &final_correction_word, bool party, size_t socket_no = 0)
  195. {
  196. __m128i root;
  197. arc4random_buf(&root, sizeof(root));
  198. root = set_lsb(root, party);
  199. const size_t from_node = std::floor(static_cast<double>(from) / nodes_per_leaf);
  200. __m128i *s[2] = {
  201. reinterpret_cast<__m128i *>(output) + nodes_in_interval * (nodes_per_leaf - 1),
  202. s[0] + nodes_in_interval / 2
  203. };
  204. int8_t *t[2] = {_t, _t + nodes_in_interval / 2};
  205. int curlayer = depth % 2;
  206. s[curlayer][0] = root;
  207. t[curlayer][0] = get_lsb(root, 0b01);
  208. __m128i *CW = (__m128i *)std::aligned_alloc(sizeof(__m256i), depth * sizeof(__m128i));
  209. for (size_t layer = 0; layer < depth; ++layer)
  210. {
  211. #ifdef VERBOSE
  212. printf("layer = %zu\n", layer);
  213. #endif
  214. curlayer = 1 - curlayer;
  215. size_t i = 0, j = 0;
  216. auto nextbit = (from_node >> (nbits - layer - 1)) & 1;
  217. size_t nodes_in_prev_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth - layer)));
  218. size_t nodes_in_cur_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth - layer - 1)));
  219. __m128i L = _mm_setzero_si128();
  220. __m128i R = _mm_setzero_si128();
  221. for (i = nextbit, j = nextbit; j < nodes_in_prev_layer - 1; ++j, i += 2)
  222. {
  223. traverse(prgkey, s[1 - curlayer][j], &s[curlayer][i]);
  224. L ^= s[curlayer][i];
  225. R ^= s[curlayer][i + 1];
  226. }
  227. if (nodes_in_prev_layer > j)
  228. {
  229. if (i < nodes_in_cur_layer - 1)
  230. {
  231. traverse(prgkey, s[1 - curlayer][j], &s[curlayer][i]);
  232. L ^= s[curlayer][i];
  233. R ^= s[curlayer][i + 1];
  234. }
  235. }
  236. compute_CW(party, socketsPb[socket_no], L, R, target_share[layer], CW[layer]);
  237. uint8_t advice_L = get_lsb(L) ^ target_share[layer];
  238. uint8_t advice_R = get_lsb(R) ^ target_share[layer];
  239. uint8_t cwt_L, cwt_R;
  240. uint8_t advice[2];
  241. uint8_t cwts[2];
  242. advice[0] = advice_L;
  243. advice[1] = advice_R;
  244. boost::asio::write(socketsPb[socket_no + 1], boost::asio::buffer(&advice, sizeof(advice)));
  245. boost::asio::read(socketsPb[socket_no + 1], boost::asio::buffer(&cwts, sizeof(cwts)));
  246. cwt_L = cwts[0];
  247. cwt_R = cwts[1];
  248. cwt_L = cwt_L ^ advice_L ^ 1;
  249. cwt_R = cwt_R ^ advice_R;
  250. for (size_t j = 0; j < nodes_in_prev_layer; ++j)
  251. {
  252. t[curlayer][2 * j] = get_lsb(s[curlayer][2 * j]) ^ (cwt_L & t[1 - curlayer][j]);
  253. s[curlayer][2 * j] = clear_lsb(xor_if(s[curlayer][2 * j], CW[layer], !t[1 - curlayer][j]), 0b11);
  254. t[curlayer][(2 * j) + 1] = get_lsb(s[curlayer][(2 * j) + 1]) ^ (cwt_R & t[1 - curlayer][j]);
  255. s[curlayer][(2 * j) + 1] = clear_lsb(xor_if(s[curlayer][(2 * j) + 1], CW[layer], !t[1 - curlayer][j]), 0b11);
  256. }
  257. }
  258. free(CW);
  259. __m128i Gamma = _mm_setzero_si128();
  260. for (size_t i = 0; i < to + 1; ++i)
  261. {
  262. Gamma[0] += output[i][0];
  263. Gamma[1] += output[i][1];
  264. }
  265. if (party)
  266. {
  267. Gamma[0] = -Gamma[0];
  268. Gamma[1] = -Gamma[1];
  269. }
  270. boost::asio::write(socketsPb[socket_no + 3], boost::asio::buffer(&Gamma, sizeof(Gamma)));
  271. boost::asio::read(socketsPb[socket_no + 3], boost::asio::buffer(&final_correction_word, sizeof(final_correction_word)));
  272. communication_cost += sizeof(Gamma);
  273. final_correction_word = Gamma; // final_correction_word + Gamma;
  274. } // dpf::__evalinterval
  275. void convert_shares(__m128i **output, int8_t **flags, size_t n_threads, size_t db_nitems, __m128i *final_correction_word, tcp::socket &sb, bool party)
  276. {
  277. for (size_t j = 0; j < db_nitems; ++j)
  278. {
  279. for (size_t k = 0; k < n_threads; ++k)
  280. {
  281. if (party)
  282. {
  283. output[k][j] = -output[k][j];
  284. flags[k][j] = -flags[k][j];
  285. }
  286. }
  287. #ifdef DEBUG
  288. int8_t out = flags[0][j];
  289. int8_t out_rec;
  290. boost::asio::write(sb, boost::asio::buffer(&out, sizeof(out)));
  291. boost::asio::read(sb, boost::asio::buffer(&out_rec, sizeof(out_rec)));
  292. out_rec = out_rec + out;
  293. if (out_rec != 0)
  294. std::cout << j << "(flags) --> " << (int)out_rec << std::endl
  295. << std::endl;
  296. __m128i out2 = output[0][j];
  297. __m128i out_rec2;
  298. boost::asio::write(sb, boost::asio::buffer(&out2, sizeof(out2)));
  299. boost::asio::read(sb, boost::asio::buffer(&out_rec2, sizeof(out_rec2)));
  300. out_rec2 = out_rec2 + out2;
  301. if (out_rec2[0] != 0)
  302. std::cout << j << "--> " << out_rec2[0] << std::endl;
  303. #endif
  304. }
  305. for (size_t i = 0; i < n_threads; ++i)
  306. {
  307. int64_t pm = 0;
  308. int64_t rb;
  309. arc4random_buf(&rb, sizeof(rb));
  310. for (size_t j = 0; j < db_nitems; ++j)
  311. {
  312. if (party)
  313. {
  314. if (flags[i][j] != 0)
  315. pm -= 1;
  316. }
  317. if (!party)
  318. {
  319. if (flags[i][j] != 0)
  320. pm += 1; // flags[0][j];
  321. }
  322. }
  323. }
  324. }
  325. void accept_conncections_from_Pb(boost::asio::io_context &io_context, std::vector<socket_t> &socketsPb, int port, size_t j)
  326. {
  327. tcp::acceptor acceptor_a(io_context, tcp::endpoint(tcp::v4(), port));
  328. tcp::socket sb_a(acceptor_a.accept());
  329. socketsPb[j] = std::move(sb_a);
  330. }
  331. int main(int argc, char *argv[])
  332. {
  333. boost::asio::io_context io_context;
  334. tcp::resolver resolver(io_context);
  335. const std::string host1 = argv[1];
  336. const size_t n_threads = atoi(argv[2]);
  337. const size_t number_of_sockets = 5 * n_threads;
  338. const size_t expo = atoi(argv[3]);
  339. const size_t maxRAM = atoi(argv[4]);
  340. const size_t db_nitems = 1ULL << expo;
  341. size_t RAM_needed_per_thread = 164 * db_nitems;
  342. std::cout << "RAM needed = " << n_threads*RAM_needed_per_thread << " bytes = " << n_threads*RAM_needed_per_thread/1073741824 << " GiB" << std::endl;
  343. std::cout << "RAM needed per thread = " << RAM_needed_per_thread << " bytes = " << (RAM_needed_per_thread>>30) << " GiB" << std::endl;
  344. size_t thread_per_batch = std::floor(double(maxRAM<<30)/RAM_needed_per_thread);
  345. if (thread_per_batch > n_threads) {
  346. thread_per_batch = n_threads;
  347. }
  348. std::cout << "thread_per_batch = " << thread_per_batch << std::endl;
  349. if (thread_per_batch < 1) {
  350. std::cout << "You need more RAM" << std::endl;
  351. exit(0);
  352. }
  353. size_t n_batches = std::ceil(double(n_threads)/thread_per_batch);
  354. std::cout << "n_batches = " << n_batches << std::endl;
  355. std::vector<socket_t> socketsPb;
  356. for (size_t j = 0; j < number_of_sockets + 1; ++j)
  357. {
  358. tcp::socket emptysocket(io_context);
  359. socketsPb.emplace_back(std::move(emptysocket));
  360. }
  361. socketsPb.reserve(number_of_sockets + 1);
  362. std::vector<int> ports;
  363. for (size_t j = 0; j < number_of_sockets; ++j)
  364. {
  365. int port = 6000;
  366. ports.push_back(port + j);
  367. }
  368. std::vector<int> ports2_0;
  369. for (size_t j = 0; j < number_of_sockets; ++j)
  370. {
  371. int port = 20000;
  372. ports2_0.push_back(port + j);
  373. }
  374. std::vector<int> ports2_1;
  375. for (size_t j = 0; j < number_of_sockets; ++j)
  376. {
  377. int port = 40000;
  378. ports2_1.push_back(port + j);
  379. }
  380. bool party;
  381. #if (PARTY == 0)
  382. party = false;
  383. for (size_t j = 0; j < number_of_sockets; ++j)
  384. {
  385. tcp::socket sb_a(io_context);
  386. boost::asio::connect(sb_a, resolver.resolve({host1, std::to_string(ports[j])}));
  387. socketsPb[j] = std::move(sb_a);
  388. }
  389. #else
  390. party = true;
  391. boost::asio::thread_pool pool2(number_of_sockets);
  392. for (size_t j = 0; j < number_of_sockets; ++j)
  393. {
  394. boost::asio::post(pool2, std::bind(accept_conncections_from_Pb, std::ref(io_context), std::ref(socketsPb), ports[j], j));
  395. }
  396. pool2.join();
  397. #endif
  398. __m128i *final_correction_word = (__m128i *)std::aligned_alloc(sizeof(__m256i), thread_per_batch * sizeof(__m128i));
  399. AES_KEY aeskey;
  400. __m128i **output = (__m128i **)malloc(sizeof(__m128i *) * thread_per_batch);
  401. int8_t **flags = (int8_t **)malloc(sizeof(uint8_t *) * thread_per_batch);
  402. for (size_t j = 0; j < thread_per_batch; ++j)
  403. {
  404. output[j] = (__m128i *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(__m128i));
  405. flags[j] = (int8_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(uint8_t));
  406. }
  407. const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  408. const bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  409. const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
  410. const size_t depth = std::ceil(std::log2(db_nitems));
  411. const size_t nbits = std::ceil(std::log2(db_nitems));
  412. const size_t nodes_in_interval = db_nitems - 1;
  413. auto start = std::chrono::steady_clock::now();
  414. #ifdef VERBOSE
  415. printf("n_threads = %zu\n\n", n_threads);
  416. #endif
  417. uint8_t **target_share_read = new uint8_t *[thread_per_batch];
  418. for (size_t i = 0; i < n_threads; i++) target_share_read[i] = new uint8_t[64];
  419. for(size_t iters = 0; iters < n_batches; ++iters)
  420. {
  421. if (n_batches > 1) {
  422. printf("Starting evalfull_mpc batch %lu / %lu\n", iters+1, n_batches);
  423. }
  424. generate_random_targets(target_share_read, thread_per_batch, party, expo);
  425. boost::asio::thread_pool pool(thread_per_batch);
  426. for (size_t j = 0; j < thread_per_batch; ++j)
  427. {
  428. boost::asio::post(pool, std::bind(evalfull_mpc, std::ref(nodes_per_leaf), std::ref(depth), std::ref(nbits), std::ref(nodes_in_interval),
  429. std::ref(aeskey), target_share_read[j], std::ref(socketsPb), 0, db_nitems - 1, output[j],
  430. flags[j], std::ref(final_correction_word[j]), party, 5 * j));
  431. }
  432. pool.join();
  433. convert_shares(output, flags, thread_per_batch, db_nitems, final_correction_word, socketsPb[0], party);
  434. }
  435. for(size_t j = 0; j < thread_per_batch; ++j)
  436. {
  437. free(output[j]);
  438. free(flags[j]);
  439. delete[] target_share_read[j];
  440. }
  441. free(output);
  442. free(flags);
  443. free(final_correction_word);
  444. delete[] target_share_read;
  445. auto end = std::chrono::steady_clock::now();
  446. std::chrono::duration<double> elapsed_seconds = end - start;
  447. std::cout << "WallClockTime: " << elapsed_seconds.count() << " s" << std::endl;
  448. std::cout << "CommunicationCost: " << communication_cost << " bytes" << std::endl;
  449. return 0;
  450. }