duoram.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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
  70. class MemRef;
  71. // When x is additively or XOR shared
  72. // U is the sharing type of the indices, while T is the sharing type
  73. // of the data in the database.
  74. template <typename U>
  75. class MemRefS;
  76. // When x is unshared explicit value
  77. class MemRefExpl;
  78. protected:
  79. // A reference to the parent shape. As with ".." in the root
  80. // directory of a filesystem, the topmost shape is indicated by
  81. // having parent = *this.
  82. const Shape &parent;
  83. // A reference to the backing physical storage
  84. Duoram &duoram;
  85. // The size of this shape
  86. size_t shape_size;
  87. // The number of bits needed to address this shape (the number of
  88. // bits in shape_size-1)
  89. nbits_t addr_size;
  90. // And a mask with the low addr_size bits set
  91. address_t addr_mask;
  92. // The Shape's context (MPCTIO and yield_t)
  93. MPCTIO &tio;
  94. yield_t &yield;
  95. // If you enable explicit-only mode, sending updates of your blind
  96. // to the server and of your blinded database to your peer will be
  97. // temporarily disabled. When you disable it (which will happen
  98. // automatically at the next ORAM read or write, or you can do it
  99. // explicitly), new random blinds will be chosen for the whole
  100. // Shape, and the blinds sent to the server, and the blinded
  101. // database sent to the peer.
  102. bool explicitmode;
  103. // A function to set the shape_size and compute addr_size and
  104. // addr_mask
  105. void set_shape_size(size_t sz);
  106. // We need a constructor because we hold non-static references; this
  107. // constructor is called by the subclass constructors
  108. Shape(const Shape &parent, Duoram &duoram, MPCTIO &tio,
  109. yield_t &yield) : parent(parent), duoram(duoram), shape_size(0),
  110. tio(tio), yield(yield), explicitmode(false) {}
  111. // Copy the given Shape except for the tio and yield
  112. Shape(const Shape &copy_from, MPCTIO &tio, yield_t &yield) :
  113. parent(copy_from.parent), duoram(copy_from.duoram),
  114. shape_size(copy_from.shape_size), tio(tio), yield(yield),
  115. explicitmode(copy_from.explicitmode) {}
  116. // The index-mapping function. Input the index relative to this
  117. // shape, and output the corresponding physical address. The
  118. // strategy is to map the index relative to this shape to the index
  119. // relative to the parent shape, call the parent's indexmap function
  120. // on that (unless this is the topmost shape), and return what it
  121. // returns. If this is the topmost shape, just return what you
  122. // would have passed to the parent's indexmap.
  123. //
  124. // This is a pure virtual function; all subclasses of Shape must
  125. // implement it, and of course Shape itself therefore cannot be
  126. // instantiated.
  127. virtual size_t indexmap(size_t idx) const = 0;
  128. // Get a pair (for the server) of references to the underlying
  129. // Duoram entries at share virtual index idx. (That is, it gets
  130. // duoram.p0_blind[indexmap(idx)], etc.)
  131. inline std::tuple<T&,T&> get_server(size_t idx) const {
  132. size_t physaddr = indexmap(idx);
  133. return std::tie(
  134. duoram.p0_blind[physaddr],
  135. duoram.p1_blind[physaddr]);
  136. }
  137. // Get a triple (for the computational players) of references to the
  138. // underlying Duoram entries at share virtual index idx. (That is,
  139. // it gets duoram.database[indexmap(idx)], etc.)
  140. inline std::tuple<T&,T&,T&> get_comp(size_t idx) const {
  141. size_t physaddr = indexmap(idx);
  142. return std::tie(
  143. duoram.database[physaddr],
  144. duoram.blind[physaddr],
  145. duoram.peer_blinded_db[physaddr]);
  146. }
  147. public:
  148. // Get the size
  149. inline size_t size() { return shape_size; }
  150. // Index into this Shape in various ways
  151. MemRefS<RegAS> operator[](const RegAS &idx) { return MemRefS<RegAS>(*this, idx); }
  152. MemRefS<RegXS> operator[](const RegXS &idx) { return MemRefS<RegXS>(*this, idx); }
  153. MemRefExpl operator[](address_t idx) { return MemRefExpl(*this, idx); }
  154. // Enable or disable explicit-only mode. Only using [] with
  155. // explicit (address_t) indices are allowed in this mode. Using []
  156. // with RegAS or RegXS indices will automatically turn off this
  157. // mode, or you can turn it off explicitly. In explicit-only mode,
  158. // updates to the memory in the Shape will not induce communication
  159. // to the server or peer, but when it turns off, a message of the
  160. // size of the entire Shape will be sent to each of the server and
  161. // the peer. This is useful if you're going to be doing multiple
  162. // explicit writes to every element of the Shape before you do your
  163. // next oblivious read or write. Bitonic sort is a prime example.
  164. void explicitonly(bool enable);
  165. // For debugging or checking your answers (using this in general is
  166. // of course insecure)
  167. // This one reconstructs the whole database
  168. std::vector<T> reconstruct() const;
  169. // This one reconstructs a single database value
  170. T reconstruct(const T& share) const;
  171. };
  172. // The most basic shape is Flat. It is almost always the topmost shape,
  173. // and serves to provide MPCTIO and yield_t context to a Duoram without
  174. // changing the indices or size (but can specify a subrange if desired).
  175. template <typename T>
  176. class Duoram<T>::Flat : public Duoram<T>::Shape {
  177. // If this is a subrange, start may be non-0, but it's usually 0
  178. size_t start;
  179. size_t len;
  180. inline size_t indexmap(size_t idx) const {
  181. size_t paridx = idx + start;
  182. if (&(this->parent) == this) {
  183. return paridx;
  184. } else {
  185. return this->parent.indexmap(paridx);
  186. }
  187. }
  188. // Internal function to aid bitonic_sort
  189. void butterfly(address_t start, nbits_t depth, bool dir);
  190. public:
  191. // Constructor. len=0 means the maximum size (the parent's size
  192. // minus start).
  193. Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield, size_t start = 0,
  194. size_t len = 0);
  195. // Copy the given Flat except for the tio and yield
  196. Flat(const Flat &copy_from, MPCTIO &tio, yield_t &yield) :
  197. Shape(copy_from, tio, yield), start(copy_from.start),
  198. len(copy_from.len) {}
  199. // Update the context (MPCTIO and yield if you've started a new
  200. // thread, or just yield if you've started a new coroutine in the
  201. // same thread). Returns a new Shape with an updated context.
  202. Flat context(MPCTIO &new_tio, yield_t &new_yield) const {
  203. return Flat(*this, new_tio, new_yield);
  204. }
  205. Flat context(yield_t &new_yield) const {
  206. return Flat(*this, this->tio, new_yield);
  207. }
  208. // Oblivious sort the elements indexed by the two given indices.
  209. // Without reconstructing the values, if dir=0, this[idx1] will
  210. // become a share of the smaller of the reconstructed values, and
  211. // this[idx2] will become a share of the larger. If dir=1, it's the
  212. // other way around.
  213. //
  214. // Note: this only works for additively shared databases
  215. template<typename U,typename V>
  216. void osort(const U &idx1, const V &idx2, bool dir=0);
  217. // Bitonic sort the elements from start to start+(1<<depth)-1, in
  218. // increasing order if dir=0 or decreasing order if dir=1. Note that
  219. // the elements must be at most 63 bits long each for the notion of
  220. // ">" to make consistent sense.
  221. void bitonic_sort(address_t start, nbits_t depth, bool dir=0);
  222. // Assuming the memory is already sorted, do an oblivious binary
  223. // search for the largest index containing the value at most the
  224. // given one. (The answer will be 0 if all of the memory elements
  225. // are greate than the target.) This Flat must be a power of 2 size.
  226. // Only available for additive shared databases for now.
  227. RegAS obliv_binary_search(RegAS &target);
  228. };
  229. // The parent class of shared memory references
  230. template <typename T>
  231. class Duoram<T>::Shape::MemRef {
  232. protected:
  233. Shape &shape;
  234. MemRef(Shape &shape): shape(shape) {}
  235. public:
  236. // Oblivious read from an additively shared index of Duoram memory
  237. virtual operator T() = 0;
  238. // Oblivious update to an additively shared index of Duoram memory
  239. virtual MemRef &operator+=(const T& M) = 0;
  240. // Convenience function
  241. MemRef &operator-=(const T& M) { *this += (-M); return *this; }
  242. };
  243. // An additive or XOR shared memory reference. You get one of these
  244. // from a Shape A and an additively shared RegAS index x, or an XOR
  245. // shared RegXS index x, with A[x]. Then you perform operations on this
  246. // object, which do the Duoram operations. As above, T is the sharing
  247. // type of the data in the database, while U is the sharing type of the
  248. // index used to create this memory reference.
  249. template <typename T> template <typename U>
  250. class Duoram<T>::Shape::MemRefS : public Duoram<T>::Shape::MemRef {
  251. U idx;
  252. public:
  253. MemRefS<U>(Shape &shape, const U &idx) :
  254. MemRef(shape), idx(idx) {}
  255. // Oblivious read from an additively shared index of Duoram memory
  256. operator T() override;
  257. // Oblivious update to an additively shared index of Duoram memory
  258. MemRefS<U> &operator+=(const T& M) override;
  259. };
  260. // An explicit memory reference. You get one of these from a Shape A
  261. // and an address_t index x with A[x]. Then you perform operations on
  262. // this object, which update the Duoram state without performing Duoram
  263. // operations.
  264. template <typename T>
  265. class Duoram<T>::Shape::MemRefExpl : public Duoram<T>::Shape::MemRef {
  266. address_t idx;
  267. public:
  268. MemRefExpl(Shape &shape, address_t idx) :
  269. MemRef(shape), idx(idx) {}
  270. // Explicit read from a given index of Duoram memory
  271. operator T() override;
  272. // Explicit update to a given index of Duoram memory
  273. MemRefExpl &operator+=(const T& M) override;
  274. // Convenience function
  275. MemRefExpl &operator-=(const T& M) { *this += (-M); return *this; }
  276. };
  277. #include "duoram.tcc"
  278. #endif