rdpf.hpp 8.5 KB

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