duoram.hpp 15 KB

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