rdpf.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #ifndef __RDPF_HPP__
  2. #define __RDPF_HPP__
  3. #include <vector>
  4. #include <iostream>
  5. #include "mpcio.hpp"
  6. #include "coroutine.hpp"
  7. #include "types.hpp"
  8. #include "bitutils.hpp"
  9. #include "dpf.hpp"
  10. // DPFs for oblivious random accesses to memory. See dpf.hpp for the
  11. // differences between the different kinds of DPFs.
  12. struct RDPF : public DPF {
  13. // The amount we have to scale the low words of the leaf values by
  14. // to get additive shares of a unit vector
  15. value_t unit_sum_inverse;
  16. // Additive share of the scaling value M_as such that the high words
  17. // of the leaf values for P0 and P1 add to M_as * e_{target}
  18. RegAS scaled_sum;
  19. // XOR share of the scaling value M_xs such that the high words
  20. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  21. RegXS scaled_xor;
  22. // If we're saving the expansion, put it here
  23. std::vector<DPFnode> expansion;
  24. RDPF() {}
  25. // Construct a DPF with the given (XOR-shared) target location, and
  26. // of the given depth, to be used for random-access memory reads and
  27. // writes. The DPF is constructed collaboratively by P0 and P1,
  28. // with the server P2 helping by providing correlated randomness,
  29. // such as SelectTriples.
  30. //
  31. // Cost:
  32. // (2 DPFnode + 2 bytes)*depth + 1 word communication in
  33. // 2*depth + 1 messages
  34. // (2 DPFnode + 1 byte)*depth communication from P2 to each party
  35. // 2^{depth+1}-2 local AES operations for P0,P1
  36. // 0 local AES operations for P2
  37. RDPF(MPCTIO &tio, yield_t &yield,
  38. RegXS target, nbits_t depth, bool save_expansion = false);
  39. // Do we have a precomputed expansion?
  40. inline bool has_expansion() const { return expansion.size() > 0; }
  41. // Get an element of the expansion
  42. inline node get_expansion(address_t index) const {
  43. return expansion[index];
  44. }
  45. // Get the leaf node for the given input
  46. //
  47. // Cost: depth AES operations
  48. DPFnode leaf(address_t input, size_t &aes_ops) const;
  49. // Expand the DPF if it's not already expanded
  50. void expand(size_t &aes_ops);
  51. // Get the bit-shared unit vector entry from the leaf node
  52. inline RegBS unit_bs(DPFnode leaf) const {
  53. RegBS b;
  54. b.bshare = get_lsb(leaf);
  55. return b;
  56. }
  57. // Get the additive-shared unit vector entry from the leaf node
  58. inline RegAS unit_as(DPFnode leaf) const {
  59. RegAS a;
  60. value_t lowword = value_t(_mm_cvtsi128_si64x(leaf));
  61. if (whichhalf == 1) {
  62. lowword = -lowword;
  63. }
  64. a.ashare = lowword * unit_sum_inverse;
  65. return a;
  66. }
  67. // Get the XOR-shared scaled vector entry from the leaf ndoe
  68. inline RegXS scaled_xs(DPFnode leaf) const {
  69. RegXS x;
  70. value_t highword =
  71. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf,8)));
  72. x.xshare = highword;
  73. return x;
  74. }
  75. // Get the additive-shared scaled vector entry from the leaf ndoe
  76. inline RegAS scaled_as(DPFnode leaf) const {
  77. RegAS a;
  78. value_t highword =
  79. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf,8)));
  80. if (whichhalf == 1) {
  81. highword = -highword;
  82. }
  83. a.ashare = highword;
  84. return a;
  85. }
  86. };
  87. // Computational peers will generate triples of RDPFs with the _same_
  88. // random target for use in Duoram. They will each hold a share of the
  89. // target (neither knowing the complete target index). They will each
  90. // give one of the DPFs (not a matching pair) to the server, but not the
  91. // shares of the target index. So computational peers will hold a
  92. // RDPFTriple (which includes both an additive and an XOR share of the
  93. // target index), while the server will hold a RDPFPair (which does
  94. // not).
  95. struct RDPFTriple {
  96. // The type of node triples
  97. using node = std::tuple<DPFnode, DPFnode, DPFnode>;
  98. RegAS as_target;
  99. RegXS xs_target;
  100. RDPF dpf[3];
  101. // The depth
  102. inline nbits_t depth() const { return dpf[0].depth(); }
  103. // The seed
  104. inline node get_seed() const {
  105. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed(),
  106. dpf[2].get_seed());
  107. }
  108. // Do we have a precomputed expansion?
  109. inline bool has_expansion() const {
  110. return dpf[0].expansion.size() > 0;
  111. }
  112. // Get an element of the expansion
  113. inline node get_expansion(address_t index) const {
  114. return std::make_tuple(dpf[0].get_expansion(index),
  115. dpf[1].get_expansion(index), dpf[2].get_expansion(index));
  116. }
  117. RDPFTriple() {}
  118. // Construct three RDPFs of the given depth all with the same
  119. // randomly generated target index.
  120. RDPFTriple(MPCTIO &tio, yield_t &yield,
  121. nbits_t depth, bool save_expansion = false);
  122. // Descend the three RDPFs in lock step
  123. node descend(const node &parent, nbits_t parentdepth,
  124. bit_t whichchild, size_t &aes_ops) const;
  125. // Templated versions of functions to get DPF components and outputs
  126. // so that the appropriate one can be selected with a template
  127. // parameter
  128. template <typename T>
  129. inline std::tuple<T,T,T> scaled_value() const;
  130. template <typename T>
  131. inline std::tuple<T,T,T> unit(node leaf) const;
  132. template <typename T>
  133. inline std::tuple<T,T,T> scaled(node leaf) const;
  134. };
  135. struct RDPFPair {
  136. // The type of node pairs
  137. using node = std::tuple<DPFnode, DPFnode>;
  138. RDPF dpf[2];
  139. RDPFPair() {}
  140. // Create an RDPFPair from an RDPFTriple, keeping two of the RDPFs
  141. // and dropping one. This _moves_ the dpfs from the triple to the
  142. // pair, so the triple will no longer be valid after using this.
  143. // which0 and which1 indicate which of the dpfs to keep.
  144. RDPFPair(RDPFTriple &&trip, int which0, int which1) {
  145. dpf[0] = std::move(trip.dpf[which0]);
  146. dpf[1] = std::move(trip.dpf[which1]);
  147. }
  148. // The depth
  149. inline nbits_t depth() const { return dpf[0].depth(); }
  150. // The seed
  151. inline node get_seed() const {
  152. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  153. }
  154. // Do we have a precomputed expansion?
  155. inline bool has_expansion() const {
  156. return dpf[0].expansion.size() > 0;
  157. }
  158. // Get an element of the expansion
  159. inline node get_expansion(address_t index) const {
  160. return std::make_tuple(dpf[0].get_expansion(index),
  161. dpf[1].get_expansion(index));
  162. }
  163. // Descend the two RDPFs in lock step
  164. node descend(const node &parent, nbits_t parentdepth,
  165. bit_t whichchild, size_t &aes_ops) const;
  166. // Templated versions of functions to get DPF components and outputs
  167. // so that the appropriate one can be selected with a template
  168. // parameter
  169. template <typename T>
  170. inline std::tuple<T,T> scaled_value() const;
  171. template <typename T>
  172. inline std::tuple<T,T> unit(node leaf) const;
  173. template <typename T>
  174. inline std::tuple<T,T> scaled(node leaf) const;
  175. };
  176. // Streaming evaluation, to avoid taking up enough memory to store
  177. // an entire evaluation. T can be RDPF, RDPFPair, or RDPFTriple.
  178. template <typename T>
  179. class StreamEval {
  180. const T &rdpf;
  181. size_t &aes_ops;
  182. bool use_expansion;
  183. nbits_t depth;
  184. address_t counter_xor_offset;
  185. address_t indexmask;
  186. address_t pathindex;
  187. address_t nextindex;
  188. std::vector<typename T::node> path;
  189. public:
  190. // Create a StreamEval object that will start its output at index
  191. // start. It will wrap around to 0 when it hits 2^depth. If
  192. // use_expansion is true, then if the DPF has been expanded, just
  193. // output values from that. If use_expansion=false or if the DPF
  194. // has not been expanded, compute the values on the fly. If
  195. // xor_offset is non-zero, then the outputs are actually
  196. // DPF(start XOR xor_offset)
  197. // DPF((start+1) XOR xor_offset)
  198. // DPF((start+2) XOR xor_offset)
  199. // etc.
  200. StreamEval(const T &rdpf, address_t start,
  201. address_t xor_offset, size_t &aes_ops,
  202. bool use_expansion = true);
  203. // Get the next value (or tuple of values) from the evaluator
  204. typename T::node next();
  205. };
  206. #include "rdpf.tcc"
  207. #endif