duoram.hpp 12 KB

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