rdpf.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #ifndef __RDPF_HPP__
  2. #define __RDPF_HPP__
  3. #include <array>
  4. #include <vector>
  5. #include <iostream>
  6. #include "mpcio.hpp"
  7. #include "coroutine.hpp"
  8. #include "types.hpp"
  9. #include "bitutils.hpp"
  10. #include "dpf.hpp"
  11. // DPFs for oblivious random accesses to memory. See dpf.hpp for the
  12. // differences between the different kinds of DPFs.
  13. // A single RDPF can use its unit vector for any as reads of the same
  14. // memory location as you like, as long as it's OK that everyone _knows_
  15. // it's the same memory location. The same RDPF can also be configured
  16. // to allow for WIDTH independent updates; if you otherwise would try to
  17. // reuse the same RDPF for multiple updates of the same memory location,
  18. // you would leak the difference between the update _values_. Typically
  19. // WIDTH=1, since most RDPFs are not reused at all.
  20. //
  21. // We implement this by have a "wide" LeafNode type that can store one
  22. // 64-bit value for the read, and WIDTH 64-bit values for the writes.
  23. // Since each DPFnode is 128 bits, you need 1 + (WIDTH/2) DPFnodes in a
  24. // LeafNode. We will also need to pass around arrays of WIDTH RegAS and
  25. // RegXS values, so we make dedicated wide types for those (RegASW and
  26. // RegXSW).
  27. template <nbits_t WIDTH>
  28. struct RDPF : public DPF {
  29. template <typename T>
  30. using W = std::array<T, WIDTH>;
  31. // The wide shared register types
  32. using RegASW = W<RegAS>;
  33. using RegXSW = W<RegXS>;
  34. // The number of 128-bit leaf node entries you need to get 1 unit
  35. // value and WIDTH scaled values (each is 64 bits)
  36. static const nbits_t LWIDTH = 1 + (WIDTH/2);
  37. using LeafNode = std::array<DPFnode,LWIDTH>;
  38. // Information for leaf levels of the RDPF. Normal RDPFs only have
  39. // one leaf level (at the bottom), but incremental RDPFs have a leaf
  40. // level for each level of the DPF.
  41. struct LeafInfo {
  42. static const nbits_t W = WIDTH;
  43. // The correction word for this leaf level
  44. LeafNode leaf_cw;
  45. // The amount we have to scale the low words of the leaf values by
  46. // to get additive shares of a unit vector
  47. value_t unit_sum_inverse;
  48. // Additive share of the scaling values M_as such that the high words
  49. // of the WIDTH leaf values for P0 and P1 add to M_as * e_{target}
  50. std::array<RegAS,WIDTH> scaled_sum;
  51. // XOR share of the scaling values M_xs such that the high words
  52. // of the WIDTH leaf values for P0 and P1 XOR to M_xs * e_{target}
  53. std::array<RegXS,WIDTH> scaled_xor;
  54. LeafInfo() : unit_sum_inverse(0) {}
  55. };
  56. // The depth of this RDPF. If this is not an incremental DPF, then
  57. // both the maximum depth and current depth are just the normal
  58. // depth (specified at DPF creation time). If this is an
  59. // incremental DPF, then the maximum depth is the one specified at
  60. // creation time, but the current depth will be between 1 and that
  61. // value (inclusive).
  62. nbits_t maxdepth, curdepth;
  63. // The LeafInfo for each leaf level. Normal RDPFs only have one
  64. // leaf level, so this will be a vector of length 1. Incremental
  65. // RDPFs will have one entry for each level in the DPF. The entry
  66. // corresponding to level i of the DPF (of total depth d) is
  67. // leaf_info[d-i].
  68. std::vector<LeafInfo> li;
  69. // The leaf correction flag bits for each leaf level. The bit for
  70. // level i (for an incremental DPF of max depth m) is leaf_cfbits &
  71. // (1<<(m-i)). For a normal (not incremental) RDPF, it's the same,
  72. // but therefore only the low bit gets used.
  73. value_t leaf_cfbits;
  74. // If we're saving the expansion, put it here
  75. std::vector<LeafNode> expansion;
  76. RDPF() {}
  77. // Construct a DPF with the given (XOR-shared) target location, and
  78. // of the given depth, to be used for random-access memory reads and
  79. // writes. The DPF is constructed collaboratively by P0 and P1,
  80. // with the server P2 helping by providing correlated randomness,
  81. // such as SelectTriples.
  82. //
  83. // Cost:
  84. // (2 DPFnode + 2 bytes)*depth + 1 word communication in
  85. // 2*depth + 1 messages
  86. // (2 DPFnode + 1 byte)*depth communication from P2 to each party
  87. // 2^{depth+1}-2 local AES operations for P0,P1
  88. // 0 local AES operations for P2
  89. RDPF(MPCTIO &tio, yield_t &yield,
  90. RegXS target, nbits_t depth, bool incremental = false,
  91. bool save_expansion = false);
  92. // Do we have a precomputed expansion?
  93. inline bool has_expansion() const { return expansion.size() > 0; }
  94. // Get an element of the expansion
  95. inline LeafNode get_expansion(address_t index) const {
  96. return expansion[index];
  97. }
  98. // The depth
  99. inline nbits_t depth() const { return curdepth; }
  100. // Get the leaf node for the given input
  101. //
  102. // Cost: depth AES operations
  103. LeafNode leaf(address_t input, size_t &aes_ops) const;
  104. // Expand the DPF if it's not already expanded
  105. void expand(size_t &aes_ops);
  106. // Descend from a node at depth parentdepth to one of its leaf children
  107. // whichchild = 0: left child
  108. // whichchild = 1: right child
  109. //
  110. // Cost: 1 AES operation
  111. inline LeafNode descend_to_leaf(const DPFnode &parent,
  112. nbits_t parentdepth, bit_t whichchild, size_t &aes_ops) const;
  113. // Get the bit-shared unit vector entry from the leaf node
  114. inline RegBS unit_bs(const LeafNode &leaf) const {
  115. RegBS b;
  116. b.bshare = get_lsb(leaf[0]);
  117. return b;
  118. }
  119. // Get the additive-shared unit vector entry from the leaf node
  120. inline RegAS unit_as(const LeafNode &leaf) const {
  121. RegAS a;
  122. value_t lowword = value_t(_mm_cvtsi128_si64x(leaf[0]));
  123. if (whichhalf == 1) {
  124. lowword = -lowword;
  125. }
  126. a.ashare = lowword * li[0].unit_sum_inverse;
  127. return a;
  128. }
  129. // Get the XOR-shared scaled vector entry from the leaf node
  130. inline RegXSW scaled_xs(const LeafNode &leaf) const {
  131. RegXSW x;
  132. nbits_t j = 0;
  133. value_t highword =
  134. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[0],8)));
  135. x[j++].xshare = highword;
  136. for (nbits_t i=1;i<LWIDTH;++i) {
  137. value_t lowword =
  138. value_t(_mm_cvtsi128_si64x(leaf[i]));
  139. value_t highword =
  140. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[i],8)));
  141. x[j++].xshare = lowword;
  142. if (j < WIDTH) {
  143. x[j++].xshare = highword;
  144. }
  145. }
  146. return x;
  147. }
  148. // Get the additive-shared scaled vector entry from the leaf node
  149. inline RegASW scaled_as(const LeafNode &leaf) const {
  150. RegASW a;
  151. nbits_t j = 0;
  152. value_t highword =
  153. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[0],8)));
  154. if (whichhalf == 1) {
  155. highword = -highword;
  156. }
  157. a[j++].ashare = highword;
  158. for (nbits_t i=1;i<WIDTH;++i) {
  159. value_t lowword =
  160. value_t(_mm_cvtsi128_si64x(leaf[i]));
  161. value_t highword =
  162. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[i],8)));
  163. if (whichhalf == 1) {
  164. lowword = -lowword;
  165. highword = -highword;
  166. }
  167. a[j++].ashare = lowword;
  168. if (j < WIDTH) {
  169. a[j++].ashare = highword;
  170. }
  171. }
  172. return a;
  173. }
  174. };
  175. // Computational peers will generate triples of RDPFs with the _same_
  176. // random target for use in Duoram. They will each hold a share of the
  177. // target (neither knowing the complete target index). They will each
  178. // give one of the DPFs (not a matching pair) to the server, but not the
  179. // shares of the target index. So computational peers will hold a
  180. // RDPFTriple (which includes both an additive and an XOR share of the
  181. // target index), while the server will hold a RDPFPair (which does
  182. // not).
  183. template <nbits_t WIDTH>
  184. struct RDPFTriple {
  185. template <typename T>
  186. using Triple = std::tuple<T, T, T>;
  187. template <typename T>
  188. using WTriple = std::tuple<
  189. typename std::array<T,WIDTH>,
  190. typename std::array<T,WIDTH>,
  191. typename std::array<T,WIDTH> >;
  192. // The type of triples of nodes, LeafNodes, and the wide shared
  193. // register types
  194. using node = Triple<DPFnode>;
  195. using LeafNode = Triple<typename RDPF<WIDTH>::LeafNode>;
  196. using RegASWT = WTriple<RegAS>;
  197. using RegXSWT = WTriple<RegXS>;
  198. RegAS as_target;
  199. RegXS xs_target;
  200. RDPF<WIDTH> dpf[3];
  201. // The depth
  202. inline nbits_t depth() const { return dpf[0].depth(); }
  203. // The seed
  204. inline node get_seed() const {
  205. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed(),
  206. dpf[2].get_seed());
  207. }
  208. // Do we have a precomputed expansion?
  209. inline bool has_expansion() const {
  210. return dpf[0].expansion.size() > 0;
  211. }
  212. // Get an element of the expansion
  213. inline LeafNode get_expansion(address_t index) const {
  214. return std::make_tuple(dpf[0].get_expansion(index),
  215. dpf[1].get_expansion(index), dpf[2].get_expansion(index));
  216. }
  217. RDPFTriple() {}
  218. // Construct three RDPFs of the given depth all with the same
  219. // randomly generated target index.
  220. RDPFTriple(MPCTIO &tio, yield_t &yield,
  221. nbits_t depth, bool incremental = false, bool save_expansion = false);
  222. // Descend the three RDPFs in lock step
  223. node descend(const node &parent, nbits_t parentdepth,
  224. bit_t whichchild, size_t &aes_ops) const;
  225. // Descend the three RDPFs in lock step to a leaf node
  226. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  227. bit_t whichchild, size_t &aes_ops) const;
  228. // Overloaded versions of functions to get DPF components and
  229. // outputs so that the appropriate one can be selected with a
  230. // parameter
  231. inline void get_target(RegAS &target) const { target = as_target; }
  232. inline void get_target(RegXS &target) const { target = xs_target; }
  233. // Additive share of the scaling value M_as such that the high words
  234. // of the leaf values for P0 and P1 add to M_as * e_{target}
  235. inline void scaled_value(RegASWT &v) const {
  236. std::get<0>(v) = dpf[0].li[0].scaled_sum;
  237. std::get<1>(v) = dpf[1].li[0].scaled_sum;
  238. std::get<2>(v) = dpf[2].li[0].scaled_sum;
  239. }
  240. // XOR share of the scaling value M_xs such that the high words
  241. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  242. inline void scaled_value(RegXSWT &v) const {
  243. std::get<0>(v) = dpf[0].li[0].scaled_xor;
  244. std::get<1>(v) = dpf[1].li[0].scaled_xor;
  245. std::get<2>(v) = dpf[2].li[0].scaled_xor;
  246. }
  247. // Get the additive-shared unit vector entry from the leaf node
  248. inline void unit(std::tuple<RegAS,RegAS,RegAS> &u, const LeafNode &leaf) const {
  249. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  250. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  251. std::get<2>(u) = dpf[2].unit_as(std::get<2>(leaf));
  252. }
  253. // Get the bit-shared unit vector entry from the leaf node
  254. inline void unit(std::tuple<RegXS,RegXS,RegXS> &u, const LeafNode &leaf) const {
  255. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  256. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  257. std::get<2>(u) = dpf[2].unit_bs(std::get<2>(leaf));
  258. }
  259. // For any more complex entry type, that type will handle the conversion
  260. // for each DPF
  261. template <typename T>
  262. inline void unit(std::tuple<T,T,T> &u, const LeafNode &leaf) const {
  263. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  264. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  265. std::get<2>(u).unit(dpf[2], std::get<2>(leaf));
  266. }
  267. // Get the additive-shared scaled vector entry from the leaf node
  268. inline void scaled(RegASWT &s, const LeafNode &leaf) const {
  269. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  270. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  271. std::get<2>(s) = dpf[2].scaled_as(std::get<2>(leaf));
  272. }
  273. // Get the XOR-shared scaled vector entry from the leaf node
  274. inline void scaled(RegXSWT &s, const LeafNode &leaf) const {
  275. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  276. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  277. std::get<2>(s) = dpf[2].scaled_xs(std::get<2>(leaf));
  278. }
  279. };
  280. template <nbits_t WIDTH>
  281. struct RDPFPair {
  282. template <typename T>
  283. using Pair = std::tuple<T, T>;
  284. template <typename T>
  285. using WPair = std::tuple<
  286. typename std::array<T,WIDTH>,
  287. typename std::array<T,WIDTH> >;
  288. // The type of pairs of nodes, LeafNodes, and the wide shared
  289. // register types
  290. using node = Pair<DPFnode>;
  291. using LeafNode = Pair<typename RDPF<WIDTH>::LeafNode>;
  292. using RegASWP = WPair<RegAS>;
  293. using RegXSWP = WPair<RegXS>;
  294. RDPF<WIDTH> dpf[2];
  295. RDPFPair() {}
  296. // Create an RDPFPair from an RDPFTriple, keeping two of the RDPFs
  297. // and dropping one. This _moves_ the dpfs from the triple to the
  298. // pair, so the triple will no longer be valid after using this.
  299. // which0 and which1 indicate which of the dpfs to keep.
  300. RDPFPair(RDPFTriple<WIDTH> &&trip, int which0, int which1) {
  301. dpf[0] = std::move(trip.dpf[which0]);
  302. dpf[1] = std::move(trip.dpf[which1]);
  303. }
  304. // The depth
  305. inline nbits_t depth() const { return dpf[0].depth(); }
  306. // The seed
  307. inline node get_seed() const {
  308. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  309. }
  310. // Do we have a precomputed expansion?
  311. inline bool has_expansion() const {
  312. return dpf[0].expansion.size() > 0;
  313. }
  314. // Get an element of the expansion
  315. inline LeafNode get_expansion(address_t index) const {
  316. return std::make_tuple(dpf[0].get_expansion(index),
  317. dpf[1].get_expansion(index));
  318. }
  319. // Descend the two RDPFs in lock step
  320. node descend(const node &parent, nbits_t parentdepth,
  321. bit_t whichchild, size_t &aes_ops) const;
  322. // Descend the two RDPFs in lock step to a leaf node
  323. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  324. bit_t whichchild, size_t &aes_ops) const;
  325. // Overloaded versions of functions to get DPF components and
  326. // outputs so that the appropriate one can be selected with a
  327. // parameter
  328. // Additive share of the scaling value M_as such that the high words
  329. // of the leaf values for P0 and P1 add to M_as * e_{target}
  330. inline void scaled_value(RegASWP &v) const {
  331. std::get<0>(v) = dpf[0].scaled_sum;
  332. std::get<1>(v) = dpf[1].scaled_sum;
  333. }
  334. // XOR share of the scaling value M_xs such that the high words
  335. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  336. inline void scaled_value(RegXSWP &v) const {
  337. std::get<0>(v) = dpf[0].scaled_xor;
  338. std::get<1>(v) = dpf[1].scaled_xor;
  339. }
  340. // Get the additive-shared unit vector entry from the leaf node
  341. inline void unit(std::tuple<RegAS,RegAS> &u, const LeafNode &leaf) const {
  342. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  343. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  344. }
  345. // Get the bit-shared unit vector entry from the leaf node
  346. inline void unit(std::tuple<RegXS,RegXS> &u, const LeafNode &leaf) const {
  347. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  348. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  349. }
  350. // For any more complex entry type, that type will handle the conversion
  351. // for each DPF
  352. template <typename T>
  353. inline void unit(std::tuple<T,T> &u, const LeafNode &leaf) const {
  354. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  355. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  356. }
  357. // Get the additive-shared scaled vector entry from the leaf node
  358. inline void scaled(RegASWP &s, const LeafNode &leaf) const {
  359. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  360. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  361. }
  362. // Get the XOR-shared scaled vector entry from the leaf node
  363. inline void scaled(RegXSWP &s, const LeafNode &leaf) const {
  364. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  365. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  366. }
  367. };
  368. // Streaming evaluation, to avoid taking up enough memory to store
  369. // an entire evaluation. T can be RDPF, RDPFPair, or RDPFTriple.
  370. template <typename T>
  371. class StreamEval {
  372. const T &rdpf;
  373. size_t &aes_ops;
  374. bool use_expansion;
  375. nbits_t depth;
  376. address_t counter_xor_offset;
  377. address_t indexmask;
  378. address_t pathindex;
  379. address_t nextindex;
  380. std::vector<typename T::node> path;
  381. public:
  382. // Create a StreamEval object that will start its output at index
  383. // start. It will wrap around to 0 when it hits 2^depth. If
  384. // use_expansion is true, then if the DPF has been expanded, just
  385. // output values from that. If use_expansion=false or if the DPF
  386. // has not been expanded, compute the values on the fly. If
  387. // xor_offset is non-zero, then the outputs are actually
  388. // DPF(start XOR xor_offset)
  389. // DPF((start+1) XOR xor_offset)
  390. // DPF((start+2) XOR xor_offset)
  391. // etc.
  392. StreamEval(const T &rdpf, address_t start,
  393. address_t xor_offset, size_t &aes_ops,
  394. bool use_expansion = true);
  395. // Get the next value (or tuple of values) from the evaluator
  396. typename T::LeafNode next();
  397. };
  398. // Parallel evaluation. This class launches a number of threads each
  399. // running a StreamEval to evaluate a chunk of the RDPF (or RDPFPair or
  400. // RDPFTriple), and accumulates the results within each chunk, and then
  401. // accumulates all the chunks together. T can be RDPF, RDPFPair, or
  402. // RDPFTriple.
  403. template <typename T>
  404. struct ParallelEval {
  405. const T &rdpf;
  406. address_t start;
  407. address_t xor_offset;
  408. address_t num_evals;
  409. int num_threads;
  410. size_t &aes_ops;
  411. // Create a Parallel evaluator that will evaluate the given rdpf at
  412. // DPF(start XOR xor_offset)
  413. // DPF((start+1) XOR xor_offset)
  414. // DPF((start+2) XOR xor_offset)
  415. // ...
  416. // DPF((start+num_evals-1) XOR xor_offset)
  417. // where all indices are taken mod 2^depth, and accumulate the
  418. // results into a single answer.
  419. ParallelEval(const T &rdpf, address_t start,
  420. address_t xor_offset, address_t num_evals,
  421. int num_threads, size_t &aes_ops) :
  422. rdpf(rdpf), start(start), xor_offset(xor_offset),
  423. num_evals(num_evals), num_threads(num_threads),
  424. aes_ops(aes_ops) {}
  425. // Run the parallel evaluator. The type V is the type of the
  426. // accumulator; init should be the "zero" value of the accumulator.
  427. // The type W (process) is a lambda type with the signature
  428. // (int, address_t, const T::node &) -> V
  429. // which will be called like this for each i from 0 to num_evals-1,
  430. // across num_thread threads:
  431. // value_i = process(t, i, DPF((start+i) XOR xor_offset))
  432. // t is the thread number (0 <= t < num_threads).
  433. // The resulting num_evals values will be combined using V's +=
  434. // operator, first accumulating the values within each thread
  435. // (starting with the init value), and then accumulating the totals
  436. // from each thread together (again starting with the init value):
  437. //
  438. // total = init
  439. // for each thread t:
  440. // accum_t = init
  441. // for each accum_i generated by thread t:
  442. // accum_t += value_i
  443. // total += accum_t
  444. template <typename V, typename W>
  445. inline V reduce(V init, W process);
  446. };
  447. #include "rdpf.tcc"
  448. #endif