share-conversion.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. uint64_t binary_to_decimal(std::array<uint64_t, 64> inp)
  2. {
  3. uint64_t output = 0;
  4. for(size_t j = 0; j < 64; ++j)
  5. {
  6. output += (1ULL << j) * inp[j];
  7. }
  8. return output;
  9. }
  10. void P2_xor_to_additive(std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1, size_t socket_no)
  11. {
  12. uint64_t x0, x1, y0, y1, gamma0, gamma1, alpha;
  13. arc4random_buf(&x0, sizeof(uint64_t));
  14. arc4random_buf(&x1, sizeof(uint64_t));
  15. arc4random_buf(&y0, sizeof(uint64_t));
  16. arc4random_buf(&y1, sizeof(uint64_t));
  17. arc4random_buf(&alpha, sizeof(uint64_t));
  18. gamma0 = (x0 * y1) - alpha;
  19. gamma1 = alpha;
  20. std::cout << "x0 = " << x0 << std::endl;
  21. std::cout << "x1 = " << x1 << std::endl;
  22. std::cout << "gamma0 = " << gamma0 << std::endl;
  23. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&x0, sizeof(x0)));
  24. boost::asio::write(sockets0[socket_no], boost::asio::buffer(&gamma0, sizeof(gamma0)));
  25. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&y1, sizeof(y1)));
  26. boost::asio::write(sockets1[socket_no], boost::asio::buffer(&gamma1, sizeof(gamma1)));
  27. }
  28. int64_t xor_to_additive(bool party, uint8_t ** target_share_read, std::vector<socket_t>& socketsPb, std::vector<socket_t>& socketsP2)
  29. {
  30. const size_t logn = 64;
  31. std::array<uint64_t, logn> b;
  32. std::array<uint64_t, logn> b_blinded;
  33. std::array<uint64_t, logn> b_recv;
  34. for(size_t j = 0; j < logn; ++j)
  35. {
  36. // arc4random_buf(&b[j], sizeof(b[j]));
  37. // b[j] = b[j] % 2;
  38. b[j] = target_share_read[0][j];
  39. #ifdef DEBUG
  40. uint8_t target_bit_rec;
  41. boost::asio::write(socketsPb[0], boost::asio::buffer(&target_share_read[0][j], sizeof(uint8_t)));
  42. boost::asio::read(socketsPb[0], boost::asio::buffer(&target_bit_rec, sizeof(uint8_t)));
  43. if(target_bit_rec != target_share_read[0][j]) std::cout << "XOR---->>>> " << j << std::endl;
  44. #endif
  45. }
  46. #ifdef DEBUG
  47. uint64_t b_ = binary_to_decimal(b);
  48. std::cout << "b_ = " << b_ << std::endl;
  49. #endif
  50. //std::array<uint64_t, logn> c;
  51. std::array<uint64_t, logn> c_mul;
  52. std::array<uint64_t, logn> d;
  53. boost::asio::write(socketsPb[0], boost::asio::buffer(&b, logn * sizeof(b[0])));
  54. boost::asio::read(socketsPb[0], boost::asio::buffer(&b_recv, logn * sizeof(b[0])));
  55. uint64_t BLIND, Gamma;
  56. boost::asio::read(socketsP2[0], boost::asio::buffer(&BLIND, sizeof(uint64_t)));
  57. boost::asio::read(socketsP2[0], boost::asio::buffer(&Gamma, sizeof(uint64_t)));
  58. for(size_t j = 0; j < logn; ++j)
  59. {
  60. b_blinded[j] = b[j] + BLIND;
  61. }
  62. //for(size_t j = 0; j < logn; ++j)
  63. {
  64. boost::asio::write(socketsPb[0], boost::asio::buffer(&b_blinded, logn * sizeof(b_blinded[0])));
  65. boost::asio::read (socketsPb[0], boost::asio::buffer(&b_recv, logn * sizeof(b_recv[0])));
  66. }
  67. #ifdef DEBUG
  68. std::cout << "BLIND = " << BLIND << std::endl;
  69. std::cout << "Gamma = " << Gamma << std::endl;
  70. #endif
  71. if(!party)
  72. {
  73. for(size_t j = 0; j < logn; ++j)
  74. {
  75. #ifdef DEBUG
  76. if(j == 0)
  77. {
  78. std::cout << "b_recv[j] = " << b_recv[j] << std::endl;
  79. std::cout << "b[j] = " << b[j] << std::endl;
  80. }
  81. #endif
  82. c_mul[j] = (b[j] * b_recv[j]) + Gamma;
  83. }
  84. }
  85. if(party)
  86. {
  87. for(size_t j = 0; j < logn; ++j)
  88. {
  89. #ifdef DEBUG
  90. if(j == 0)
  91. {
  92. std::cout << "BLIND = " << BLIND << std::endl;
  93. std::cout << "b_blinded[j] = " << b_blinded[j] << std::endl;
  94. }
  95. #endif
  96. c_mul[j] = -(BLIND * b_recv[j]) + Gamma;
  97. }
  98. }
  99. #ifdef DEBUG
  100. for(size_t j = 0; j < 1; ++j)
  101. {
  102. std::cout << "b = " << b[j] << std::endl;
  103. uint64_t mul_Rec = 0;
  104. boost::asio::write(socketsPb[0], boost::asio::buffer(&c_mul[j], sizeof(c_mul[j])));
  105. boost::asio::read(socketsPb[0], boost::asio::buffer(&mul_Rec, sizeof(mul_Rec)));
  106. std::cout << "c_mul = " << c_mul[j] << std::endl;
  107. mul_Rec = mul_Rec + c_mul[j];
  108. std::cout << "mul_Rec = " << mul_Rec << std::endl;
  109. }
  110. #endif
  111. for(size_t j = 0; j < logn; ++j)
  112. {
  113. d[j] = (b[j] - 2 * c_mul[j]);
  114. }
  115. #ifdef DEBUG
  116. std::array<uint64_t, logn> b_reconstruction_;
  117. std::array<uint64_t, logn> d_reconstruction_;
  118. std::array<uint64_t, logn> d_recv;
  119. for(size_t j = 0; j < logn; ++j)
  120. {
  121. boost::asio::write(socketsPb[0], boost::asio::buffer(&d[j], sizeof(d[j])));
  122. boost::asio::read(socketsPb[0], boost::asio::buffer(&d_recv[j], sizeof(d_recv[j])));
  123. }
  124. boost::asio::write(socketsPb[0], boost::asio::buffer(&b, logn * sizeof(b[0])));
  125. boost::asio::read (socketsPb[0], boost::asio::buffer(&b_recv, logn * sizeof(b_recv[0])));
  126. for(size_t j = 0; j < logn; ++j)
  127. {
  128. uint64_t d_reconstruction = d[j] + d_recv[j];
  129. d_reconstruction_[j] = d_reconstruction;
  130. uint64_t b_reconstruction = b[j] ^ b_recv[j];
  131. b_reconstruction_[j] = b_reconstruction;
  132. assert(d_reconstruction == b_reconstruction);
  133. //std::cout << d_reconstruction << " <----> " << d[j] << std::endl;
  134. //std::cout << d_reconstruction << " <-> " << b_reconstruction << std::endl;
  135. }
  136. uint64_t b_value = binary_to_decimal(b_reconstruction_);
  137. std::cout << "b_value = " << b_value << std::endl;
  138. std::cout << "logn = " << logn << std::endl;
  139. #endif
  140. uint64_t R_share = 0;
  141. for(size_t j = 0; j < logn; ++j)
  142. {
  143. R_share += (1ULL << j) * d[j];
  144. }
  145. #ifdef DEBUG
  146. std::cout << "R_share = " << R_share << std::endl;
  147. R_share = binary_to_decimal(d);
  148. std::cout << "R_share = " << R_share << std::endl;
  149. uint64_t R_share_reconstruction;
  150. boost::asio::write(socketsPb[0], boost::asio::buffer(&R_share, sizeof(R_share)));
  151. boost::asio::read(socketsPb[0], boost::asio::buffer(&R_share_reconstruction, sizeof(R_share_reconstruction)));
  152. R_share_reconstruction = R_share_reconstruction + R_share;
  153. std::cout << "R_share_reconstruction = " << R_share_reconstruction << std::endl;
  154. std::cout << "b_value = " << b_value << std::endl;
  155. std::cout << "d_recons = " << binary_to_decimal(d_reconstruction_)<< std::endl;
  156. #endif
  157. return R_share;
  158. }
  159. void convert_shares(size_t i, __m128i ** output, int8_t ** flags, size_t n_threads, size_t db_nitems, __m128i * final_correction_word,
  160. int64_t ** leaves, int64_t ** leafbits,
  161. tcp::socket& sb, tcp::socket& s2, bool party)
  162. {
  163. #ifdef DEBUG
  164. std::cout << "share conversion " << i << "-th, thread started runing" << std::endl << std::endl;
  165. #endif
  166. for(size_t j = 0; j < db_nitems; ++j)
  167. {
  168. if(party)
  169. {
  170. output[i][j] = -output[i][j];
  171. flags[i][j] = -flags[i][j];
  172. }
  173. }
  174. int64_t pm = 0;
  175. int64_t rb;
  176. arc4random_buf(&rb, sizeof(rb));
  177. for(size_t j = 0; j < db_nitems; ++j)
  178. {
  179. if(party)
  180. {
  181. if(flags[i][j] != 0) pm -= 1;
  182. }
  183. if(!party)
  184. {
  185. if(flags[i][j] != 0) pm += 1;
  186. }
  187. }
  188. //int64_t rb_prime = du_attalah_Pb(rb, pm, s2, sb);
  189. int64_t FCWshare = du_attalah_Pb(final_correction_word[i][1], pm, s2, sb);
  190. FCWshare+=rb;
  191. int64_t FCWshare_reconstruction;
  192. boost::asio::write(sb, boost::asio::buffer(&FCWshare, sizeof(FCWshare)));
  193. boost::asio::read(sb, boost::asio::buffer(&FCWshare_reconstruction, sizeof(FCWshare_reconstruction)));
  194. FCWshare_reconstruction = FCWshare_reconstruction + FCWshare;
  195. int64_t PM = pm + rb;
  196. int64_t PM_recv;
  197. boost::asio::write(sb, boost::asio::buffer(&PM, sizeof(PM)));
  198. boost::asio::read(sb, boost::asio::buffer(&PM_recv, sizeof(PM_recv)));
  199. int64_t * flags_ = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  200. int64_t * outs_ = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  201. for(size_t j = 0; j < db_nitems; ++j)
  202. {
  203. outs_[j] = output[0][j][0];
  204. leaves[i][j] = output[i][j][0];
  205. #ifdef DEBUG
  206. int64_t out_rec;
  207. boost::asio::write(sb, boost::asio::buffer(&outs_[j], sizeof(outs_[j])));
  208. boost::asio::read(sb, boost::asio::buffer(&out_rec, sizeof(out_rec)));
  209. out_rec = out_rec + outs_[j];
  210. if(out_rec != 0) std::cout << j << "-> " << out_rec << std::endl;
  211. #endif
  212. flags_[j] = (flags[i][j] * pm) + (flags[i][j] * PM_recv) + (flags[i][j] * rb);
  213. flags_[j] += output[i][j][1];
  214. if(!party)
  215. {
  216. flags_[j] -= (flags[i][j] * FCWshare_reconstruction);
  217. }
  218. if(party)
  219. {
  220. flags_[j] -= (flags[i][j] * FCWshare_reconstruction);
  221. }
  222. #ifdef DEBUG
  223. int64_t flags_rec;
  224. boost::asio::write(sb, boost::asio::buffer(&flags_[j], sizeof(flags_[j])));
  225. boost::asio::read(sb, boost::asio::buffer(&flags_rec, sizeof(flags_rec)));
  226. flags_rec = flags_rec + flags_[j];
  227. if(flags_rec != 0)
  228. {
  229. std::cout << j << " ---> Flag Reconstruction = " << flags_rec << std::endl;
  230. }
  231. #endif
  232. flags[i][j] = flags_[j];
  233. if(flags[i][j] == 128 || flags[i][j] == -128) flags[i][j] = 0;
  234. leafbits[i][j] = flags[i][j];
  235. #ifdef DEBUG
  236. int8_t flags_rec2;
  237. boost::asio::write(sb, boost::asio::buffer(&flags[i][j], sizeof(flags[i][j])));
  238. boost::asio::read(sb, boost::asio::buffer(&flags_rec2, sizeof(flags_rec2)));
  239. flags_rec2 = flags_rec2 + flags[i][j];
  240. if(flags_rec2 != 0)
  241. {
  242. std::cout << j << " ---> Flag Reconstruction = " << (int) flags_rec2 << std::endl;
  243. if(flags_rec2 != 1) std::cout << (int) flags[i][j] << "-> " << flags_[j] << std::endl;
  244. }
  245. #endif
  246. }
  247. write_evalfull_outs_into_a_file(party, i, db_nitems, flags, leaves[0], final_correction_word);
  248. }