duoram.tcc 26 KB

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