duoram-utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. if(party)
  41. {
  42. DB[j] = 0;
  43. updated_DB[j] = 0;
  44. blinded_DB_recv[j] = 0;
  45. }
  46. else
  47. {
  48. DB[j] = 0;
  49. updated_DB[j] = 0;
  50. blinded_DB_recv[j] = 0;
  51. }
  52. }
  53. for(size_t j = 0; j < db_nitems; ++j)
  54. {
  55. blinds[j] = 0;
  56. updated_blinds[j] = blinds[j];
  57. updated_blinded_DB_recv[j] = blinded_DB_recv[j];
  58. }
  59. }
  60. void debug_(tcp::socket& in2, tcp::socket& sb, size_t db_nitems)
  61. {
  62. for(size_t j = 0; j < db_nitems; ++j)
  63. {
  64. DB_t debug_blinds2;
  65. boost::asio::read(in2, boost::asio::buffer(&debug_blinds2, sizeof(debug_blinds2)));
  66. assert(blinds[j] == debug_blinds2);
  67. }
  68. for(size_t jj = 0; jj < db_nitems; ++jj)
  69. {
  70. DB_t debug_refresh;
  71. boost::asio::write(sb, boost::asio::buffer(&updated_blinded_DB_recv[jj], sizeof(updated_blinded_DB_recv[jj])));
  72. boost::asio::read(sb, boost::asio::buffer(&debug_refresh, sizeof(debug_refresh)));
  73. assert(debug_refresh == DB[jj] + blinds[jj]);
  74. }
  75. DB_t DB_out;
  76. boost::asio::write(sb, boost::asio::buffer(&DB[target_index], sizeof(DB[target_index])));
  77. boost::asio::read(sb, boost::asio::buffer(&DB_out, sizeof(DB_out)));
  78. DB_out = DB_out + DB[target_index];
  79. std::cout << "DB_out = " << DB_out << std::endl;
  80. }
  81. DB_t print_reconstruction(tcp::socket& sb, DB_t output)
  82. {
  83. DB_t out_reconstruction;
  84. boost::asio::write(sb, boost::asio::buffer(&output, sizeof(output)));
  85. boost::asio::read(sb, boost::asio::buffer(&out_reconstruction, sizeof(out_reconstruction)));
  86. out_reconstruction = out_reconstruction + output;
  87. return out_reconstruction;
  88. }
  89. int read_database_shares(bool party, size_t db_nitems)
  90. {
  91. if(party)
  92. {
  93. int const in { open( "DB1", O_RDONLY ) };
  94. size_t r = read(in, DB, db_nitems * sizeof(DB_t));
  95. if(r < 0) {
  96. perror("Read error");
  97. close(in);
  98. return 1;
  99. }
  100. }
  101. if(!party)
  102. {
  103. int const in { open( "DB0", O_RDONLY ) };
  104. size_t r = read(in, DB, db_nitems * sizeof(DB_t));
  105. if(r < 0) {
  106. perror("Read error");
  107. close(in);
  108. return 1;
  109. }
  110. }
  111. return 0;
  112. }
  113. void generate_random_distinguished_points(bool party)
  114. {
  115. if(party)
  116. {
  117. for(size_t j = 0; j < number_of_writes; ++j)
  118. {
  119. distinguised_value[j] = j + 2;
  120. }
  121. }
  122. if(!party)
  123. {
  124. for(size_t j = 0; j < number_of_writes; ++j)
  125. {
  126. distinguised_value[j] = j + 2;
  127. }
  128. }
  129. }
  130. /* This function is never called*/
  131. template<typename T1>
  132. void rotate_all( T1 * orginal_vector1, T1 * rotated_vector1,
  133. T1 * orginal_vector3, T1 * rotated_vector3,
  134. T1 * orginal_vector5, T1 * rotated_vector5,
  135. size_t rotate_by, size_t db_nitems)
  136. {
  137. for(size_t j = 0; j < db_nitems; ++j)
  138. {
  139. rotated_vector1[(j + rotate_by) % db_nitems] = orginal_vector1[j];
  140. rotated_vector3[(j + rotate_by) % db_nitems] = orginal_vector3[j];
  141. rotated_vector5[(j + rotate_by) % db_nitems] = orginal_vector5[j];
  142. }
  143. }
  144. DB_t dot_product_with_bool(DB_t D[], int8_t flags[], size_t db_nitems, size_t rotate_by = 0)
  145. {
  146. DB_t result = 0;
  147. for(size_t j = 0; j < db_nitems; ++j)
  148. {
  149. result = result + (D[(j + rotate_by) % db_nitems] * flags[j]);
  150. }
  151. return result;
  152. }