rdpf.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 node
  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 node
  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. // Overloaded versions of functions to get DPF components and
  126. // outputs so that the appropriate one can be selected with a
  127. // parameter
  128. inline void get_target(RegAS &target) const { target = as_target; }
  129. inline void get_target(RegXS &target) const { target = xs_target; }
  130. // Additive share of the scaling value M_as such that the high words
  131. // of the leaf values for P0 and P1 add to M_as * e_{target}
  132. inline void scaled_value(std::tuple<RegAS,RegAS,RegAS> &v) const {
  133. std::get<0>(v) = dpf[0].scaled_sum;
  134. std::get<1>(v) = dpf[1].scaled_sum;
  135. std::get<2>(v) = dpf[2].scaled_sum;
  136. }
  137. // XOR share of the scaling value M_xs such that the high words
  138. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  139. inline void scaled_value(std::tuple<RegXS,RegXS,RegXS> &v) const {
  140. std::get<0>(v) = dpf[0].scaled_xor;
  141. std::get<1>(v) = dpf[1].scaled_xor;
  142. std::get<2>(v) = dpf[2].scaled_xor;
  143. }
  144. // Get the additive-shared unit vector entry from the leaf node
  145. inline void unit(std::tuple<RegAS,RegAS,RegAS> &u, node leaf) const {
  146. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  147. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  148. std::get<2>(u) = dpf[2].unit_as(std::get<2>(leaf));
  149. }
  150. // Get the bit-shared unit vector entry from the leaf node
  151. inline void unit(std::tuple<RegXS,RegXS,RegXS> &u, node leaf) const {
  152. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  153. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  154. std::get<2>(u) = dpf[2].unit_bs(std::get<2>(leaf));
  155. }
  156. // For any more complex entry type, that type will handle the conversion
  157. // for each DPF
  158. template <typename T>
  159. inline void unit(std::tuple<T,T,T> &u, node leaf) const {
  160. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  161. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  162. std::get<2>(u).unit(dpf[2], std::get<2>(leaf));
  163. }
  164. // Get the additive-shared scaled vector entry from the leaf node
  165. inline void scaled(std::tuple<RegAS,RegAS,RegAS> &s, node leaf) const {
  166. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  167. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  168. std::get<2>(s) = dpf[2].scaled_as(std::get<2>(leaf));
  169. }
  170. // Get the XOR-shared scaled vector entry from the leaf node
  171. inline void scaled(std::tuple<RegXS,RegXS,RegXS> &s, node leaf) const {
  172. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  173. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  174. std::get<2>(s) = dpf[2].scaled_xs(std::get<2>(leaf));
  175. }
  176. };
  177. struct RDPFPair {
  178. // The type of node pairs
  179. using node = std::tuple<DPFnode, DPFnode>;
  180. RDPF dpf[2];
  181. RDPFPair() {}
  182. // Create an RDPFPair from an RDPFTriple, keeping two of the RDPFs
  183. // and dropping one. This _moves_ the dpfs from the triple to the
  184. // pair, so the triple will no longer be valid after using this.
  185. // which0 and which1 indicate which of the dpfs to keep.
  186. RDPFPair(RDPFTriple &&trip, int which0, int which1) {
  187. dpf[0] = std::move(trip.dpf[which0]);
  188. dpf[1] = std::move(trip.dpf[which1]);
  189. }
  190. // The depth
  191. inline nbits_t depth() const { return dpf[0].depth(); }
  192. // The seed
  193. inline node get_seed() const {
  194. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  195. }
  196. // Do we have a precomputed expansion?
  197. inline bool has_expansion() const {
  198. return dpf[0].expansion.size() > 0;
  199. }
  200. // Get an element of the expansion
  201. inline node get_expansion(address_t index) const {
  202. return std::make_tuple(dpf[0].get_expansion(index),
  203. dpf[1].get_expansion(index));
  204. }
  205. // Descend the two RDPFs in lock step
  206. node descend(const node &parent, nbits_t parentdepth,
  207. bit_t whichchild, size_t &aes_ops) const;
  208. // Overloaded versions of functions to get DPF components and
  209. // outputs so that the appropriate one can be selected with a
  210. // parameter
  211. // Additive share of the scaling value M_as such that the high words
  212. // of the leaf values for P0 and P1 add to M_as * e_{target}
  213. inline void scaled_value(std::tuple<RegAS,RegAS> &v) const {
  214. std::get<0>(v) = dpf[0].scaled_sum;
  215. std::get<1>(v) = dpf[1].scaled_sum;
  216. }
  217. // XOR share of the scaling value M_xs such that the high words
  218. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  219. inline void scaled_value(std::tuple<RegXS,RegXS> &v) const {
  220. std::get<0>(v) = dpf[0].scaled_xor;
  221. std::get<1>(v) = dpf[1].scaled_xor;
  222. }
  223. // Get the additive-shared unit vector entry from the leaf node
  224. inline void unit(std::tuple<RegAS,RegAS> &u, node leaf) const {
  225. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  226. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  227. }
  228. // Get the bit-shared unit vector entry from the leaf node
  229. inline void unit(std::tuple<RegXS,RegXS> &u, node leaf) const {
  230. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  231. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  232. }
  233. // For any more complex entry type, that type will handle the conversion
  234. // for each DPF
  235. template <typename T>
  236. inline void unit(std::tuple<T,T> &u, node leaf) const {
  237. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  238. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  239. }
  240. // Get the additive-shared scaled vector entry from the leaf node
  241. inline void scaled(std::tuple<RegAS,RegAS> &s, node leaf) const {
  242. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  243. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  244. }
  245. // Get the XOR-shared scaled vector entry from the leaf node
  246. inline void scaled(std::tuple<RegXS,RegXS> &s, node leaf) const {
  247. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  248. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  249. }
  250. };
  251. // Streaming evaluation, to avoid taking up enough memory to store
  252. // an entire evaluation. T can be RDPF, RDPFPair, or RDPFTriple.
  253. template <typename T>
  254. class StreamEval {
  255. const T &rdpf;
  256. size_t &aes_ops;
  257. bool use_expansion;
  258. nbits_t depth;
  259. address_t counter_xor_offset;
  260. address_t indexmask;
  261. address_t pathindex;
  262. address_t nextindex;
  263. std::vector<typename T::node> path;
  264. public:
  265. // Create a StreamEval object that will start its output at index
  266. // start. It will wrap around to 0 when it hits 2^depth. If
  267. // use_expansion is true, then if the DPF has been expanded, just
  268. // output values from that. If use_expansion=false or if the DPF
  269. // has not been expanded, compute the values on the fly. If
  270. // xor_offset is non-zero, then the outputs are actually
  271. // DPF(start XOR xor_offset)
  272. // DPF((start+1) XOR xor_offset)
  273. // DPF((start+2) XOR xor_offset)
  274. // etc.
  275. StreamEval(const T &rdpf, address_t start,
  276. address_t xor_offset, size_t &aes_ops,
  277. bool use_expansion = true);
  278. // Get the next value (or tuple of values) from the evaluator
  279. typename T::node next();
  280. };
  281. // Parallel evaluation. This class launches a number of threads each
  282. // running a StreamEval to evaluate a chunk of the RDPF (or RDPFPair or
  283. // RDPFTriple), and accumulates the results within each chunk, and then
  284. // accumulates all the chunks together. T can be RDPF, RDPFPair, or
  285. // RDPFTriple.
  286. template <typename T>
  287. struct ParallelEval {
  288. const T &rdpf;
  289. address_t start;
  290. address_t xor_offset;
  291. address_t num_evals;
  292. int num_threads;
  293. size_t &aes_ops;
  294. // Create a Parallel evaluator that will evaluate the given rdpf at
  295. // DPF(start XOR xor_offset)
  296. // DPF((start+1) XOR xor_offset)
  297. // DPF((start+2) XOR xor_offset)
  298. // ...
  299. // DPF((start+num_evals-1) XOR xor_offset)
  300. // where all indices are taken mod 2^depth, and accumulate the
  301. // results into a single answer.
  302. ParallelEval(const T &rdpf, address_t start,
  303. address_t xor_offset, address_t num_evals,
  304. int num_threads, size_t &aes_ops) :
  305. rdpf(rdpf), start(start), xor_offset(xor_offset),
  306. num_evals(num_evals), num_threads(num_threads),
  307. aes_ops(aes_ops) {}
  308. // Run the parallel evaluator. The type V is the type of the
  309. // accumulator; init should be the "zero" value of the accumulator.
  310. // The type W (process) is a lambda type with the signature
  311. // (int, address_t, const T::node &) -> V
  312. // which will be called like this for each i from 0 to num_evals-1,
  313. // across num_thread threads:
  314. // value_i = process(t, i, DPF((start+i) XOR xor_offset))
  315. // t is the thread number (0 <= t < num_threads).
  316. // The resulting num_evals values will be combined using V's +=
  317. // operator, first accumulating the values within each thread
  318. // (starting with the init value), and then accumulating the totals
  319. // from each thread together (again starting with the init value):
  320. //
  321. // total = init
  322. // for each thread t:
  323. // accum_t = init
  324. // for each accum_i generated by thread t:
  325. // accum_t += value_i
  326. // total += accum_t
  327. template <typename V, typename W>
  328. inline V reduce(V init, W process);
  329. };
  330. #include "rdpf.tcc"
  331. #endif