rdpf.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 save_expansion = false);
  91. // Do we have a precomputed expansion?
  92. inline bool has_expansion() const { return expansion.size() > 0; }
  93. // Get an element of the expansion
  94. inline LeafNode get_expansion(address_t index) const {
  95. return expansion[index];
  96. }
  97. // The depth
  98. inline nbits_t depth() const { return curdepth; }
  99. // Get the leaf node for the given input
  100. //
  101. // Cost: depth AES operations
  102. LeafNode leaf(address_t input, size_t &aes_ops) const;
  103. // Expand the DPF if it's not already expanded
  104. void expand(size_t &aes_ops);
  105. // Descend from a node at depth parentdepth to one of its leaf children
  106. // whichchild = 0: left child
  107. // whichchild = 1: right child
  108. //
  109. // Cost: 1 AES operation
  110. inline LeafNode descend_to_leaf(const DPFnode &parent,
  111. nbits_t parentdepth, bit_t whichchild, size_t &aes_ops) const;
  112. // Get the bit-shared unit vector entry from the leaf node
  113. inline RegBS unit_bs(const LeafNode &leaf) const {
  114. RegBS b;
  115. b.bshare = get_lsb(leaf[0]);
  116. return b;
  117. }
  118. // Get the additive-shared unit vector entry from the leaf node
  119. inline RegAS unit_as(const LeafNode &leaf) const {
  120. RegAS a;
  121. value_t lowword = value_t(_mm_cvtsi128_si64x(leaf[0]));
  122. if (whichhalf == 1) {
  123. lowword = -lowword;
  124. }
  125. a.ashare = lowword * li[0].unit_sum_inverse;
  126. return a;
  127. }
  128. // Get the XOR-shared scaled vector entry from the leaf node
  129. inline RegXSW scaled_xs(const LeafNode &leaf) const {
  130. RegXSW x;
  131. nbits_t j = 0;
  132. value_t highword =
  133. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[0],8)));
  134. x[j++].xshare = highword;
  135. for (nbits_t i=1;i<LWIDTH;++i) {
  136. value_t lowword =
  137. value_t(_mm_cvtsi128_si64x(leaf[i]));
  138. value_t highword =
  139. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[i],8)));
  140. x[j++].xshare = lowword;
  141. if (j < WIDTH) {
  142. x[j++].xshare = highword;
  143. }
  144. }
  145. return x;
  146. }
  147. // Get the additive-shared scaled vector entry from the leaf node
  148. inline RegASW scaled_as(const LeafNode &leaf) const {
  149. RegASW a;
  150. nbits_t j = 0;
  151. value_t highword =
  152. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[0],8)));
  153. if (whichhalf == 1) {
  154. highword = -highword;
  155. }
  156. a[j++].ashare = highword;
  157. for (nbits_t i=1;i<WIDTH;++i) {
  158. value_t lowword =
  159. value_t(_mm_cvtsi128_si64x(leaf[i]));
  160. value_t highword =
  161. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leaf[i],8)));
  162. if (whichhalf == 1) {
  163. lowword = -lowword;
  164. highword = -highword;
  165. }
  166. a[j++].ashare = lowword;
  167. if (j < WIDTH) {
  168. a[j++].ashare = highword;
  169. }
  170. }
  171. return a;
  172. }
  173. };
  174. // Computational peers will generate triples of RDPFs with the _same_
  175. // random target for use in Duoram. They will each hold a share of the
  176. // target (neither knowing the complete target index). They will each
  177. // give one of the DPFs (not a matching pair) to the server, but not the
  178. // shares of the target index. So computational peers will hold a
  179. // RDPFTriple (which includes both an additive and an XOR share of the
  180. // target index), while the server will hold a RDPFPair (which does
  181. // not).
  182. template <nbits_t WIDTH>
  183. struct RDPFTriple {
  184. template <typename T>
  185. using Triple = std::tuple<T, T, T>;
  186. template <typename T>
  187. using WTriple = std::tuple<
  188. typename std::array<T,WIDTH>,
  189. typename std::array<T,WIDTH>,
  190. typename std::array<T,WIDTH> >;
  191. // The type of triples of nodes, LeafNodes, and the wide shared
  192. // register types
  193. using node = Triple<DPFnode>;
  194. using LeafNode = Triple<typename RDPF<WIDTH>::LeafNode>;
  195. using RegASWT = WTriple<RegAS>;
  196. using RegXSWT = WTriple<RegXS>;
  197. RegAS as_target;
  198. RegXS xs_target;
  199. RDPF<WIDTH> dpf[3];
  200. // The depth
  201. inline nbits_t depth() const { return dpf[0].depth(); }
  202. // The seed
  203. inline node get_seed() const {
  204. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed(),
  205. dpf[2].get_seed());
  206. }
  207. // Do we have a precomputed expansion?
  208. inline bool has_expansion() const {
  209. return dpf[0].expansion.size() > 0;
  210. }
  211. // Get an element of the expansion
  212. inline LeafNode get_expansion(address_t index) const {
  213. return std::make_tuple(dpf[0].get_expansion(index),
  214. dpf[1].get_expansion(index), dpf[2].get_expansion(index));
  215. }
  216. RDPFTriple() {}
  217. // Construct three RDPFs of the given depth all with the same
  218. // randomly generated target index.
  219. RDPFTriple(MPCTIO &tio, yield_t &yield,
  220. nbits_t depth, bool save_expansion = false);
  221. // Descend the three RDPFs in lock step
  222. node descend(const node &parent, nbits_t parentdepth,
  223. bit_t whichchild, size_t &aes_ops) const;
  224. // Descend the three RDPFs in lock step to a leaf node
  225. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  226. bit_t whichchild, size_t &aes_ops) const;
  227. // Overloaded versions of functions to get DPF components and
  228. // outputs so that the appropriate one can be selected with a
  229. // parameter
  230. inline void get_target(RegAS &target) const { target = as_target; }
  231. inline void get_target(RegXS &target) const { target = xs_target; }
  232. // Additive share of the scaling value M_as such that the high words
  233. // of the leaf values for P0 and P1 add to M_as * e_{target}
  234. inline void scaled_value(RegASWT &v) const {
  235. std::get<0>(v) = dpf[0].li[0].scaled_sum;
  236. std::get<1>(v) = dpf[1].li[0].scaled_sum;
  237. std::get<2>(v) = dpf[2].li[0].scaled_sum;
  238. }
  239. // XOR share of the scaling value M_xs such that the high words
  240. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  241. inline void scaled_value(RegXSWT &v) const {
  242. std::get<0>(v) = dpf[0].li[0].scaled_xor;
  243. std::get<1>(v) = dpf[1].li[0].scaled_xor;
  244. std::get<2>(v) = dpf[2].li[0].scaled_xor;
  245. }
  246. // Get the additive-shared unit vector entry from the leaf node
  247. inline void unit(std::tuple<RegAS,RegAS,RegAS> &u, const LeafNode &leaf) const {
  248. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  249. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  250. std::get<2>(u) = dpf[2].unit_as(std::get<2>(leaf));
  251. }
  252. // Get the bit-shared unit vector entry from the leaf node
  253. inline void unit(std::tuple<RegXS,RegXS,RegXS> &u, const LeafNode &leaf) const {
  254. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  255. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  256. std::get<2>(u) = dpf[2].unit_bs(std::get<2>(leaf));
  257. }
  258. // For any more complex entry type, that type will handle the conversion
  259. // for each DPF
  260. template <typename T>
  261. inline void unit(std::tuple<T,T,T> &u, const LeafNode &leaf) const {
  262. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  263. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  264. std::get<2>(u).unit(dpf[2], std::get<2>(leaf));
  265. }
  266. // Get the additive-shared scaled vector entry from the leaf node
  267. inline void scaled(RegASWT &s, const LeafNode &leaf) const {
  268. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  269. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  270. std::get<2>(s) = dpf[2].scaled_as(std::get<2>(leaf));
  271. }
  272. // Get the XOR-shared scaled vector entry from the leaf node
  273. inline void scaled(RegXSWT &s, const LeafNode &leaf) const {
  274. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  275. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  276. std::get<2>(s) = dpf[2].scaled_xs(std::get<2>(leaf));
  277. }
  278. };
  279. template <nbits_t WIDTH>
  280. struct RDPFPair {
  281. template <typename T>
  282. using Pair = std::tuple<T, T>;
  283. template <typename T>
  284. using WPair = std::tuple<
  285. typename std::array<T,WIDTH>,
  286. typename std::array<T,WIDTH> >;
  287. // The type of pairs of nodes, LeafNodes, and the wide shared
  288. // register types
  289. using node = Pair<DPFnode>;
  290. using LeafNode = Pair<typename RDPF<WIDTH>::LeafNode>;
  291. using RegASWP = WPair<RegAS>;
  292. using RegXSWP = WPair<RegXS>;
  293. RDPF<WIDTH> dpf[2];
  294. RDPFPair() {}
  295. // Create an RDPFPair from an RDPFTriple, keeping two of the RDPFs
  296. // and dropping one. This _moves_ the dpfs from the triple to the
  297. // pair, so the triple will no longer be valid after using this.
  298. // which0 and which1 indicate which of the dpfs to keep.
  299. RDPFPair(RDPFTriple<WIDTH> &&trip, int which0, int which1) {
  300. dpf[0] = std::move(trip.dpf[which0]);
  301. dpf[1] = std::move(trip.dpf[which1]);
  302. }
  303. // The depth
  304. inline nbits_t depth() const { return dpf[0].depth(); }
  305. // The seed
  306. inline node get_seed() const {
  307. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  308. }
  309. // Do we have a precomputed expansion?
  310. inline bool has_expansion() const {
  311. return dpf[0].expansion.size() > 0;
  312. }
  313. // Get an element of the expansion
  314. inline LeafNode get_expansion(address_t index) const {
  315. return std::make_tuple(dpf[0].get_expansion(index),
  316. dpf[1].get_expansion(index));
  317. }
  318. // Descend the two RDPFs in lock step
  319. node descend(const node &parent, nbits_t parentdepth,
  320. bit_t whichchild, size_t &aes_ops) const;
  321. // Descend the two RDPFs in lock step to a leaf node
  322. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  323. bit_t whichchild, size_t &aes_ops) const;
  324. // Overloaded versions of functions to get DPF components and
  325. // outputs so that the appropriate one can be selected with a
  326. // parameter
  327. // Additive share of the scaling value M_as such that the high words
  328. // of the leaf values for P0 and P1 add to M_as * e_{target}
  329. inline void scaled_value(RegASWP &v) const {
  330. std::get<0>(v) = dpf[0].scaled_sum;
  331. std::get<1>(v) = dpf[1].scaled_sum;
  332. }
  333. // XOR share of the scaling value M_xs such that the high words
  334. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  335. inline void scaled_value(RegXSWP &v) const {
  336. std::get<0>(v) = dpf[0].scaled_xor;
  337. std::get<1>(v) = dpf[1].scaled_xor;
  338. }
  339. // Get the additive-shared unit vector entry from the leaf node
  340. inline void unit(std::tuple<RegAS,RegAS> &u, const LeafNode &leaf) const {
  341. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  342. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  343. }
  344. // Get the bit-shared unit vector entry from the leaf node
  345. inline void unit(std::tuple<RegXS,RegXS> &u, const LeafNode &leaf) const {
  346. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  347. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  348. }
  349. // For any more complex entry type, that type will handle the conversion
  350. // for each DPF
  351. template <typename T>
  352. inline void unit(std::tuple<T,T> &u, const LeafNode &leaf) const {
  353. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  354. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  355. }
  356. // Get the additive-shared scaled vector entry from the leaf node
  357. inline void scaled(RegASWP &s, const LeafNode &leaf) const {
  358. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  359. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  360. }
  361. // Get the XOR-shared scaled vector entry from the leaf node
  362. inline void scaled(RegXSWP &s, const LeafNode &leaf) const {
  363. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  364. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  365. }
  366. };
  367. // Streaming evaluation, to avoid taking up enough memory to store
  368. // an entire evaluation. T can be RDPF, RDPFPair, or RDPFTriple.
  369. template <typename T>
  370. class StreamEval {
  371. const T &rdpf;
  372. size_t &aes_ops;
  373. bool use_expansion;
  374. nbits_t depth;
  375. address_t counter_xor_offset;
  376. address_t indexmask;
  377. address_t pathindex;
  378. address_t nextindex;
  379. std::vector<typename T::node> path;
  380. public:
  381. // Create a StreamEval object that will start its output at index
  382. // start. It will wrap around to 0 when it hits 2^depth. If
  383. // use_expansion is true, then if the DPF has been expanded, just
  384. // output values from that. If use_expansion=false or if the DPF
  385. // has not been expanded, compute the values on the fly. If
  386. // xor_offset is non-zero, then the outputs are actually
  387. // DPF(start XOR xor_offset)
  388. // DPF((start+1) XOR xor_offset)
  389. // DPF((start+2) XOR xor_offset)
  390. // etc.
  391. StreamEval(const T &rdpf, address_t start,
  392. address_t xor_offset, size_t &aes_ops,
  393. bool use_expansion = true);
  394. // Get the next value (or tuple of values) from the evaluator
  395. typename T::LeafNode next();
  396. };
  397. // Parallel evaluation. This class launches a number of threads each
  398. // running a StreamEval to evaluate a chunk of the RDPF (or RDPFPair or
  399. // RDPFTriple), and accumulates the results within each chunk, and then
  400. // accumulates all the chunks together. T can be RDPF, RDPFPair, or
  401. // RDPFTriple.
  402. template <typename T>
  403. struct ParallelEval {
  404. const T &rdpf;
  405. address_t start;
  406. address_t xor_offset;
  407. address_t num_evals;
  408. int num_threads;
  409. size_t &aes_ops;
  410. // Create a Parallel evaluator that will evaluate the given rdpf at
  411. // DPF(start XOR xor_offset)
  412. // DPF((start+1) XOR xor_offset)
  413. // DPF((start+2) XOR xor_offset)
  414. // ...
  415. // DPF((start+num_evals-1) XOR xor_offset)
  416. // where all indices are taken mod 2^depth, and accumulate the
  417. // results into a single answer.
  418. ParallelEval(const T &rdpf, address_t start,
  419. address_t xor_offset, address_t num_evals,
  420. int num_threads, size_t &aes_ops) :
  421. rdpf(rdpf), start(start), xor_offset(xor_offset),
  422. num_evals(num_evals), num_threads(num_threads),
  423. aes_ops(aes_ops) {}
  424. // Run the parallel evaluator. The type V is the type of the
  425. // accumulator; init should be the "zero" value of the accumulator.
  426. // The type W (process) is a lambda type with the signature
  427. // (int, address_t, const T::node &) -> V
  428. // which will be called like this for each i from 0 to num_evals-1,
  429. // across num_thread threads:
  430. // value_i = process(t, i, DPF((start+i) XOR xor_offset))
  431. // t is the thread number (0 <= t < num_threads).
  432. // The resulting num_evals values will be combined using V's +=
  433. // operator, first accumulating the values within each thread
  434. // (starting with the init value), and then accumulating the totals
  435. // from each thread together (again starting with the init value):
  436. //
  437. // total = init
  438. // for each thread t:
  439. // accum_t = init
  440. // for each accum_i generated by thread t:
  441. // accum_t += value_i
  442. // total += accum_t
  443. template <typename V, typename W>
  444. inline V reduce(V init, W process);
  445. };
  446. #include "rdpf.tcc"
  447. #endif