duoram.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #ifndef __DUORAM_HPP__
  2. #define __DUORAM_HPP__
  3. #include "types.hpp"
  4. #include "mpcio.hpp"
  5. #include "coroutine.hpp"
  6. // Implementation of the 3-party protocols described in:
  7. // Adithya Vadapalli, Ryan Henry, Ian Goldberg, "Duoram: A
  8. // Bandwidth-Efficient Distributed ORAM for 2- and 3-Party Computation".
  9. // A Duoram object is like physical memory: it's just a flat address
  10. // space, and you can't access it directly. Instead, you need to access
  11. // it through a "Shape", such as Flat, Tree, Path, etc. Shapes can be
  12. // nested, so you can have a Path of a Subtree of a Tree sitting on the
  13. // base Duoram. Each Shape's parent must remain in scope (references to
  14. // it must remain valid) for the lifetime of the child Shapre. Each
  15. // shape is bound to a context, which is a thread-specific MPCTIO and a
  16. // coroutine-specific yield_t. If you launch new threads and/or
  17. // coroutines, you'll need to make a copy of the current Shape for your
  18. // new context, and call context() on it. Be sure not to call context()
  19. // on a Shape shared with other threads or coroutines.
  20. // This is templated, because you can have a Duoram of additively shared
  21. // (RegAS) or XOR shared (RegXS) elements, or std::arrays of those to
  22. // get "wide" memory cells.
  23. // The initial implementation is focused on additive shares.
  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. // Pass the player number and desired size
  49. Duoram(int player, size_t size);
  50. // Get the size
  51. inline size_t size() { return oram_size; }
  52. // Get the basic Flat shape for this Duoram
  53. Flat flat(MPCTIO &tio, yield_t &yield, size_t start = 0,
  54. size_t len = 0) {
  55. return Flat(*this, tio, yield, start, len);
  56. }
  57. // For debugging; print the contents of the Duoram to stdout
  58. void dump() const;
  59. };
  60. // The parent class of all Shapes. This is an abstract class that
  61. // cannot itself be instantiated.
  62. template <typename T>
  63. class Duoram<T>::Shape {
  64. // Subclasses should be able to access _other_ Shapes' indexmap
  65. friend class Flat;
  66. // When you index into a shape (A[x]), you get one of these types,
  67. // depending on the type of x (the index), _not_ on the type T (the
  68. // underlying type of the Duoram). That is, you can have an
  69. // additive-shared index (x) into an XOR-shared database (T), for
  70. // example.
  71. // The parent class of the MemRef* classes, used when deferencing a
  72. // Shape with A[x]
  73. class MemRef;
  74. // When x is additively or XOR shared
  75. // U is the sharing type of the indices, while T is the sharing type
  76. // of the data in the database.
  77. template <typename U>
  78. class MemRefS;
  79. // When x is unshared explicit value
  80. class MemRefExpl;
  81. // When x is a vector or array of values of type U, used to denote a
  82. // collection of independent memory operations that can be performed
  83. // simultaneously. Sh is the specific Shape subtype used to create
  84. // the MemRefInd.
  85. template <typename U, typename Sh>
  86. class MemRefInd;
  87. protected:
  88. // A reference to the parent shape. As with ".." in the root
  89. // directory of a filesystem, the topmost shape is indicated by
  90. // having parent = *this.
  91. const Shape &parent;
  92. // A reference to the backing physical storage
  93. Duoram &duoram;
  94. // The size of this shape
  95. size_t shape_size;
  96. // The number of bits needed to address this shape (the number of
  97. // bits in shape_size-1)
  98. nbits_t addr_size;
  99. // And a mask with the low addr_size bits set
  100. address_t addr_mask;
  101. // The Shape's context (MPCTIO and yield_t)
  102. MPCTIO &tio;
  103. yield_t &yield;
  104. // If you enable explicit-only mode, sending updates of your blind
  105. // to the server and of your blinded database to your peer will be
  106. // temporarily disabled. When you disable it (which will happen
  107. // automatically at the next ORAM read or write, or you can do it
  108. // explicitly), new random blinds will be chosen for the whole
  109. // Shape, and the blinds sent to the server, and the blinded
  110. // database sent to the peer.
  111. bool explicitmode;
  112. // A function to set the shape_size and compute addr_size and
  113. // addr_mask
  114. void set_shape_size(size_t sz);
  115. // We need a constructor because we hold non-static references; this
  116. // constructor is called by the subclass constructors
  117. Shape(const Shape &parent, Duoram &duoram, MPCTIO &tio,
  118. yield_t &yield) : parent(parent), duoram(duoram), shape_size(0),
  119. tio(tio), yield(yield), explicitmode(false) {}
  120. // Copy the given Shape except for the tio and yield
  121. Shape(const Shape &copy_from, MPCTIO &tio, yield_t &yield) :
  122. parent(copy_from.parent), duoram(copy_from.duoram),
  123. shape_size(copy_from.shape_size),
  124. addr_size(copy_from.addr_size), addr_mask(copy_from.addr_mask),
  125. tio(tio), yield(yield),
  126. explicitmode(copy_from.explicitmode) {}
  127. // The index-mapping function. Input the index relative to this
  128. // shape, and output the corresponding physical address. The
  129. // strategy is to map the index relative to this shape to the index
  130. // relative to the parent shape, call the parent's indexmap function
  131. // on that (unless this is the topmost shape), and return what it
  132. // returns. If this is the topmost shape, just return what you
  133. // would have passed to the parent's indexmap.
  134. //
  135. // This is a pure virtual function; all subclasses of Shape must
  136. // implement it, and of course Shape itself therefore cannot be
  137. // instantiated.
  138. virtual size_t indexmap(size_t idx) const = 0;
  139. // Get a pair (for the server) of references to the underlying
  140. // Duoram entries at share virtual index idx. (That is, it gets
  141. // duoram.p0_blind[indexmap(idx)], etc.)
  142. inline std::tuple<T&,T&> get_server(size_t idx) const {
  143. size_t physaddr = indexmap(idx);
  144. return std::tie(
  145. duoram.p0_blind[physaddr],
  146. duoram.p1_blind[physaddr]);
  147. }
  148. // Get a triple (for the computational players) of references to the
  149. // underlying Duoram entries at share virtual index idx. (That is,
  150. // it gets duoram.database[indexmap(idx)], etc.)
  151. inline std::tuple<T&,T&,T&> get_comp(size_t idx) const {
  152. size_t physaddr = indexmap(idx);
  153. return std::tie(
  154. duoram.database[physaddr],
  155. duoram.blind[physaddr],
  156. duoram.peer_blinded_db[physaddr]);
  157. }
  158. public:
  159. // Get the size
  160. inline size_t size() { return shape_size; }
  161. // Index into this Shape in various ways
  162. MemRefS<RegAS> operator[](const RegAS &idx) { return MemRefS<RegAS>(*this, idx); }
  163. MemRefS<RegXS> operator[](const RegXS &idx) { return MemRefS<RegXS>(*this, idx); }
  164. MemRefExpl operator[](address_t idx) { return MemRefExpl(*this, idx); }
  165. // Enable or disable explicit-only mode. Only using [] with
  166. // explicit (address_t) indices are allowed in this mode. Using []
  167. // with RegAS or RegXS indices will automatically turn off this
  168. // mode, or you can turn it off explicitly. In explicit-only mode,
  169. // updates to the memory in the Shape will not induce communication
  170. // to the server or peer, but when it turns off, a message of the
  171. // size of the entire Shape will be sent to each of the server and
  172. // the peer. This is useful if you're going to be doing multiple
  173. // explicit writes to every element of the Shape before you do your
  174. // next oblivious read or write. Bitonic sort is a prime example.
  175. void explicitonly(bool enable);
  176. // For debugging or checking your answers (using this in general is
  177. // of course insecure)
  178. // This one reconstructs the whole database
  179. std::vector<T> reconstruct() const;
  180. // This one reconstructs a single database value
  181. T reconstruct(const T& share) const;
  182. };
  183. // The most basic shape is Flat. It is almost always the topmost shape,
  184. // and serves to provide MPCTIO and yield_t context to a Duoram without
  185. // changing the indices or size (but can specify a subrange if desired).
  186. template <typename T>
  187. class Duoram<T>::Flat : public Duoram<T>::Shape {
  188. // If this is a subrange, start may be non-0, but it's usually 0
  189. size_t start;
  190. size_t len;
  191. inline size_t indexmap(size_t idx) const {
  192. size_t paridx = idx + start;
  193. if (&(this->parent) == this) {
  194. return paridx;
  195. } else {
  196. return this->parent.indexmap(paridx);
  197. }
  198. }
  199. // Internal function to aid bitonic_sort
  200. void butterfly(address_t start, nbits_t depth, bool dir);
  201. public:
  202. // Constructor. len=0 means the maximum size (the parent's size
  203. // minus start).
  204. Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield, size_t start = 0,
  205. size_t len = 0);
  206. // Copy the given Flat except for the tio and yield
  207. Flat(const Flat &copy_from, MPCTIO &tio, yield_t &yield) :
  208. Shape(copy_from, tio, yield), start(copy_from.start),
  209. len(copy_from.len) {}
  210. // Update the context (MPCTIO and yield if you've started a new
  211. // thread, or just yield if you've started a new coroutine in the
  212. // same thread). Returns a new Shape with an updated context.
  213. Flat context(MPCTIO &new_tio, yield_t &new_yield) const {
  214. return Flat(*this, new_tio, new_yield);
  215. }
  216. Flat context(yield_t &new_yield) const {
  217. return Flat(*this, this->tio, new_yield);
  218. }
  219. // Generate independent memory references for this Flat
  220. template <typename U>
  221. Duoram::Shape::MemRefInd<U, Flat> indep(const std::vector<U> &indcs) {
  222. typename Duoram<T>::Shape::template MemRefInd<U,Flat> res(*this, indcs);
  223. return res;
  224. }
  225. template <typename U, size_t N>
  226. Duoram::Shape::MemRefInd<U, Flat> indep(const std::array<U,N> &indcs) {
  227. typename Duoram<T>::Shape::template MemRefInd<U,Flat> res(*this, indcs);
  228. return res;
  229. }
  230. // Oblivious sort the elements indexed by the two given indices.
  231. // Without reconstructing the values, if dir=0, this[idx1] will
  232. // become a share of the smaller of the reconstructed values, and
  233. // this[idx2] will become a share of the larger. If dir=1, it's the
  234. // other way around.
  235. //
  236. // Note: this only works for additively shared databases
  237. template<typename U,typename V>
  238. void osort(const U &idx1, const V &idx2, bool dir=0);
  239. // Bitonic sort the elements from start to start+(1<<depth)-1, in
  240. // increasing order if dir=0 or decreasing order if dir=1. Note that
  241. // the elements must be at most 63 bits long each for the notion of
  242. // ">" to make consistent sense.
  243. void bitonic_sort(address_t start, nbits_t depth, bool dir=0);
  244. // Assuming the memory is already sorted, do an oblivious binary
  245. // search for the largest index containing the value at most the
  246. // given one. (The answer will be 0 if all of the memory elements
  247. // are greate than the target.) This Flat must be a power of 2 size.
  248. // Only available for additive shared databases for now.
  249. RegAS obliv_binary_search(RegAS &target);
  250. };
  251. // The parent class of shared memory references
  252. template <typename T>
  253. class Duoram<T>::Shape::MemRef {
  254. protected:
  255. Shape &shape;
  256. MemRef(Shape &shape): shape(shape) {}
  257. public:
  258. // Oblivious read from a shared index of Duoram memory
  259. virtual operator T() = 0;
  260. // Oblivious update to a shared index of Duoram memory
  261. virtual MemRef &operator+=(const T& M) = 0;
  262. // Oblivious write to a shared index of Duoram memory
  263. virtual MemRef &operator=(const T& M) = 0;
  264. // Convenience function
  265. MemRef &operator-=(const T& M) { *this += (-M); return *this; }
  266. };
  267. // An additive or XOR shared memory reference. You get one of these
  268. // from a Shape A and an additively shared RegAS index x, or an XOR
  269. // shared RegXS index x, with A[x]. Then you perform operations on this
  270. // object, which do the Duoram operations. As above, T is the sharing
  271. // type of the data in the database, while U is the sharing type of the
  272. // index used to create this memory reference.
  273. template <typename T> template <typename U>
  274. class Duoram<T>::Shape::MemRefS : public Duoram<T>::Shape::MemRef {
  275. U idx;
  276. private:
  277. // Oblivious update to a shared index of Duoram memory, only for
  278. // ST = RegAS or RegXS
  279. template <typename ST>
  280. MemRefS<U> &oram_update(const ST& M, const prac_template_true&);
  281. // Oblivious update to a shared index of Duoram memory, for
  282. // ST not RegAS or RegXS
  283. template <typename ST>
  284. MemRefS<U> &oram_update(const ST& M, const prac_template_false&);
  285. public:
  286. MemRefS<U>(Shape &shape, const U &idx) :
  287. MemRef(shape), idx(idx) {}
  288. // Oblivious read from a shared index of Duoram memory
  289. operator T() override;
  290. // Oblivious update to a shared index of Duoram memory
  291. MemRefS<U> &operator+=(const T& M) override;
  292. // Oblivious write to a shared index of Duoram memory
  293. MemRefS<U> &operator=(const T& M) override;
  294. };
  295. // An explicit memory reference. You get one of these from a Shape A
  296. // and an address_t index x with A[x]. Then you perform operations on
  297. // this object, which update the Duoram state without performing Duoram
  298. // operations.
  299. template <typename T>
  300. class Duoram<T>::Shape::MemRefExpl : public Duoram<T>::Shape::MemRef {
  301. address_t idx;
  302. public:
  303. MemRefExpl(Shape &shape, address_t idx) :
  304. MemRef(shape), idx(idx) {}
  305. // Explicit read from a given index of Duoram memory
  306. operator T() override;
  307. // Explicit update to a given index of Duoram memory
  308. MemRefExpl &operator+=(const T& M) override;
  309. // Explicit write to a given index of Duoram memory
  310. MemRefExpl &operator=(const T& M) override;
  311. // Convenience function
  312. MemRefExpl &operator-=(const T& M) { *this += (-M); return *this; }
  313. };
  314. // A collection of independent memory references that can be processed
  315. // simultaneously. You get one of these from a Shape A (of specific
  316. // subclass Sh) and a vector or array of indices v with each element of
  317. // type U.
  318. template <typename T> template <typename U, typename Sh>
  319. class Duoram<T>::Shape::MemRefInd {
  320. Sh &shape;
  321. std::vector<U> indcs;
  322. public:
  323. MemRefInd(Sh &shape, std::vector<U> indcs) :
  324. shape(shape), indcs(indcs) {}
  325. template <size_t N>
  326. MemRefInd(Sh &shape, std::array<U,N> aindcs) :
  327. shape(shape) { for ( auto &i : aindcs ) { indcs.push_back(i); } }
  328. // Independent reads from shared or explicit indices of Duoram memory
  329. operator std::vector<T>();
  330. // Independent updates to shared or explicit indices of Duoram memory
  331. MemRefInd &operator+=(const std::vector<T>& M);
  332. template <size_t N>
  333. MemRefInd &operator+=(const std::array<T,N>& M);
  334. // Independent writes to shared or explicit indices of Duoram memory
  335. MemRefInd &operator=(const std::vector<T>& M);
  336. template <size_t N>
  337. MemRefInd &operator=(const std::array<T,N>& M);
  338. // Convenience function
  339. MemRefInd &operator-=(const std::vector<T>& M) { *this += (-M); return *this; }
  340. template <size_t N>
  341. MemRefInd &operator-=(const std::array<T,N>& M) { *this += (-M); return *this; }
  342. };
  343. #include "duoram.tcc"
  344. #endif