rdpf.hpp 19 KB

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