duoram.tcc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. // Bitonic sort the elements from start to start+len-1, in
  167. // increasing order if dir=0 or decreasing order if dir=1. Note that
  168. // the elements must be at most 63 bits long each for the notion of
  169. // ">" to make consistent sense.
  170. template <typename T>
  171. void Duoram<T>::Flat::bitonic_sort(address_t start, address_t len, bool dir)
  172. {
  173. if (len < 2) return;
  174. if (len == 2) {
  175. osort(start, start+1, dir);
  176. return;
  177. }
  178. address_t leftlen, rightlen;
  179. leftlen = (len+1) >> 1;
  180. rightlen = len >> 1;
  181. // Recurse on the first half (opposite to the desired order)
  182. // and the second half (desired order) in parallel
  183. run_coroutines(this->yield,
  184. [this, start, leftlen, dir](yield_t &yield) {
  185. Flat Acoro = context(yield);
  186. Acoro.bitonic_sort(start, leftlen, !dir);
  187. },
  188. [this, start, leftlen, rightlen, dir](yield_t &yield) {
  189. Flat Acoro = context(yield);
  190. Acoro.bitonic_sort(start+leftlen, rightlen, dir);
  191. });
  192. // Merge the two into the desired order
  193. butterfly(start, len, dir);
  194. }
  195. // Internal function to aid bitonic_sort
  196. template <typename T>
  197. void Duoram<T>::Flat::butterfly(address_t start, address_t len, bool dir)
  198. {
  199. if (len < 2) return;
  200. if (len == 2) {
  201. osort(start, start+1, dir);
  202. return;
  203. }
  204. address_t leftlen, rightlen, offset, num_swaps;
  205. // leftlen = (len+1) >> 1;
  206. leftlen = 1;
  207. while(2*leftlen < len) {
  208. leftlen *= 2;
  209. }
  210. rightlen = len - leftlen;
  211. offset = leftlen;
  212. num_swaps = rightlen;
  213. // Sort pairs of elements offset apart in parallel
  214. std::vector<coro_t> coroutines;
  215. for (address_t i=0; i<num_swaps;++i) {
  216. coroutines.emplace_back(
  217. [this, start, offset, dir, i](yield_t &yield) {
  218. Flat Acoro = context(yield);
  219. Acoro.osort(start+i, start+i+offset, dir);
  220. });
  221. }
  222. run_coroutines(this->yield, coroutines);
  223. // Recurse on each half in parallel
  224. run_coroutines(this->yield,
  225. [this, start, leftlen, dir](yield_t &yield) {
  226. Flat Acoro = context(yield);
  227. Acoro.butterfly(start, leftlen, dir);
  228. },
  229. [this, start, leftlen, rightlen, dir](yield_t &yield) {
  230. Flat Acoro = context(yield);
  231. Acoro.butterfly(start+leftlen, rightlen, dir);
  232. });
  233. }
  234. // Helper functions to specialize the read and update operations for
  235. // RegAS and RegXS shared indices
  236. template <typename U>
  237. inline address_t IfRegAS(address_t val);
  238. template <typename U>
  239. inline address_t IfRegXS(address_t val);
  240. template <>
  241. inline address_t IfRegAS<RegAS>(address_t val) { return val; }
  242. template <>
  243. inline address_t IfRegAS<RegXS>(address_t val) { return 0; }
  244. template <>
  245. inline address_t IfRegXS<RegAS>(address_t val) { return 0; }
  246. template <>
  247. inline address_t IfRegXS<RegXS>(address_t val) { return val; }
  248. // Oblivious read from an additively or XOR shared index of Duoram memory
  249. // T is the sharing type of the _values_ in the database; U is the
  250. // sharing type of the _indices_ in the database. If we are referencing
  251. // an entire entry of type T, then the field type FT will equal T, and
  252. // the field selector type FST will be nullopt_t. If we are referencing
  253. // a particular field of T, then FT will be the type of the field (RegAS
  254. // or RegXS) and FST will be a pointer-to-member T::* type pointing to
  255. // that field. Sh is the specific Shape subtype used to create the
  256. // MemRefS. WIDTH is the RDPF width to use.
  257. template <typename T>
  258. template <typename U,typename FT,typename FST,typename Sh,nbits_t WIDTH>
  259. Duoram<T>::Shape::MemRefS<U,FT,FST,Sh,WIDTH>::operator FT()
  260. {
  261. FT res;
  262. Sh &shape = this->shape;
  263. shape.explicitonly(false);
  264. int player = shape.tio.player();
  265. if (player < 2) {
  266. // Computational players do this
  267. RDPFTriple<1> dt = shape.tio.rdpftriple(shape.yield, shape.addr_size);
  268. // Compute the index offset
  269. U indoffset;
  270. dt.get_target(indoffset);
  271. indoffset -= oblividx->idx;
  272. // We only need two of the DPFs for reading
  273. RDPF2of3<1> dp(dt, 0, player == 0 ? 2 : 1);
  274. // Send it to the peer and the server
  275. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  276. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  277. shape.yield();
  278. // Receive the above from the peer
  279. U peerindoffset;
  280. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  281. // Reconstruct the total offset
  282. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  283. // Evaluate the DPFs and compute the dotproducts
  284. ParallelEval pe(dp, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  285. shape.shape_size, shape.tio.cpu_nthreads(),
  286. shape.tio.aes_ops());
  287. FT init;
  288. res = pe.reduce(init, [this, &dp, &shape] (int thread_num,
  289. address_t i, const RDPFPair<1>::LeafNode &leaf) {
  290. // The values from the two DPFs, which will each be of type T
  291. std::tuple<FT,FT> V;
  292. dp.unit(V, leaf);
  293. auto [V0, V1] = V;
  294. // References to the appropriate cells in our database, our
  295. // blind, and our copy of the peer's blinded database
  296. auto [DB, BL, PBD] = shape.get_comp(i, fieldsel);
  297. return (DB + PBD).mulshare(V0) - BL.mulshare(V1-V0);
  298. });
  299. shape.yield();
  300. // Receive the cancellation term from the server
  301. FT gamma;
  302. shape.tio.iostream_server() >> gamma;
  303. res += gamma;
  304. } else {
  305. // The server does this
  306. RDPFPair<1> dp = shape.tio.rdpfpair(shape.yield, shape.addr_size);
  307. U p0indoffset, p1indoffset;
  308. shape.yield();
  309. // Receive the index offset from the computational players and
  310. // combine them
  311. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  312. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  313. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  314. // Evaluate the DPFs to compute the cancellation terms
  315. std::tuple<FT,FT> init, gamma;
  316. ParallelEval pe(dp, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  317. shape.shape_size, shape.tio.cpu_nthreads(),
  318. shape.tio.aes_ops());
  319. gamma = pe.reduce(init, [this, &dp, &shape] (int thread_num,
  320. address_t i, const RDPFPair<1>::LeafNode &leaf) {
  321. // The values from the two DPFs, each of type FT
  322. std::tuple<FT,FT> V;
  323. dp.unit(V, leaf);
  324. auto [V0, V1] = V;
  325. // shape.get_server(i) returns a pair of references to the
  326. // appropriate cells in the two blinded databases
  327. auto [BL0, BL1] = shape.get_server(i, fieldsel);
  328. return std::make_tuple(-BL0.mulshare(V1), -BL1.mulshare(V0));
  329. });
  330. // Choose a random blinding factor
  331. FT rho;
  332. rho.randomize();
  333. std::get<0>(gamma) += rho;
  334. std::get<1>(gamma) -= rho;
  335. // Send the cancellation terms to the computational players
  336. shape.tio.iostream_p0() << std::get<0>(gamma);
  337. shape.tio.iostream_p1() << std::get<1>(gamma);
  338. shape.yield();
  339. }
  340. return res; // The server will always get 0
  341. }
  342. // Oblivious update to a shared index of Duoram memory, only for
  343. // FT = RegAS or RegXS. The template parameters are as above.
  344. template <typename T>
  345. template <typename U, typename FT, typename FST, typename Sh, nbits_t WIDTH>
  346. typename Duoram<T>::Shape::template MemRefS<U,FT,FST,Sh,WIDTH>
  347. &Duoram<T>::Shape::MemRefS<U,FT,FST,Sh,WIDTH>::oram_update(const FT& M,
  348. const prac_template_true &)
  349. {
  350. Sh &shape = this->shape;
  351. shape.explicitonly(false);
  352. int player = shape.tio.player();
  353. if (player < 2) {
  354. // Computational players do this
  355. RDPFTriple<1> dt = shape.tio.rdpftriple(shape.yield, shape.addr_size);
  356. // Compute the index and message offsets
  357. U indoffset;
  358. dt.get_target(indoffset);
  359. indoffset -= oblividx->idx;
  360. RDPF<1>::W<FT> MW;
  361. MW[0] = M;
  362. auto Moffset = std::make_tuple(MW, MW, MW);
  363. RDPFTriple<1>::WTriple<FT> scaled_val;
  364. dt.scaled_value(scaled_val);
  365. Moffset -= scaled_val;
  366. // Send them to the peer, and everything except the first offset
  367. // to the server
  368. shape.tio.queue_peer(&indoffset, BITBYTES(shape.addr_size));
  369. shape.tio.iostream_peer() << Moffset;
  370. shape.tio.queue_server(&indoffset, BITBYTES(shape.addr_size));
  371. shape.tio.iostream_server() << std::get<1>(Moffset) <<
  372. std::get<2>(Moffset);
  373. shape.yield();
  374. // Receive the above from the peer
  375. U peerindoffset;
  376. RDPFTriple<1>::WTriple<FT> peerMoffset;
  377. shape.tio.recv_peer(&peerindoffset, BITBYTES(shape.addr_size));
  378. shape.tio.iostream_peer() >> peerMoffset;
  379. // Reconstruct the total offsets
  380. auto indshift = combine(indoffset, peerindoffset, shape.addr_size);
  381. auto Mshift = combine(Moffset, peerMoffset);
  382. // Evaluate the DPFs and add them to the database
  383. ParallelEval pe(dt, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  384. shape.shape_size, shape.tio.cpu_nthreads(),
  385. shape.tio.aes_ops());
  386. int init = 0;
  387. pe.reduce(init, [this, &dt, &shape, &Mshift, player] (int thread_num,
  388. address_t i, const RDPFTriple<1>::LeafNode &leaf) {
  389. // The values from the three DPFs
  390. RDPFTriple<1>::WTriple<FT> scaled;
  391. std::tuple<FT,FT,FT> unit;
  392. dt.scaled(scaled, leaf);
  393. dt.unit(unit, leaf);
  394. auto [V0, V1, V2] = scaled + unit * Mshift;
  395. // References to the appropriate cells in our database, our
  396. // blind, and our copy of the peer's blinded database
  397. auto [DB, BL, PBD] = shape.get_comp(i,fieldsel);
  398. DB += V0[0];
  399. if (player == 0) {
  400. BL -= V1[0];
  401. PBD += V2[0]-V0[0];
  402. } else {
  403. BL -= V2[0];
  404. PBD += V1[0]-V0[0];
  405. }
  406. return 0;
  407. });
  408. } else {
  409. // The server does this
  410. RDPFPair<1> dp = shape.tio.rdpfpair(shape.yield, shape.addr_size);
  411. U p0indoffset, p1indoffset;
  412. RDPFPair<1>::WPair<FT> p0Moffset, p1Moffset;
  413. shape.yield();
  414. // Receive the index and message offsets from the computational
  415. // players and combine them
  416. shape.tio.recv_p0(&p0indoffset, BITBYTES(shape.addr_size));
  417. shape.tio.iostream_p0() >> p0Moffset;
  418. shape.tio.recv_p1(&p1indoffset, BITBYTES(shape.addr_size));
  419. shape.tio.iostream_p1() >> p1Moffset;
  420. auto indshift = combine(p0indoffset, p1indoffset, shape.addr_size);
  421. auto Mshift = combine(p0Moffset, p1Moffset);
  422. // Evaluate the DPFs and subtract them from the blinds
  423. ParallelEval pe(dp, IfRegAS<U>(indshift), IfRegXS<U>(indshift),
  424. shape.shape_size, shape.tio.cpu_nthreads(),
  425. shape.tio.aes_ops());
  426. int init = 0;
  427. pe.reduce(init, [this, &dp, &shape, &Mshift] (int thread_num,
  428. address_t i, const RDPFPair<1>::LeafNode &leaf) {
  429. // The values from the two DPFs
  430. RDPFPair<1>::WPair<FT> scaled;
  431. std::tuple<FT,FT> unit;
  432. dp.scaled(scaled, leaf);
  433. dp.unit(unit, leaf);
  434. auto [V0, V1] = scaled + unit * Mshift;
  435. // shape.get_server(i) returns a pair of references to the
  436. // appropriate cells in the two blinded databases, so we can
  437. // subtract the pair directly.
  438. auto [BL0, BL1] = shape.get_server(i,fieldsel);
  439. BL0 -= V0[0];
  440. BL1 -= V1[0];
  441. return 0;
  442. });
  443. }
  444. return *this;
  445. }
  446. // Oblivious update to a shared index of Duoram memory, only for
  447. // FT not RegAS or RegXS. The template parameters are as above.
  448. template <typename T>
  449. template <typename U, typename FT, typename FST, typename Sh, nbits_t WIDTH>
  450. typename Duoram<T>::Shape::template MemRefS<U,FT,FST,Sh,WIDTH>
  451. &Duoram<T>::Shape::MemRefS<U,FT,FST,Sh,WIDTH>::oram_update(const FT& M,
  452. const prac_template_false &)
  453. {
  454. T::update(shape, shape.yield, oblividx->idx, M);
  455. return *this;
  456. }
  457. // Oblivious update to an additively or XOR shared index of Duoram
  458. // memory. The template parameters are as above.
  459. template <typename T>
  460. template <typename U, typename FT, typename FST, typename Sh, nbits_t WIDTH>
  461. typename Duoram<T>::Shape::template MemRefS<U,FT,FST,Sh,WIDTH>
  462. &Duoram<T>::Shape::MemRefS<U,FT,FST,Sh,WIDTH>::operator+=(const FT& M)
  463. {
  464. return oram_update(M, prac_basic_Reg_S<FT>());
  465. }
  466. // Oblivious write to an additively or XOR shared index of Duoram
  467. // memory. The template parameters are as above.
  468. template <typename T>
  469. template <typename U, typename FT, typename FST, typename Sh, nbits_t WIDTH>
  470. typename Duoram<T>::Shape::template MemRefS<U,FT,FST,Sh,WIDTH>
  471. &Duoram<T>::Shape::MemRefS<U,FT,FST,Sh,WIDTH>::operator=(const FT& M)
  472. {
  473. FT oldval = *this;
  474. FT update = M - oldval;
  475. *this += update;
  476. return *this;
  477. }
  478. // Oblivious sort with the provided other element. Without
  479. // reconstructing the values, *this will become a share of the
  480. // smaller of the reconstructed values, and other will become a
  481. // share of the larger.
  482. //
  483. // Note: this only works for additively shared databases
  484. template <> template <typename U,typename V>
  485. void Duoram<RegAS>::Flat::osort(const U &idx1, const V &idx2, bool dir)
  486. {
  487. // Load the values in parallel
  488. RegAS val1, val2;
  489. run_coroutines(yield,
  490. [this, &idx1, &val1](yield_t &yield) {
  491. Flat Acoro = context(yield);
  492. val1 = Acoro[idx1];
  493. },
  494. [this, &idx2, &val2](yield_t &yield) {
  495. Flat Acoro = context(yield);
  496. val2 = Acoro[idx2];
  497. });
  498. // Get a CDPF
  499. CDPF cdpf = tio.cdpf(yield);
  500. // Use it to compare the values
  501. RegAS diff = val1-val2;
  502. auto [lt, eq, gt] = cdpf.compare(tio, yield, diff, tio.aes_ops());
  503. RegBS cmp = dir ? lt : gt;
  504. // Get additive shares of cmp*diff
  505. RegAS cmp_diff;
  506. mpc_flagmult(tio, yield, cmp_diff, cmp, diff);
  507. // Update the two locations in parallel
  508. run_coroutines(yield,
  509. [this, &idx1, &cmp_diff](yield_t &yield) {
  510. Flat Acoro = context(yield);
  511. Acoro[idx1] -= cmp_diff;
  512. },
  513. [this, &idx2, &cmp_diff](yield_t &yield) {
  514. Flat Acoro = context(yield);
  515. Acoro[idx2] += cmp_diff;
  516. });
  517. }
  518. // Explicit read from a given index of Duoram memory
  519. template <typename T> template <typename FT, typename FST>
  520. Duoram<T>::Shape::MemRefExpl<FT,FST>::operator FT()
  521. {
  522. Shape &shape = this->shape;
  523. FT res;
  524. int player = shape.tio.player();
  525. if (player < 2) {
  526. res = std::get<0>(shape.get_comp(idx, fieldsel));
  527. }
  528. return res; // The server will always get 0
  529. }
  530. // Explicit update to a given index of Duoram memory
  531. template <typename T> template <typename FT, typename FST>
  532. typename Duoram<T>::Shape::template MemRefExpl<FT,FST>
  533. &Duoram<T>::Shape::MemRefExpl<FT,FST>::operator+=(const FT& M)
  534. {
  535. Shape &shape = this->shape;
  536. int player = shape.tio.player();
  537. // In explicit-only mode, just update the local DB; we'll sync the
  538. // blinds and the blinded DB when we leave explicit-only mode.
  539. if (shape.explicitmode) {
  540. if (player < 2) {
  541. auto [ DB, BL, PBD ] = shape.get_comp(idx, fieldsel);
  542. DB += M;
  543. }
  544. return *this;
  545. }
  546. if (player < 2) {
  547. // Computational players do this
  548. // Pick a blinding factor
  549. FT blind;
  550. blind.randomize();
  551. // Send the blind to the server, and the blinded value to the
  552. // peer
  553. shape.tio.iostream_server() << blind;
  554. shape.tio.iostream_peer() << (M + blind);
  555. shape.yield();
  556. // Receive the peer's blinded value
  557. FT peerblinded;
  558. shape.tio.iostream_peer() >> peerblinded;
  559. // Our database, our blind, the peer's blinded database
  560. auto [ DB, BL, PBD ] = shape.get_comp(idx, fieldsel);
  561. DB += M;
  562. BL += blind;
  563. PBD += peerblinded;
  564. } else if (player == 2) {
  565. // The server does this
  566. shape.yield();
  567. // Receive the updates to the blinds
  568. FT p0blind, p1blind;
  569. shape.tio.iostream_p0() >> p0blind;
  570. shape.tio.iostream_p1() >> p1blind;
  571. // The two computational parties' blinds
  572. auto [ BL0, BL1 ] = shape.get_server(idx, fieldsel);
  573. BL0 += p0blind;
  574. BL1 += p1blind;
  575. }
  576. return *this;
  577. }
  578. // Explicit write to a given index of Duoram memory
  579. template <typename T> template <typename FT, typename FST>
  580. typename Duoram<T>::Shape::template MemRefExpl<FT,FST>
  581. &Duoram<T>::Shape::MemRefExpl<FT,FST>::operator=(const FT& M)
  582. {
  583. FT oldval = *this;
  584. FT update = M - oldval;
  585. *this += update;
  586. return *this;
  587. }
  588. // Independent U-shared reads into a Shape of subtype Sh on a Duoram
  589. // with values of sharing type T
  590. template <typename T> template <typename U, typename Sh>
  591. Duoram<T>::Shape::MemRefInd<U,Sh>::operator std::vector<T>()
  592. {
  593. std::vector<T> res;
  594. size_t size = indcs.size();
  595. res.resize(size);
  596. std::vector<coro_t> coroutines;
  597. for (size_t i=0;i<size;++i) {
  598. coroutines.emplace_back([this, &res, i] (yield_t &yield) {
  599. Sh Sh_coro = shape.context(yield);
  600. res[i] = Sh_coro[indcs[i]];
  601. });
  602. }
  603. run_coroutines(shape.yield, coroutines);
  604. return res;
  605. }
  606. // Independent U-shared updates into a Shape of subtype Sh on a Duoram
  607. // with values of sharing type T (vector version)
  608. template <typename T> template <typename U, typename Sh>
  609. typename Duoram<T>::Shape::template MemRefInd<U,Sh>
  610. &Duoram<T>::Shape::MemRefInd<U,Sh>::operator+=(const std::vector<T>& M)
  611. {
  612. size_t size = indcs.size();
  613. assert(M.size() == size);
  614. std::vector<coro_t> coroutines;
  615. for (size_t i=0;i<size;++i) {
  616. coroutines.emplace_back([this, &M, i] (yield_t &yield) {
  617. Sh Sh_coro = shape.context(yield);
  618. Sh_coro[indcs[i]] += M[i];
  619. });
  620. }
  621. run_coroutines(shape.yield, coroutines);
  622. return *this;
  623. }
  624. // Independent U-shared updates into a Shape of subtype Sh on a Duoram
  625. // with values of sharing type T (array version)
  626. template <typename T> template <typename U, typename Sh> template <size_t N>
  627. typename Duoram<T>::Shape::template MemRefInd<U,Sh>
  628. &Duoram<T>::Shape::MemRefInd<U,Sh>::operator+=(const std::array<T,N>& M)
  629. {
  630. size_t size = indcs.size();
  631. assert(N == size);
  632. std::vector<coro_t> coroutines;
  633. for (size_t i=0;i<size;++i) {
  634. coroutines.emplace_back([this, &M, i] (yield_t &yield) {
  635. Sh Sh_coro = shape.context(yield);
  636. Sh_coro[indcs[i]] += M[i];
  637. });
  638. }
  639. run_coroutines(shape.yield, coroutines);
  640. return *this;
  641. }
  642. // Independent U-shared writes into a Shape of subtype Sh on a Duoram
  643. // with values of sharing type T (vector version)
  644. template <typename T> template <typename U, typename Sh>
  645. typename Duoram<T>::Shape::template MemRefInd<U,Sh>
  646. &Duoram<T>::Shape::MemRefInd<U,Sh>::operator=(const std::vector<T>& M)
  647. {
  648. size_t size = indcs.size();
  649. assert(M.size() == size);
  650. std::vector<coro_t> coroutines;
  651. for (size_t i=0;i<size;++i) {
  652. coroutines.emplace_back([this, &M, i] (yield_t &yield) {
  653. Sh Sh_coro = shape.context(yield);
  654. Sh_coro[indcs[i]] = M[i];
  655. });
  656. }
  657. run_coroutines(shape.yield, coroutines);
  658. return *this;
  659. }
  660. // Independent U-shared writes into a Shape of subtype Sh on a Duoram
  661. // with values of sharing type T (array version)
  662. template <typename T> template <typename U, typename Sh> template <size_t N>
  663. typename Duoram<T>::Shape::template MemRefInd<U,Sh>
  664. &Duoram<T>::Shape::MemRefInd<U,Sh>::operator=(const std::array<T,N>& M)
  665. {
  666. size_t size = indcs.size();
  667. assert(N == size);
  668. std::vector<coro_t> coroutines;
  669. for (size_t i=0;i<size;++i) {
  670. coroutines.emplace_back([this, &M, i] (yield_t &yield) {
  671. Sh Sh_coro = shape.context(yield);
  672. Sh_coro[indcs[i]] = M[i];
  673. });
  674. }
  675. run_coroutines(shape.yield, coroutines);
  676. return *this;
  677. }