dpfgen.h 12 KB

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