duoram.tcc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // Templated method implementations for duoram.hpp
  2. #include <stdio.h>
  3. // Pass the player number and desired size
  4. template <typename T>
  5. Duoram<T>::Duoram(int player, size_t size) : player(player),
  6. oram_size(size), p0_blind(blind), p1_blind(peer_blinded_db) {
  7. if (player < 2) {
  8. database.resize(size);
  9. blind.resize(size);
  10. peer_blinded_db.resize(size);
  11. } else {
  12. p0_blind.resize(size);
  13. p1_blind.resize(size);
  14. }
  15. }
  16. // For debugging; print the contents of the Duoram to stdout
  17. template <typename T>
  18. void Duoram<T>::dump() const
  19. {
  20. for (size_t i=0; i<oram_size; ++i) {
  21. if (player < 2) {
  22. printf("%04lx %016lx %016lx %016lx\n",
  23. i, database[i].share(), blind[i].share(),
  24. peer_blinded_db[i].share());
  25. } else {
  26. printf("%04lx %016lx %016lx\n",
  27. i, p0_blind[i].share(), p1_blind[i].share());
  28. }
  29. }
  30. printf("\n");
  31. }
  32. // For debugging or checking your answers (using this in general is
  33. // of course insecure)
  34. // This one reconstructs the whole database
  35. template <typename T>
  36. std::vector<T> Duoram<T>::Shape::reconstruct() const
  37. {
  38. int player = tio.player();
  39. std::vector<T> res;
  40. res.resize(duoram.size());
  41. // Player 1 sends their share of the database to player 0
  42. if (player == 1) {
  43. tio.queue_peer(duoram.database.data(), duoram.size()*sizeof(T));
  44. } else if (player == 0) {
  45. tio.recv_peer(res.data(), duoram.size()*sizeof(T));
  46. for(size_t i=0;i<duoram.size();++i) {
  47. res[i] += duoram.database[i];
  48. }
  49. }
  50. // The server (player 2) does nothing
  51. // Players 1 and 2 will get an empty vector here
  52. return res;
  53. }
  54. // This one reconstructs a single database value
  55. template <typename T>
  56. T Duoram<T>::Shape::reconstruct(const T& share) const
  57. {
  58. int player = tio.player();
  59. T res;
  60. // Player 1 sends their share of the value to player 0
  61. if (player == 1) {
  62. tio.queue_peer(&share, sizeof(T));
  63. } else if (player == 0) {
  64. tio.recv_peer(&res, sizeof(T));
  65. res += share;
  66. }
  67. // The server (player 2) does nothing
  68. // Players 1 and 2 will get 0 here
  69. return res;
  70. }
  71. // Function to set the shape_size of a shape and compute the number of
  72. // bits you need to address a shape of that size (which is the number of
  73. // bits in sz-1). This is typically called by subclass constructors.
  74. template <typename T>
  75. void Duoram<T>::Shape::set_shape_size(size_t sz)
  76. {
  77. shape_size = sz;
  78. // Compute the number of bits in (sz-1)
  79. // But use 0 if sz=0 for some reason (though that should never
  80. // happen)
  81. if (sz > 1) {
  82. addr_size = 64-__builtin_clzll(sz-1);
  83. addr_mask = address_t((size_t(1)<<addr_size)-1);
  84. } else {
  85. addr_size = 0;
  86. addr_mask = 0;
  87. }
  88. }
  89. // Constructor for the Flat shape. len=0 means the maximum size (the
  90. // parent's size minus start).
  91. template <typename T>
  92. Duoram<T>::Flat::Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield,
  93. size_t start, size_t len) : Shape(*this, duoram, tio, yield)
  94. {
  95. size_t parentsize = duoram.size();
  96. if (start > parentsize) {
  97. start = parentsize;
  98. }
  99. this->start = start;
  100. size_t maxshapesize = parentsize - start;
  101. if (len > maxshapesize || len == 0) {
  102. len = maxshapesize;
  103. }
  104. this->set_shape_size(len);
  105. }
  106. // Oblivious read from an additively shared index of Duoram memory
  107. template <typename T>
  108. Duoram<T>::Shape::MemRefAS::operator T()
  109. {
  110. T res;
  111. int player = shape.tio.player();
  112. if (player < 2) {
  113. // Computational players do this
  114. RDPFTriple dt = shape.tio.rdpftriple(shape.addr_size);
  115. // Compute the index offset
  116. RegAS indoffset = idx;
  117. indoffset -= dt.as_target;
  118. // We only need two of the DPFs for reading
  119. RDPFPair dp(std::move(dt), 0, player == 0 ? 2 : 1);
  120. // Send it to the peer and the server
  121. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  122. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  123. shape.yield();
  124. // Receive the above from the peer
  125. RegAS peerindoffset;
  126. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  127. // Reconstruct the total offset
  128. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  129. // Evaluate the DPFs and compute the dotproducts
  130. StreamEval ev(dp, -indshift, shape.tio.aes_ops());
  131. for (size_t i=0; i<shape.shape_size; ++i) {
  132. auto L = ev.next();
  133. // The values from the two DPFs
  134. auto [V0, V1] = dp.unit<T>(L);
  135. // References to the appropriate cells in our database, our
  136. // blind, and our copy of the peer's blinded database
  137. auto [DB, BL, PBD] = shape.get_comp(i);
  138. res += (DB + PBD) * V0.share() - BL * (V1-V0).share();
  139. }
  140. // Receive the cancellation term from the server
  141. T gamma;
  142. shape.tio.iostream_server() >> gamma;
  143. res += gamma;
  144. } else {
  145. // The server does this
  146. RDPFPair dp = shape.tio.rdpfpair(shape.addr_size);
  147. RegAS p0indoffset, p1indoffset;
  148. // Receive the index offset from the computational players and
  149. // combine them
  150. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  151. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  152. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  153. // Evaluate the DPFs to compute the cancellation terms
  154. T gamma0, gamma1;
  155. StreamEval ev(dp, -indshift, shape.tio.aes_ops());
  156. for (size_t i=0; i<shape.shape_size; ++i) {
  157. auto L = ev.next();
  158. // The values from the two DPFs
  159. auto [V0, V1] = dp.unit<T>(L);
  160. // shape.get_server(i) returns a pair of references to the
  161. // appropriate cells in the two blinded databases
  162. auto [BL0, BL1] = shape.get_server(i);
  163. gamma0 -= BL0 * V1.share();
  164. gamma1 -= BL1 * V0.share();
  165. }
  166. // Choose a random blinding factor
  167. T rho;
  168. rho.randomize();
  169. gamma0 += rho;
  170. gamma1 -= rho;
  171. // Send the cancellation terms to the computational players
  172. shape.tio.iostream_p0() << gamma0;
  173. shape.tio.iostream_p1() << gamma1;
  174. }
  175. return res; // The server will always get 0
  176. }
  177. // Oblivious update to an additively shared index of Duoram memory
  178. template <typename T>
  179. typename Duoram<T>::Shape::MemRefAS
  180. &Duoram<T>::Shape::MemRefAS::operator+=(const T& M)
  181. {
  182. int player = shape.tio.player();
  183. if (player < 2) {
  184. // Computational players do this
  185. RDPFTriple dt = shape.tio.rdpftriple(shape.addr_size);
  186. // Compute the index and message offsets
  187. RegAS indoffset = idx;
  188. indoffset -= dt.as_target;
  189. auto Moffset = std::make_tuple(M, M, M);
  190. Moffset -= dt.scaled_value<T>();
  191. // Send them to the peer, and everything except the first offset
  192. // to the server
  193. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  194. shape.tio.iostream_peer() << Moffset;
  195. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  196. shape.tio.iostream_server() << std::get<1>(Moffset) <<
  197. std::get<2>(Moffset);
  198. shape.yield();
  199. // Receive the above from the peer
  200. RegAS peerindoffset;
  201. std::tuple<T,T,T> peerMoffset;
  202. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  203. shape.tio.iostream_peer() >> peerMoffset;
  204. // Reconstruct the total offsets
  205. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  206. auto Mshift = combine(Moffset, peerMoffset);
  207. // Evaluate the DPFs and add them to the database
  208. StreamEval ev(dt, -indshift, shape.tio.aes_ops());
  209. for (size_t i=0; i<shape.shape_size; ++i) {
  210. auto L = ev.next();
  211. // The values from the three DPFs
  212. auto [V0, V1, V2] = dt.scaled<T>(L) + dt.unit<T>(L) * Mshift;
  213. // References to the appropriate cells in our database, our
  214. // blind, and our copy of the peer's blinded database
  215. auto [DB, BL, PBD] = shape.get_comp(i);
  216. DB += V0;
  217. if (player == 0) {
  218. BL -= V1;
  219. PBD += V2-V0;
  220. } else {
  221. BL -= V2;
  222. PBD += V1-V0;
  223. }
  224. }
  225. } else {
  226. // The server does this
  227. RDPFPair dp = shape.tio.rdpfpair(shape.addr_size);
  228. RegAS p0indoffset, p1indoffset;
  229. std::tuple<T,T> p0Moffset, p1Moffset;
  230. // Receive the index and message offsets from the computational
  231. // players and combine them
  232. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  233. shape.tio.iostream_p0() >> p0Moffset;
  234. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  235. shape.tio.iostream_p1() >> p1Moffset;
  236. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  237. auto Mshift = combine(p0Moffset, p1Moffset);
  238. // Evaluate the DPFs and subtract them from the blinds
  239. StreamEval ev(dp, -indshift, shape.tio.aes_ops());
  240. for (size_t i=0; i<shape.shape_size; ++i) {
  241. auto L = ev.next();
  242. // The values from the two DPFs
  243. auto V = dp.scaled<T>(L) + dp.unit<T>(L) * Mshift;
  244. // shape.get_server(i) returns a pair of references to the
  245. // appropriate cells in the two blinded databases, so we can
  246. // subtract the pair directly.
  247. shape.get_server(i) -= V;
  248. }
  249. }
  250. return *this;
  251. }