duoram.tcc 25 KB

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