duoram-write.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // void write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[], DB_t final_CW, bool party, size_t db_nitems)
  2. // {
  3. // //DB_t tmp = 0;
  4. // if(!party) final_CW = -final_CW;
  5. // for(size_t j = 0; j < db_nitems; ++j)
  6. // {
  7. // // if(flags[j] != 0)
  8. // // {
  9. // // tmp = final_CW;
  10. // // }
  11. // // else
  12. // // {
  13. // // tmp = 0;
  14. // // }
  15. // DB[j] = DB[j] + b[j] + ((flags[j] != 0) ? final_CW : 0);;
  16. // // if(!party) updated_DB[j] = DB[j] + b[j] - tmp;
  17. // // if(party) updated_DB[j] = DB[j] + b[j] + tmp;
  18. // }
  19. // }
  20. void write_into_the_database(DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t b[], DB_t final_CW, bool party, size_t db_nitems, size_t rotate_by = 0)
  21. {
  22. // if(!party) final_CW = -final_CW;
  23. //std::cout << "rotate_by = " << rotate_by << std::endl;
  24. // std::cout << "final_CW = " << final_CW << std::endl;
  25. for(size_t j = 0; j < db_nitems; ++j)
  26. {
  27. // DB[j] = DB[j] + b[(j + rotate_by) % db_nitems] + ((flags[(j + rotate_by) % db_nitems] != 0) ? final_CW : 0);
  28. const size_t ind = (j + rotate_by) % db_nitems;
  29. //DB_t tmp = ( flags[(j + rotate_by) % db_nitems] * final_CW);
  30. DB_t tmp = (flags[ind] * final_CW);
  31. //std::cout << j << " --->> tmp = " << tmp << " ---- " << (int)flags[(j + rotate_by) % db_nitems] << " * " << final_CW << "----> ind = " << ind << std::endl;
  32. DB[j] = DB[j] + b[ind] + tmp;
  33. updated_DB[j] = DB[j];
  34. //DB[j] = DB[j] + b[(j + rotate_by) % db_nitems] + tmp;
  35. }
  36. }
  37. void duoram_update(tcp::socket& in2, tcp::socket& sb, DB_t DB[], DB_t updated_DB[], int8_t flags[], DB_t standard_basis[], DB_t M0, DB_t alpha0, bool party, size_t db_nitems, size_t offset = 0)
  38. {
  39. DB_t tmp = M0 + alpha0;
  40. DB_t recv_final_cw;
  41. write(sb, boost::asio::buffer(&tmp, sizeof(tmp)));
  42. write(in2, boost::asio::buffer(&tmp, sizeof(tmp)));
  43. read(sb, boost::asio::buffer(&recv_final_cw, sizeof(recv_final_cw)));
  44. recv_final_cw = recv_final_cw + tmp;
  45. write_into_the_database(DB, updated_DB, flags, standard_basis, recv_final_cw, party, db_nitems, offset);
  46. }
  47. void reconstruct_database(tcp::socket& sb, DB_t DB[], const size_t db_nitems)
  48. {
  49. for(size_t j = 0; j < db_nitems; ++j)
  50. {
  51. DB_t DB_j;
  52. boost::asio::write(sb, boost::asio::buffer(&DB[j], sizeof(DB[j])));
  53. boost::asio::read(sb, boost::asio::buffer(&DB_j, sizeof(DB_j)));
  54. DB_j = DB_j + DB[j];
  55. if(DB_j != 0) std::cout << j << " -> " << DB_j << std::endl;
  56. }
  57. }
  58. auto DuoramUpdate(bool party, size_t db_nitems, size_t rotate_by_,
  59. DB_t DB[], DB_t updated_DB[], int8_t writing_b[], DB_t b[], DB_t final_CW, DB_t message,
  60. int8_t writing_c[], int8_t writing_d[], DB_t c[], DB_t d[])
  61. {
  62. // final_CW = final_CW + message;
  63. // read_flags(party, db_nitems);
  64. // rotate_by_[w] = WritePb_[w].shift + WritePb_recv[w].shift;
  65. // std::cout << "rotate_by_[w] = " << rotate_by_[w] << std::endl;
  66. write_into_the_database(DB, updated_DB, writing_b, b, final_CW, party, db_nitems, - rotate_by_);
  67. if(!party) duoram_refresh(writing_b, writing_c, writing_d, b, c, d, final_CW, party, db_nitems, -rotate_by_);
  68. if( party) duoram_refresh(writing_b, writing_d, writing_c, b, d, c, final_CW, party, db_nitems, -rotate_by_);
  69. }