rdpf.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 = Triple<typename RDPF<WIDTH>::W<T>>;
  188. // The type of triples of nodes, LeafNodes, and the wide shared
  189. // register types
  190. using node = Triple<DPFnode>;
  191. using LeafNode = Triple<typename RDPF<WIDTH>::LeafNode>;
  192. using RegASWT = WTriple<RegAS>;
  193. using RegXSWT = WTriple<RegXS>;
  194. RegAS as_target;
  195. RegXS xs_target;
  196. RDPF<WIDTH> dpf[3];
  197. // The depth
  198. inline nbits_t depth() const { return dpf[0].depth(); }
  199. // The seed
  200. inline node get_seed() const {
  201. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed(),
  202. dpf[2].get_seed());
  203. }
  204. // Do we have a precomputed expansion?
  205. inline bool has_expansion() const {
  206. return dpf[0].expansion.size() > 0;
  207. }
  208. // Get an element of the expansion
  209. inline LeafNode get_expansion(address_t index) const {
  210. return std::make_tuple(dpf[0].get_expansion(index),
  211. dpf[1].get_expansion(index), dpf[2].get_expansion(index));
  212. }
  213. RDPFTriple() {}
  214. // Construct three RDPFs of the given depth all with the same
  215. // randomly generated target index.
  216. RDPFTriple(MPCTIO &tio, yield_t &yield,
  217. nbits_t depth, bool save_expansion = false);
  218. // Descend the three RDPFs in lock step
  219. node descend(const node &parent, nbits_t parentdepth,
  220. bit_t whichchild, size_t &aes_ops) const;
  221. // Descend the three RDPFs in lock step to a leaf node
  222. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  223. bit_t whichchild, size_t &aes_ops) const;
  224. // Overloaded versions of functions to get DPF components and
  225. // outputs so that the appropriate one can be selected with a
  226. // parameter
  227. inline void get_target(RegAS &target) const { target = as_target; }
  228. inline void get_target(RegXS &target) const { target = xs_target; }
  229. // Additive share of the scaling value M_as such that the high words
  230. // of the leaf values for P0 and P1 add to M_as * e_{target}
  231. inline void scaled_value(RegASWT &v) const {
  232. std::get<0>(v) = dpf[0].li[0].scaled_sum;
  233. std::get<1>(v) = dpf[1].li[0].scaled_sum;
  234. std::get<2>(v) = dpf[2].li[0].scaled_sum;
  235. }
  236. // XOR share of the scaling value M_xs such that the high words
  237. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  238. inline void scaled_value(RegXSWT &v) const {
  239. std::get<0>(v) = dpf[0].li[0].scaled_xor;
  240. std::get<1>(v) = dpf[1].li[0].scaled_xor;
  241. std::get<2>(v) = dpf[2].li[0].scaled_xor;
  242. }
  243. // Get the additive-shared unit vector entry from the leaf node
  244. inline void unit(std::tuple<RegAS,RegAS,RegAS> &u, const LeafNode &leaf) const {
  245. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  246. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  247. std::get<2>(u) = dpf[2].unit_as(std::get<2>(leaf));
  248. }
  249. // Get the bit-shared unit vector entry from the leaf node
  250. inline void unit(std::tuple<RegXS,RegXS,RegXS> &u, const LeafNode &leaf) const {
  251. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  252. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  253. std::get<2>(u) = dpf[2].unit_bs(std::get<2>(leaf));
  254. }
  255. // For any more complex entry type, that type will handle the conversion
  256. // for each DPF
  257. template <typename T>
  258. inline void unit(std::tuple<T,T,T> &u, const LeafNode &leaf) const {
  259. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  260. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  261. std::get<2>(u).unit(dpf[2], std::get<2>(leaf));
  262. }
  263. // Get the additive-shared scaled vector entry from the leaf node
  264. inline void scaled(RegASWT &s, const LeafNode &leaf) const {
  265. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  266. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  267. std::get<2>(s) = dpf[2].scaled_as(std::get<2>(leaf));
  268. }
  269. // Get the XOR-shared scaled vector entry from the leaf node
  270. inline void scaled(RegXSWT &s, const LeafNode &leaf) const {
  271. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  272. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  273. std::get<2>(s) = dpf[2].scaled_xs(std::get<2>(leaf));
  274. }
  275. };
  276. template <nbits_t WIDTH>
  277. struct RDPFPair {
  278. template <typename T>
  279. using Pair = std::tuple<T, T>;
  280. template <typename T>
  281. using WPair = Pair<typename RDPF<WIDTH>::W<T>>;
  282. // The type of pairs of nodes, LeafNodes, and the wide shared
  283. // register types
  284. using node = Pair<DPFnode>;
  285. using LeafNode = Pair<typename RDPF<WIDTH>::LeafNode>;
  286. using RegASWP = WPair<RegAS>;
  287. using RegXSWP = WPair<RegXS>;
  288. RDPF<WIDTH> dpf[2];
  289. RDPFPair() {}
  290. // Create an RDPFPair from an RDPFTriple, keeping two of the RDPFs
  291. // and dropping one. This _moves_ the dpfs from the triple to the
  292. // pair, so the triple will no longer be valid after using this.
  293. // which0 and which1 indicate which of the dpfs to keep.
  294. RDPFPair(RDPFTriple<WIDTH> &&trip, int which0, int which1) {
  295. dpf[0] = std::move(trip.dpf[which0]);
  296. dpf[1] = std::move(trip.dpf[which1]);
  297. }
  298. // The depth
  299. inline nbits_t depth() const { return dpf[0].depth(); }
  300. // The seed
  301. inline node get_seed() const {
  302. return std::make_tuple(dpf[0].get_seed(), dpf[1].get_seed());
  303. }
  304. // Do we have a precomputed expansion?
  305. inline bool has_expansion() const {
  306. return dpf[0].expansion.size() > 0;
  307. }
  308. // Get an element of the expansion
  309. inline LeafNode get_expansion(address_t index) const {
  310. return std::make_tuple(dpf[0].get_expansion(index),
  311. dpf[1].get_expansion(index));
  312. }
  313. // Descend the two RDPFs in lock step
  314. node descend(const node &parent, nbits_t parentdepth,
  315. bit_t whichchild, size_t &aes_ops) const;
  316. // Descend the two RDPFs in lock step to a leaf node
  317. LeafNode descend_to_leaf(const node &parent, nbits_t parentdepth,
  318. bit_t whichchild, size_t &aes_ops) const;
  319. // Overloaded versions of functions to get DPF components and
  320. // outputs so that the appropriate one can be selected with a
  321. // parameter
  322. // Additive share of the scaling value M_as such that the high words
  323. // of the leaf values for P0 and P1 add to M_as * e_{target}
  324. inline void scaled_value(RegASWP &v) const {
  325. std::get<0>(v) = dpf[0].scaled_sum;
  326. std::get<1>(v) = dpf[1].scaled_sum;
  327. }
  328. // XOR share of the scaling value M_xs such that the high words
  329. // of the leaf values for P0 and P1 XOR to M_xs * e_{target}
  330. inline void scaled_value(RegXSWP &v) const {
  331. std::get<0>(v) = dpf[0].scaled_xor;
  332. std::get<1>(v) = dpf[1].scaled_xor;
  333. }
  334. // Get the additive-shared unit vector entry from the leaf node
  335. inline void unit(std::tuple<RegAS,RegAS> &u, const LeafNode &leaf) const {
  336. std::get<0>(u) = dpf[0].unit_as(std::get<0>(leaf));
  337. std::get<1>(u) = dpf[1].unit_as(std::get<1>(leaf));
  338. }
  339. // Get the bit-shared unit vector entry from the leaf node
  340. inline void unit(std::tuple<RegXS,RegXS> &u, const LeafNode &leaf) const {
  341. std::get<0>(u) = dpf[0].unit_bs(std::get<0>(leaf));
  342. std::get<1>(u) = dpf[1].unit_bs(std::get<1>(leaf));
  343. }
  344. // For any more complex entry type, that type will handle the conversion
  345. // for each DPF
  346. template <typename T>
  347. inline void unit(std::tuple<T,T> &u, const LeafNode &leaf) const {
  348. std::get<0>(u).unit(dpf[0], std::get<0>(leaf));
  349. std::get<1>(u).unit(dpf[1], std::get<1>(leaf));
  350. }
  351. // Get the additive-shared scaled vector entry from the leaf node
  352. inline void scaled(RegASWP &s, const LeafNode &leaf) const {
  353. std::get<0>(s) = dpf[0].scaled_as(std::get<0>(leaf));
  354. std::get<1>(s) = dpf[1].scaled_as(std::get<1>(leaf));
  355. }
  356. // Get the XOR-shared scaled vector entry from the leaf node
  357. inline void scaled(RegXSWP &s, const LeafNode &leaf) const {
  358. std::get<0>(s) = dpf[0].scaled_xs(std::get<0>(leaf));
  359. std::get<1>(s) = dpf[1].scaled_xs(std::get<1>(leaf));
  360. }
  361. };
  362. // Streaming evaluation, to avoid taking up enough memory to store
  363. // an entire evaluation. T can be RDPF, RDPFPair, or RDPFTriple.
  364. template <typename T>
  365. class StreamEval {
  366. const T &rdpf;
  367. size_t &aes_ops;
  368. bool use_expansion;
  369. nbits_t depth;
  370. address_t counter_xor_offset;
  371. address_t indexmask;
  372. address_t pathindex;
  373. address_t nextindex;
  374. std::vector<typename T::node> path;
  375. public:
  376. // Create a StreamEval object that will start its output at index
  377. // start. It will wrap around to 0 when it hits 2^depth. If
  378. // use_expansion is true, then if the DPF has been expanded, just
  379. // output values from that. If use_expansion=false or if the DPF
  380. // has not been expanded, compute the values on the fly. If
  381. // xor_offset is non-zero, then the outputs are actually
  382. // DPF(start XOR xor_offset)
  383. // DPF((start+1) XOR xor_offset)
  384. // DPF((start+2) XOR xor_offset)
  385. // etc.
  386. StreamEval(const T &rdpf, address_t start,
  387. address_t xor_offset, size_t &aes_ops,
  388. bool use_expansion = true);
  389. // Get the next value (or tuple of values) from the evaluator
  390. typename T::LeafNode next();
  391. };
  392. // Parallel evaluation. This class launches a number of threads each
  393. // running a StreamEval to evaluate a chunk of the RDPF (or RDPFPair or
  394. // RDPFTriple), and accumulates the results within each chunk, and then
  395. // accumulates all the chunks together. T can be RDPF, RDPFPair, or
  396. // RDPFTriple.
  397. template <typename T>
  398. struct ParallelEval {
  399. const T &rdpf;
  400. address_t start;
  401. address_t xor_offset;
  402. address_t num_evals;
  403. int num_threads;
  404. size_t &aes_ops;
  405. // Create a Parallel evaluator that will evaluate the given rdpf at
  406. // DPF(start XOR xor_offset)
  407. // DPF((start+1) XOR xor_offset)
  408. // DPF((start+2) XOR xor_offset)
  409. // ...
  410. // DPF((start+num_evals-1) XOR xor_offset)
  411. // where all indices are taken mod 2^depth, and accumulate the
  412. // results into a single answer.
  413. ParallelEval(const T &rdpf, address_t start,
  414. address_t xor_offset, address_t num_evals,
  415. int num_threads, size_t &aes_ops) :
  416. rdpf(rdpf), start(start), xor_offset(xor_offset),
  417. num_evals(num_evals), num_threads(num_threads),
  418. aes_ops(aes_ops) {}
  419. // Run the parallel evaluator. The type V is the type of the
  420. // accumulator; init should be the "zero" value of the accumulator.
  421. // The type W (process) is a lambda type with the signature
  422. // (int, address_t, const T::node &) -> V
  423. // which will be called like this for each i from 0 to num_evals-1,
  424. // across num_thread threads:
  425. // value_i = process(t, i, DPF((start+i) XOR xor_offset))
  426. // t is the thread number (0 <= t < num_threads).
  427. // The resulting num_evals values will be combined using V's +=
  428. // operator, first accumulating the values within each thread
  429. // (starting with the init value), and then accumulating the totals
  430. // from each thread together (again starting with the init value):
  431. //
  432. // total = init
  433. // for each thread t:
  434. // accum_t = init
  435. // for each accum_i generated by thread t:
  436. // accum_t += value_i
  437. // total += accum_t
  438. template <typename V, typename W>
  439. inline V reduce(V init, W process);
  440. };
  441. #include "rdpf.tcc"
  442. #endif