duoram-utils.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. void reconstruct_database(tcp::socket& sb, DB_t DB[], const size_t db_nitems)
  73. {
  74. for(size_t j = 0; j < db_nitems; ++j)
  75. {
  76. DB_t DB_j;
  77. boost::asio::write(sb, boost::asio::buffer(&DB[j], sizeof(DB[j])));
  78. boost::asio::read(sb, boost::asio::buffer(&DB_j, sizeof(DB_j)));
  79. DB_j = DB_j + DB[j];
  80. if(DB_j != 0) std::cout << j << " -> " << DB_j << std::endl;
  81. }
  82. }
  83. DB_t print_reconstruction(tcp::socket& sb, DB_t output)
  84. {
  85. DB_t out_reconstruction;
  86. boost::asio::write(sb, boost::asio::buffer(&output, sizeof(output)));
  87. boost::asio::read(sb, boost::asio::buffer(&out_reconstruction, sizeof(out_reconstruction)));
  88. out_reconstruction = out_reconstruction + output;
  89. return out_reconstruction;
  90. }
  91. int read_database_shares(bool party, size_t db_nitems)
  92. {
  93. if(party)
  94. {
  95. int const in { open( "DB1", O_RDONLY ) };
  96. size_t r = read(in, DB, db_nitems * sizeof(DB_t));
  97. if(r < 0) {
  98. perror("Read error");
  99. close(in);
  100. return 1;
  101. }
  102. }
  103. if(!party)
  104. {
  105. int const in { open( "DB0", O_RDONLY ) };
  106. size_t r = read(in, DB, db_nitems * sizeof(DB_t));
  107. if(r < 0) {
  108. perror("Read error");
  109. close(in);
  110. return 1;
  111. }
  112. }
  113. return 0;
  114. }
  115. void generate_random_distinguished_points(bool party)
  116. {
  117. if(party)
  118. {
  119. for(size_t j = 0; j < number_of_writes; ++j)
  120. {
  121. distinguised_value[j] = j + 2;
  122. }
  123. }
  124. if(!party)
  125. {
  126. for(size_t j = 0; j < number_of_writes; ++j)
  127. {
  128. distinguised_value[j] = j + 2;
  129. }
  130. }
  131. }
  132. DB_t dot_product_with_bool(DB_t D[], int8_t flags[], size_t db_nitems, size_t rotate_by = 0)
  133. {
  134. DB_t result = 0;
  135. for(size_t j = 0; j < db_nitems; ++j)
  136. {
  137. result = result + (D[(j + rotate_by) % db_nitems] * flags[j]);
  138. }
  139. return result;
  140. }