rdpf.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. template <nbits_t WIDTH>
  13. struct RDPF : public DPF {
  14. // The amount we have to scale the low words of the leaf values by
  15. // to get additive shares of a unit vector
  16. value_t unit_sum_inverse;
  17. // Additive share of the scaling value M_as such that the high words
  18. // of the leaf values for P0 and P1 add to M_as * e_{target}
  19. RegAS scaled_sum;
  20. // XOR share of the scaling value M_xs such that the high words
  21. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  22. RegXS scaled_xor;
  23. // If we're saving the expansion, put it here
  24. std::vector<DPFnode> expansion;
  25. RDPF() {}
  26. // Construct a DPF with the given (XOR-shared) target location, and
  27. // of the given depth, to be used for random-access memory reads and
  28. // writes. The DPF is constructed collaboratively by P0 and P1,
  29. // with the server P2 helping by providing correlated randomness,
  30. // such as SelectTriples.
  31. //
  32. // Cost:
  33. // (2 DPFnode + 2 bytes)*depth + 1 word communication in
  34. // 2*depth + 1 messages
  35. // (2 DPFnode + 1 byte)*depth communication from P2 to each party
  36. // 2^{depth+1}-2 local AES operations for P0,P1
  37. // 0 local AES operations for P2
  38. RDPF(MPCTIO &tio, yield_t &yield,
  39. RegXS target, nbits_t depth, bool save_expansion = false);
  40. // Do we have a precomputed expansion?
  41. inline bool has_expansion() const { return expansion.size() > 0; }
  42. // Get an element of the expansion
  43. inline node get_expansion(address_t index) const {
  44. return expansion[index];
  45. }
  46. // Get the leaf node for the given input
  47. //
  48. // Cost: depth AES operations
  49. DPFnode leaf(address_t input, size_t &aes_ops) const;
  50. // Expand the DPF if it's not already expanded
  51. void expand(size_t &aes_ops);
  52. // Get the bit-shared unit vector entry from the leaf node
  53. inline RegBS unit_bs(DPFnode leaf) const {
  54. RegBS b;
  55. b.bshare = get_lsb(leaf);
  56. return b;
  57. }
  58. // Get the additive-shared unit vector entry from the leaf node
  59. inline RegAS unit_as(DPFnode leaf) const {
  60. RegAS a;
  61. value_t lowword = value_t(_mm_cvtsi128_si64x(leaf));
  62. if (whichhalf == 1) {
  63. lowword = -lowword;
  64. }
  65. a.ashare = lowword * unit_sum_inverse;
  66. return a;
  67. }
  68. // Get the XOR-shared scaled vector entry from the leaf node
  69. inline RegXS scaled_xs(DPFnode leaf) const {
  70. RegXS x;
  71. value_t highword =
  72. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf,8)));
  73. x.xshare = highword;
  74. return x;
  75. }
  76. // Get the additive-shared scaled vector entry from the leaf node
  77. inline RegAS scaled_as(DPFnode leaf) const {
  78. RegAS a;
  79. value_t highword =
  80. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf,8)));
  81. if (whichhalf == 1) {
  82. highword = -highword;
  83. }
  84. a.ashare = highword;
  85. return a;
  86. }
  87. };
  88. // Computational peers will generate triples of RDPFs with the _same_
  89. // random target for use in Duoram. They will each hold a share of the
  90. // target (neither knowing the complete target index). They will each
  91. // give one of the DPFs (not a matching pair) to the server, but not the
  92. // shares of the target index. So computational peers will hold a
  93. // RDPFTriple (which includes both an additive and an XOR share of the
  94. // target index), while the server will hold a RDPFPair (which does
  95. // not).
  96. template <nbits_t WIDTH>
  97. struct RDPFTriple {
  98. // The type of node triples
  99. using node = std::tuple<DPFnode, DPFnode, DPFnode>;
  100. RegAS as_target;
  101. RegXS xs_target;
  102. RDPF<WIDTH> dpf[3];
  103. // The depth
  104. inline nbits_t depth() const { return dpf[0].depth(); }
  105. // The seed
  106. inline node get_seed() const {
  107. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed(),
  108. dpf[2].get_seed());
  109. }
  110. // Do we have a precomputed expansion?
  111. inline bool has_expansion() const {
  112. return dpf[0].expansion.size() > 0;
  113. }
  114. // Get an element of the expansion
  115. inline node get_expansion(address_t index) const {
  116. return std::make_tuple(dpf[0].get_expansion(index),
  117. dpf[1].get_expansion(index), dpf[2].get_expansion(index));
  118. }
  119. RDPFTriple() {}
  120. // Construct three RDPFs of the given depth all with the same
  121. // randomly generated target index.
  122. RDPFTriple(MPCTIO &tio, yield_t &yield,
  123. nbits_t depth, bool save_expansion = false);
  124. // Descend the three RDPFs in lock step
  125. node descend(const node &parent, nbits_t parentdepth,
  126. bit_t whichchild, size_t &aes_ops) const;
  127. // Overloaded versions of functions to get DPF components and
  128. // outputs so that the appropriate one can be selected with a
  129. // parameter
  130. inline void get_target(RegAS &target) const { target = as_target; }
  131. inline void get_target(RegXS &target) const { target = xs_target; }
  132. // Additive share of the scaling value M_as such that the high words
  133. // of the leaf values for P0 and P1 add to M_as * e_{target}
  134. inline void scaled_value(std::tuple<RegAS,RegAS,RegAS> &v) const {
  135. std::get<0>(v) = dpf[0].scaled_sum;
  136. std::get<1>(v) = dpf[1].scaled_sum;
  137. std::get<2>(v) = dpf[2].scaled_sum;
  138. }
  139. // XOR share of the scaling value M_xs such that the high words
  140. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  141. inline void scaled_value(std::tuple<RegXS,RegXS,RegXS> &v) const {
  142. std::get<0>(v) = dpf[0].scaled_xor;
  143. std::get<1>(v) = dpf[1].scaled_xor;
  144. std::get<2>(v) = dpf[2].scaled_xor;
  145. }
  146. // Get the additive-shared unit vector entry from the leaf node
  147. inline void unit(std::tuple<RegAS,RegAS,RegAS> &u, node leaf) const {
  148. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  149. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  150. std::get<2>(u) = dpf[2].unit_as(std::get<2>(leaf));
  151. }
  152. // Get the bit-shared unit vector entry from the leaf node
  153. inline void unit(std::tuple<RegXS,RegXS,RegXS> &u, node leaf) const {
  154. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  155. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  156. std::get<2>(u) = dpf[2].unit_bs(std::get<2>(leaf));
  157. }
  158. // For any more complex entry type, that type will handle the conversion
  159. // for each DPF
  160. template <typename T>
  161. inline void unit(std::tuple<T,T,T> &u, node leaf) const {
  162. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  163. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  164. std::get<2>(u).unit(dpf[2], std::get<2>(leaf));
  165. }
  166. // Get the additive-shared scaled vector entry from the leaf node
  167. inline void scaled(std::tuple<RegAS,RegAS,RegAS> &s, node leaf) const {
  168. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  169. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  170. std::get<2>(s) = dpf[2].scaled_as(std::get<2>(leaf));
  171. }
  172. // Get the XOR-shared scaled vector entry from the leaf node
  173. inline void scaled(std::tuple<RegXS,RegXS,RegXS> &s, node leaf) const {
  174. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  175. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  176. std::get<2>(s) = dpf[2].scaled_xs(std::get<2>(leaf));
  177. }
  178. };
  179. template <nbits_t WIDTH>
  180. struct RDPFPair {
  181. // The type of node pairs
  182. using node = std::tuple<DPFnode, DPFnode>;
  183. RDPF<WIDTH> dpf[2];
  184. RDPFPair() {}
  185. // Create an RDPFPair from an RDPFTriple, keeping two of the RDPFs
  186. // and dropping one. This _moves_ the dpfs from the triple to the
  187. // pair, so the triple will no longer be valid after using this.
  188. // which0 and which1 indicate which of the dpfs to keep.
  189. RDPFPair(RDPFTriple<WIDTH> &&trip, int which0, int which1) {
  190. dpf[0] = std::move(trip.dpf[which0]);
  191. dpf[1] = std::move(trip.dpf[which1]);
  192. }
  193. // The depth
  194. inline nbits_t depth() const { return dpf[0].depth(); }
  195. // The seed
  196. inline node get_seed() const {
  197. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  198. }
  199. // Do we have a precomputed expansion?
  200. inline bool has_expansion() const {
  201. return dpf[0].expansion.size() > 0;
  202. }
  203. // Get an element of the expansion
  204. inline node get_expansion(address_t index) const {
  205. return std::make_tuple(dpf[0].get_expansion(index),
  206. dpf[1].get_expansion(index));
  207. }
  208. // Descend the two RDPFs in lock step
  209. node descend(const node &parent, nbits_t parentdepth,
  210. bit_t whichchild, size_t &aes_ops) const;
  211. // Overloaded versions of functions to get DPF components and
  212. // outputs so that the appropriate one can be selected with a
  213. // parameter
  214. // Additive share of the scaling value M_as such that the high words
  215. // of the leaf values for P0 and P1 add to M_as * e_{target}
  216. inline void scaled_value(std::tuple<RegAS,RegAS> &v) const {
  217. std::get<0>(v) = dpf[0].scaled_sum;
  218. std::get<1>(v) = dpf[1].scaled_sum;
  219. }
  220. // XOR share of the scaling value M_xs such that the high words
  221. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  222. inline void scaled_value(std::tuple<RegXS,RegXS> &v) const {
  223. std::get<0>(v) = dpf[0].scaled_xor;
  224. std::get<1>(v) = dpf[1].scaled_xor;
  225. }
  226. // Get the additive-shared unit vector entry from the leaf node
  227. inline void unit(std::tuple<RegAS,RegAS> &u, node leaf) const {
  228. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  229. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  230. }
  231. // Get the bit-shared unit vector entry from the leaf node
  232. inline void unit(std::tuple<RegXS,RegXS> &u, node leaf) const {
  233. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  234. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  235. }
  236. // For any more complex entry type, that type will handle the conversion
  237. // for each DPF
  238. template <typename T>
  239. inline void unit(std::tuple<T,T> &u, node leaf) const {
  240. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  241. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  242. }
  243. // Get the additive-shared scaled vector entry from the leaf node
  244. inline void scaled(std::tuple<RegAS,RegAS> &s, node leaf) const {
  245. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  246. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  247. }
  248. // Get the XOR-shared scaled vector entry from the leaf node
  249. inline void scaled(std::tuple<RegXS,RegXS> &s, node leaf) const {
  250. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  251. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  252. }
  253. };
  254. // Streaming evaluation, to avoid taking up enough memory to store
  255. // an entire evaluation. T can be RDPF, RDPFPair, or RDPFTriple.
  256. template <typename T>
  257. class StreamEval {
  258. const T &rdpf;
  259. size_t &aes_ops;
  260. bool use_expansion;
  261. nbits_t depth;
  262. address_t counter_xor_offset;
  263. address_t indexmask;
  264. address_t pathindex;
  265. address_t nextindex;
  266. std::vector<typename T::node> path;
  267. public:
  268. // Create a StreamEval object that will start its output at index
  269. // start. It will wrap around to 0 when it hits 2^depth. If
  270. // use_expansion is true, then if the DPF has been expanded, just
  271. // output values from that. If use_expansion=false or if the DPF
  272. // has not been expanded, compute the values on the fly. If
  273. // xor_offset is non-zero, then the outputs are actually
  274. // DPF(start XOR xor_offset)
  275. // DPF((start+1) XOR xor_offset)
  276. // DPF((start+2) XOR xor_offset)
  277. // etc.
  278. StreamEval(const T &rdpf, address_t start,
  279. address_t xor_offset, size_t &aes_ops,
  280. bool use_expansion = true);
  281. // Get the next value (or tuple of values) from the evaluator
  282. typename T::node next();
  283. };
  284. // Parallel evaluation. This class launches a number of threads each
  285. // running a StreamEval to evaluate a chunk of the RDPF (or RDPFPair or
  286. // RDPFTriple), and accumulates the results within each chunk, and then
  287. // accumulates all the chunks together. T can be RDPF, RDPFPair, or
  288. // RDPFTriple.
  289. template <typename T>
  290. struct ParallelEval {
  291. const T &rdpf;
  292. address_t start;
  293. address_t xor_offset;
  294. address_t num_evals;
  295. int num_threads;
  296. size_t &aes_ops;
  297. // Create a Parallel evaluator that will evaluate the given rdpf at
  298. // DPF(start XOR xor_offset)
  299. // DPF((start+1) XOR xor_offset)
  300. // DPF((start+2) XOR xor_offset)
  301. // ...
  302. // DPF((start+num_evals-1) XOR xor_offset)
  303. // where all indices are taken mod 2^depth, and accumulate the
  304. // results into a single answer.
  305. ParallelEval(const T &rdpf, address_t start,
  306. address_t xor_offset, address_t num_evals,
  307. int num_threads, size_t &aes_ops) :
  308. rdpf(rdpf), start(start), xor_offset(xor_offset),
  309. num_evals(num_evals), num_threads(num_threads),
  310. aes_ops(aes_ops) {}
  311. // Run the parallel evaluator. The type V is the type of the
  312. // accumulator; init should be the "zero" value of the accumulator.
  313. // The type W (process) is a lambda type with the signature
  314. // (int, address_t, const T::node &) -> V
  315. // which will be called like this for each i from 0 to num_evals-1,
  316. // across num_thread threads:
  317. // value_i = process(t, i, DPF((start+i) XOR xor_offset))
  318. // t is the thread number (0 <= t < num_threads).
  319. // The resulting num_evals values will be combined using V's +=
  320. // operator, first accumulating the values within each thread
  321. // (starting with the init value), and then accumulating the totals
  322. // from each thread together (again starting with the init value):
  323. //
  324. // total = init
  325. // for each thread t:
  326. // accum_t = init
  327. // for each accum_i generated by thread t:
  328. // accum_t += value_i
  329. // total += accum_t
  330. template <typename V, typename W>
  331. inline V reduce(V init, W process);
  332. };
  333. #include "rdpf.tcc"
  334. #endif