duoram.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. // For debugging or checking your answers (using this in general is
  219. // of course insecure)
  220. // This one reconstructs the whole database
  221. std::vector<T> reconstruct() const;
  222. // This one reconstructs a single database value
  223. T reconstruct(const T& share) const;
  224. };
  225. // The most basic shape is Flat. It is almost always the topmost shape,
  226. // and serves to provide MPCTIO and yield_t context to a Duoram without
  227. // changing the indices or size (but can specify a subrange if desired).
  228. template <typename T>
  229. class Duoram<T>::Flat : public Duoram<T>::Shape {
  230. // If this is a subrange, start may be non-0, but it's usually 0
  231. size_t start;
  232. size_t len;
  233. inline size_t indexmap(size_t idx) const {
  234. size_t paridx = idx + start;
  235. return paridx;
  236. }
  237. // Internal function to aid bitonic_sort
  238. void butterfly(address_t start, address_t len, bool dir);
  239. public:
  240. // Constructor. len=0 means the maximum size (the parent's size
  241. // minus start).
  242. Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield, size_t start = 0,
  243. size_t len = 0);
  244. // Copy the given Flat except for the tio and yield
  245. Flat(const Flat &copy_from, MPCTIO &tio, yield_t &yield) :
  246. Shape(copy_from, tio, yield), start(copy_from.start),
  247. len(copy_from.len) {}
  248. // Update the context (MPCTIO and yield if you've started a new
  249. // thread, or just yield if you've started a new coroutine in the
  250. // same thread). Returns a new Shape with an updated context.
  251. Flat context(MPCTIO &new_tio, yield_t &new_yield) const {
  252. return Flat(*this, new_tio, new_yield);
  253. }
  254. Flat context(yield_t &new_yield) const {
  255. return Flat(*this, this->tio, new_yield);
  256. }
  257. // Index into this Flat in various ways
  258. typename Duoram::Shape::template MemRefS<RegAS,T,std::nullopt_t,Flat,1>
  259. operator[](const RegAS &idx) {
  260. typename Duoram<T>::Shape::
  261. template MemRefS<RegAS,T,std::nullopt_t,Flat,1>
  262. res(*this, idx, std::nullopt);
  263. return res;
  264. }
  265. typename Duoram::Shape::template MemRefS<RegXS,T,std::nullopt_t,Flat,1>
  266. operator[](const RegXS &idx) {
  267. typename Duoram<T>::Shape::
  268. template MemRefS<RegXS,T,std::nullopt_t,Flat,1>
  269. res(*this, idx, std::nullopt);
  270. return res;
  271. }
  272. typename Duoram::Shape::template MemRefExpl<T,std::nullopt_t>
  273. operator[](address_t idx) {
  274. typename Duoram<T>::Shape::
  275. template MemRefExpl<T,std::nullopt_t>
  276. res(*this, idx, std::nullopt);
  277. return res;
  278. }
  279. template <typename U>
  280. Duoram::Shape::MemRefInd<U, Flat>
  281. operator[](const std::vector<U> &indcs) {
  282. typename Duoram<T>::Shape::
  283. template MemRefInd<U,Flat>
  284. res(*this, indcs);
  285. return res;
  286. }
  287. template <typename U, size_t N>
  288. Duoram::Shape::MemRefInd<U, Flat>
  289. operator[](const std::array<U,N> &indcs) {
  290. typename Duoram<T>::Shape::
  291. template MemRefInd<U,Flat>
  292. res(*this, indcs);
  293. return res;
  294. }
  295. // Oblivious sort the elements indexed by the two given indices.
  296. // Without reconstructing the values, if dir=0, this[idx1] will
  297. // become a share of the smaller of the reconstructed values, and
  298. // this[idx2] will become a share of the larger. If dir=1, it's the
  299. // other way around.
  300. //
  301. // Note: this only works for additively shared databases
  302. template<typename U,typename V>
  303. void osort(const U &idx1, const V &idx2, bool dir=0);
  304. // Bitonic sort the elements from start to start+len-1, in
  305. // increasing order if dir=0 or decreasing order if dir=1. Note that
  306. // the elements must be at most 63 bits long each for the notion of
  307. // ">" to make consistent sense.
  308. void bitonic_sort(address_t start, address_t len, bool dir=0);
  309. // Assuming the memory is already sorted, do an oblivious binary
  310. // search for the smallest index containing the value at least the
  311. // given one. (The answer will be the length of the Flat if all
  312. // elements are smaller than the target.) Only available for additive
  313. // shared databases for now.
  314. RegAS obliv_binary_search(RegAS &target);
  315. };
  316. // Oblivious indices for use in related-index ORAM accesses.
  317. template <typename T>
  318. template <typename U, nbits_t WIDTH>
  319. class Duoram<T>::OblivIndex {
  320. template <typename Ux,typename FT,typename FST,typename Sh,nbits_t WIDTHx>
  321. friend class Shape::MemRefS;
  322. int player;
  323. std::optional<RDPFTriple<WIDTH>> dt;
  324. std::optional<RDPFPair<WIDTH>> dp;
  325. nbits_t curdepth, maxdepth;
  326. nbits_t next_windex;
  327. bool incremental;
  328. U idx;
  329. public:
  330. // Non-incremental constructor
  331. OblivIndex(MPCTIO &tio, yield_t &yield, const U &idx, nbits_t depth) :
  332. player(tio.player()), curdepth(depth), maxdepth(depth),
  333. next_windex(0), incremental(false), idx(idx)
  334. {
  335. if (player < 2) {
  336. dt = tio.rdpftriple(yield, depth);
  337. } else {
  338. dp = tio.rdpfpair(yield, depth);
  339. }
  340. }
  341. // Incremental constructor: only for U=RegXS
  342. OblivIndex(MPCTIO &tio, yield_t &yield, nbits_t depth) :
  343. player(tio.player()), curdepth(0), maxdepth(depth),
  344. next_windex(0), incremental(true), idx(RegXS())
  345. {
  346. if (player < 2) {
  347. dt = tio.rdpftriple(yield, depth, true);
  348. } else {
  349. dp = tio.rdpfpair(yield, depth, true);
  350. }
  351. }
  352. };
  353. // An additive or XOR shared memory reference. You get one of these
  354. // from a Shape A and an additively shared RegAS index x, or an XOR
  355. // shared RegXS index x, with A[x]. Then you perform operations on this
  356. // object, which do the Duoram operations. As above, T is the sharing
  357. // type of the data in the database, while U is the sharing type of the
  358. // index used to create this memory reference. If we are referencing an
  359. // entire entry of type T, then the field type FT will equal T, and the
  360. // field selector type FST will be nullopt_t. If we are referencing a
  361. // particular field of T, then FT will be the type of the field (RegAS
  362. // or RegXS) and FST will be a pointer-to-member T::* type pointing to
  363. // that field. Sh is the specific Shape subtype used to create the
  364. // MemRefS. WIDTH is the RDPF width to use.
  365. template <typename T>
  366. template <typename U, typename FT, typename FST, typename Sh, nbits_t WIDTH>
  367. class Duoram<T>::Shape::MemRefS {
  368. Sh &shape;
  369. // oblividx is a reference to the OblivIndex we're using. In the
  370. // common case, we own the actual OblivIndex, and it's stored in
  371. // our_oblividx, and oblividx is a pointer to that. Sometimes
  372. // (for example incremental ORAM accesses), the caller will own (and
  373. // modify between uses) the OblivIndex. In that case, oblividx will
  374. // be a pointer to the caller's OblivIndex object, and
  375. // our_oblividx will be nullopt.
  376. std::optional<Duoram<T>::OblivIndex<U,WIDTH>> our_oblividx;
  377. Duoram<T>::OblivIndex<U,WIDTH> *oblividx;
  378. FST fieldsel;
  379. private:
  380. // Oblivious update to a shared index of Duoram memory, only for
  381. // FT = RegAS or RegXS
  382. MemRefS<U,FT,FST,Sh,WIDTH> &oram_update(const FT& M, const prac_template_true&);
  383. // Oblivious update to a shared index of Duoram memory, for
  384. // FT not RegAS or RegXS
  385. MemRefS<U,FT,FST,Sh,WIDTH> &oram_update(const FT& M, const prac_template_false&);
  386. public:
  387. MemRefS<U,FT,FST,Sh,WIDTH>(Sh &shape, const U &idx, FST fieldsel) :
  388. shape(shape), fieldsel(fieldsel) {
  389. our_oblividx.emplace(shape.tio, shape.yield, idx,
  390. shape.addr_size);
  391. oblividx = &(*our_oblividx);
  392. }
  393. // Create a MemRefExpl for accessing a partcular field of T
  394. template <typename SFT>
  395. MemRefS<U,SFT,SFT T::*,Sh,WIDTH> field(SFT T::*subfieldsel) {
  396. auto res = MemRefS<U,SFT,SFT T::*,Sh,WIDTH>(this->shape,
  397. oblividx->idx, subfieldsel);
  398. return res;
  399. }
  400. // Oblivious read from a shared index of Duoram memory
  401. operator FT();
  402. // Oblivious update to a shared index of Duoram memory
  403. MemRefS<U,FT,FST,Sh,WIDTH> &operator+=(const FT& M);
  404. // Oblivious write to a shared index of Duoram memory
  405. MemRefS<U,FT,FST,Sh,WIDTH> &operator=(const FT& M);
  406. };
  407. // An explicit memory reference. You get one of these from a Shape A
  408. // and an address_t index x with A[x]. Then you perform operations on
  409. // this object, which update the Duoram state without performing Duoram
  410. // operations. If we are referencing an entire entry of type T, then
  411. // the field type FT will equal T, and the field selector type FST will
  412. // be nullopt_t. If we are referencing a particular field of T, then FT
  413. // will be the type of the field (RegAS or RegXS) and FST will be a
  414. // pointer-to-member T::* type pointing to that field.
  415. template <typename T> template <typename FT, typename FST>
  416. class Duoram<T>::Shape::MemRefExpl {
  417. Shape &shape;
  418. address_t idx;
  419. FST fieldsel;
  420. public:
  421. MemRefExpl(Shape &shape, address_t idx, FST fieldsel) :
  422. shape(shape), idx(idx), fieldsel(fieldsel) {}
  423. // Create a MemRefExpl for accessing a partcular field of T
  424. template <typename SFT>
  425. MemRefExpl<SFT,SFT T::*> field(SFT T::*subfieldsel) {
  426. auto res = MemRefExpl<SFT,SFT T::*>(this->shape, idx, subfieldsel);
  427. return res;
  428. }
  429. // Explicit read from a given index of Duoram memory
  430. operator FT();
  431. // Explicit update to a given index of Duoram memory
  432. MemRefExpl &operator+=(const FT& M);
  433. // Explicit write to a given index of Duoram memory
  434. MemRefExpl &operator=(const FT& M);
  435. // Convenience function
  436. MemRefExpl &operator-=(const FT& M) { *this += (-M); return *this; }
  437. };
  438. // A collection of independent memory references that can be processed
  439. // simultaneously. You get one of these from a Shape A (of specific
  440. // subclass Sh) and a vector or array of indices v with each element of
  441. // type U.
  442. template <typename T> template <typename U, typename Sh>
  443. class Duoram<T>::Shape::MemRefInd {
  444. Sh &shape;
  445. std::vector<U> indcs;
  446. public:
  447. MemRefInd(Sh &shape, std::vector<U> indcs) :
  448. shape(shape), indcs(indcs) {}
  449. template <size_t N>
  450. MemRefInd(Sh &shape, std::array<U,N> aindcs) :
  451. shape(shape) { for ( auto &i : aindcs ) { indcs.push_back(i); } }
  452. // Independent reads from shared or explicit indices of Duoram memory
  453. operator std::vector<T>();
  454. // Independent updates to shared or explicit indices of Duoram memory
  455. MemRefInd &operator+=(const std::vector<T>& M);
  456. template <size_t N>
  457. MemRefInd &operator+=(const std::array<T,N>& M);
  458. // Independent writes to shared or explicit indices of Duoram memory
  459. MemRefInd &operator=(const std::vector<T>& M);
  460. template <size_t N>
  461. MemRefInd &operator=(const std::array<T,N>& M);
  462. // Convenience function
  463. MemRefInd &operator-=(const std::vector<T>& M) { *this += (-M); return *this; }
  464. template <size_t N>
  465. MemRefInd &operator-=(const std::array<T,N>& M) { *this += (-M); return *this; }
  466. };
  467. #include "duoram.tcc"
  468. #endif