rdpf.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #if 0
  99. // Streaming evaluation, to avoid taking up enough memory to store
  100. // an entire evaluation
  101. class Eval {
  102. friend class RDPF; // So eval() can call the Eval constructor
  103. const RDPF &rdpf;
  104. size_t &op_counter;
  105. bool use_expansion;
  106. nbits_t depth;
  107. address_t indexmask;
  108. address_t pathindex;
  109. address_t nextindex;
  110. std::vector<DPFnode> path;
  111. Eval(const RDPF &rdpf, size_t &op_counter, address_t start,
  112. bool use_expansion);
  113. public:
  114. DPFnode next();
  115. };
  116. // Create an Eval object that will start its output at index start.
  117. // It will wrap around to 0 when it hits 2^depth. If use_expansion
  118. // is true, then if the DPF has been expanded, just output values
  119. // from that. If use_expansion=false or if the DPF has not been
  120. // expanded, compute the values on the fly.
  121. StreamEval<RDPF> eval(address_t start, size_t &op_counter,
  122. bool use_expansion=true) const;
  123. #endif
  124. // Get the bit-shared unit vector entry from the leaf node
  125. inline RegBS unit_bs(DPFnode leaf) const {
  126. RegBS b;
  127. b.bshare = get_lsb(leaf);
  128. return b;
  129. }
  130. // Get the additive-shared unit vector entry from the leaf node
  131. inline RegAS unit_as(DPFnode leaf) const {
  132. RegAS a;
  133. value_t lowword = value_t(_mm_cvtsi128_si64x(leaf));
  134. if (whichhalf == 1) {
  135. lowword = -lowword;
  136. }
  137. a.ashare = lowword * unit_sum_inverse;
  138. return a;
  139. }
  140. // Get the XOR-shared scaled vector entry from the leaf ndoe
  141. inline RegXS scaled_xs(DPFnode leaf) const {
  142. RegXS x;
  143. value_t highword =
  144. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf,8)));
  145. x.xshare = highword;
  146. return x;
  147. }
  148. // Get the additive-shared scaled vector entry from the leaf ndoe
  149. inline RegAS scaled_as(DPFnode leaf) const {
  150. RegAS a;
  151. value_t highword =
  152. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf,8)));
  153. if (whichhalf == 1) {
  154. highword = -highword;
  155. }
  156. a.ashare = highword;
  157. return a;
  158. }
  159. };
  160. // Computational peers will generate triples of RDPFs with the _same_
  161. // random target for use in Duoram. They will each hold a share of the
  162. // target (neither knowing the complete target index). They will each
  163. // give one of the DPFs (not a matching pair) to the server, but not the
  164. // shares of the target index. So computational peers will hold a
  165. // RDPFTriple (which includes both an additive and an XOR share of the
  166. // target index), while the server will hold a RDPFPair (which does
  167. // not).
  168. struct RDPFTriple {
  169. // The type of node triples
  170. using node = std::tuple<DPFnode, DPFnode, DPFnode>;
  171. RegAS as_target;
  172. RegXS xs_target;
  173. RDPF dpf[3];
  174. // The depth
  175. inline nbits_t depth() const { return dpf[0].depth(); }
  176. // The seed
  177. inline node get_seed() const {
  178. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed(),
  179. dpf[2].get_seed());
  180. }
  181. // Do we have a precomputed expansion?
  182. inline bool has_expansion() const {
  183. return dpf[0].expansion.size() > 0;
  184. }
  185. // Get an element of the expansion
  186. inline node get_expansion(address_t index) const {
  187. return std::make_tuple(dpf[0].get_expansion(index),
  188. dpf[1].get_expansion(index), dpf[2].get_expansion(index));
  189. }
  190. RDPFTriple() {}
  191. // Construct three RDPFs of the given depth all with the same
  192. // randomly generated target index.
  193. RDPFTriple(MPCTIO &tio, yield_t &yield,
  194. nbits_t depth, bool save_expansion = false);
  195. // Descend the three RDPFs in lock step
  196. node descend(const node &parent, nbits_t parentdepth,
  197. bit_t whichchild, size_t &op_counter) const;
  198. };
  199. struct RDPFPair {
  200. // The type of node pairs
  201. using node = std::tuple<DPFnode, DPFnode>;
  202. RDPF dpf[2];
  203. // The depth
  204. inline nbits_t depth() const { return dpf[0].depth(); }
  205. // The seed
  206. inline node get_seed() const {
  207. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  208. }
  209. // Do we have a precomputed expansion?
  210. inline bool has_expansion() const {
  211. return dpf[0].expansion.size() > 0;
  212. }
  213. // Get an element of the expansion
  214. inline node get_expansion(address_t index) const {
  215. return std::make_tuple(dpf[0].get_expansion(index),
  216. dpf[1].get_expansion(index));
  217. }
  218. // Descend the two RDPFs in lock step
  219. node descend(const node &parent, nbits_t parentdepth,
  220. bit_t whichchild, size_t &op_counter) const;
  221. };
  222. #include "rdpf.tcc"
  223. #endif