duoram.tcc 26 KB

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