duoram.tcc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 = dt.as_target;
  117. indoffset -= idx;
  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. shape.yield();
  175. }
  176. return res; // The server will always get 0
  177. }
  178. // Oblivious update to an additively shared index of Duoram memory
  179. template <typename T>
  180. typename Duoram<T>::Shape::MemRefAS
  181. &Duoram<T>::Shape::MemRefAS::operator+=(const T& M)
  182. {
  183. int player = shape.tio.player();
  184. if (player < 2) {
  185. // Computational players do this
  186. RDPFTriple dt = shape.tio.rdpftriple(shape.addr_size);
  187. // Compute the index and message offsets
  188. RegAS indoffset = idx;
  189. indoffset -= dt.as_target;
  190. auto Moffset = std::make_tuple(M, M, M);
  191. Moffset -= dt.scaled_value<T>();
  192. // Send them to the peer, and everything except the first offset
  193. // to the server
  194. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  195. shape.tio.iostream_peer() << Moffset;
  196. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  197. shape.tio.iostream_server() << std::get<1>(Moffset) <<
  198. std::get<2>(Moffset);
  199. shape.yield();
  200. // Receive the above from the peer
  201. RegAS peerindoffset;
  202. std::tuple<T,T,T> peerMoffset;
  203. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  204. shape.tio.iostream_peer() >> peerMoffset;
  205. // Reconstruct the total offsets
  206. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  207. auto Mshift = combine(Moffset, peerMoffset);
  208. // Evaluate the DPFs and add them to the database
  209. StreamEval ev(dt, -indshift, shape.tio.aes_ops());
  210. for (size_t i=0; i<shape.shape_size; ++i) {
  211. auto L = ev.next();
  212. // The values from the three DPFs
  213. auto [V0, V1, V2] = dt.scaled<T>(L) + dt.unit<T>(L) * Mshift;
  214. // References to the appropriate cells in our database, our
  215. // blind, and our copy of the peer's blinded database
  216. auto [DB, BL, PBD] = shape.get_comp(i);
  217. DB += V0;
  218. if (player == 0) {
  219. BL -= V1;
  220. PBD += V2-V0;
  221. } else {
  222. BL -= V2;
  223. PBD += V1-V0;
  224. }
  225. }
  226. } else {
  227. // The server does this
  228. RDPFPair dp = shape.tio.rdpfpair(shape.addr_size);
  229. RegAS p0indoffset, p1indoffset;
  230. std::tuple<T,T> p0Moffset, p1Moffset;
  231. // Receive the index and message offsets from the computational
  232. // players and combine them
  233. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  234. shape.tio.iostream_p0() >> p0Moffset;
  235. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  236. shape.tio.iostream_p1() >> p1Moffset;
  237. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  238. auto Mshift = combine(p0Moffset, p1Moffset);
  239. // Evaluate the DPFs and subtract them from the blinds
  240. StreamEval ev(dp, -indshift, shape.tio.aes_ops());
  241. for (size_t i=0; i<shape.shape_size; ++i) {
  242. auto L = ev.next();
  243. // The values from the two DPFs
  244. auto V = dp.scaled<T>(L) + dp.unit<T>(L) * Mshift;
  245. // shape.get_server(i) returns a pair of references to the
  246. // appropriate cells in the two blinded databases, so we can
  247. // subtract the pair directly.
  248. shape.get_server(i) -= V;
  249. }
  250. }
  251. return *this;
  252. }
  253. // Explicit read from a given index of Duoram memory
  254. template <typename T>
  255. Duoram<T>::Shape::MemRefExpl::operator T()
  256. {
  257. T res;
  258. int player = shape.tio.player();
  259. if (player < 2) {
  260. res = std::get<0>(shape.get_comp(idx));
  261. }
  262. return res; // The server will always get 0
  263. }
  264. // Explicit update to a given index of Duoram memory
  265. template <typename T>
  266. typename Duoram<T>::Shape::MemRefExpl
  267. &Duoram<T>::Shape::MemRefExpl::operator+=(const T& M)
  268. {
  269. int player = shape.tio.player();
  270. if (player < 2) {
  271. // Computational players do this
  272. // Pick a blinding factor
  273. T blind;
  274. blind.randomize();
  275. // Send the blind to the server, and the blinded value to the
  276. // peer
  277. shape.tio.iostream_server() << blind;
  278. shape.tio.iostream_peer() << (M + blind);
  279. shape.yield();
  280. // Receive the peer's blinded value
  281. T peerblinded;
  282. shape.tio.iostream_peer() >> peerblinded;
  283. // Our database, our blind, the peer's blinded database
  284. auto [ DB, BL, PBD ] = shape.get_comp(idx);
  285. DB += M;
  286. BL += blind;
  287. PBD += peerblinded;
  288. } else if (player == 2) {
  289. // The server does this
  290. // Receive the updates to the blinds
  291. T p0blind, p1blind;
  292. shape.tio.iostream_p0() >> p0blind;
  293. shape.tio.iostream_p1() >> p1blind;
  294. // The two computational parties' blinds
  295. auto [ BL0, BL1 ] = shape.get_server(idx);
  296. BL0 += p0blind;
  297. BL1 += p1blind;
  298. }
  299. return *this;
  300. }