duoram.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. #ifndef __DUORAM_HPP__
  2. #define __DUORAM_HPP__
  3. #include <optional>
  4. #include "types.hpp"
  5. #include "mpcio.hpp"
  6. #include "coroutine.hpp"
  7. // Implementation of the 3-party protocols described in:
  8. // Adithya Vadapalli, Ryan Henry, Ian Goldberg, "Duoram: A
  9. // Bandwidth-Efficient Distributed ORAM for 2- and 3-Party Computation".
  10. // A Duoram object is like physical memory: it's just a flat address
  11. // space, and you can't access it directly. Instead, you need to access
  12. // it through a "Shape", such as Flat, Tree, Path, etc. Shapes can be
  13. // nested, so you can have a Path of a Subtree of a Tree sitting on the
  14. // base Duoram. Each Shape's parent must remain in scope (references to
  15. // it must remain valid) for the lifetime of the child Shapre. Each
  16. // shape is bound to a context, which is a thread-specific MPCTIO and a
  17. // coroutine-specific yield_t. If you launch new threads and/or
  18. // coroutines, you'll need to make a copy of the current Shape for your
  19. // new context, and call context() on it. Be sure not to call context()
  20. // on a Shape shared with other threads or coroutines.
  21. // This is templated, because you can have a Duoram of additively shared
  22. // (RegAS) or XOR shared (RegXS) elements, or more complex cell types
  23. // (see cell.hpp for example).
  24. template <typename T>
  25. class Duoram {
  26. // The computational parties have three vectors: the share of the
  27. // database itself, the party's own blinding factors for its
  28. // database share, and the _other_ computational party's blinded
  29. // database share (its database share plus its blind).
  30. // The player number (0 and 1 for the computational parties and 2
  31. // for the server) and the size of the Duoram
  32. int player;
  33. size_t oram_size;
  34. // The server has two vectors: a copy of each computational party's
  35. // blind. The database vector will remain empty.
  36. std::vector<T> database; // computational parties only
  37. std::vector<T> blind; // computational parties use this name
  38. std::vector<T> &p0_blind; // server uses this name
  39. std::vector<T> peer_blinded_db; // computational parties
  40. std::vector<T> &p1_blind; // server
  41. public:
  42. // The type of this Duoram
  43. using type = T;
  44. // The different Shapes are subclasses of this inner class
  45. class Shape;
  46. // These are the different Shapes that exist
  47. class Flat;
  48. class Pad;
  49. class Stride;
  50. // Oblivious indices for use in related-index ORAM accesses
  51. template <typename U, nbits_t WIDTH>
  52. class OblivIndex;
  53. // Pass the player number and desired size
  54. Duoram(int player, size_t size);
  55. // Get the size
  56. inline size_t size() { return oram_size; }
  57. // Get the basic Flat shape for this Duoram
  58. Flat flat(MPCTIO &tio, yield_t &yield, size_t start = 0,
  59. size_t len = 0) {
  60. return Flat(*this, tio, yield, start, len);
  61. }
  62. // For debugging; print the contents of the Duoram to stdout
  63. void dump() const;
  64. };
  65. // The parent class of all Shapes. This is an abstract class that
  66. // cannot itself be instantiated.
  67. template <typename T>
  68. class Duoram<T>::Shape {
  69. // Subclasses should be able to access _other_ Shapes'
  70. // get_{comp,server} functions
  71. friend class Flat;
  72. friend class Pad;
  73. friend class Stride;
  74. template <typename U, nbits_t WIDTH>
  75. friend class OblivIndex;
  76. // When you index into a shape (A[x]), you get one of these types,
  77. // depending on the type of x (the index), _not_ on the type T (the
  78. // underlying type of the Duoram). That is, you can have an
  79. // additive-shared index (x) into an XOR-shared database (T), for
  80. // example.
  81. // When x is additively or XOR shared
  82. // U is the sharing type of the indices, while T is the sharing type
  83. // of the data in the database. If we are referencing an entire
  84. // entry of type T, then the field type FT will equal T, and the
  85. // field selector type FST will be nullopt_t. If we are referencing
  86. // a particular field of T, then FT will be the type of the field
  87. // (RegAS or RegXS) and FST will be a pointer-to-member T::* type
  88. // pointing to that field. Sh is the specific Shape subtype used to
  89. // create the MemRefS. WIDTH is the RDPF width to use.
  90. template <typename U, typename FT, typename FST, typename Sh, nbits_t WIDTH>
  91. class MemRefS;
  92. // When x is unshared explicit value. FT and FST are as above.
  93. template <typename FT, typename FST>
  94. class MemRefExpl;
  95. // When x is a vector or array of values of type U, used to denote a
  96. // collection of independent memory operations that can be performed
  97. // simultaneously. Sh is the specific Shape subtype used to create
  98. // the MemRefInd.
  99. template <typename U, typename Sh>
  100. class MemRefInd;
  101. protected:
  102. // A reference to the parent shape. As with ".." in the root
  103. // directory of a filesystem, the topmost shape is indicated by
  104. // having parent = *this.
  105. const Shape &parent;
  106. // A reference to the backing physical storage
  107. Duoram &duoram;
  108. // The size of this shape
  109. size_t shape_size;
  110. // The number of bits needed to address this shape (the number of
  111. // bits in shape_size-1)
  112. nbits_t addr_size;
  113. // And a mask with the low addr_size bits set
  114. address_t addr_mask;
  115. // The Shape's context (MPCTIO and yield_t)
  116. MPCTIO &tio;
  117. yield_t &yield;
  118. // If you enable explicit-only mode, sending updates of your blind
  119. // to the server and of your blinded database to your peer will be
  120. // temporarily disabled. When you disable it (which will happen
  121. // automatically at the next ORAM read or write, or you can do it
  122. // explicitly), new random blinds will be chosen for the whole
  123. // Shape, and the blinds sent to the server, and the blinded
  124. // database sent to the peer.
  125. bool explicitmode;
  126. // A function to set the shape_size and compute addr_size and
  127. // addr_mask
  128. void set_shape_size(size_t sz);
  129. // We need a constructor because we hold non-static references; this
  130. // constructor is called by the subclass constructors
  131. Shape(const Shape &parent, Duoram &duoram, MPCTIO &tio,
  132. yield_t &yield) : parent(parent), duoram(duoram), shape_size(0),
  133. tio(tio), yield(yield), explicitmode(false) {}
  134. // Copy the given Shape except for the tio and yield
  135. Shape(const Shape &copy_from, MPCTIO &tio, yield_t &yield) :
  136. parent(copy_from.parent), duoram(copy_from.duoram),
  137. shape_size(copy_from.shape_size),
  138. addr_size(copy_from.addr_size), addr_mask(copy_from.addr_mask),
  139. tio(tio), yield(yield),
  140. explicitmode(copy_from.explicitmode) {}
  141. // The index-mapping function. Input the index relative to this
  142. // shape, and output the corresponding index relative to the parent
  143. // shape.
  144. //
  145. // This is a pure virtual function; all subclasses of Shape must
  146. // implement it, and of course Shape itself therefore cannot be
  147. // instantiated.
  148. virtual size_t indexmap(size_t idx) const = 0;
  149. // Get a pair (for the server) of references to the underlying
  150. // Duoram entries at share virtual index idx.
  151. virtual inline std::tuple<T&,T&> get_server(size_t idx,
  152. std::nullopt_t null = std::nullopt) const {
  153. size_t parindex = indexmap(idx);
  154. if (&(this->parent) == this) {
  155. return std::tie(
  156. duoram.p0_blind[parindex],
  157. duoram.p1_blind[parindex]);
  158. } else {
  159. return this->parent.get_server(parindex, null);
  160. }
  161. }
  162. // Get a triple (for the computational players) of references to the
  163. // underlying Duoram entries at share virtual index idx.
  164. virtual inline std::tuple<T&,T&,T&> get_comp(size_t idx,
  165. std::nullopt_t null = std::nullopt) const {
  166. size_t parindex = indexmap(idx);
  167. if (&(this->parent) == this) {
  168. return std::tie(
  169. duoram.database[parindex],
  170. duoram.blind[parindex],
  171. duoram.peer_blinded_db[parindex]);
  172. } else {
  173. return this->parent.get_comp(parindex, null);
  174. }
  175. }
  176. // Get a pair (for the server) of references to a particular field
  177. // of the underlying Duoram entries at share virtual index idx.
  178. template <typename FT>
  179. inline std::tuple<FT&,FT&> get_server(size_t idx, FT T::*field) const {
  180. size_t parindex = indexmap(idx);
  181. if (&(this->parent) == this) {
  182. return std::tie(
  183. duoram.p0_blind[parindex].*field,
  184. duoram.p1_blind[parindex].*field);
  185. } else {
  186. return this->parent.get_server(parindex, field);
  187. }
  188. }
  189. // Get a triple (for the computational players) of references to a
  190. // particular field to the underlying Duoram entries at share
  191. // virtual index idx.
  192. template <typename FT>
  193. inline std::tuple<FT&,FT&,FT&> get_comp(size_t idx, FT T::*field) const {
  194. size_t parindex = indexmap(idx);
  195. if (&(this->parent) == this) {
  196. return std::tie(
  197. duoram.database[parindex].*field,
  198. duoram.blind[parindex].*field,
  199. duoram.peer_blinded_db[parindex].*field);
  200. } else {
  201. return this->parent.get_comp(parindex, field);
  202. }
  203. }
  204. public:
  205. // Get the size
  206. inline size_t size() { return shape_size; }
  207. // Enable or disable explicit-only mode. Only using [] with
  208. // explicit (address_t) indices are allowed in this mode. Using []
  209. // with RegAS or RegXS indices will automatically turn off this
  210. // mode, or you can turn it off explicitly. In explicit-only mode,
  211. // updates to the memory in the Shape will not induce communication
  212. // to the server or peer, but when it turns off, a message of the
  213. // size of the entire Shape will be sent to each of the server and
  214. // the peer. This is useful if you're going to be doing multiple
  215. // explicit writes to every element of the Shape before you do your
  216. // next oblivious read or write. Bitonic sort is a prime example.
  217. void explicitonly(bool enable);
  218. // Create an OblivIndex, non-incrementally (supply the shares of the
  219. // index directly) or incrementally (the bits of the index will be
  220. // supplied later, one at a time)
  221. // Non-incremental, RegXS index
  222. OblivIndex<RegXS,1> oblivindex(const RegXS &idx, nbits_t depth=0) {
  223. if (depth == 0) {
  224. depth = this->addr_size;
  225. }
  226. typename Duoram<T>::OblivIndex<RegXS,1>
  227. res(this->tio, this->yield, idx, depth);
  228. return res;
  229. }
  230. // Non-incremental, RegAS index
  231. OblivIndex<RegAS,1> oblivindex(const RegAS &idx, nbits_t depth=0) {
  232. if (depth == 0) {
  233. depth = this->addr_size;
  234. }
  235. typename Duoram<T>::OblivIndex<RegAS,1>
  236. res(this->tio, this->yield, idx, depth);
  237. return res;
  238. }
  239. // Incremental (requires RegXS index, supplied bit-by-bit later)
  240. OblivIndex<RegXS,1> oblivindex(nbits_t depth=0) {
  241. if (depth == 0) {
  242. depth = this->addr_size;
  243. }
  244. typename Duoram<T>::OblivIndex<RegXS,1>
  245. res(this->tio, this->yield, depth);
  246. return res;
  247. }
  248. // For debugging or checking your answers (using this in general is
  249. // of course insecure)
  250. // This one reconstructs the whole database
  251. std::vector<T> reconstruct() const;
  252. // This one reconstructs a single database value
  253. T reconstruct(const T& share) const;
  254. };
  255. // The most basic shape is Flat. It is almost always the topmost shape,
  256. // and serves to provide MPCTIO and yield_t context to a Duoram without
  257. // changing the indices or size (but can specify a subrange if desired).
  258. template <typename T>
  259. class Duoram<T>::Flat : public Duoram<T>::Shape {
  260. // If this is a subrange, start may be non-0, but it's usually 0
  261. size_t start;
  262. size_t len;
  263. inline size_t indexmap(size_t idx) const {
  264. size_t paridx = idx + start;
  265. return paridx;
  266. }
  267. // Internal function to aid bitonic_sort
  268. void butterfly(address_t start, address_t len, bool dir);
  269. public:
  270. // Constructor. len=0 means the maximum size (the parent's size
  271. // minus start).
  272. Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield, size_t start = 0,
  273. size_t len = 0);
  274. // Copy the given Flat except for the tio and yield
  275. Flat(const Flat &copy_from, MPCTIO &tio, yield_t &yield) :
  276. Shape(copy_from, tio, yield), start(copy_from.start),
  277. len(copy_from.len) {}
  278. // Update the context (MPCTIO and yield if you've started a new
  279. // thread, or just yield if you've started a new coroutine in the
  280. // same thread). Returns a new Shape with an updated context.
  281. Flat context(MPCTIO &new_tio, yield_t &new_yield) const {
  282. return Flat(*this, new_tio, new_yield);
  283. }
  284. Flat context(yield_t &new_yield) const {
  285. return Flat(*this, this->tio, new_yield);
  286. }
  287. // Index into this Flat in various ways
  288. typename Duoram::Shape::template MemRefS<RegAS,T,std::nullopt_t,Flat,1>
  289. operator[](const RegAS &idx) {
  290. typename Duoram<T>::Shape::
  291. template MemRefS<RegAS,T,std::nullopt_t,Flat,1>
  292. res(*this, idx, std::nullopt);
  293. return res;
  294. }
  295. typename Duoram::Shape::template MemRefS<RegXS,T,std::nullopt_t,Flat,1>
  296. operator[](const RegXS &idx) {
  297. typename Duoram<T>::Shape::
  298. template MemRefS<RegXS,T,std::nullopt_t,Flat,1>
  299. res(*this, idx, std::nullopt);
  300. return res;
  301. }
  302. template <typename U, nbits_t WIDTH>
  303. typename Duoram::Shape::template MemRefS<U,T,std::nullopt_t,Flat,WIDTH>
  304. operator[](OblivIndex<U,WIDTH> &obidx) {
  305. typename Duoram<T>::Shape::
  306. template MemRefS<RegXS,T,std::nullopt_t,Flat,1>
  307. res(*this, obidx, std::nullopt);
  308. return res;
  309. }
  310. typename Duoram::Shape::template MemRefExpl<T,std::nullopt_t>
  311. operator[](address_t idx) {
  312. typename Duoram<T>::Shape::
  313. template MemRefExpl<T,std::nullopt_t>
  314. res(*this, idx, std::nullopt);
  315. return res;
  316. }
  317. template <typename U>
  318. Duoram::Shape::MemRefInd<U, Flat>
  319. operator[](const std::vector<U> &indcs) {
  320. typename Duoram<T>::Shape::
  321. template MemRefInd<U,Flat>
  322. res(*this, indcs);
  323. return res;
  324. }
  325. template <typename U, size_t N>
  326. Duoram::Shape::MemRefInd<U, Flat>
  327. operator[](const std::array<U,N> &indcs) {
  328. typename Duoram<T>::Shape::
  329. template MemRefInd<U,Flat>
  330. res(*this, indcs);
  331. return res;
  332. }
  333. // Oblivious sort the elements indexed by the two given indices.
  334. // Without reconstructing the values, if dir=0, this[idx1] will
  335. // become a share of the smaller of the reconstructed values, and
  336. // this[idx2] will become a share of the larger. If dir=1, it's the
  337. // other way around.
  338. //
  339. // Note: this only works for additively shared databases
  340. template<typename U,typename V>
  341. void osort(const U &idx1, const V &idx2, bool dir=0);
  342. // Bitonic sort the elements from start to start+len-1, in
  343. // increasing order if dir=0 or decreasing order if dir=1. Note that
  344. // the elements must be at most 63 bits long each for the notion of
  345. // ">" to make consistent sense.
  346. void bitonic_sort(address_t start, address_t len, bool dir=0);
  347. // Assuming the memory is already sorted, do an oblivious binary
  348. // search for the smallest index containing the value at least the
  349. // given one. (The answer will be the length of the Flat if all
  350. // elements are smaller than the target.) Only available for additive
  351. // shared databases for now.
  352. // The basic version uses log(N) ORAM reads of size N, where N is
  353. // the smallest power of 2 strictly larger than the Flat size
  354. RegAS basic_binary_search(RegAS &target);
  355. // This version does 1 ORAM read of size 2, 1 of size 4, 1 of size
  356. // 8, ..., 1 of size N/2, where N is the smallest power of 2
  357. // strictly larger than the Flat size
  358. RegXS binary_search(RegAS &target);
  359. };
  360. // Oblivious indices for use in related-index ORAM accesses.
  361. template <typename T>
  362. template <typename U, nbits_t WIDTH>
  363. class Duoram<T>::OblivIndex {
  364. template <typename Ux,typename FT,typename FST,typename Sh,nbits_t WIDTHx>
  365. friend class Shape::MemRefS;
  366. int player;
  367. std::optional<RDPFTriple<WIDTH>> dt;
  368. std::optional<RDPFPair<WIDTH>> dp;
  369. nbits_t curdepth, maxdepth;
  370. nbits_t next_windex;
  371. bool incremental;
  372. U idx;
  373. public:
  374. // Non-incremental constructor
  375. OblivIndex(MPCTIO &tio, yield_t &yield, const U &idx, nbits_t depth) :
  376. player(tio.player()), curdepth(depth), maxdepth(depth),
  377. next_windex(0), incremental(false), idx(idx)
  378. {
  379. if (player < 2) {
  380. dt = tio.rdpftriple(yield, depth);
  381. } else {
  382. dp = tio.rdpfpair(yield, depth);
  383. }
  384. }
  385. // Incremental constructor: only for U=RegXS
  386. OblivIndex(MPCTIO &tio, yield_t &yield, nbits_t depth) :
  387. player(tio.player()), curdepth(0), maxdepth(depth),
  388. next_windex(0), incremental(true), idx(RegXS())
  389. {
  390. if (player < 2) {
  391. dt = tio.rdpftriple(yield, depth, true);
  392. } else {
  393. dp = tio.rdpfpair(yield, depth, true);
  394. }
  395. }
  396. // Incrementally append a (shared) bit to the oblivious index
  397. void incr(RegBS bit)
  398. {
  399. assert(incremental);
  400. idx.xshare = (idx.xshare << 1) | value_t(bit.bshare);
  401. ++curdepth;
  402. if (player < 2) {
  403. dt->depth(curdepth);
  404. } else {
  405. dp->depth(curdepth);
  406. }
  407. }
  408. // Get a copy of the index
  409. U index() { return idx; }
  410. };
  411. // An additive or XOR shared memory reference. You get one of these
  412. // from a Shape A and an additively shared RegAS index x, or an XOR
  413. // shared RegXS index x, with A[x]. Then you perform operations on this
  414. // object, which do the Duoram operations. As above, T is the sharing
  415. // type of the data in the database, while U is the sharing type of the
  416. // index used to create this memory reference. If we are referencing an
  417. // entire entry of type T, then the field type FT will equal T, and the
  418. // field selector type FST will be nullopt_t. If we are referencing a
  419. // particular field of T, then FT will be the type of the field (RegAS
  420. // or RegXS) and FST will be a pointer-to-member T::* type pointing to
  421. // that field. Sh is the specific Shape subtype used to create the
  422. // MemRefS. WIDTH is the RDPF width to use.
  423. template <typename T>
  424. template <typename U, typename FT, typename FST, typename Sh, nbits_t WIDTH>
  425. class Duoram<T>::Shape::MemRefS {
  426. Sh &shape;
  427. // oblividx is a reference to the OblivIndex we're using. In the
  428. // common case, we own the actual OblivIndex, and it's stored in
  429. // our_oblividx, and oblividx is a pointer to that. Sometimes
  430. // (for example incremental ORAM accesses), the caller will own (and
  431. // modify between uses) the OblivIndex. In that case, oblividx will
  432. // be a pointer to the caller's OblivIndex object, and
  433. // our_oblividx will be nullopt.
  434. std::optional<Duoram<T>::OblivIndex<U,WIDTH>> our_oblividx;
  435. Duoram<T>::OblivIndex<U,WIDTH> *oblividx;
  436. FST fieldsel;
  437. private:
  438. // Oblivious update to a shared index of Duoram memory, only for
  439. // FT = RegAS or RegXS
  440. MemRefS<U,FT,FST,Sh,WIDTH> &oram_update(const FT& M, const prac_template_true&);
  441. // Oblivious update to a shared index of Duoram memory, for
  442. // FT not RegAS or RegXS
  443. MemRefS<U,FT,FST,Sh,WIDTH> &oram_update(const FT& M, const prac_template_false&);
  444. public:
  445. MemRefS<U,FT,FST,Sh,WIDTH>(Sh &shape, const U &idx, FST fieldsel) :
  446. shape(shape), fieldsel(fieldsel) {
  447. our_oblividx.emplace(shape.tio, shape.yield, idx,
  448. shape.addr_size);
  449. oblividx = &(*our_oblividx);
  450. }
  451. MemRefS<U,FT,FST,Sh,WIDTH>(Sh &shape, OblivIndex<U,WIDTH> &obidx, FST fieldsel) :
  452. shape(shape), fieldsel(fieldsel) {
  453. oblividx = &obidx;
  454. }
  455. // Create a MemRefExpl for accessing a partcular field of T
  456. template <typename SFT>
  457. MemRefS<U,SFT,SFT T::*,Sh,WIDTH> field(SFT T::*subfieldsel) {
  458. auto res = MemRefS<U,SFT,SFT T::*,Sh,WIDTH>(this->shape,
  459. oblividx->idx, subfieldsel);
  460. return res;
  461. }
  462. // Oblivious read from a shared index of Duoram memory
  463. operator FT();
  464. // Oblivious update to a shared index of Duoram memory
  465. MemRefS<U,FT,FST,Sh,WIDTH> &operator+=(const FT& M);
  466. // Oblivious write to a shared index of Duoram memory
  467. MemRefS<U,FT,FST,Sh,WIDTH> &operator=(const FT& M);
  468. };
  469. // An explicit memory reference. You get one of these from a Shape A
  470. // and an address_t index x with A[x]. Then you perform operations on
  471. // this object, which update the Duoram state without performing Duoram
  472. // operations. If we are referencing an entire entry of type T, then
  473. // the field type FT will equal T, and the field selector type FST will
  474. // be nullopt_t. If we are referencing a particular field of T, then FT
  475. // will be the type of the field (RegAS or RegXS) and FST will be a
  476. // pointer-to-member T::* type pointing to that field.
  477. template <typename T> template <typename FT, typename FST>
  478. class Duoram<T>::Shape::MemRefExpl {
  479. Shape &shape;
  480. address_t idx;
  481. FST fieldsel;
  482. public:
  483. MemRefExpl(Shape &shape, address_t idx, FST fieldsel) :
  484. shape(shape), idx(idx), fieldsel(fieldsel) {}
  485. // Create a MemRefExpl for accessing a partcular field of T
  486. template <typename SFT>
  487. MemRefExpl<SFT,SFT T::*> field(SFT T::*subfieldsel) {
  488. auto res = MemRefExpl<SFT,SFT T::*>(this->shape, idx, subfieldsel);
  489. return res;
  490. }
  491. // Explicit read from a given index of Duoram memory
  492. operator FT();
  493. // Explicit update to a given index of Duoram memory
  494. MemRefExpl &operator+=(const FT& M);
  495. // Explicit write to a given index of Duoram memory
  496. MemRefExpl &operator=(const FT& M);
  497. // Convenience function
  498. MemRefExpl &operator-=(const FT& M) { *this += (-M); return *this; }
  499. };
  500. // A collection of independent memory references that can be processed
  501. // simultaneously. You get one of these from a Shape A (of specific
  502. // subclass Sh) and a vector or array of indices v with each element of
  503. // type U.
  504. template <typename T> template <typename U, typename Sh>
  505. class Duoram<T>::Shape::MemRefInd {
  506. Sh &shape;
  507. std::vector<U> indcs;
  508. public:
  509. MemRefInd(Sh &shape, std::vector<U> indcs) :
  510. shape(shape), indcs(indcs) {}
  511. template <size_t N>
  512. MemRefInd(Sh &shape, std::array<U,N> aindcs) :
  513. shape(shape) { for ( auto &i : aindcs ) { indcs.push_back(i); } }
  514. // Independent reads from shared or explicit indices of Duoram memory
  515. operator std::vector<T>();
  516. // Independent updates to shared or explicit indices of Duoram memory
  517. MemRefInd &operator+=(const std::vector<T>& M);
  518. template <size_t N>
  519. MemRefInd &operator+=(const std::array<T,N>& M);
  520. // Independent writes to shared or explicit indices of Duoram memory
  521. MemRefInd &operator=(const std::vector<T>& M);
  522. template <size_t N>
  523. MemRefInd &operator=(const std::array<T,N>& M);
  524. // Convenience function
  525. MemRefInd &operator-=(const std::vector<T>& M) { *this += (-M); return *this; }
  526. template <size_t N>
  527. MemRefInd &operator-=(const std::array<T,N>& M) { *this += (-M); return *this; }
  528. };
  529. #include "duoram.tcc"
  530. #endif