duoram-utils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. enum step
  2. {
  3. write_out = 0,
  4. num_steps
  5. };
  6. size_t duoram_progress[step::num_steps] = { 0 };
  7. const size_t number_of_writes = 2;
  8. typedef int64_t DB_t;
  9. struct DuORAM_Write
  10. {
  11. size_t shift;
  12. DB_t CW;
  13. };
  14. size_t target_index = 2;
  15. size_t bytes_written2 =0;
  16. DB_t final_cw;
  17. DB_t * DB;
  18. DB_t * updated_DB;
  19. DB_t * blinds;
  20. DB_t * updated_blinds;
  21. DB_t * blinded_DB;
  22. DB_t * blinded_DB_recv;
  23. DB_t * updated_blinded_DB_recv;
  24. DB_t M;
  25. DB_t * reading_temp;
  26. DB_t distinguised_value[number_of_writes];
  27. DB_t * b;
  28. DB_t * c;
  29. DB_t * d;
  30. int8_t * reading_b;
  31. int8_t * reading_c;
  32. int8_t * reading_d;
  33. int8_t * writing_b;
  34. int8_t * writing_c;
  35. int8_t * writing_d;
  36. void setup(DB_t * DB, DB_t * updated_DB, DB_t * blinded_DB_recv, DB_t * blinds, DB_t * updated_blinds, DB_t * updated_blinded_DB_recv, size_t db_nitems, bool party)
  37. {
  38. for(size_t j = 0; j < db_nitems; ++j)
  39. {
  40. DB[j] = 0;
  41. updated_DB[j] = 0;
  42. blinded_DB_recv[j] = 0;
  43. }
  44. for(size_t j = 0; j < db_nitems; ++j)
  45. {
  46. blinds[j] = 0;
  47. updated_blinds[j] = blinds[j];
  48. updated_blinded_DB_recv[j] = blinded_DB_recv[j];
  49. }
  50. }
  51. void debug_(tcp::socket& in2, tcp::socket& sb, size_t db_nitems)
  52. {
  53. for(size_t j = 0; j < db_nitems; ++j)
  54. {
  55. DB_t debug_blinds2;
  56. boost::asio::read(in2, boost::asio::buffer(&debug_blinds2, sizeof(debug_blinds2)));
  57. assert(blinds[j] == debug_blinds2);
  58. }
  59. for(size_t jj = 0; jj < db_nitems; ++jj)
  60. {
  61. DB_t debug_refresh;
  62. boost::asio::write(sb, boost::asio::buffer(&updated_blinded_DB_recv[jj], sizeof(updated_blinded_DB_recv[jj])));
  63. boost::asio::read(sb, boost::asio::buffer(&debug_refresh, sizeof(debug_refresh)));
  64. assert(debug_refresh == DB[jj] + blinds[jj]);
  65. }
  66. DB_t DB_out;
  67. boost::asio::write(sb, boost::asio::buffer(&DB[target_index], sizeof(DB[target_index])));
  68. boost::asio::read(sb, boost::asio::buffer(&DB_out, sizeof(DB_out)));
  69. DB_out = DB_out + DB[target_index];
  70. std::cout << "DB_out = " << DB_out << std::endl;
  71. }
  72. DB_t print_reconstruction(tcp::socket& sb, DB_t output)
  73. {
  74. DB_t out_reconstruction;
  75. boost::asio::write(sb, boost::asio::buffer(&output, sizeof(output)));
  76. boost::asio::read(sb, boost::asio::buffer(&out_reconstruction, sizeof(out_reconstruction)));
  77. out_reconstruction = out_reconstruction + output;
  78. return out_reconstruction;
  79. }
  80. int read_database_shares(bool party, size_t db_nitems)
  81. {
  82. if(party)
  83. {
  84. int const in { open( "DB1", O_RDONLY ) };
  85. size_t r = read(in, DB, db_nitems * sizeof(DB_t));
  86. if(r < 0) {
  87. perror("Read error");
  88. close(in);
  89. return 1;
  90. }
  91. }
  92. if(!party)
  93. {
  94. int const in { open( "DB0", O_RDONLY ) };
  95. size_t r = read(in, DB, db_nitems * sizeof(DB_t));
  96. if(r < 0) {
  97. perror("Read error");
  98. close(in);
  99. return 1;
  100. }
  101. }
  102. return 0;
  103. }
  104. void generate_random_distinguished_points(bool party)
  105. {
  106. if(party)
  107. {
  108. for(size_t j = 0; j < number_of_writes; ++j)
  109. {
  110. distinguised_value[j] = j + 2;
  111. }
  112. }
  113. if(!party)
  114. {
  115. for(size_t j = 0; j < number_of_writes; ++j)
  116. {
  117. distinguised_value[j] = j + 2;
  118. }
  119. }
  120. }
  121. DB_t dot_product_with_bool(DB_t D[], int8_t flags[], size_t db_nitems, size_t rotate_by = 0)
  122. {
  123. DB_t result = 0;
  124. for(size_t j = 0; j < db_nitems; ++j)
  125. {
  126. result = result + (D[(j + rotate_by) % db_nitems] * flags[j]);
  127. }
  128. return result;
  129. }