dpfgen.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. struct dpfP2
  2. {
  3. __m128i root;
  4. __m128i CW[26];
  5. uint8_t cwt_L[26];
  6. uint8_t cwt_R[26];
  7. };
  8. void generate_random_targets(uint8_t ** target_share_read, size_t n_threads, bool party, size_t expo)
  9. {
  10. for(size_t i = 0; i < n_threads; i++)
  11. {
  12. target_share_read[i] = new uint8_t[64];
  13. }
  14. for(size_t j = 0; j < 64; ++j)
  15. {
  16. for(size_t i = 0; i < n_threads; ++i)
  17. {
  18. srand(3);
  19. uint8_t random_value;
  20. arc4random_buf(&random_value, sizeof(uint8_t));
  21. target_share_read[i][j] = random_value;//rand();
  22. target_share_read[i][j] = 0;//target_share_read[i][j] % 2;
  23. if(party) target_share_read[i][expo-2] = 1;
  24. }
  25. }
  26. }
  27. void write_evalfull_outs_into_a_file(bool party, size_t i, size_t db_nitems, int8_t ** flags, int64_t * outs_, __m128i * final_correction_word)
  28. {
  29. if(!party)
  30. {
  31. char const * p0_filename0;
  32. char tmp[100];
  33. p0_filename0 = "../duoram-online/party0_read_flags_b";
  34. strcpy(tmp, p0_filename0);
  35. strcat(tmp, std::to_string(i).c_str());
  36. int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  37. int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
  38. if(written<0) {
  39. perror("Write error");
  40. }
  41. close(w0);
  42. p0_filename0 = "../duoram-online/party0_read_flags_c";
  43. strcpy(tmp, p0_filename0);
  44. strcat(tmp, std::to_string(i).c_str());
  45. int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  46. written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
  47. if(written<0) {
  48. perror("Write error");
  49. }
  50. close(w1);
  51. p0_filename0 = "../duoram-online/party0_read_flags_d";
  52. strcpy(tmp, p0_filename0);
  53. strcat(tmp, std::to_string(i).c_str());
  54. int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  55. written = write(w2, flags[i], db_nitems * sizeof(flags[i][0]));
  56. if(written<0) {
  57. perror("Write error");
  58. }
  59. close(w2);
  60. p0_filename0 = "../duoram-online/party0_write_flags_b";
  61. strcpy(tmp, p0_filename0);
  62. strcat(tmp, std::to_string(i).c_str());
  63. int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  64. written = write(w4, flags[i], db_nitems * sizeof(flags[i][0]));
  65. if(written<0) {
  66. perror("Write error");
  67. }
  68. close(w4);
  69. p0_filename0 = "../duoram-online/party0_write_flags_c";
  70. strcpy(tmp, p0_filename0);
  71. strcat(tmp, std::to_string(i).c_str());
  72. int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  73. written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
  74. if(written<0) {
  75. perror("Write error");
  76. }
  77. close(w5);
  78. p0_filename0 = "../duoram-online/party0_write_flags_d";
  79. strcpy(tmp, p0_filename0);
  80. strcat(tmp, std::to_string(i).c_str());
  81. int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  82. written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));
  83. if(written<0) {
  84. perror("Write error");
  85. }
  86. close(w6);
  87. p0_filename0 = "../duoram-online/party0_write_b";
  88. strcpy(tmp, p0_filename0);
  89. strcat(tmp, std::to_string(i).c_str());
  90. w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  91. written = write(w4, outs_, db_nitems * sizeof(outs_[0]));
  92. if(written<0) {
  93. perror("Write error");
  94. }
  95. close(w4);
  96. p0_filename0 = "../duoram-online/party0_write_c";
  97. strcpy(tmp, p0_filename0);
  98. strcat(tmp, std::to_string(i).c_str());
  99. w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  100. written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
  101. if(written<0) {
  102. perror("Write error");
  103. }
  104. close(w5);
  105. p0_filename0 = "../duoram-online/party0_write_d";
  106. strcpy(tmp, p0_filename0);
  107. strcat(tmp, std::to_string(i).c_str());
  108. w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  109. written = write(w6, outs_, db_nitems * sizeof(outs_[0]));
  110. if(written<0) {
  111. perror("Write error");
  112. }
  113. close(w6);
  114. }
  115. if(party)
  116. {
  117. char const * p0_filename0;
  118. char tmp[100];
  119. p0_filename0 = "../duoram-online/party1_read_flags_b";
  120. strcpy(tmp, p0_filename0);
  121. strcat(tmp, std::to_string(i).c_str());
  122. int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  123. int written = write(w0, flags[i], db_nitems * sizeof(flags[i][0]));
  124. if(written<0) {
  125. perror("Write error");
  126. }
  127. close(w0);
  128. p0_filename0 = "../duoram-online/party1_read_flags_c";
  129. strcpy(tmp, p0_filename0);
  130. strcat(tmp, std::to_string(i).c_str());
  131. int w1 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  132. written = write(w1, flags[i], db_nitems * sizeof(flags[i][0]));
  133. if(written<0) {
  134. perror("Write error");
  135. }
  136. close(w1);
  137. p0_filename0 = "../duoram-online/party1_read_flags_d";
  138. strcpy(tmp, p0_filename0);
  139. strcat(tmp, std::to_string(i).c_str());
  140. int w2 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  141. written = write(w2,flags[i], db_nitems * sizeof(flags[i][0]));
  142. if(written<0) {
  143. perror("Write error");
  144. }
  145. close(w2);
  146. p0_filename0 = "../duoram-online/party1_write_flags_b";
  147. strcpy(tmp, p0_filename0);
  148. strcat(tmp, std::to_string(i).c_str());
  149. int w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  150. written = write(w4,flags[i], db_nitems * sizeof(flags[i][0]));
  151. if(written<0) {
  152. perror("Write error");
  153. }
  154. close(w4);
  155. p0_filename0 = "../duoram-online/party1_write_flags_c";
  156. strcpy(tmp, p0_filename0);
  157. strcat(tmp, std::to_string(i).c_str());
  158. int w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  159. written = write(w5, flags[i], db_nitems * sizeof(flags[i][0]));
  160. if(written<0) {
  161. perror("Write error");
  162. }
  163. close(w5);
  164. p0_filename0 = "../duoram-online/party1_write_flags_d";
  165. strcpy(tmp, p0_filename0);
  166. strcat(tmp, std::to_string(i).c_str());
  167. int w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  168. written = write(w6, flags[i], db_nitems * sizeof(flags[i][0]));
  169. if(written<0) {
  170. perror("Write error");
  171. }
  172. close(w6);
  173. p0_filename0 = "../duoram-online/party1_write_b";
  174. strcpy(tmp, p0_filename0);
  175. strcat(tmp, std::to_string(i).c_str());
  176. w4 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  177. written = write(w4, outs_, db_nitems * sizeof(outs_[0]));
  178. if(written<0) {
  179. perror("Write error");
  180. }
  181. close(w4);
  182. // char const * p0_filename0;
  183. p0_filename0 = "../duoram-online/party1_write_c";
  184. strcpy(tmp, p0_filename0);
  185. strcat(tmp, std::to_string(i).c_str());
  186. w5 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  187. written = write(w5, outs_, db_nitems * sizeof(outs_[0]));
  188. if(written<0) {
  189. perror("Write error");
  190. }
  191. close(w5);
  192. p0_filename0 = "../duoram-online/party1_write_d";
  193. strcpy(tmp, p0_filename0);
  194. strcat(tmp, std::to_string(i).c_str());
  195. w6 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  196. written = write(w6, outs_, db_nitems * sizeof(outs_[0]));
  197. if(written<0) {
  198. perror("Write error");
  199. }
  200. close(w6);
  201. }
  202. if(party)
  203. {
  204. char const * p1_filename0;
  205. char tmp[100];
  206. p1_filename0 = "../duoram-online/FCW1";
  207. strcpy(tmp, p1_filename0);
  208. strcat(tmp, std::to_string(i).c_str());
  209. int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  210. int written = write(w0, &final_correction_word[0][0], sizeof(final_correction_word[0][0]));
  211. if(written<0) {
  212. perror("Write error");
  213. }
  214. close(w0);
  215. }
  216. if(!party)
  217. {
  218. char const * p0_filename0;
  219. char tmp[100];
  220. p0_filename0 = "../duoram-online/FCW0";
  221. strcpy(tmp, p0_filename0);
  222. strcat(tmp, std::to_string(i).c_str());
  223. int w0 = open( tmp, O_WRONLY | O_CREAT, S_IWRITE | S_IREAD);
  224. int written = write(w0, &final_correction_word[0][0], sizeof(final_correction_word[0][0]));
  225. if(written<0) {
  226. perror("Write error");
  227. }
  228. close(w0);
  229. }
  230. }
  231. void compute_CW(tcp::socket& sout, tcp::socket& sin, __m128i L, __m128i R, uint8_t bit, __m128i & CW)
  232. {
  233. struct cw_construction
  234. {
  235. __m128i rand_b, gamma_b;
  236. uint8_t bit_b;
  237. };
  238. cw_construction computecw;
  239. read(sin, boost::asio::buffer(&computecw, sizeof(computecw)));
  240. __m128i rand_b = computecw.rand_b;
  241. __m128i gamma_b = computecw.gamma_b;
  242. uint8_t bit_b = computecw.bit_b;
  243. #ifdef DEBUG
  244. __m128i rand_b2, gamma_b2;
  245. uint8_t bit_b2;
  246. read(sin, boost::asio::buffer(&rand_b2, sizeof(rand_b)));
  247. read(sin, boost::asio::buffer(&gamma_b2, sizeof(gamma_b)));
  248. read(sin, boost::asio::buffer(&bit_b2, sizeof(bit_b)));
  249. assert(rand_b2[0] == rand_b[0]);
  250. assert(rand_b2[1] == rand_b[1]);
  251. assert(gamma_b2[0] == gamma_b[0]);
  252. assert(gamma_b2[1] == gamma_b[1]);
  253. assert(bit_b2 == bit_b);
  254. #endif
  255. uint8_t blinded_bit, blinded_bit_read;
  256. blinded_bit = bit ^ bit_b;
  257. __m128i blinded_L = L ^ R ^ rand_b;
  258. __m128i blinded_L_read;
  259. struct BlindsCW
  260. {
  261. __m128i blinded_message;
  262. uint8_t blinded_bit;
  263. };
  264. BlindsCW blinds_sent, blinds_recv;
  265. blinds_sent.blinded_bit = blinded_bit;
  266. blinds_sent.blinded_message = blinded_L;
  267. boost::asio::write(sout, boost::asio::buffer(&blinds_sent, sizeof(blinds_sent)));
  268. boost::asio::read(sout, boost::asio::buffer(&blinds_recv, sizeof(blinds_recv)));
  269. blinded_bit_read = blinds_recv.blinded_bit;
  270. blinded_L_read = blinds_recv.blinded_message;
  271. __m128i out_ = R ^ gamma_b;//_mm_setzero_si128;
  272. if(bit)
  273. {
  274. out_ ^= (L ^ R ^ blinded_L_read);
  275. }
  276. if(blinded_bit_read)
  277. {
  278. out_ ^= rand_b;
  279. }
  280. __m128i out_reconstruction;
  281. boost::asio::write(sout, boost::asio::buffer(&out_, sizeof(out_)));
  282. boost::asio::read(sout, boost::asio::buffer(&out_reconstruction, sizeof(out_reconstruction)));
  283. out_reconstruction = out_ ^ out_reconstruction;
  284. CW = out_reconstruction;
  285. #ifdef DEBUG
  286. uint8_t bit_reconstruction;
  287. boost::asio::write(sout, boost::asio::buffer(&bit, sizeof(bit)));
  288. boost::asio::read(sout, boost::asio::buffer(&bit_reconstruction, sizeof(bit_reconstruction)));
  289. bit_reconstruction = bit ^ bit_reconstruction;
  290. __m128i L_reconstruction;
  291. boost::asio::write(sout, boost::asio::buffer(&L, sizeof(L)));
  292. boost::asio::read(sout, boost::asio::buffer(&L_reconstruction, sizeof(L_reconstruction)));
  293. L_reconstruction = L ^ L_reconstruction;
  294. __m128i R_reconstruction;
  295. boost::asio::write(sout, boost::asio::buffer(&R, sizeof(R)));
  296. boost::asio::read(sout, boost::asio::buffer(&R_reconstruction, sizeof(R_reconstruction)));
  297. R_reconstruction = R ^ R_reconstruction;
  298. __m128i CW_debug;
  299. if(bit_reconstruction != 0)
  300. {
  301. CW_debug = L_reconstruction;
  302. }
  303. else
  304. {
  305. CW_debug = R_reconstruction;
  306. }
  307. assert(CW_debug[0] == CW[0]);
  308. assert(CW_debug[1] == CW[1]);
  309. #endif
  310. }
  311. template<typename node_t, typename prgkey_t>
  312. static inline void traverse(const prgkey_t & prgkey, const node_t & seed, node_t s[2])
  313. {
  314. dpf::PRG(prgkey, clear_lsb(seed, 0b11), s, 2);
  315. } // dpf::expand
  316. /**
  317. * @brief
  318. *
  319. * @param nodes_per_leaf
  320. * @param depth
  321. * @param nbits
  322. * @param nodes_in_interval
  323. * @param prgkey The PRG Key
  324. * @param target_share
  325. * @param socketsPb Array of sockets to write to Pb
  326. * @param socketsP2 Array of sockets to write to P2
  327. * @param from
  328. * @param to
  329. * @param output
  330. * @param _t
  331. * @param final_correction_word the final correction word is written in to this
  332. * @param party Party
  333. * @param socket_no
  334. */
  335. inline void create_dpfs( size_t db_nitems,
  336. const AES_KEY& prgkey, uint8_t target_share[64], std::vector<socket_t>& socketsPb, std::vector<socket_t>& socketsP2,
  337. const size_t from, const size_t to, __m128i * output, int8_t * _t, __m128i& final_correction_word, bool party, size_t socket_no, size_t ind)
  338. {
  339. const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  340. const bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  341. const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
  342. const size_t depth = std::ceil(std::log2(db_nitems));
  343. const size_t nbits = std::ceil(std::log2(db_nitems));
  344. const size_t nodes_in_interval = db_nitems-1;
  345. __m128i root;
  346. arc4random_buf(&root, sizeof(root));
  347. root = set_lsb(root, party);
  348. const size_t from_node = std::floor(static_cast<double>(from) / nodes_per_leaf);
  349. __m128i * s[2] = {
  350. reinterpret_cast<__m128i *>(output) + nodes_in_interval * (nodes_per_leaf - 1),
  351. s[0] + nodes_in_interval / 2
  352. };
  353. int8_t * t[2] = { _t, _t + nodes_in_interval / 2};
  354. int curlayer = depth % 2;
  355. s[curlayer][0] = root;
  356. t[curlayer][0] = get_lsb(root, 0b01);
  357. __m128i * CW = (__m128i *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(__m128i));
  358. if(ind == 0)
  359. {
  360. std::cout << "root = " << root[0] << " " << root[1] << std::endl;
  361. std::cout << "t[curlayer][0] " << (int) t[curlayer][0] << std::endl;
  362. }
  363. // struct dpfP2
  364. // {
  365. // __m128i root;
  366. // __m128i CW[26];
  367. // uint8_t cwt_L[26];
  368. // uint8_t cwt_R[26];
  369. // };
  370. dpfP2 dpf_instance;
  371. dpf_instance.root = root;
  372. for (size_t layer = 0; layer < depth; ++layer)
  373. {
  374. #ifdef VERBOSE
  375. printf("layer = %zu\n", layer);
  376. #endif
  377. curlayer = 1-curlayer;
  378. size_t i=0, j=0;
  379. auto nextbit = (from_node >> (nbits-layer-1)) & 1;
  380. size_t nodes_in_prev_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer)));
  381. size_t nodes_in_cur_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer-1)));
  382. __m128i L = _mm_setzero_si128();
  383. __m128i R = _mm_setzero_si128();
  384. for (i = nextbit, j = nextbit; j < nodes_in_prev_layer-1; ++j, i+=2)
  385. {
  386. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  387. L ^= s[curlayer][i];
  388. R ^= s[curlayer][i+1];
  389. }
  390. if (nodes_in_prev_layer > j)
  391. {
  392. if (i < nodes_in_cur_layer - 1)
  393. {
  394. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  395. L ^= s[curlayer][i];
  396. R ^= s[curlayer][i+1];
  397. }
  398. }
  399. compute_CW(socketsPb[socket_no], socketsP2[socket_no], L, R, target_share[layer], CW[layer]);
  400. uint8_t advice_L = get_lsb(L) ^ target_share[layer];
  401. uint8_t advice_R = get_lsb(R) ^ target_share[layer];
  402. uint8_t cwt_L, cwt_R;
  403. uint8_t advice[2];
  404. uint8_t cwts[2];
  405. advice[0] = advice_L;
  406. advice[1] = advice_R;
  407. boost::asio::write(socketsPb[socket_no+1], boost::asio::buffer(&advice, sizeof(advice)));
  408. boost::asio::read(socketsPb[socket_no+1], boost::asio::buffer(&cwts, sizeof(cwts)));
  409. cwt_L = cwts[0];
  410. cwt_R = cwts[1];
  411. cwt_L = cwt_L ^ advice_L ^ 1;
  412. cwt_R = cwt_R ^ advice_R;
  413. #ifdef DEBUG
  414. if(ind == 0)
  415. {
  416. std::cout << "CW reconstruction = " << CW[layer][0] << " " << CW[layer][1] << std::endl;
  417. std::cout << " cwt_L = " << (int) cwt_L << std::endl;
  418. std::cout << " cwt_R = " << (int) cwt_R << std::endl;
  419. }
  420. #endif
  421. dpf_instance.CW[layer] = CW[layer];
  422. dpf_instance.cwt_L[layer] = cwt_L;
  423. dpf_instance.cwt_R[layer] = cwt_R;
  424. for(size_t j = 0; j < nodes_in_prev_layer; ++j)
  425. {
  426. t[curlayer][2*j] = get_lsb(s[curlayer][2*j]) ^ (cwt_L & t[1-curlayer][j]);
  427. s[curlayer][2*j] = clear_lsb(xor_if(s[curlayer][2*j], CW[layer], !t[1-curlayer][j]), 0b11);
  428. t[curlayer][(2*j)+1] = get_lsb(s[curlayer][(2*j)+1]) ^ (cwt_R & t[1-curlayer][j]);
  429. s[curlayer][(2*j)+1] = clear_lsb(xor_if(s[curlayer][(2*j)+1], CW[layer], !t[1-curlayer][j]), 0b11);
  430. }
  431. }
  432. boost::asio::write(socketsP2[socket_no+1], boost::asio::buffer(&dpf_instance, sizeof(dpfP2)));
  433. __m128i Gamma = _mm_setzero_si128();
  434. for (size_t i = 0; i < to + 1; ++i)
  435. {
  436. Gamma[0] += output[i][0];
  437. Gamma[1] += output[i][1]; // the correction word for share conversion
  438. }
  439. if(party)
  440. {
  441. Gamma[0] = -Gamma[0];
  442. Gamma[1] = -Gamma[1]; // the correction word for share conversion
  443. }
  444. // boost::asio::write(socketsPb[socket_no + 3], boost::asio::buffer(&Gamma, sizeof(Gamma)));
  445. // boost::asio::read(socketsPb[socket_no + 3], boost::asio::buffer(&final_correction_word, sizeof(final_correction_word)));
  446. final_correction_word = Gamma; // final_correction_word + Gamma;
  447. } // dpf::__evalinterval
  448. inline void evaluate_dpfs( size_t db_nitems, dpfP2 dpfinstance, const AES_KEY& prgkey, const size_t from, const size_t to,
  449. __m128i * output, int8_t * _t, __m128i& final_correction_word, bool party,
  450. size_t ind)
  451. {
  452. const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  453. const bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  454. const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
  455. const size_t depth = std::ceil(std::log2(db_nitems));
  456. const size_t nbits = std::ceil(std::log2(db_nitems));
  457. const size_t nodes_in_interval = db_nitems-1;
  458. __m128i root = dpfinstance.root;
  459. __m128i * CW = (__m128i *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(__m128i));
  460. uint8_t * cwt_L = (uint8_t *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(uint8_t));
  461. uint8_t * cwt_R = (uint8_t *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(uint8_t));
  462. for(size_t j = 0; j < depth; ++j)
  463. {
  464. CW[j] = dpfinstance.CW[j];
  465. cwt_L[j] = dpfinstance.cwt_L[j];
  466. cwt_R[j] = dpfinstance.cwt_R[j];
  467. }
  468. root = set_lsb(root, party);
  469. const size_t from_node = std::floor(static_cast<double>(from) / nodes_per_leaf);
  470. __m128i * s[2] = {
  471. reinterpret_cast<__m128i *>(output) + nodes_in_interval * (nodes_per_leaf - 1),
  472. s[0] + nodes_in_interval / 2
  473. };
  474. int8_t * t[2] = { _t, _t + nodes_in_interval / 2};
  475. int curlayer = depth % 2;
  476. s[curlayer][0] = root;
  477. t[curlayer][0] = get_lsb(root, 0b01);
  478. if(ind == 0)
  479. {
  480. std::cout << "root = " << root[0] << " " << root[1] << std::endl;
  481. std::cout << "t[curlayer][0] " << (int) t[curlayer][0] << std::endl;
  482. }
  483. for (size_t layer = 0; layer < depth; ++layer)
  484. {
  485. #ifdef VERBOSE
  486. printf("layer = %zu\n", layer);
  487. #endif
  488. curlayer = 1-curlayer;
  489. size_t i=0, j=0;
  490. auto nextbit = (from_node >> (nbits-layer-1)) & 1;
  491. size_t nodes_in_prev_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer)));
  492. size_t nodes_in_cur_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer-1)));
  493. for (i = nextbit, j = nextbit; j < nodes_in_prev_layer-1; ++j, i+=2)
  494. {
  495. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  496. }
  497. if (nodes_in_prev_layer > j)
  498. {
  499. if (i < nodes_in_cur_layer - 1)
  500. {
  501. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  502. }
  503. }
  504. //#ifdef DEBUG
  505. if(ind == 0)
  506. {
  507. std::cout << "CW reconstruction = " << CW[layer][0] << " " << CW[layer][1] << std::endl;
  508. std::cout << " cwt_L = " << (int) cwt_L[layer] << std::endl;
  509. std::cout << " cwt_R = " << (int) cwt_R[layer] << std::endl;
  510. }
  511. //#endif
  512. for(size_t j = 0; j < nodes_in_prev_layer; ++j)
  513. {
  514. t[curlayer][2*j] = get_lsb(s[curlayer][2*j]) ^ (cwt_L[layer] & t[1-curlayer][j]);
  515. s[curlayer][2*j] = clear_lsb(xor_if(s[curlayer][2*j], CW[layer], !t[1-curlayer][j]), 0b11);
  516. t[curlayer][(2*j)+1] = get_lsb(s[curlayer][(2*j)+1]) ^ (cwt_R[layer] & t[1-curlayer][j]);
  517. s[curlayer][(2*j)+1] = clear_lsb(xor_if(s[curlayer][(2*j)+1], CW[layer], !t[1-curlayer][j]), 0b11);
  518. }
  519. }
  520. __m128i Gamma = _mm_setzero_si128();
  521. for (size_t i = 0; i < to + 1; ++i)
  522. {
  523. Gamma[0] += output[i][0];
  524. Gamma[1] += output[i][1]; // the correction word for share conversion
  525. }
  526. if(party)
  527. {
  528. Gamma[0] = -Gamma[0];
  529. Gamma[1] = -Gamma[1]; // the correction word for share conversion
  530. }
  531. final_correction_word = Gamma; // final_correction_word + Gamma;
  532. } // dpf::__evalinterval
  533. // void convert_shares(size_t i, __m128i ** output, int8_t ** flags, size_t n_threads, size_t db_nitems, __m128i * final_correction_word,
  534. // int64_t ** leaves, int64_t ** leafbits,
  535. // tcp::socket& sb, tcp::socket& s2, bool party)
  536. // {
  537. // #ifdef DEBUG
  538. // std::cout << "share conversion " << i << "-th, thread started runing" << std::endl << std::endl;
  539. // #endif
  540. // for(size_t j = 0; j < db_nitems; ++j)
  541. // {
  542. // if(party)
  543. // {
  544. // output[i][j] = -output[i][j];
  545. // flags[i][j] = -flags[i][j];
  546. // }
  547. // }
  548. // int64_t pm = 0;
  549. // int64_t rb, rb_prime;
  550. // arc4random_buf(&rb, sizeof(rb));
  551. // for(size_t j = 0; j < db_nitems; ++j)
  552. // {
  553. // if(party)
  554. // {
  555. // if(flags[i][j] != 0) pm -= 1;
  556. // }
  557. // if(!party)
  558. // {
  559. // if(flags[i][j] != 0) pm += 1;//flags[0][j];
  560. // }
  561. // }
  562. // rb_prime = du_attalah_Pb(rb, pm, s2, sb);
  563. // int64_t FCWshare = du_attalah_Pb(final_correction_word[i][1] + rb_prime, pm, s2, sb);
  564. // int64_t tmpfcw = du_attalah_Pb(final_correction_word[i][1], pm, s2, sb);
  565. // int64_t test0, test1, test;
  566. // test1 = 0;
  567. // test0 = 0;
  568. // for(size_t j = 0; j < db_nitems; ++j)
  569. // {
  570. // test0 += flags[i][j] * final_correction_word[i][1];
  571. // if(flags[i][j] == 0) test1 += final_correction_word[i][1];
  572. // }
  573. // test1 = -test1;
  574. // test = test0 + test1;
  575. // //tmpfcw += rb;
  576. // int64_t test_reconstruction;
  577. // boost::asio::write(sb, boost::asio::buffer(&test, sizeof(test)));
  578. // boost::asio::read(sb, boost::asio::buffer(&test_reconstruction, sizeof(test_reconstruction)));
  579. // test_reconstruction = test_reconstruction + test;
  580. // int64_t tmpfcw_reconstruction;
  581. // boost::asio::write(sb, boost::asio::buffer(&tmpfcw, sizeof(tmpfcw)));
  582. // boost::asio::read(sb, boost::asio::buffer(&tmpfcw_reconstruction, sizeof(tmpfcw_reconstruction)));
  583. // tmpfcw_reconstruction = tmpfcw_reconstruction + tmpfcw;
  584. // int64_t FCWshare_reconstruction;
  585. // boost::asio::write(sb, boost::asio::buffer(&FCWshare, sizeof(FCWshare)));
  586. // boost::asio::read(sb, boost::asio::buffer(&FCWshare_reconstruction, sizeof(FCWshare_reconstruction)));
  587. // FCWshare_reconstruction = FCWshare_reconstruction + FCWshare;
  588. // //std::cout << tmpfcw_reconstruction << " <----> " << test_reconstruction << std::endl;
  589. // int64_t PM = pm + rb;
  590. // int64_t PM_recv;
  591. // boost::asio::write(sb, boost::asio::buffer(&PM, sizeof(PM)));
  592. // boost::asio::read(sb, boost::asio::buffer(&PM_recv, sizeof(PM_recv)));
  593. // int64_t * flags_ = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  594. // int64_t * outs_ = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  595. // for(size_t j = 0; j < db_nitems; ++j)
  596. // {
  597. // outs_[j] = output[0][j][0];
  598. // leaves[i][j] = output[i][j][0];
  599. // #ifdef DEBUG
  600. // int64_t out_rec;
  601. // boost::asio::write(sb, boost::asio::buffer(&outs_[j], sizeof(outs_[j])));
  602. // boost::asio::read(sb, boost::asio::buffer(&out_rec, sizeof(out_rec)));
  603. // out_rec = out_rec + outs_[j];
  604. // if(out_rec != 0) std::cout << j << "-> " << out_rec << std::endl;
  605. // #endif
  606. // flags_[j] = (flags[i][j] * pm) + (flags[i][j] * PM_recv) + (flags[i][j] * rb);
  607. // flags_[j] += output[i][j][1];
  608. // if(!party)
  609. // {
  610. // flags_[j] -= (flags[i][j] * FCWshare_reconstruction);
  611. // }
  612. // if(party)
  613. // {
  614. // flags_[j] -= (flags[i][j] * FCWshare_reconstruction);
  615. // }
  616. // #ifdef DEBUG
  617. // int64_t flags_rec;
  618. // boost::asio::write(sb, boost::asio::buffer(&flags_[j], sizeof(flags_[j])));
  619. // boost::asio::read(sb, boost::asio::buffer(&flags_rec, sizeof(flags_rec)));
  620. // flags_rec = flags_rec + flags_[j];
  621. // if(flags_rec != 0)
  622. // {
  623. // std::cout << j << " ---> Flag Reconstruction = " << flags_rec << std::endl;
  624. // }
  625. // #endif
  626. // flags[i][j] = flags_[j];
  627. // if(flags[i][j] == 128 || flags[i][j] == -128) flags[i][j] = 0;
  628. // leafbits[i][j] = flags[i][j];
  629. // #ifdef DEBUG
  630. // int8_t flags_rec2;
  631. // boost::asio::write(sb, boost::asio::buffer(&flags[i][j], sizeof(flags[i][j])));
  632. // boost::asio::read(sb, boost::asio::buffer(&flags_rec2, sizeof(flags_rec2)));
  633. // flags_rec2 = flags_rec2 + flags[i][j];
  634. // if(flags_rec2 != 0)
  635. // {
  636. // std::cout << j << " ---> Flag Reconstruction = " << (int) flags_rec2 << std::endl;
  637. // if(flags_rec2 != 1) std::cout << (int) flags[i][j] << "-> " << flags_[j] << std::endl;
  638. // }
  639. // #endif
  640. // }
  641. // write_evalfull_outs_into_a_file(party, i, db_nitems, flags, leaves[0], final_correction_word);
  642. // }