duoram.tcc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. // Templated method implementations for duoram.hpp
  2. #include <stdio.h>
  3. #include "cdpf.hpp"
  4. // Pass the player number and desired size
  5. template <typename T>
  6. Duoram<T>::Duoram(int player, size_t size) : player(player),
  7. oram_size(size), p0_blind(blind), p1_blind(peer_blinded_db) {
  8. if (player < 2) {
  9. database.resize(size);
  10. blind.resize(size);
  11. peer_blinded_db.resize(size);
  12. } else {
  13. p0_blind.resize(size);
  14. p1_blind.resize(size);
  15. }
  16. }
  17. // For debugging; print the contents of the Duoram to stdout
  18. template <typename T>
  19. void Duoram<T>::dump() const
  20. {
  21. for (size_t i=0; i<oram_size; ++i) {
  22. if (player < 2) {
  23. printf("%04lx %016lx %016lx %016lx\n",
  24. i, database[i].share(), blind[i].share(),
  25. peer_blinded_db[i].share());
  26. } else {
  27. printf("%04lx %016lx %016lx\n",
  28. i, p0_blind[i].share(), p1_blind[i].share());
  29. }
  30. }
  31. printf("\n");
  32. }
  33. // For debugging or checking your answers (using this in general is
  34. // of course insecure)
  35. // This one reconstructs the whole database
  36. template <typename T>
  37. std::vector<T> Duoram<T>::Shape::reconstruct() const
  38. {
  39. int player = tio.player();
  40. std::vector<T> res;
  41. res.resize(duoram.size());
  42. // Player 1 sends their share of the database to player 0
  43. if (player == 1) {
  44. tio.queue_peer(duoram.database.data(), duoram.size()*sizeof(T));
  45. } else if (player == 0) {
  46. tio.recv_peer(res.data(), duoram.size()*sizeof(T));
  47. for(size_t i=0;i<duoram.size();++i) {
  48. res[i] += duoram.database[i];
  49. }
  50. }
  51. // The server (player 2) does nothing
  52. // Players 1 and 2 will get an empty vector here
  53. return res;
  54. }
  55. // This one reconstructs a single database value
  56. template <typename T>
  57. T Duoram<T>::Shape::reconstruct(const T& share) const
  58. {
  59. int player = tio.player();
  60. T res;
  61. // Player 1 sends their share of the value to player 0
  62. if (player == 1) {
  63. tio.queue_peer(&share, sizeof(T));
  64. } else if (player == 0) {
  65. tio.recv_peer(&res, sizeof(T));
  66. res += share;
  67. }
  68. // The server (player 2) does nothing
  69. // Players 1 and 2 will get 0 here
  70. return res;
  71. }
  72. // Function to set the shape_size of a shape and compute the number of
  73. // bits you need to address a shape of that size (which is the number of
  74. // bits in sz-1). This is typically called by subclass constructors.
  75. template <typename T>
  76. void Duoram<T>::Shape::set_shape_size(size_t sz)
  77. {
  78. shape_size = sz;
  79. // Compute the number of bits in (sz-1)
  80. // But use 0 if sz=0 for some reason (though that should never
  81. // happen)
  82. if (sz > 1) {
  83. addr_size = 64-__builtin_clzll(sz-1);
  84. addr_mask = address_t((size_t(1)<<addr_size)-1);
  85. } else {
  86. addr_size = 0;
  87. addr_mask = 0;
  88. }
  89. }
  90. // Constructor for the Flat shape. len=0 means the maximum size (the
  91. // parent's size minus start).
  92. template <typename T>
  93. Duoram<T>::Flat::Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield,
  94. size_t start, size_t len) : Shape(*this, duoram, tio, yield)
  95. {
  96. size_t parentsize = duoram.size();
  97. if (start > parentsize) {
  98. start = parentsize;
  99. }
  100. this->start = start;
  101. size_t maxshapesize = parentsize - start;
  102. if (len > maxshapesize || len == 0) {
  103. len = maxshapesize;
  104. }
  105. this->len = len;
  106. this->set_shape_size(len);
  107. }
  108. // Bitonic sort the elements from start to start+(1<<depth)-1, in
  109. // increasing order if dir=0 or decreasing order if dir=1. Note that
  110. // the elements must be at most 63 bits long each for the notion of
  111. // ">" to make consistent sense.
  112. template <typename T>
  113. void Duoram<T>::Flat::bitonic_sort(address_t start, nbits_t depth, bool dir)
  114. {
  115. if (depth == 0) return;
  116. if (depth == 1) {
  117. osort(start, start+1, dir);
  118. return;
  119. }
  120. // Recurse on the first half (increasing order) and the second half
  121. // (decreasing order) in parallel
  122. run_coroutines(this->yield,
  123. [&](yield_t &yield) {
  124. Flat Acoro = context(yield);
  125. Acoro.bitonic_sort(start, depth-1, 0);
  126. },
  127. [&](yield_t &yield) {
  128. Flat Acoro = context(yield);
  129. Acoro.bitonic_sort(start+(1<<(depth-1)), depth-1, 1);
  130. });
  131. // Merge the two into the desired order
  132. butterfly(start, depth, dir);
  133. }
  134. // Internal function to aid bitonic_sort
  135. template <typename T>
  136. void Duoram<T>::Flat::butterfly(address_t start, nbits_t depth, bool dir)
  137. {
  138. if (depth == 0) return;
  139. if (depth == 1) {
  140. osort(start, start+1, dir);
  141. return;
  142. }
  143. // Sort pairs of elements half the width apart in parallel
  144. address_t halfwidth = address_t(1)<<(depth-1);
  145. std::vector<coro_t> coroutines;
  146. for (address_t i=0; i<halfwidth;++i) {
  147. coroutines.emplace_back([&](yield_t &yield) {
  148. Flat Acoro = context(yield);
  149. Acoro.osort(start+i, start+i+halfwidth, dir);
  150. });
  151. }
  152. run_coroutines(this->yield, coroutines);
  153. // Recurse on each half in parallel
  154. run_coroutines(this->yield,
  155. [&](yield_t &yield) {
  156. Flat Acoro = context(yield);
  157. Acoro.butterfly(start, depth-1, dir);
  158. },
  159. [&](yield_t &yield) {
  160. Flat Acoro = context(yield);
  161. Acoro.butterfly(start+halfwidth, depth-1, dir);
  162. });
  163. }
  164. // Assuming the memory is already sorted, do an oblivious binary
  165. // search for the largest index containing the value at most the
  166. // given one. (The answer will be 0 if all of the memory elements
  167. // are greate than the target.) This Flat must be a power of 2 size.
  168. // Only available for additive shared databases for now.
  169. template <>
  170. RegAS Duoram<RegAS>::Flat::obliv_binary_search(RegAS &target)
  171. {
  172. nbits_t depth = this->addr_size;
  173. // Start in the middle
  174. RegAS index;
  175. index.set(this->tio.player() ? 0 : 1<<(depth-1));
  176. // Invariant: index points to the first element of the right half of
  177. // the remaining possible range
  178. while (depth > 0) {
  179. // Obliviously read the value there
  180. RegAS val = operator[](index);
  181. // Compare it to the target
  182. CDPF cdpf = tio.cdpf();
  183. auto [lt, eq, gt] = cdpf.compare(this->tio, this->yield,
  184. val-target, tio.aes_ops());
  185. if (depth > 1) {
  186. // If val > target, the answer is strictly to the left
  187. // and we should subtract 2^{depth-2} from index
  188. // If val <= target, the answer is here or to the right
  189. // and we should add 2^{depth-2} to index
  190. // So we unconditionally subtract 2^{depth-2} from index, and
  191. // add (lt+eq)*2^{depth-1}.
  192. RegAS uncond;
  193. uncond.set(tio.player() ? 0 : address_t(1)<<(depth-2));
  194. RegAS cond;
  195. cond.set(tio.player() ? 0 : address_t(1)<<(depth-1));
  196. RegAS condprod;
  197. RegBS le = lt ^ eq;
  198. mpc_flagmult(this->tio, this->yield, condprod, le, cond);
  199. index -= uncond;
  200. index += condprod;
  201. } else {
  202. // If val > target, the answer is strictly to the left
  203. // If val <= target, the answer is here or to the right
  204. // so subtract gt from index
  205. RegAS cond;
  206. cond.set(tio.player() ? 0 : 1);
  207. RegAS condprod;
  208. mpc_flagmult(this->tio, this->yield, condprod, gt, cond);
  209. index -= condprod;
  210. }
  211. --depth;
  212. }
  213. return index;
  214. }
  215. // Oblivious read from an additively shared index of Duoram memory
  216. template <typename T>
  217. Duoram<T>::Shape::MemRefAS::operator T()
  218. {
  219. T res;
  220. const Shape &shape = this->shape;
  221. int player = shape.tio.player();
  222. if (player < 2) {
  223. // Computational players do this
  224. RDPFTriple dt = shape.tio.rdpftriple(shape.addr_size);
  225. // Compute the index offset
  226. RegAS indoffset = dt.as_target;
  227. indoffset -= idx;
  228. // We only need two of the DPFs for reading
  229. RDPFPair dp(std::move(dt), 0, player == 0 ? 2 : 1);
  230. // The RDPFTriple dt is now broken, since we've moved things out
  231. // of it.
  232. // Send it to the peer and the server
  233. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  234. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  235. shape.yield();
  236. // Receive the above from the peer
  237. RegAS peerindoffset;
  238. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  239. // Reconstruct the total offset
  240. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  241. // Evaluate the DPFs and compute the dotproducts
  242. StreamEval ev(dp, indshift, 0, shape.tio.aes_ops());
  243. for (size_t i=0; i<shape.shape_size; ++i) {
  244. auto L = ev.next();
  245. // The values from the two DPFs
  246. auto [V0, V1] = dp.unit<T>(L);
  247. // References to the appropriate cells in our database, our
  248. // blind, and our copy of the peer's blinded database
  249. auto [DB, BL, PBD] = shape.get_comp(i);
  250. res += (DB + PBD) * V0.share() - BL * (V1-V0).share();
  251. }
  252. // Receive the cancellation term from the server
  253. T gamma;
  254. shape.tio.iostream_server() >> gamma;
  255. res += gamma;
  256. } else {
  257. // The server does this
  258. RDPFPair dp = shape.tio.rdpfpair(shape.addr_size);
  259. RegAS p0indoffset, p1indoffset;
  260. // Receive the index offset from the computational players and
  261. // combine them
  262. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  263. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  264. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  265. // Evaluate the DPFs to compute the cancellation terms
  266. T gamma0, gamma1;
  267. StreamEval ev(dp, indshift, 0, shape.tio.aes_ops());
  268. for (size_t i=0; i<shape.shape_size; ++i) {
  269. auto L = ev.next();
  270. // The values from the two DPFs
  271. auto [V0, V1] = dp.unit<T>(L);
  272. // shape.get_server(i) returns a pair of references to the
  273. // appropriate cells in the two blinded databases
  274. auto [BL0, BL1] = shape.get_server(i);
  275. gamma0 -= BL0 * V1.share();
  276. gamma1 -= BL1 * V0.share();
  277. }
  278. // Choose a random blinding factor
  279. T rho;
  280. rho.randomize();
  281. gamma0 += rho;
  282. gamma1 -= rho;
  283. // Send the cancellation terms to the computational players
  284. shape.tio.iostream_p0() << gamma0;
  285. shape.tio.iostream_p1() << gamma1;
  286. shape.yield();
  287. }
  288. return res; // The server will always get 0
  289. }
  290. // Oblivious update to an additively shared index of Duoram memory
  291. template <typename T>
  292. typename Duoram<T>::Shape::MemRefAS
  293. &Duoram<T>::Shape::MemRefAS::operator+=(const T& M)
  294. {
  295. const Shape &shape = this->shape;
  296. int player = shape.tio.player();
  297. if (player < 2) {
  298. // Computational players do this
  299. RDPFTriple dt = shape.tio.rdpftriple(shape.addr_size);
  300. // Compute the index and message offsets
  301. RegAS indoffset = dt.as_target;
  302. indoffset -= idx;
  303. auto Moffset = std::make_tuple(M, M, M);
  304. Moffset -= dt.scaled_value<T>();
  305. // Send them to the peer, and everything except the first offset
  306. // to the server
  307. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  308. shape.tio.iostream_peer() << Moffset;
  309. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  310. shape.tio.iostream_server() << std::get<1>(Moffset) <<
  311. std::get<2>(Moffset);
  312. shape.yield();
  313. // Receive the above from the peer
  314. RegAS peerindoffset;
  315. std::tuple<T,T,T> peerMoffset;
  316. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  317. shape.tio.iostream_peer() >> peerMoffset;
  318. // Reconstruct the total offsets
  319. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  320. auto Mshift = combine(Moffset, peerMoffset);
  321. // Evaluate the DPFs and add them to the database
  322. StreamEval ev(dt, indshift, 0, shape.tio.aes_ops());
  323. for (size_t i=0; i<shape.shape_size; ++i) {
  324. auto L = ev.next();
  325. // The values from the three DPFs
  326. auto [V0, V1, V2] = dt.scaled<T>(L) + dt.unit<T>(L) * Mshift;
  327. // References to the appropriate cells in our database, our
  328. // blind, and our copy of the peer's blinded database
  329. auto [DB, BL, PBD] = shape.get_comp(i);
  330. DB += V0;
  331. if (player == 0) {
  332. BL -= V1;
  333. PBD += V2-V0;
  334. } else {
  335. BL -= V2;
  336. PBD += V1-V0;
  337. }
  338. }
  339. } else {
  340. // The server does this
  341. RDPFPair dp = shape.tio.rdpfpair(shape.addr_size);
  342. RegAS p0indoffset, p1indoffset;
  343. std::tuple<T,T> p0Moffset, p1Moffset;
  344. // Receive the index and message offsets from the computational
  345. // players and combine them
  346. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  347. shape.tio.iostream_p0() >> p0Moffset;
  348. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  349. shape.tio.iostream_p1() >> p1Moffset;
  350. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  351. auto Mshift = combine(p0Moffset, p1Moffset);
  352. // Evaluate the DPFs and subtract them from the blinds
  353. StreamEval ev(dp, indshift, 0, shape.tio.aes_ops());
  354. for (size_t i=0; i<shape.shape_size; ++i) {
  355. auto L = ev.next();
  356. // The values from the two DPFs
  357. auto V = dp.scaled<T>(L) + dp.unit<T>(L) * Mshift;
  358. // shape.get_server(i) returns a pair of references to the
  359. // appropriate cells in the two blinded databases, so we can
  360. // subtract the pair directly.
  361. shape.get_server(i) -= V;
  362. }
  363. }
  364. return *this;
  365. }
  366. // Oblivious sort with the provided other element. Without
  367. // reconstructing the values, *this will become a share of the
  368. // smaller of the reconstructed values, and other will become a
  369. // share of the larger.
  370. //
  371. // Note: this only works for additively shared databases
  372. template <> template <typename U,typename V>
  373. void Duoram<RegAS>::Flat::osort(const U &idx1, const V &idx2, bool dir)
  374. {
  375. // Load the values in parallel
  376. RegAS val1, val2;
  377. run_coroutines(yield,
  378. [&](yield_t &yield) {
  379. Flat Acoro = context(yield);
  380. val1 = Acoro[idx1];
  381. },
  382. [&](yield_t &yield) {
  383. Flat Acoro = context(yield);
  384. val2 = Acoro[idx2];
  385. });
  386. // Get a CDPF
  387. CDPF cdpf = tio.cdpf();
  388. // Use it to compare the values
  389. RegAS diff = val1-val2;
  390. auto [lt, eq, gt] = cdpf.compare(tio, yield, diff, tio.aes_ops());
  391. RegBS cmp = dir ? lt : gt;
  392. // Get additive shares of cmp*diff
  393. RegAS cmp_diff;
  394. mpc_flagmult(tio, yield, cmp_diff, cmp, diff);
  395. // Update the two locations in parallel
  396. run_coroutines(yield,
  397. [&](yield_t &yield) {
  398. Flat Acoro = context(yield);
  399. Acoro[idx1] -= cmp_diff;
  400. },
  401. [&](yield_t &yield) {
  402. Flat Acoro = context(yield);
  403. Acoro[idx2] += cmp_diff;
  404. });
  405. }
  406. // The MemRefXS routines are almost identical to the MemRefAS routines,
  407. // but I couldn't figure out how to get them to be two instances of a
  408. // template. Sorry for the code duplication.
  409. // Oblivious read from an XOR shared index of Duoram memory
  410. template <typename T>
  411. Duoram<T>::Shape::MemRefXS::operator T()
  412. {
  413. const Shape &shape = this->shape;
  414. T res;
  415. int player = shape.tio.player();
  416. if (player < 2) {
  417. // Computational players do this
  418. RDPFTriple dt = shape.tio.rdpftriple(shape.addr_size);
  419. // Compute the index offset
  420. RegXS indoffset = dt.xs_target;
  421. indoffset -= idx;
  422. // We only need two of the DPFs for reading
  423. RDPFPair dp(std::move(dt), 0, player == 0 ? 2 : 1);
  424. // Send it to the peer and the server
  425. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  426. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  427. shape.yield();
  428. // Receive the above from the peer
  429. RegXS peerindoffset;
  430. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  431. // Reconstruct the total offset
  432. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  433. // Evaluate the DPFs and compute the dotproducts
  434. StreamEval ev(dp, 0, indshift, shape.tio.aes_ops());
  435. for (size_t i=0; i<shape.shape_size; ++i) {
  436. auto L = ev.next();
  437. // The values from the two DPFs
  438. auto [V0, V1] = dp.unit<T>(L);
  439. // References to the appropriate cells in our database, our
  440. // blind, and our copy of the peer's blinded database
  441. auto [DB, BL, PBD] = shape.get_comp(i);
  442. res += (DB + PBD) * V0.share() - BL * (V1-V0).share();
  443. }
  444. // Receive the cancellation term from the server
  445. T gamma;
  446. shape.tio.iostream_server() >> gamma;
  447. res += gamma;
  448. } else {
  449. // The server does this
  450. RDPFPair dp = shape.tio.rdpfpair(shape.addr_size);
  451. RegXS p0indoffset, p1indoffset;
  452. // Receive the index offset from the computational players and
  453. // combine them
  454. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  455. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  456. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  457. // Evaluate the DPFs to compute the cancellation terms
  458. T gamma0, gamma1;
  459. StreamEval ev(dp, 0, indshift, shape.tio.aes_ops());
  460. for (size_t i=0; i<shape.shape_size; ++i) {
  461. auto L = ev.next();
  462. // The values from the two DPFs
  463. auto [V0, V1] = dp.unit<T>(L);
  464. // shape.get_server(i) returns a pair of references to the
  465. // appropriate cells in the two blinded databases
  466. auto [BL0, BL1] = shape.get_server(i);
  467. gamma0 -= BL0 * V1.share();
  468. gamma1 -= BL1 * V0.share();
  469. }
  470. // Choose a random blinding factor
  471. T rho;
  472. rho.randomize();
  473. gamma0 += rho;
  474. gamma1 -= rho;
  475. // Send the cancellation terms to the computational players
  476. shape.tio.iostream_p0() << gamma0;
  477. shape.tio.iostream_p1() << gamma1;
  478. shape.yield();
  479. }
  480. return res; // The server will always get 0
  481. }
  482. // Oblivious update to an XOR shared index of Duoram memory
  483. template <typename T>
  484. typename Duoram<T>::Shape::MemRefXS
  485. &Duoram<T>::Shape::MemRefXS::operator+=(const T& M)
  486. {
  487. const Shape &shape = this->shape;
  488. int player = shape.tio.player();
  489. if (player < 2) {
  490. // Computational players do this
  491. RDPFTriple dt = shape.tio.rdpftriple(shape.addr_size);
  492. // Compute the index and message offsets
  493. RegXS indoffset = dt.xs_target;
  494. indoffset -= idx;
  495. auto Moffset = std::make_tuple(M, M, M);
  496. Moffset -= dt.scaled_value<T>();
  497. // Send them to the peer, and everything except the first offset
  498. // to the server
  499. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  500. shape.tio.iostream_peer() << Moffset;
  501. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  502. shape.tio.iostream_server() << std::get<1>(Moffset) <<
  503. std::get<2>(Moffset);
  504. shape.yield();
  505. // Receive the above from the peer
  506. RegXS peerindoffset;
  507. std::tuple<T,T,T> peerMoffset;
  508. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  509. shape.tio.iostream_peer() >> peerMoffset;
  510. // Reconstruct the total offsets
  511. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  512. auto Mshift = combine(Moffset, peerMoffset);
  513. // Evaluate the DPFs and add them to the database
  514. StreamEval ev(dt, 0, indshift, shape.tio.aes_ops());
  515. for (size_t i=0; i<shape.shape_size; ++i) {
  516. auto L = ev.next();
  517. // The values from the three DPFs
  518. auto [V0, V1, V2] = dt.scaled<T>(L) + dt.unit<T>(L) * Mshift;
  519. // References to the appropriate cells in our database, our
  520. // blind, and our copy of the peer's blinded database
  521. auto [DB, BL, PBD] = shape.get_comp(i);
  522. DB += V0;
  523. if (player == 0) {
  524. BL -= V1;
  525. PBD += V2-V0;
  526. } else {
  527. BL -= V2;
  528. PBD += V1-V0;
  529. }
  530. }
  531. } else {
  532. // The server does this
  533. RDPFPair dp = shape.tio.rdpfpair(shape.addr_size);
  534. RegXS p0indoffset, p1indoffset;
  535. std::tuple<T,T> p0Moffset, p1Moffset;
  536. // Receive the index and message offsets from the computational
  537. // players and combine them
  538. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  539. shape.tio.iostream_p0() >> p0Moffset;
  540. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  541. shape.tio.iostream_p1() >> p1Moffset;
  542. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  543. auto Mshift = combine(p0Moffset, p1Moffset);
  544. // Evaluate the DPFs and subtract them from the blinds
  545. StreamEval ev(dp, 0, indshift, shape.tio.aes_ops());
  546. for (size_t i=0; i<shape.shape_size; ++i) {
  547. auto L = ev.next();
  548. // The values from the two DPFs
  549. auto V = dp.scaled<T>(L) + dp.unit<T>(L) * Mshift;
  550. // shape.get_server(i) returns a pair of references to the
  551. // appropriate cells in the two blinded databases, so we can
  552. // subtract the pair directly.
  553. shape.get_server(i) -= V;
  554. }
  555. }
  556. return *this;
  557. }
  558. // Explicit read from a given index of Duoram memory
  559. template <typename T>
  560. Duoram<T>::Shape::MemRefExpl::operator T()
  561. {
  562. const Shape &shape = this->shape;
  563. T res;
  564. int player = shape.tio.player();
  565. if (player < 2) {
  566. res = std::get<0>(shape.get_comp(idx));
  567. }
  568. return res; // The server will always get 0
  569. }
  570. // Explicit update to a given index of Duoram memory
  571. template <typename T>
  572. typename Duoram<T>::Shape::MemRefExpl
  573. &Duoram<T>::Shape::MemRefExpl::operator+=(const T& M)
  574. {
  575. const Shape &shape = this->shape;
  576. int player = shape.tio.player();
  577. if (player < 2) {
  578. // Computational players do this
  579. // Pick a blinding factor
  580. T blind;
  581. blind.randomize();
  582. // Send the blind to the server, and the blinded value to the
  583. // peer
  584. shape.tio.iostream_server() << blind;
  585. shape.tio.iostream_peer() << (M + blind);
  586. shape.yield();
  587. // Receive the peer's blinded value
  588. T peerblinded;
  589. shape.tio.iostream_peer() >> peerblinded;
  590. // Our database, our blind, the peer's blinded database
  591. auto [ DB, BL, PBD ] = shape.get_comp(idx);
  592. DB += M;
  593. BL += blind;
  594. PBD += peerblinded;
  595. } else if (player == 2) {
  596. // The server does this
  597. // Receive the updates to the blinds
  598. T p0blind, p1blind;
  599. shape.tio.iostream_p0() >> p0blind;
  600. shape.tio.iostream_p1() >> p1blind;
  601. // The two computational parties' blinds
  602. auto [ BL0, BL1 ] = shape.get_server(idx);
  603. BL0 += p0blind;
  604. BL1 += p1blind;
  605. }
  606. return *this;
  607. }