share-conversion.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //template<size_t N>
  2. int64_t binary_to_decimal(int64_t* inp, size_t logn)
  3. {
  4. int64_t output = 0;
  5. for(size_t j = 0; j < logn; ++j)
  6. {
  7. output += (1ULL << j) * inp[j];
  8. }
  9. return output;
  10. }
  11. struct shareconversion
  12. {
  13. int64_t PM, PM_recv, rb, FCWshare_reconstruction;
  14. };
  15. struct shareconversion_Pb
  16. {
  17. int64_t PM, FCW;
  18. };
  19. /**
  20. * @brief This function is used to convert the XOR-ed shared flag bits into additive shares of the same
  21. *
  22. * @param sockets0
  23. * @param sockets1
  24. * @param socket_no
  25. */
  26. void convert_sharesP2 ( size_t db_nitems, __m128i** output0, int8_t ** flags0, __m128i** output1, int8_t ** flags1,
  27. int64_t ** leaves0, int64_t ** leafbits0, int64_t ** leaves1, int64_t ** leafbits1,
  28. std::vector<socket_t>& sockets0, std::vector<socket_t>& sockets1, size_t i, size_t socket_no = 0)
  29. {
  30. du_attalah_P2(sockets0, sockets1, socket_no);
  31. shareconversion shareconversionP0, shareconversionP1;
  32. boost::asio::read(sockets0[i], boost::asio::buffer(&shareconversionP0, sizeof(shareconversion)));
  33. boost::asio::read(sockets1[i], boost::asio::buffer(&shareconversionP1, sizeof(shareconversion)));
  34. for(size_t j = 0; j < db_nitems; ++j)
  35. {
  36. output1[i][j] = -output1[i][j];
  37. flags1[i][j] = -flags1[i][j];
  38. }
  39. int64_t * flags0_ = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  40. int64_t * flags1_ = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  41. for(size_t j = 0; j < db_nitems; ++j)
  42. {
  43. leaves0[i][j] = output0[i][j][0];
  44. leaves1[i][j] = output1[i][j][0];
  45. flags0_[j] = (flags0[i][j] * shareconversionP0.PM) + (flags0[i][j] * shareconversionP0.PM_recv) + (flags0[i][j] * shareconversionP0.rb);
  46. flags0_[j] += output0[i][j][1];
  47. flags1_[j] = (flags1[i][j] * shareconversionP1.PM) + (flags1[i][j] * shareconversionP1.PM_recv) + (flags1[i][j] * shareconversionP1.rb);
  48. flags1_[j] += output1[i][j][1];
  49. flags0_[j] -= (flags0[i][j] * shareconversionP0.FCWshare_reconstruction);
  50. flags1_[j] -= (flags1[i][j] * shareconversionP1.FCWshare_reconstruction);
  51. flags0[i][j] = flags0_[j];
  52. flags1[i][j] = flags1_[j];
  53. if(flags0[i][j] == 128 || flags0[i][j] == -128) flags0[i][j] = 0;
  54. leafbits0[i][j] = flags0[i][j];
  55. if(flags1[i][j] == 128 || flags1[i][j] == -128) flags1[i][j] = 0;
  56. leafbits1[i][j] = flags1[i][j];
  57. }
  58. std::free(flags0_);
  59. std::free(flags1_);
  60. #ifdef VERBOSE
  61. for(size_t j = 0; j < db_nitems; ++j)
  62. {
  63. int64_t leafbit_reconstruction = leafbits0[i][j] + leafbits1[i][j];
  64. if(leafbit_reconstruction != 0) std::cout << std::dec << j << ":-> " << leafbit_reconstruction << std::endl;
  65. }
  66. #endif
  67. }
  68. void P2_xor_to_additive(tcp::socket& s0, tcp::socket& s1, size_t socket_no)
  69. {
  70. uint64_t x0, x1, y0, y1, gamma0, gamma1, alpha;
  71. arc4random_buf(&x0, sizeof(uint64_t));
  72. arc4random_buf(&x1, sizeof(uint64_t));
  73. arc4random_buf(&y0, sizeof(uint64_t));
  74. arc4random_buf(&y1, sizeof(uint64_t));
  75. arc4random_buf(&alpha, sizeof(uint64_t));
  76. gamma0 = (x0 * y1) - alpha;
  77. gamma1 = alpha;
  78. #ifdef VERBOSE
  79. std::cout << "x0 = " << x0 << std::endl;
  80. std::cout << "x1 = " << x1 << std::endl;
  81. std::cout << "gamma0 = " << gamma0 << std::endl;
  82. #endif
  83. boost::asio::write(s0, boost::asio::buffer(&x0, sizeof(x0)));
  84. boost::asio::write(s0, boost::asio::buffer(&gamma0, sizeof(gamma0)));
  85. boost::asio::write(s1, boost::asio::buffer(&y1, sizeof(y1)));
  86. boost::asio::write(s1, boost::asio::buffer(&gamma1, sizeof(gamma1)));
  87. }
  88. void xor_to_additive(bool party, uint8_t * target_share_read, tcp::socket& sb, tcp::socket& s2, const size_t height, int64_t& R_share)
  89. {
  90. const size_t logn = height;
  91. //std::array<uint64_t, logn> b, b_blinded, b_recv;
  92. int64_t b[64], b_blinded[64], b_recv[64];
  93. for(size_t j = 0; j < logn; ++j)
  94. {
  95. b[j] = target_share_read[logn-j - 1];
  96. #ifdef DEBUG
  97. uint8_t target_bit_rec;
  98. boost::asio::write(sb, boost::asio::buffer(&target_share_read[j], sizeof(uint8_t)));
  99. boost::asio::read(sb, boost::asio::buffer(&target_bit_rec, sizeof(uint8_t)));
  100. if(target_bit_rec != target_share_read[j]) std::cout << "non-zero XOR index = " << j << std::endl;
  101. #endif
  102. }
  103. #ifdef DEBUG
  104. uint64_t b_ = binary_to_decimal(b, logn);;
  105. std::cout << "b_ = " << b_ << std::endl;
  106. #endif
  107. // std::array<uint64_t, logn> c_mul;
  108. // std::array<uint64_t, logn> d;
  109. //uint64_t c_mul[logn], d[logn];
  110. int64_t * c_mul = (int64_t*) malloc(logn * sizeof(int64_t));
  111. int64_t * d = (int64_t*) malloc(logn * sizeof(int64_t));
  112. int64_t BLIND, Gamma;
  113. boost::asio::read(s2, boost::asio::buffer(&BLIND, sizeof(int64_t)));
  114. boost::asio::read(s2, boost::asio::buffer(&Gamma, sizeof(int64_t)));
  115. for(size_t j = 0; j < logn; ++j)
  116. {
  117. b_blinded[j] = b[j] + BLIND;
  118. }
  119. boost::asio::write(sb, boost::asio::buffer(&b_blinded, logn * sizeof(b_blinded[0])));
  120. boost::asio::read (sb, boost::asio::buffer(&b_recv, logn * sizeof(b_recv[0])));
  121. #ifdef DEBUG
  122. std::cout << "BLIND = " << BLIND << std::endl;
  123. std::cout << "Gamma = " << Gamma << std::endl;
  124. #endif
  125. // uint64_t R_share = 0;
  126. if(!party)
  127. {
  128. for(size_t j = 0; j < logn; ++j)
  129. {
  130. c_mul[j] = (b[j] * b_recv[j]) + Gamma;
  131. d[j] = (b[j] - 2 * c_mul[j]);
  132. R_share += (1ULL << j) * d[j];
  133. }
  134. }
  135. if(party)
  136. {
  137. for(size_t j = 0; j < logn; ++j)
  138. {
  139. c_mul[j] = -(BLIND * b_recv[j]) + Gamma;
  140. d[j] = (b[j] - 2 * c_mul[j]);
  141. R_share += (1ULL << j) * d[j];
  142. }
  143. }
  144. #ifdef DEBUG
  145. for(size_t j = 0; j < 1; ++j)
  146. {
  147. std::cout << "b = " << b[j] << std::endl;
  148. int64_t mul_Rec = 0;
  149. boost::asio::write(sb, boost::asio::buffer(&c_mul[j], sizeof(c_mul[j])));
  150. boost::asio::read(sb, boost::asio::buffer(&mul_Rec, sizeof(mul_Rec)));
  151. std::cout << "c_mul = " << c_mul[j] << std::endl;
  152. mul_Rec = mul_Rec + c_mul[j];
  153. std::cout << "mul_Rec = " << mul_Rec << std::endl;
  154. }
  155. //std::array<uint64_t, logn> b_reconstruction_;
  156. //std::array<uint64_t, logn> d_reconstruction_;
  157. // uint64_t b_reconstruction_[logn], d_reconstruction_[logn], d_recv[logn];
  158. int64_t * b_reconstruction_ = (int64_t*) malloc(logn * sizeof(int64_t));
  159. int64_t * d_reconstruction_ = (int64_t*) malloc(logn * sizeof(int64_t));
  160. int64_t * d_recv = (int64_t*) malloc(logn * sizeof(int64_t));
  161. //std::array<uint64_t, logn> d_recv;
  162. for(size_t j = 0; j < logn; ++j)
  163. {
  164. boost::asio::write(sb, boost::asio::buffer(&d[j], sizeof(d[j])));
  165. boost::asio::read(sb, boost::asio::buffer(&d_recv[j], sizeof(d_recv[j])));
  166. }
  167. boost::asio::write(sb, boost::asio::buffer(&b, logn * sizeof(b[0])));
  168. boost::asio::read (sb, boost::asio::buffer(&b_recv, logn * sizeof(b_recv[0])));
  169. for(size_t j = 0; j < logn; ++j)
  170. {
  171. int64_t d_reconstruction = d[j] + d_recv[j];
  172. d_reconstruction_[j] = d_reconstruction;
  173. int64_t b_reconstruction = b[j] ^ b_recv[j];
  174. b_reconstruction_[j] = b_reconstruction;
  175. assert(d_reconstruction == b_reconstruction);
  176. }
  177. int64_t b_value = binary_to_decimal(b_reconstruction_, logn);
  178. std::cout << "b_value = " << b_value << std::endl;
  179. std::cout << "logn = " << logn << std::endl;
  180. std::cout << "R_share = " << R_share << std::endl;
  181. #endif
  182. R_share = binary_to_decimal(d, logn);
  183. #ifdef DEBUG
  184. std::cout << "R_share = " << R_share << std::endl;
  185. int64_t R_share_reconstruction;
  186. boost::asio::write(sb, boost::asio::buffer(&R_share, sizeof(R_share)));
  187. boost::asio::read(sb, boost::asio::buffer(&R_share_reconstruction, sizeof(R_share_reconstruction)));
  188. R_share_reconstruction = R_share_reconstruction + R_share;
  189. std::cout << "R_share_reconstruction = " << R_share_reconstruction << std::endl;
  190. std::cout << "b_value = " << b_value << std::endl;
  191. std::cout << "d_recons = " << binary_to_decimal(d_reconstruction_, logn) << std::endl;
  192. std::free(b_reconstruction_);
  193. std::free(d_reconstruction_);
  194. std::free(d_recv);
  195. #endif
  196. std::free(c_mul);
  197. std::free(d);
  198. // return R_share;
  199. }
  200. void convert_shares(size_t i, __m128i ** output, int8_t ** flags, size_t n_threads, size_t db_nitems, __m128i * final_correction_word,
  201. int64_t ** leaves, int64_t ** leafbits, std::vector<socket_t>& socketsb, std::vector<socket_t>& sockets2, bool party)
  202. {
  203. #ifdef DEBUG
  204. std::cout << "share conversion " << i << "-th, thread started runing" << std::endl << std::endl;
  205. #endif
  206. for(size_t j = 0; j < db_nitems; ++j)
  207. {
  208. if(party)
  209. {
  210. output[i][j] = -output[i][j];
  211. flags[i][j] = -flags[i][j];
  212. }
  213. }
  214. int64_t pm = 0;
  215. int64_t rb;
  216. arc4random_buf(&rb, sizeof(rb));
  217. for(size_t j = 0; j < db_nitems; ++j)
  218. {
  219. if(party)
  220. {
  221. if(flags[i][j] != 0) pm -= 1;
  222. }
  223. if(!party)
  224. {
  225. if(flags[i][j] != 0) pm += 1;
  226. }
  227. }
  228. int64_t FCWshare = du_attalah_Pb(final_correction_word[i][1], pm, sockets2[i], socketsb[i]);
  229. //FCWshare+=rb;
  230. //int64_t FCWshare_reconstruction;
  231. shareconversion_Pb share_b, share_b_recv;
  232. // boost::asio::write(socketsb[i], boost::asio::buffer(&FCWshare, sizeof(FCWshare)));
  233. // boost::asio::read(socketsb[i], boost::asio::buffer(&FCWshare_reconstruction, sizeof(FCWshare_reconstruction)));
  234. // FCWshare_reconstruction = FCWshare_reconstruction + FCWshare;
  235. // int64_t PM = pm + rb;
  236. share_b.PM = pm + rb;// PM;
  237. share_b.FCW = FCWshare+=rb;
  238. boost::asio::write(socketsb[i], boost::asio::buffer(&share_b, sizeof(share_b)));
  239. boost::asio::read(socketsb[i], boost::asio::buffer(&share_b_recv, sizeof(share_b_recv)));
  240. share_b_recv.FCW = share_b_recv.FCW + share_b.FCW;
  241. // int64_t PM_recv;
  242. // boost::asio::write(socketsb[i], boost::asio::buffer(&PM, sizeof(PM))); //Sending the blinded shares of +/- 1
  243. // boost::asio::read(socketsb[i], boost::asio::buffer(&PM_recv, sizeof(PM_recv))); //Receiving the blinded shares of +/- 1
  244. int64_t * flags_ = (int64_t *)std::aligned_alloc(sizeof(node_t), db_nitems * sizeof(int64_t));
  245. shareconversion P2_shareconversion;
  246. P2_shareconversion.PM = pm;
  247. P2_shareconversion.PM_recv = share_b_recv.PM;// PM_recv;
  248. P2_shareconversion.rb = rb;
  249. P2_shareconversion.FCWshare_reconstruction = share_b_recv.FCW; //FCWshare_reconstruction;
  250. boost::asio::write(sockets2[i], boost::asio::buffer(&P2_shareconversion, sizeof(shareconversion)));
  251. for(size_t j = 0; j < db_nitems; ++j)
  252. {
  253. leaves[i][j] = output[i][j][0];
  254. flags_[j] = (flags[i][j] * pm) + (flags[i][j] * share_b_recv.PM) + (flags[i][j] * rb);
  255. flags_[j] += output[i][j][1];
  256. flags_[j] -= (flags[i][j] * P2_shareconversion.FCWshare_reconstruction);
  257. #ifdef DEBUG
  258. int64_t flags_rec;
  259. boost::asio::write(socketsb[i], boost::asio::buffer(&flags_[j], sizeof(flags_[j])));
  260. boost::asio::read(socketsb[i], boost::asio::buffer(&flags_rec, sizeof(flags_rec)));
  261. flags_rec = flags_rec + flags_[j];
  262. if(flags_rec != 0)
  263. {
  264. std::cout << j << " ---> Flag Reconstruction = " << flags_rec << std::endl;
  265. }
  266. #endif
  267. flags[i][j] = flags_[j];
  268. if(flags[i][j] == 128 || flags[i][j] == -128) flags[i][j] = 0;
  269. leafbits[i][j] = flags[i][j];
  270. #ifdef DEBUG
  271. int8_t flags_rec2;
  272. boost::asio::write(socketsb[i], boost::asio::buffer(&flags[i][j], sizeof(flags[i][j])));
  273. boost::asio::read(socketsb[i], boost::asio::buffer(&flags_rec2, sizeof(flags_rec2)));
  274. flags_rec2 = flags_rec2 + flags[i][j];
  275. int64_t flags_rec3;
  276. boost::asio::write(socketsb[i], boost::asio::buffer(&flags_[j], sizeof(flags_[j])));
  277. boost::asio::read(socketsb[i], boost::asio::buffer(&flags_rec3, sizeof(flags_rec3)));
  278. flags_rec3 = flags_rec3 + flags_[j];
  279. if(flags_rec2 != 0)
  280. {
  281. std::cout << j << " ---> Flag Reconstruction = " << (int) flags_rec2 << "----->>>>> " << flags_rec3 << std::endl;
  282. if(flags_rec2 != 1) std::cout << (int) flags[i][j] << "-> " << flags_[j] << std::endl;
  283. }
  284. #endif
  285. }
  286. // write_evalfull_outs_into_a_file(party, i, db_nitems, flags[i], leaves[i], final_correction_word[i]);
  287. }