dpfgen.h 14 KB

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