duoram.hpp 13 KB

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