dpfgen.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. struct cw_construction
  28. {
  29. __m128i rand_b, gamma_b;
  30. uint8_t bit_b;
  31. };
  32. struct BlindsCW
  33. {
  34. __m128i blinded_message;
  35. uint8_t blinded_bit;
  36. };
  37. struct reconstructioncw
  38. {
  39. __m128i cw;
  40. uint8_t cwbit[2];
  41. };
  42. // void compute_CW_bits(tcp::socket& sout, __m128i L, __m128i R, uint8_t bit, uint8_t &cwt_L, uint8_t &cwt_R)
  43. // {
  44. // uint8_t advice_L = get_lsb(L) ^ bit;
  45. // uint8_t advice_R = get_lsb(R) ^ bit;
  46. // uint8_t advice[2];
  47. // uint8_t cwts[2];
  48. // advice[0] = advice_L;
  49. // advice[1] = advice_R;
  50. // boost::asio::write(sout, boost::asio::buffer(&advice, sizeof(advice)));
  51. // boost::asio::read(sout, boost::asio::buffer(&cwts, sizeof(cwts)));
  52. // cwt_L = cwts[0];
  53. // cwt_R = cwts[1];
  54. // cwt_L = cwt_L ^ advice_L ^ 1;
  55. // cwt_R = cwt_R ^ advice_R;
  56. // }
  57. void compute_CW(tcp::socket& sout, tcp::socket& sin, __m128i L, __m128i R, uint8_t bit, __m128i & CW, uint8_t &cwt_L, uint8_t &cwt_R)
  58. {
  59. cw_construction computecw;
  60. reconstructioncw cwsent, cwrecv;
  61. //Communication from P2
  62. read(sin, boost::asio::buffer(&computecw, sizeof(computecw)));
  63. __m128i rand_b = computecw.rand_b;
  64. __m128i gamma_b = computecw.gamma_b;
  65. uint8_t bit_b = computecw.bit_b;
  66. #ifdef DEBUG
  67. __m128i rand_b2, gamma_b2;
  68. uint8_t bit_b2;
  69. read(sin, boost::asio::buffer(&rand_b2, sizeof(rand_b)));
  70. read(sin, boost::asio::buffer(&gamma_b2, sizeof(gamma_b)));
  71. read(sin, boost::asio::buffer(&bit_b2, sizeof(bit_b)));
  72. assert(rand_b2[0] == rand_b[0]);
  73. assert(rand_b2[1] == rand_b[1]);
  74. assert(gamma_b2[0] == gamma_b[0]);
  75. assert(gamma_b2[1] == gamma_b[1]);
  76. assert(bit_b2 == bit_b);
  77. #endif
  78. uint8_t blinded_bit, blinded_bit_read;
  79. blinded_bit = bit ^ bit_b;
  80. __m128i blinded_L = L ^ R ^ rand_b;
  81. __m128i blinded_L_read;
  82. BlindsCW blinds_sent, blinds_recv;
  83. blinds_sent.blinded_bit = blinded_bit;
  84. blinds_sent.blinded_message = blinded_L;
  85. //exchange blinded shares for OSWAP.
  86. boost::asio::write(sout, boost::asio::buffer(&blinds_sent, sizeof(BlindsCW)));
  87. boost::asio::read(sout, boost::asio::buffer(&blinds_recv, sizeof(BlindsCW)));
  88. blinded_bit_read = blinds_recv.blinded_bit;
  89. blinded_L_read = blinds_recv.blinded_message;
  90. //__m128i out_ = R ^ gamma_b;//_mm_setzero_si128;
  91. cwsent.cw = R ^ gamma_b;
  92. if(bit)
  93. {
  94. cwsent.cw ^= (L ^ R ^ blinded_L_read);
  95. }
  96. if(blinded_bit_read)
  97. {
  98. cwsent.cw ^= rand_b;
  99. }
  100. // uint8_t advice_L = get_lsb(L) ^ bit;
  101. // uint8_t advice_R = get_lsb(R) ^ bit;
  102. // uint8_t advice[2];
  103. // // uint8_t cwts[2];
  104. // advice[0] = advice_L;
  105. // advice[1] = advice_R;
  106. //__m128i out_reconstruction;
  107. //cwsent.cw = out_;
  108. cwsent.cwbit[0] = get_lsb(L) ^ bit;//advice[0];
  109. cwsent.cwbit[1] = get_lsb(R) ^ bit;//advice[1];
  110. boost::asio::write(sout, boost::asio::buffer(&cwsent, sizeof(cwsent)));
  111. boost::asio::read(sout, boost::asio::buffer(&cwrecv, sizeof(cwrecv)));
  112. // boost::asio::write(sout, boost::asio::buffer(&out_, sizeof(out_)));
  113. // boost::asio::read(sout, boost::asio::buffer(&CW, sizeof(CW)));
  114. // CW = out_ ^ CW;
  115. cwrecv.cw ^= cwsent.cw;
  116. cwrecv.cwbit[0] ^= (cwsent.cwbit[0] ^ 1);
  117. cwrecv.cwbit[1] ^= (cwsent.cwbit[1]);
  118. // assert(CW[0] == cwrecv.cw[0]);
  119. // assert(CW[1] == cwrecv.cw[1]);
  120. // boost::asio::write(sout, boost::asio::buffer(&advice, sizeof(advice)));
  121. // boost::asio::read(sout, boost::asio::buffer(&cwts, sizeof(cwts)));
  122. // cwt_L = cwts[0];
  123. // cwt_R = cwts[1];
  124. // cwt_L = cwt_L ^ advice_L ^ 1;
  125. // cwt_R = cwt_R ^ advice_R;
  126. // assert(cwt_L == cwrecv.cwbit[0]);
  127. // assert(cwt_R == cwrecv.cwbit[1]);
  128. cwt_L = cwrecv.cwbit[0];
  129. cwt_R = cwrecv.cwbit[1];
  130. CW = cwrecv.cw;
  131. // The following asserts the correctness of ComputeCW
  132. #ifdef DEBUG
  133. uint8_t bit_reconstruction;
  134. boost::asio::write(sout, boost::asio::buffer(&bit, sizeof(bit)));
  135. boost::asio::read(sout, boost::asio::buffer(&bit_reconstruction, sizeof(bit_reconstruction)));
  136. bit_reconstruction = bit ^ bit_reconstruction;
  137. __m128i L_reconstruction;
  138. boost::asio::write(sout, boost::asio::buffer(&L, sizeof(L)));
  139. boost::asio::read(sout, boost::asio::buffer(&L_reconstruction, sizeof(L_reconstruction)));
  140. L_reconstruction = L ^ L_reconstruction;
  141. __m128i R_reconstruction;
  142. boost::asio::write(sout, boost::asio::buffer(&R, sizeof(R)));
  143. boost::asio::read(sout, boost::asio::buffer(&R_reconstruction, sizeof(R_reconstruction)));
  144. R_reconstruction = R ^ R_reconstruction;
  145. __m128i CW_debug;
  146. if(bit_reconstruction != 0)
  147. {
  148. CW_debug = L_reconstruction;
  149. }
  150. else
  151. {
  152. CW_debug = R_reconstruction;
  153. }
  154. assert(CW_debug[0] == CW[0]);
  155. assert(CW_debug[1] == CW[1]);
  156. #endif
  157. }
  158. template<typename node_t, typename prgkey_t>
  159. static inline void traverse(const prgkey_t & prgkey, const node_t & seed, node_t s[2])
  160. {
  161. dpf::PRG(prgkey, clear_lsb(seed, 0b11), s, 2);
  162. } // dpf::expand
  163. /**
  164. * @brief
  165. *
  166. * @param nodes_per_leaf
  167. * @param depth
  168. * @param nbits
  169. * @param nodes_in_interval
  170. * @param prgkey The PRG Key
  171. * @param target_share
  172. * @param socketsPb Array of sockets to write to Pb
  173. * @param socketsP2 Array of sockets to write to P2
  174. * @param from
  175. * @param to
  176. * @param output
  177. * @param _t
  178. * @param final_correction_word the final correction word is written in to this
  179. * @param party Party
  180. * @param socket_no
  181. */
  182. inline void create_dpfs ( size_t db_nitems, const AES_KEY& prgkey, uint8_t target_share[64], std::vector<socket_t>& socketsPb, std::vector<socket_t>& socketsP2,
  183. const size_t from, const size_t to, __m128i * output, int8_t * _t, __m128i& final_correction_word, dpfP2 * dpf_instance,
  184. bool party, size_t socket_no, size_t ind = 0)
  185. {
  186. const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  187. const bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  188. const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
  189. const size_t depth = std::ceil(std::log2(db_nitems));
  190. const size_t nbits = std::ceil(std::log2(db_nitems));
  191. const size_t nodes_in_interval = db_nitems-1;
  192. __m128i root;
  193. arc4random_buf(&root, sizeof(root));
  194. root = set_lsb(root, party);
  195. const size_t from_node = std::floor(static_cast<double>(from) / nodes_per_leaf);
  196. __m128i * s[2] = {
  197. reinterpret_cast<__m128i *>(output) + nodes_in_interval * (nodes_per_leaf - 1),
  198. s[0] + nodes_in_interval / 2
  199. };
  200. int8_t * t[2] = { _t, _t + nodes_in_interval / 2};
  201. int curlayer = depth % 2;
  202. s[curlayer][0] = root;
  203. t[curlayer][0] = get_lsb(root, 0b01);
  204. __m128i * CW = (__m128i *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(__m128i));
  205. #ifdef VERBOSE
  206. if(ind == 0)
  207. {
  208. std::cout << "root = " << root[0] << " " << root[1] << std::endl;
  209. std::cout << "t[curlayer][0] " << (int) t[curlayer][0] << std::endl;
  210. }
  211. #endif
  212. dpf_instance[ind].root = root;
  213. for (size_t layer = 0; layer < depth; ++layer)
  214. {
  215. #ifdef VERBOSE
  216. printf("layer = %zu\n", layer);
  217. #endif
  218. curlayer = 1-curlayer;
  219. size_t i=0, j=0;
  220. auto nextbit = (from_node >> (nbits-layer-1)) & 1;
  221. size_t nodes_in_prev_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer)));
  222. size_t nodes_in_cur_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer-1)));
  223. __m128i L = _mm_setzero_si128();
  224. __m128i R = _mm_setzero_si128();
  225. for (i = nextbit, j = nextbit; j < nodes_in_prev_layer-1; ++j, i+=2)
  226. {
  227. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  228. L ^= s[curlayer][i];
  229. R ^= s[curlayer][i+1];
  230. }
  231. if (nodes_in_prev_layer > j)
  232. {
  233. if (i < nodes_in_cur_layer - 1)
  234. {
  235. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  236. L ^= s[curlayer][i];
  237. R ^= s[curlayer][i+1];
  238. }
  239. }
  240. uint8_t cwt_L, cwt_R;
  241. // Computes the correction word using OSWAP
  242. compute_CW(socketsPb[socket_no], socketsP2[socket_no], L, R, target_share[layer], CW[layer], cwt_L, cwt_R);
  243. // Computes the correction word bits
  244. //compute_CW_bits(socketsPb[socket_no+1], L, R, target_share[layer], cwt_L, cwt_R);
  245. #ifdef DEBUG
  246. if(ind == 0)
  247. {
  248. std::cout << "CW reconstruction = " << CW[layer][0] << " " << CW[layer][1] << std::endl;
  249. std::cout << " cwt_L = " << (int) cwt_L << std::endl;
  250. std::cout << " cwt_R = " << (int) cwt_R << std::endl;
  251. }
  252. #endif
  253. dpf_instance[ind].CW[layer] = CW[layer];
  254. dpf_instance[ind].cwt_L[layer] = cwt_L;
  255. dpf_instance[ind].cwt_R[layer] = cwt_R;
  256. for(size_t j = 0; j < nodes_in_prev_layer; ++j)
  257. {
  258. t[curlayer][2*j] = get_lsb(s[curlayer][2*j]) ^ (cwt_L & t[1-curlayer][j]);
  259. s[curlayer][2*j] = clear_lsb(xor_if(s[curlayer][2*j], CW[layer], !t[1-curlayer][j]), 0b11);
  260. t[curlayer][(2*j)+1] = get_lsb(s[curlayer][(2*j)+1]) ^ (cwt_R & t[1-curlayer][j]);
  261. s[curlayer][(2*j)+1] = clear_lsb(xor_if(s[curlayer][(2*j)+1], CW[layer], !t[1-curlayer][j]), 0b11);
  262. }
  263. }
  264. boost::asio::write(socketsP2[socket_no+1], boost::asio::buffer(&dpf_instance[ind], sizeof(dpfP2)));
  265. __m128i Gamma = _mm_setzero_si128();
  266. for (size_t i = 0; i < to + 1; ++i)
  267. {
  268. Gamma[0] += output[i][0]; // the correction word for duoram update
  269. Gamma[1] += output[i][1]; // the correction word for share conversion
  270. }
  271. if(party)
  272. {
  273. Gamma[0] = -Gamma[0]; // the correction word for duoram update
  274. Gamma[1] = -Gamma[1]; // the correction word for share conversion
  275. }
  276. #ifdef DEBUG
  277. boost::asio::write(socketsPb[socket_no + 3], boost::asio::buffer(&Gamma, sizeof(Gamma)));
  278. boost::asio::read(socketsPb[socket_no + 3], boost::asio::buffer(&final_correction_word, sizeof(final_correction_word)));
  279. #endif
  280. final_correction_word = Gamma;
  281. } // create_dpfs
  282. inline void evaluate_dpfs( size_t db_nitems, dpfP2 dpfinstance, const AES_KEY& prgkey, const size_t from, const size_t to,
  283. __m128i * output, int8_t * _t, bool party, size_t ind)
  284. {
  285. const size_t bits_per_leaf = std::is_same<leaf_t, bool>::value ? 1 : sizeof(leaf_t) * CHAR_BIT;
  286. const bool is_packed = (sizeof(leaf_t) < sizeof(node_t));
  287. const size_t nodes_per_leaf = is_packed ? 1 : std::ceil(static_cast<double>(bits_per_leaf) / (sizeof(node_t) * CHAR_BIT));
  288. const size_t depth = std::ceil(std::log2(db_nitems));
  289. const size_t nbits = std::ceil(std::log2(db_nitems));
  290. const size_t nodes_in_interval = db_nitems-1;
  291. __m128i root = dpfinstance.root;
  292. __m128i * CW = (__m128i *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(__m128i));
  293. uint8_t * cwt_L = (uint8_t *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(uint8_t));
  294. uint8_t * cwt_R = (uint8_t *) std::aligned_alloc(sizeof(__m256i), depth * sizeof(uint8_t));
  295. for(size_t j = 0; j < depth; ++j)
  296. {
  297. CW[j] = dpfinstance.CW[j];
  298. cwt_L[j] = dpfinstance.cwt_L[j];
  299. cwt_R[j] = dpfinstance.cwt_R[j];
  300. }
  301. root = set_lsb(root, party);
  302. const size_t from_node = std::floor(static_cast<double>(from) / nodes_per_leaf);
  303. __m128i * s[2] = {
  304. reinterpret_cast<__m128i *>(output) + nodes_in_interval * (nodes_per_leaf - 1),
  305. s[0] + nodes_in_interval / 2
  306. };
  307. int8_t * t[2] = { _t, _t + nodes_in_interval / 2};
  308. int curlayer = depth % 2;
  309. s[curlayer][0] = root;
  310. t[curlayer][0] = get_lsb(root, 0b01);
  311. #ifdef VERBOSE
  312. if(ind == 0)
  313. {
  314. std::cout << "root = " << root[0] << " " << root[1] << std::endl;
  315. std::cout << "t[curlayer][0] " << (int) t[curlayer][0] << std::endl;
  316. }
  317. #endif
  318. for (size_t layer = 0; layer < depth; ++layer)
  319. {
  320. #ifdef VERBOSE
  321. printf("layer = %zu\n", layer);
  322. #endif
  323. curlayer = 1-curlayer;
  324. size_t i=0, j=0;
  325. auto nextbit = (from_node >> (nbits-layer-1)) & 1;
  326. size_t nodes_in_prev_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer)));
  327. size_t nodes_in_cur_layer = std::ceil(static_cast<double>(nodes_in_interval) / (1ULL << (depth-layer-1)));
  328. for (i = nextbit, j = nextbit; j < nodes_in_prev_layer-1; ++j, i+=2)
  329. {
  330. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  331. }
  332. if (nodes_in_prev_layer > j)
  333. {
  334. if (i < nodes_in_cur_layer - 1)
  335. {
  336. traverse(prgkey, s[1-curlayer][j], &s[curlayer][i]);
  337. }
  338. }
  339. #ifdef VERBOSE
  340. if(ind == 0)
  341. {
  342. std::cout << "CW reconstruction = " << CW[layer][0] << " " << CW[layer][1] << std::endl;
  343. std::cout << " cwt_L = " << (int) cwt_L[layer] << std::endl;
  344. std::cout << " cwt_R = " << (int) cwt_R[layer] << std::endl;
  345. }
  346. #endif
  347. for(size_t j = 0; j < nodes_in_prev_layer; ++j)
  348. {
  349. t[curlayer][2*j] = get_lsb(s[curlayer][2*j]) ^ (cwt_L[layer] & t[1-curlayer][j]);
  350. s[curlayer][2*j] = clear_lsb(xor_if(s[curlayer][2*j], CW[layer], !t[1-curlayer][j]), 0b11);
  351. t[curlayer][(2*j)+1] = get_lsb(s[curlayer][(2*j)+1]) ^ (cwt_R[layer] & t[1-curlayer][j]);
  352. s[curlayer][(2*j)+1] = clear_lsb(xor_if(s[curlayer][(2*j)+1], CW[layer], !t[1-curlayer][j]), 0b11);
  353. }
  354. }
  355. } // evaluate_dpfs