rdpf.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. // If we're saving the expansion, put it here
  55. std::vector<LeafNode> expansion;
  56. LeafInfo() : unit_sum_inverse(0) {}
  57. };
  58. // The depth of this RDPF. If this is not an incremental DPF, then
  59. // both the maximum depth and current depth are just the normal
  60. // depth (specified at DPF creation time). If this is an
  61. // incremental DPF, then the maximum depth is the one specified at
  62. // creation time, but the current depth will be between 1 and that
  63. // value (inclusive).
  64. nbits_t maxdepth, curdepth;
  65. // The LeafInfo for each leaf level. Normal RDPFs only have one
  66. // leaf level, so this will be a vector of length 1. Incremental
  67. // RDPFs will have one entry for each level in the DPF. The entry
  68. // corresponding to level i of the DPF (of total depth d) is
  69. // leaf_info[d-i].
  70. std::vector<LeafInfo> li;
  71. // The leaf correction flag bits for each leaf level. The bit for
  72. // level i (for an incremental DPF of max depth m) is leaf_cfbits &
  73. // (1<<(m-i)). For a normal (not incremental) RDPF, it's the same,
  74. // but therefore only the low bit gets used.
  75. value_t leaf_cfbits;
  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 {
  94. return li[maxdepth-curdepth].expansion.size() > 0;
  95. }
  96. // Get an element of the expansion
  97. inline LeafNode get_expansion(address_t index) const {
  98. return li[maxdepth-curdepth].expansion[index];
  99. }
  100. // The depth
  101. inline nbits_t depth() const { return curdepth; }
  102. // Set the current depth for an incremental RDPF; 0 means to use
  103. // maxdepth
  104. inline void depth(nbits_t newdepth) {
  105. if (newdepth > 0 && newdepth < maxdepth) {
  106. curdepth = newdepth;
  107. } else {
  108. curdepth = maxdepth;
  109. }
  110. }
  111. // Get the leaf node for the given input
  112. //
  113. // Cost: depth AES operations
  114. LeafNode leaf(address_t input, size_t &aes_ops) const;
  115. // Expand the DPF if it's not already expanded
  116. void expand(size_t &aes_ops);
  117. // Descend from a node at depth parentdepth to one of its leaf children
  118. // whichchild = 0: left child
  119. // whichchild = 1: right child
  120. //
  121. // Cost: 1 AES operation
  122. inline LeafNode descend_to_leaf(const DPFnode &parent,
  123. nbits_t parentdepth, bit_t whichchild, size_t &aes_ops) const;
  124. // Get the bit-shared unit vector entry from the leaf node
  125. inline RegBS unit_bs(const LeafNode &leaf) const {
  126. RegBS b;
  127. b.bshare = get_lsb(leaf[0]);
  128. return b;
  129. }
  130. // Get the additive-shared unit vector entry from the leaf node
  131. inline RegAS unit_as(const LeafNode &leaf) const {
  132. RegAS a;
  133. value_t lowword = value_t(_mm_cvtsi128_si64x(leaf[0]));
  134. if (whichhalf == 1) {
  135. lowword = -lowword;
  136. }
  137. a.ashare = lowword * li[maxdepth-curdepth].unit_sum_inverse;
  138. return a;
  139. }
  140. // Get the XOR-shared scaled vector entry from the leaf node
  141. inline RegXSW scaled_xs(const LeafNode &leaf) const {
  142. RegXSW x;
  143. nbits_t j = 0;
  144. value_t highword =
  145. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[0],8)));
  146. x[j++].xshare = highword;
  147. for (nbits_t i=1;i<LWIDTH;++i) {
  148. value_t lowword =
  149. value_t(_mm_cvtsi128_si64x(leaf[i]));
  150. value_t highword =
  151. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[i],8)));
  152. x[j++].xshare = lowword;
  153. if (j < WIDTH) {
  154. x[j++].xshare = highword;
  155. }
  156. }
  157. return x;
  158. }
  159. // Get the additive-shared scaled vector entry from the leaf node
  160. inline RegASW scaled_as(const LeafNode &leaf) const {
  161. RegASW a;
  162. nbits_t j = 0;
  163. value_t highword =
  164. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[0],8)));
  165. if (whichhalf == 1) {
  166. highword = -highword;
  167. }
  168. a[j++].ashare = highword;
  169. for (nbits_t i=1;i<WIDTH;++i) {
  170. value_t lowword =
  171. value_t(_mm_cvtsi128_si64x(leaf[i]));
  172. value_t highword =
  173. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[i],8)));
  174. if (whichhalf == 1) {
  175. lowword = -lowword;
  176. highword = -highword;
  177. }
  178. a[j++].ashare = lowword;
  179. if (j < WIDTH) {
  180. a[j++].ashare = highword;
  181. }
  182. }
  183. return a;
  184. }
  185. private:
  186. // Expand one leaf layer of the DPF if it's not already expanded
  187. void expand_leaf_layer(nbits_t li_index, size_t &aes_ops);
  188. };
  189. // Computational peers will generate triples of RDPFs with the _same_
  190. // random target for use in Duoram. They will each hold a share of the
  191. // target (neither knowing the complete target index). They will each
  192. // give one of the DPFs (not a matching pair) to the server, but not the
  193. // shares of the target index. So computational peers will hold a
  194. // RDPFTriple (which includes both an additive and an XOR share of the
  195. // target index), while the server will hold a RDPFPair (which does
  196. // not).
  197. template <nbits_t WIDTH>
  198. struct RDPFTriple {
  199. template <typename T>
  200. using Triple = std::tuple<T, T, T>;
  201. template <typename T>
  202. using WTriple = std::tuple<
  203. typename std::array<T,WIDTH>,
  204. typename std::array<T,WIDTH>,
  205. typename std::array<T,WIDTH> >;
  206. // The type of triples of nodes, LeafNodes, and the wide shared
  207. // register types
  208. using node = Triple<DPFnode>;
  209. using LeafNode = Triple<typename RDPF<WIDTH>::LeafNode>;
  210. using RegASWT = WTriple<RegAS>;
  211. using RegXSWT = WTriple<RegXS>;
  212. RegAS as_target;
  213. RegXS xs_target;
  214. RDPF<WIDTH> dpf[3];
  215. // The depth
  216. inline nbits_t depth() const { return dpf[0].depth(); }
  217. // Set the current depth for an incremental RDPFTriple; 0 means to
  218. // use maxdepth
  219. inline void depth(nbits_t newdepth) {
  220. dpf[0].depth(newdepth);
  221. dpf[1].depth(newdepth);
  222. dpf[2].depth(newdepth);
  223. }
  224. // The seed
  225. inline node get_seed() const {
  226. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed(),
  227. dpf[2].get_seed());
  228. }
  229. // Do we have a precomputed expansion?
  230. inline bool has_expansion() const {
  231. int li_index = dpf[0].maxdepth - dpf[0].curdepth;
  232. return dpf[0].li[li_index].expansion.size() > 0;
  233. }
  234. // Get an element of the expansion
  235. inline LeafNode get_expansion(address_t index) const {
  236. return std::make_tuple(dpf[0].get_expansion(index),
  237. dpf[1].get_expansion(index), dpf[2].get_expansion(index));
  238. }
  239. RDPFTriple() {}
  240. // Construct three RDPFs of the given depth all with the same
  241. // randomly generated target index.
  242. RDPFTriple(MPCTIO &tio, yield_t &yield,
  243. nbits_t depth, bool incremental = false, bool save_expansion = false);
  244. // Descend the three RDPFs in lock step
  245. node descend(const node &parent, nbits_t parentdepth,
  246. bit_t whichchild, size_t &aes_ops) const;
  247. // Descend the three RDPFs in lock step to a leaf node
  248. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  249. bit_t whichchild, size_t &aes_ops) const;
  250. // Overloaded versions of functions to get DPF components and
  251. // outputs so that the appropriate one can be selected with a
  252. // parameter
  253. inline void get_target(RegAS &target) const { target = as_target; }
  254. inline void get_target(RegXS &target) const { target = xs_target; }
  255. // Additive share of the scaling value M_as such that the high words
  256. // of the leaf values for P0 and P1 add to M_as * e_{target}
  257. inline void scaled_value(RegASWT &v) const {
  258. int li_index = dpf[0].maxdepth - dpf[0].curdepth;
  259. std::get<0>(v) = dpf[0].li[li_index].scaled_sum;
  260. std::get<1>(v) = dpf[1].li[li_index].scaled_sum;
  261. std::get<2>(v) = dpf[2].li[li_index].scaled_sum;
  262. }
  263. // XOR share of the scaling value M_xs such that the high words
  264. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  265. inline void scaled_value(RegXSWT &v) const {
  266. int li_index = dpf[0].maxdepth - dpf[0].curdepth;
  267. std::get<0>(v) = dpf[0].li[li_index].scaled_xor;
  268. std::get<1>(v) = dpf[1].li[li_index].scaled_xor;
  269. std::get<2>(v) = dpf[2].li[li_index].scaled_xor;
  270. }
  271. // Get the additive-shared unit vector entry from the leaf node
  272. inline void unit(std::tuple<RegAS,RegAS,RegAS> &u, const LeafNode &leaf) const {
  273. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  274. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  275. std::get<2>(u) = dpf[2].unit_as(std::get<2>(leaf));
  276. }
  277. // Get the bit-shared unit vector entry from the leaf node
  278. inline void unit(std::tuple<RegXS,RegXS,RegXS> &u, const LeafNode &leaf) const {
  279. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  280. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  281. std::get<2>(u) = dpf[2].unit_bs(std::get<2>(leaf));
  282. }
  283. // For any more complex entry type, that type will handle the conversion
  284. // for each DPF
  285. template <typename T>
  286. inline void unit(std::tuple<T,T,T> &u, const LeafNode &leaf) const {
  287. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  288. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  289. std::get<2>(u).unit(dpf[2], std::get<2>(leaf));
  290. }
  291. // Get the additive-shared scaled vector entry from the leaf node
  292. inline void scaled(RegASWT &s, const LeafNode &leaf) const {
  293. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  294. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  295. std::get<2>(s) = dpf[2].scaled_as(std::get<2>(leaf));
  296. }
  297. // Get the XOR-shared scaled vector entry from the leaf node
  298. inline void scaled(RegXSWT &s, const LeafNode &leaf) const {
  299. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  300. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  301. std::get<2>(s) = dpf[2].scaled_xs(std::get<2>(leaf));
  302. }
  303. };
  304. template <nbits_t WIDTH>
  305. struct RDPFPair {
  306. template <typename T>
  307. using Pair = std::tuple<T, T>;
  308. template <typename T>
  309. using WPair = std::tuple<
  310. typename std::array<T,WIDTH>,
  311. typename std::array<T,WIDTH> >;
  312. // The type of pairs of nodes, LeafNodes, and the wide shared
  313. // register types
  314. using node = Pair<DPFnode>;
  315. using LeafNode = Pair<typename RDPF<WIDTH>::LeafNode>;
  316. using RegASWP = WPair<RegAS>;
  317. using RegXSWP = WPair<RegXS>;
  318. RDPF<WIDTH> dpf[2];
  319. RDPFPair() {}
  320. // The depth
  321. inline nbits_t depth() const { return dpf[0].depth(); }
  322. // Set the current depth for an incremental RDPFPair; 0 means to use
  323. // maxdepth
  324. inline void depth(nbits_t newdepth) {
  325. dpf[0].depth(newdepth);
  326. dpf[1].depth(newdepth);
  327. }
  328. // The seed
  329. inline node get_seed() const {
  330. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  331. }
  332. // Do we have a precomputed expansion?
  333. inline bool has_expansion() const {
  334. int li_index = dpf[0].maxdepth - dpf[0].curdepth;
  335. return dpf[0].li[li_index].expansion.size() > 0;
  336. }
  337. // Get an element of the expansion
  338. inline LeafNode get_expansion(address_t index) const {
  339. return std::make_tuple(dpf[0].get_expansion(index),
  340. dpf[1].get_expansion(index));
  341. }
  342. // Descend the two RDPFs in lock step
  343. node descend(const node &parent, nbits_t parentdepth,
  344. bit_t whichchild, size_t &aes_ops) const;
  345. // Descend the two RDPFs in lock step to a leaf node
  346. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  347. bit_t whichchild, size_t &aes_ops) const;
  348. // Overloaded versions of functions to get DPF components and
  349. // outputs so that the appropriate one can be selected with a
  350. // parameter
  351. // Additive share of the scaling value M_as such that the high words
  352. // of the leaf values for P0 and P1 add to M_as * e_{target}
  353. inline void scaled_value(RegASWP &v) const {
  354. std::get<0>(v) = dpf[0].scaled_sum;
  355. std::get<1>(v) = dpf[1].scaled_sum;
  356. }
  357. // XOR share of the scaling value M_xs such that the high words
  358. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  359. inline void scaled_value(RegXSWP &v) const {
  360. std::get<0>(v) = dpf[0].scaled_xor;
  361. std::get<1>(v) = dpf[1].scaled_xor;
  362. }
  363. // Get the additive-shared unit vector entry from the leaf node
  364. inline void unit(std::tuple<RegAS,RegAS> &u, const LeafNode &leaf) const {
  365. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  366. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  367. }
  368. // Get the bit-shared unit vector entry from the leaf node
  369. inline void unit(std::tuple<RegXS,RegXS> &u, const LeafNode &leaf) const {
  370. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  371. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  372. }
  373. // For any more complex entry type, that type will handle the conversion
  374. // for each DPF
  375. template <typename T>
  376. inline void unit(std::tuple<T,T> &u, const LeafNode &leaf) const {
  377. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  378. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  379. }
  380. // Get the additive-shared scaled vector entry from the leaf node
  381. inline void scaled(RegASWP &s, const LeafNode &leaf) const {
  382. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  383. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  384. }
  385. // Get the XOR-shared scaled vector entry from the leaf node
  386. inline void scaled(RegXSWP &s, const LeafNode &leaf) const {
  387. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  388. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  389. }
  390. };
  391. // These are used by computational peers, who hold RPDFTriples, but when
  392. // reading, only need to use 2 of the 3 RDPFs. The API follows that of
  393. // RDPFPair, but internally, it holds two references to external RDPFs,
  394. // instead of holding the RDPFs themselves.
  395. template <nbits_t WIDTH>
  396. struct RDPF2of3 {
  397. template <typename T>
  398. using Pair = std::tuple<T, T>;
  399. template <typename T>
  400. using WPair = std::tuple<
  401. typename std::array<T,WIDTH>,
  402. typename std::array<T,WIDTH> >;
  403. // The type of pairs of nodes, LeafNodes, and the wide shared
  404. // register types
  405. using node = Pair<DPFnode>;
  406. using LeafNode = Pair<typename RDPF<WIDTH>::LeafNode>;
  407. using RegASWP = WPair<RegAS>;
  408. using RegXSWP = WPair<RegXS>;
  409. const RDPF<WIDTH> &dpf0, &dpf1;
  410. // Create an RDPFPair from an RDPFTriple, keeping two of the RDPFs
  411. // and dropping one. This _moves_ the dpfs from the triple to the
  412. // pair, so the triple will no longer be valid after using this.
  413. // which0 and which1 indicate which of the dpfs to keep.
  414. RDPF2of3(const RDPFTriple<WIDTH> &trip, int which0, int which1) :
  415. dpf0(trip.dpf[which0]), dpf1(trip.dpf[which1]) {}
  416. // The depth
  417. inline nbits_t depth() const { return dpf0.depth(); }
  418. // Set the current depth for an incremental RDPFPair; 0 means to use
  419. // maxdepth
  420. inline void depth(nbits_t newdepth) {
  421. dpf0.depth(newdepth);
  422. dpf1.depth(newdepth);
  423. }
  424. // The seed
  425. inline node get_seed() const {
  426. return std::make_tuple(dpf0.get_seed(), dpf1.get_seed());
  427. }
  428. // Do we have a precomputed expansion?
  429. inline bool has_expansion() const {
  430. int li_index = dpf0.maxdepth - dpf0.curdepth;
  431. return dpf0.li[li_index].expansion.size() > 0;
  432. }
  433. // Get an element of the expansion
  434. inline LeafNode get_expansion(address_t index) const {
  435. return std::make_tuple(dpf0.get_expansion(index),
  436. dpf1.get_expansion(index));
  437. }
  438. // Descend the two RDPFs in lock step
  439. node descend(const node &parent, nbits_t parentdepth,
  440. bit_t whichchild, size_t &aes_ops) const;
  441. // Descend the two RDPFs in lock step to a leaf node
  442. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  443. bit_t whichchild, size_t &aes_ops) const;
  444. // Overloaded versions of functions to get DPF components and
  445. // outputs so that the appropriate one can be selected with a
  446. // parameter
  447. // Additive share of the scaling value M_as such that the high words
  448. // of the leaf values for P0 and P1 add to M_as * e_{target}
  449. inline void scaled_value(RegASWP &v) const {
  450. std::get<0>(v) = dpf0.scaled_sum;
  451. std::get<1>(v) = dpf1.scaled_sum;
  452. }
  453. // XOR share of the scaling value M_xs such that the high words
  454. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  455. inline void scaled_value(RegXSWP &v) const {
  456. std::get<0>(v) = dpf0.scaled_xor;
  457. std::get<1>(v) = dpf1.scaled_xor;
  458. }
  459. // Get the additive-shared unit vector entry from the leaf node
  460. inline void unit(std::tuple<RegAS,RegAS> &u, const LeafNode &leaf) const {
  461. std::get<0>(u) = dpf0.unit_as(std::get<0>(leaf));
  462. std::get<1>(u) = dpf1.unit_as(std::get<1>(leaf));
  463. }
  464. // Get the bit-shared unit vector entry from the leaf node
  465. inline void unit(std::tuple<RegXS,RegXS> &u, const LeafNode &leaf) const {
  466. std::get<0>(u) = dpf0.unit_bs(std::get<0>(leaf));
  467. std::get<1>(u) = dpf1.unit_bs(std::get<1>(leaf));
  468. }
  469. // For any more complex entry type, that type will handle the conversion
  470. // for each DPF
  471. template <typename T>
  472. inline void unit(std::tuple<T,T> &u, const LeafNode &leaf) const {
  473. std::get<0>(u).unit(dpf0, std::get<0>(leaf));
  474. std::get<1>(u).unit(dpf1, std::get<1>(leaf));
  475. }
  476. // Get the additive-shared scaled vector entry from the leaf node
  477. inline void scaled(RegASWP &s, const LeafNode &leaf) const {
  478. std::get<0>(s) = dpf0.scaled_as(std::get<0>(leaf));
  479. std::get<1>(s) = dpf1.scaled_as(std::get<1>(leaf));
  480. }
  481. // Get the XOR-shared scaled vector entry from the leaf node
  482. inline void scaled(RegXSWP &s, const LeafNode &leaf) const {
  483. std::get<0>(s) = dpf0.scaled_xs(std::get<0>(leaf));
  484. std::get<1>(s) = dpf1.scaled_xs(std::get<1>(leaf));
  485. }
  486. };
  487. // Streaming evaluation, to avoid taking up enough memory to store
  488. // an entire evaluation. T can be RDPF, RDPFPair, or RDPFTriple.
  489. template <typename T>
  490. class StreamEval {
  491. const T &rdpf;
  492. size_t &aes_ops;
  493. bool use_expansion;
  494. nbits_t depth;
  495. address_t counter_xor_offset;
  496. address_t indexmask;
  497. address_t pathindex;
  498. address_t nextindex;
  499. std::vector<typename T::node> path;
  500. public:
  501. // Create a StreamEval object that will start its output at index
  502. // start. It will wrap around to 0 when it hits 2^depth. If
  503. // use_expansion is true, then if the DPF has been expanded, just
  504. // output values from that. If use_expansion=false or if the DPF
  505. // has not been expanded, compute the values on the fly. If
  506. // xor_offset is non-zero, then the outputs are actually
  507. // DPF(start XOR xor_offset)
  508. // DPF((start+1) XOR xor_offset)
  509. // DPF((start+2) XOR xor_offset)
  510. // etc.
  511. StreamEval(const T &rdpf, address_t start,
  512. address_t xor_offset, size_t &aes_ops,
  513. bool use_expansion = true);
  514. // Get the next value (or tuple of values) from the evaluator
  515. typename T::LeafNode next();
  516. };
  517. // Parallel evaluation. This class launches a number of threads each
  518. // running a StreamEval to evaluate a chunk of the RDPF (or RDPFPair or
  519. // RDPFTriple), and accumulates the results within each chunk, and then
  520. // accumulates all the chunks together. T can be RDPF, RDPFPair, or
  521. // RDPFTriple.
  522. template <typename T>
  523. struct ParallelEval {
  524. const T &rdpf;
  525. address_t start;
  526. address_t xor_offset;
  527. address_t num_evals;
  528. int num_threads;
  529. size_t &aes_ops;
  530. // Create a Parallel evaluator that will evaluate the given rdpf at
  531. // DPF(start XOR xor_offset)
  532. // DPF((start+1) XOR xor_offset)
  533. // DPF((start+2) XOR xor_offset)
  534. // ...
  535. // DPF((start+num_evals-1) XOR xor_offset)
  536. // where all indices are taken mod 2^depth, and accumulate the
  537. // results into a single answer.
  538. ParallelEval(const T &rdpf, address_t start,
  539. address_t xor_offset, address_t num_evals,
  540. int num_threads, size_t &aes_ops) :
  541. rdpf(rdpf), start(start), xor_offset(xor_offset),
  542. num_evals(num_evals), num_threads(num_threads),
  543. aes_ops(aes_ops) {}
  544. // Run the parallel evaluator. The type V is the type of the
  545. // accumulator; init should be the "zero" value of the accumulator.
  546. // The type W (process) is a lambda type with the signature
  547. // (int, address_t, const T::node &) -> V
  548. // which will be called like this for each i from 0 to num_evals-1,
  549. // across num_thread threads:
  550. // value_i = process(t, i, DPF((start+i) XOR xor_offset))
  551. // t is the thread number (0 <= t < num_threads).
  552. // The resulting num_evals values will be combined using V's +=
  553. // operator, first accumulating the values within each thread
  554. // (starting with the init value), and then accumulating the totals
  555. // from each thread together (again starting with the init value):
  556. //
  557. // total = init
  558. // for each thread t:
  559. // accum_t = init
  560. // for each accum_i generated by thread t:
  561. // accum_t += value_i
  562. // total += accum_t
  563. template <typename V, typename W>
  564. inline V reduce(V init, W process);
  565. };
  566. #include "rdpf.tcc"
  567. #endif