rdpf.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. #include <bsd/stdlib.h> // arc4random_buf
  2. #include "rdpf.hpp"
  3. #include "bitutils.hpp"
  4. #include "mpcops.hpp"
  5. // Compute the multiplicative inverse of x mod 2^{VALUE_BITS}
  6. // This is the same as computing x to the power of
  7. // 2^{VALUE_BITS-1}-1.
  8. static value_t inverse_value_t(value_t x)
  9. {
  10. int expon = 1;
  11. value_t xe = x;
  12. // Invariant: xe = x^(2^expon - 1) mod 2^{VALUE_BITS}
  13. // Goal: compute x^(2^{VALUE_BITS-1} - 1)
  14. while (expon < VALUE_BITS-1) {
  15. xe = xe * xe * x;
  16. ++expon;
  17. }
  18. return xe;
  19. }
  20. #undef RDPF_MTGEN_TIMING_1
  21. #ifdef RDPF_MTGEN_TIMING_1
  22. // Timing tests for multithreaded generation of RDPFs
  23. // nthreads = 0 to not launch threads at all
  24. // run for num_iters iterations, output the number of millisections
  25. // total for all of the iterations
  26. //
  27. // Results: roughly 50 µs to launch the thread pool with 1 thread, and
  28. // roughly 30 additional µs for each additional thread. Each iteration
  29. // of the inner loop takes about 4 to 5 ns. This works out to around
  30. // level 19 where it starts being worth it to multithread, and you
  31. // should use at most sqrt(2^{level}/6000) threads.
  32. static void mtgen_timetest_1(nbits_t level, int nthreads,
  33. size_t num_iters, const DPFnode *curlevel,
  34. DPFnode *nextlevel, size_t &aes_ops)
  35. {
  36. if (num_iters == 0) {
  37. num_iters = 1;
  38. }
  39. size_t prev_aes_ops = aes_ops;
  40. DPFnode L = _mm_setzero_si128();
  41. DPFnode R = _mm_setzero_si128();
  42. // The tweak causes us to compute something slightly different every
  43. // iteration of the loop, so that the compiler doesn't notice we're
  44. // doing the same thing num_iters times and optimize it away
  45. DPFnode tweak = _mm_setzero_si128();
  46. auto start = boost::chrono::steady_clock::now();
  47. for(size_t iter=0;iter<num_iters;++iter) {
  48. tweak += 1; // This actually adds the 128-bit value whose high
  49. // and low 64-bits words are both 1, but that's
  50. // fine.
  51. size_t curlevel_size = size_t(1)<<level;
  52. if (nthreads == 0) {
  53. size_t laes_ops = 0;
  54. for(size_t i=0;i<curlevel_size;++i) {
  55. DPFnode lchild, rchild;
  56. prgboth(lchild, rchild, curlevel[i]^tweak, laes_ops);
  57. L = (L ^ lchild);
  58. R = (R ^ rchild);
  59. nextlevel[2*i] = lchild;
  60. nextlevel[2*i+1] = rchild;
  61. }
  62. aes_ops += laes_ops;
  63. } else {
  64. DPFnode tL[nthreads];
  65. DPFnode tR[nthreads];
  66. size_t taes_ops[nthreads];
  67. size_t threadstart = 0;
  68. size_t threadchunk = curlevel_size / nthreads;
  69. size_t threadextra = curlevel_size % nthreads;
  70. boost::asio::thread_pool pool(nthreads);
  71. for (int t=0;t<nthreads;++t) {
  72. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  73. size_t threadend = threadstart + threadsize;
  74. boost::asio::post(pool,
  75. [t, &tL, &tR, &taes_ops, threadstart, threadend,
  76. &curlevel, &nextlevel, tweak] {
  77. DPFnode L = _mm_setzero_si128();
  78. DPFnode R = _mm_setzero_si128();
  79. size_t aes_ops = 0;
  80. for(size_t i=threadstart;i<threadend;++i) {
  81. DPFnode lchild, rchild;
  82. prgboth(lchild, rchild, curlevel[i]^tweak, aes_ops);
  83. L = (L ^ lchild);
  84. R = (R ^ rchild);
  85. nextlevel[2*i] = lchild;
  86. nextlevel[2*i+1] = rchild;
  87. }
  88. tL[t] = L;
  89. tR[t] = R;
  90. taes_ops[t] = aes_ops;
  91. });
  92. threadstart = threadend;
  93. }
  94. pool.join();
  95. for (int t=0;t<nthreads;++t) {
  96. L ^= tL[t];
  97. R ^= tR[t];
  98. aes_ops += taes_ops[t];
  99. }
  100. }
  101. }
  102. auto elapsed =
  103. boost::chrono::steady_clock::now() - start;
  104. std::cout << "timetest_1 " << int(level) << " " << nthreads << " "
  105. << num_iters << " " << boost::chrono::duration_cast
  106. <boost::chrono::milliseconds>(elapsed) << " " <<
  107. (aes_ops-prev_aes_ops) << " AES\n";
  108. dump_node(L);
  109. dump_node(R);
  110. }
  111. #endif
  112. // Construct a DPF with the given (XOR-shared) target location, and
  113. // of the given depth, to be used for random-access memory reads and
  114. // writes. The DPF is construction collaboratively by P0 and P1,
  115. // with the server P2 helping by providing various kinds of
  116. // correlated randomness, such as MultTriples and AndTriples.
  117. //
  118. // This algorithm is based on Appendix C from the Duoram paper, with a
  119. // small optimization noted below.
  120. RDPF::RDPF(MPCTIO &tio, yield_t &yield,
  121. RegXS target, nbits_t depth, bool save_expansion)
  122. {
  123. int player = tio.player();
  124. size_t &aes_ops = tio.aes_ops();
  125. // Choose a random seed
  126. arc4random_buf(&seed, sizeof(seed));
  127. // Ensure the flag bits (the lsb of each node) are different
  128. seed = set_lsb(seed, !!player);
  129. cfbits = 0;
  130. whichhalf = (player == 1);
  131. // The root level is just the seed
  132. nbits_t level = 0;
  133. DPFnode *curlevel = NULL;
  134. DPFnode *nextlevel = new DPFnode[1];
  135. nextlevel[0] = seed;
  136. // Construct each intermediate level
  137. while(level < depth) {
  138. delete[] curlevel;
  139. curlevel = nextlevel;
  140. if (save_expansion && level == depth-1) {
  141. expansion.resize(1<<depth);
  142. nextlevel = expansion.data();
  143. } else {
  144. nextlevel = new DPFnode[1<<(level+1)];
  145. }
  146. // Invariant: curlevel has 2^level elements; nextlevel has
  147. // 2^{level+1} elements
  148. // The bit-shared choice bit is bit (depth-level-1) of the
  149. // XOR-shared target index
  150. RegBS bs_choice = target.bit(depth-level-1);
  151. size_t curlevel_size = (size_t(1)<<level);
  152. DPFnode L = _mm_setzero_si128();
  153. DPFnode R = _mm_setzero_si128();
  154. // The server doesn't need to do this computation, but it does
  155. // need to execute mpc_reconstruct_choice so that it sends
  156. // the AndTriples at the appropriate time.
  157. if (player < 2) {
  158. #ifdef RDPF_MTGEN_TIMING_1
  159. if (player == 0) {
  160. mtgen_timetest_1(level, 0, (1<<23)>>level, curlevel,
  161. nextlevel, aes_ops);
  162. size_t niters = 2048;
  163. if (level > 8) niters = (1<<20)>>level;
  164. for(int t=1;t<=8;++t) {
  165. mtgen_timetest_1(level, t, niters, curlevel,
  166. nextlevel, aes_ops);
  167. }
  168. mtgen_timetest_1(level, 0, (1<<23)>>level, curlevel,
  169. nextlevel, aes_ops);
  170. }
  171. #endif
  172. // Using the timing results gathered above, decide whether
  173. // to multithread, and if so, how many threads to use.
  174. // tio.cpu_nthreads() is the maximum number we have
  175. // available.
  176. int max_nthreads = tio.cpu_nthreads();
  177. if (max_nthreads == 1 || level < 19) {
  178. // No threading
  179. size_t laes_ops = 0;
  180. for(size_t i=0;i<curlevel_size;++i) {
  181. DPFnode lchild, rchild;
  182. prgboth(lchild, rchild, curlevel[i], laes_ops);
  183. L = (L ^ lchild);
  184. R = (R ^ rchild);
  185. nextlevel[2*i] = lchild;
  186. nextlevel[2*i+1] = rchild;
  187. }
  188. aes_ops += laes_ops;
  189. } else {
  190. size_t curlevel_size = size_t(1)<<level;
  191. int nthreads =
  192. int(ceil(sqrt(double(curlevel_size/6000))));
  193. if (nthreads > max_nthreads) {
  194. nthreads = max_nthreads;
  195. }
  196. DPFnode tL[nthreads];
  197. DPFnode tR[nthreads];
  198. size_t taes_ops[nthreads];
  199. size_t threadstart = 0;
  200. size_t threadchunk = curlevel_size / nthreads;
  201. size_t threadextra = curlevel_size % nthreads;
  202. boost::asio::thread_pool pool(nthreads);
  203. for (int t=0;t<nthreads;++t) {
  204. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  205. size_t threadend = threadstart + threadsize;
  206. boost::asio::post(pool,
  207. [t, &tL, &tR, &taes_ops, threadstart, threadend,
  208. &curlevel, &nextlevel] {
  209. DPFnode L = _mm_setzero_si128();
  210. DPFnode R = _mm_setzero_si128();
  211. size_t aes_ops = 0;
  212. for(size_t i=threadstart;i<threadend;++i) {
  213. DPFnode lchild, rchild;
  214. prgboth(lchild, rchild, curlevel[i], aes_ops);
  215. L = (L ^ lchild);
  216. R = (R ^ rchild);
  217. nextlevel[2*i] = lchild;
  218. nextlevel[2*i+1] = rchild;
  219. }
  220. tL[t] = L;
  221. tR[t] = R;
  222. taes_ops[t] = aes_ops;
  223. });
  224. threadstart = threadend;
  225. }
  226. pool.join();
  227. for (int t=0;t<nthreads;++t) {
  228. L ^= tL[t];
  229. R ^= tR[t];
  230. aes_ops += taes_ops[t];
  231. }
  232. }
  233. }
  234. // If we're going left (bs_choice = 0), we want the correction
  235. // word to be the XOR of our right side and our peer's right
  236. // side; if bs_choice = 1, it should be the XOR or our left side
  237. // and our peer's left side.
  238. // We also have to ensure that the flag bits (the lsb) of the
  239. // side that will end up the same be of course the same, but
  240. // also that the flag bits (the lsb) of the side that will end
  241. // up different _must_ be different. That is, it's not enough
  242. // for the nodes of the child selected by choice to be different
  243. // as 128-bit values; they also have to be different in their
  244. // lsb.
  245. // This is where we make a small optimization over Appendix C of
  246. // the Duoram paper: instead of keeping separate correction flag
  247. // bits for the left and right children, we observe that the low
  248. // bit of the overall correction word effectively serves as one
  249. // of those bits, so we just need to store one extra bit per
  250. // level, not two. (We arbitrarily choose the one for the right
  251. // child.)
  252. // Note that the XOR of our left and right child before and
  253. // after applying the correction word won't change, since the
  254. // correction word is applied to either both children or
  255. // neither, depending on the value of the parent's flag. So in
  256. // particular, the XOR of the flag bits won't change, and if our
  257. // children's flag's XOR equals our peer's children's flag's
  258. // XOR, then we won't have different flag bits even for the
  259. // children that have different 128-bit values.
  260. // So we compute our_parity = lsb(L^R)^player, and we XOR that
  261. // into the R value in the correction word computation. At the
  262. // same time, we exchange these parity values to compute the
  263. // combined parity, which we store in the DPF. Then when the
  264. // DPF is evaluated, if the parent's flag is set, not only apply
  265. // the correction work to both children, but also apply the
  266. // (combined) parity bit to just the right child. Then for
  267. // unequal nodes (where the flag bit is different), exactly one
  268. // of the four children (two for P0 and two for P1) will have
  269. // the parity bit applied, which will set the XOR of the lsb of
  270. // those four nodes to just L0^R0^L1^R1^our_parity^peer_parity
  271. // = 1 because everything cancels out except player (for which
  272. // one player is 0 and the other is 1).
  273. bool our_parity_bit = get_lsb(L ^ R) ^ !!player;
  274. DPFnode our_parity = lsb128_mask[our_parity_bit];
  275. DPFnode CW;
  276. bool peer_parity_bit;
  277. // Exchange the parities and do mpc_reconstruct_choice at the
  278. // same time (bundled into the same rounds)
  279. run_coroutines(yield,
  280. [this, &tio, &our_parity_bit, &peer_parity_bit](yield_t &yield) {
  281. tio.queue_peer(&our_parity_bit, 1);
  282. yield();
  283. uint8_t peer_parity_byte;
  284. tio.recv_peer(&peer_parity_byte, 1);
  285. peer_parity_bit = peer_parity_byte & 1;
  286. },
  287. [this, &tio, &CW, &L, &R, &bs_choice, &our_parity](yield_t &yield) {
  288. mpc_reconstruct_choice(tio, yield, CW, bs_choice,
  289. (R ^ our_parity), L);
  290. });
  291. bool parity_bit = our_parity_bit ^ peer_parity_bit;
  292. cfbits |= (value_t(parity_bit)<<level);
  293. DPFnode CWR = CW ^ lsb128_mask[parity_bit];
  294. if (player < 2) {
  295. // The timing of each iteration of the inner loop is
  296. // comparable to the above, so just use the same
  297. // computations. All of this could be tuned, of course.
  298. if (level < depth-1) {
  299. // Using the timing results gathered above, decide whether
  300. // to multithread, and if so, how many threads to use.
  301. // tio.cpu_nthreads() is the maximum number we have
  302. // available.
  303. int max_nthreads = tio.cpu_nthreads();
  304. if (max_nthreads == 1 || level < 19) {
  305. // No threading
  306. for(size_t i=0;i<curlevel_size;++i) {
  307. bool flag = get_lsb(curlevel[i]);
  308. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  309. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  310. }
  311. } else {
  312. int nthreads =
  313. int(ceil(sqrt(double(curlevel_size/6000))));
  314. if (nthreads > max_nthreads) {
  315. nthreads = max_nthreads;
  316. }
  317. size_t threadstart = 0;
  318. size_t threadchunk = curlevel_size / nthreads;
  319. size_t threadextra = curlevel_size % nthreads;
  320. boost::asio::thread_pool pool(nthreads);
  321. for (int t=0;t<nthreads;++t) {
  322. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  323. size_t threadend = threadstart + threadsize;
  324. boost::asio::post(pool, [CW, CWR, threadstart, threadend,
  325. &curlevel, &nextlevel] {
  326. for(size_t i=threadstart;i<threadend;++i) {
  327. bool flag = get_lsb(curlevel[i]);
  328. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  329. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  330. }
  331. });
  332. threadstart = threadend;
  333. }
  334. pool.join();
  335. }
  336. } else {
  337. // Recall there are four potentially useful vectors that
  338. // can come out of a DPF:
  339. // - (single-bit) bitwise unit vector
  340. // - additive-shared unit vector
  341. // - XOR-shared scaled unit vector
  342. // - additive-shared scaled unit vector
  343. //
  344. // (No single DPF should be used for both of the first
  345. // two or both of the last two, though, since they're
  346. // correlated; you _can_ use one of the first two and
  347. // one of the last two.)
  348. //
  349. // For each 128-bit leaf, the low bit is the flag bit,
  350. // and we're guaranteed that the flag bits (and indeed
  351. // the whole 128-bit value) for P0 and P1 are the same
  352. // for every leaf except the target, and that the flag
  353. // bits definitely differ for the target (and the other
  354. // 127 bits are independently random on each side).
  355. //
  356. // We divide the 128-bit leaf into a low 64-bit word and
  357. // a high 64-bit word. We use the low word for the unit
  358. // vector and the high word for the scaled vector; this
  359. // choice is not arbitrary: the flag bit in the low word
  360. // means that the sum of all the low words (with P1's
  361. // low words negated) across both P0 and P1 is
  362. // definitely odd, so we can compute that sum's inverse
  363. // mod 2^64, and store it now during precomputation. At
  364. // evaluation time for the additive-shared unit vector,
  365. // we will output this global inverse times the low word
  366. // of each leaf, which will make the sum of all of those
  367. // values 1. (This technique replaces the protocol in
  368. // Appendix D of the Duoram paper.)
  369. //
  370. // For the scaled vector, we just have to compute shares
  371. // of what the scaled vector is a sharing _of_, but
  372. // that's just XORing or adding all of each party's
  373. // local high words; no communication needed.
  374. value_t low_sum = 0;
  375. value_t high_sum = 0;
  376. value_t high_xor = 0;
  377. // Using the timing results gathered above, decide whether
  378. // to multithread, and if so, how many threads to use.
  379. // tio.cpu_nthreads() is the maximum number we have
  380. // available.
  381. int max_nthreads = tio.cpu_nthreads();
  382. if (max_nthreads == 1 || level < 19) {
  383. // No threading
  384. for(size_t i=0;i<curlevel_size;++i) {
  385. bool flag = get_lsb(curlevel[i]);
  386. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  387. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  388. if (save_expansion) {
  389. nextlevel[2*i] = leftchild;
  390. nextlevel[2*i+1] = rightchild;
  391. }
  392. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  393. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  394. value_t lefthigh =
  395. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  396. value_t righthigh =
  397. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  398. low_sum += (leftlow + rightlow);
  399. high_sum += (lefthigh + righthigh);
  400. high_xor ^= (lefthigh ^ righthigh);
  401. }
  402. } else {
  403. int nthreads =
  404. int(ceil(sqrt(double(curlevel_size/6000))));
  405. if (nthreads > max_nthreads) {
  406. nthreads = max_nthreads;
  407. }
  408. value_t tlow_sum[nthreads];
  409. value_t thigh_sum[nthreads];
  410. value_t thigh_xor[nthreads];
  411. size_t threadstart = 0;
  412. size_t threadchunk = curlevel_size / nthreads;
  413. size_t threadextra = curlevel_size % nthreads;
  414. boost::asio::thread_pool pool(nthreads);
  415. for (int t=0;t<nthreads;++t) {
  416. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  417. size_t threadend = threadstart + threadsize;
  418. boost::asio::post(pool,
  419. [t, &tlow_sum, &thigh_sum, &thigh_xor, threadstart, threadend,
  420. &curlevel, &nextlevel, CW, CWR, save_expansion] {
  421. value_t low_sum = 0;
  422. value_t high_sum = 0;
  423. value_t high_xor = 0;
  424. for(size_t i=threadstart;i<threadend;++i) {
  425. bool flag = get_lsb(curlevel[i]);
  426. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  427. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  428. if (save_expansion) {
  429. nextlevel[2*i] = leftchild;
  430. nextlevel[2*i+1] = rightchild;
  431. }
  432. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  433. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  434. value_t lefthigh =
  435. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  436. value_t righthigh =
  437. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  438. low_sum += (leftlow + rightlow);
  439. high_sum += (lefthigh + righthigh);
  440. high_xor ^= (lefthigh ^ righthigh);
  441. }
  442. tlow_sum[t] = low_sum;
  443. thigh_sum[t] = high_sum;
  444. thigh_xor[t] = high_xor;
  445. });
  446. threadstart = threadend;
  447. }
  448. pool.join();
  449. for (int t=0;t<nthreads;++t) {
  450. low_sum += tlow_sum[t];
  451. high_sum += thigh_sum[t];
  452. high_xor ^= thigh_xor[t];
  453. }
  454. }
  455. if (player == 1) {
  456. low_sum = -low_sum;
  457. high_sum = -high_sum;
  458. }
  459. scaled_sum.ashare = high_sum;
  460. scaled_xor.xshare = high_xor;
  461. // Exchange low_sum and add them up
  462. tio.queue_peer(&low_sum, sizeof(low_sum));
  463. yield();
  464. value_t peer_low_sum;
  465. tio.recv_peer(&peer_low_sum, sizeof(peer_low_sum));
  466. low_sum += peer_low_sum;
  467. // The low_sum had better be odd
  468. assert(low_sum & 1);
  469. unit_sum_inverse = inverse_value_t(low_sum);
  470. }
  471. cw.push_back(CW);
  472. } else if (level == depth-1) {
  473. yield();
  474. }
  475. ++level;
  476. }
  477. delete[] curlevel;
  478. if (!save_expansion) {
  479. delete[] nextlevel;
  480. }
  481. }
  482. // Get the leaf node for the given input
  483. DPFnode RDPF::leaf(address_t input, size_t &aes_ops) const
  484. {
  485. // If we have a precomputed expansion, just use it
  486. if (expansion.size()) {
  487. return expansion[input];
  488. }
  489. nbits_t totdepth = depth();
  490. DPFnode node = seed;
  491. for (nbits_t d=0;d<totdepth;++d) {
  492. bit_t dir = !!(input & (address_t(1)<<(totdepth-d-1)));
  493. node = descend(node, d, dir, aes_ops);
  494. }
  495. return node;
  496. }
  497. // Expand the DPF if it's not already expanded
  498. //
  499. // This routine is slightly more efficient than repeatedly calling
  500. // StreamEval::next(), but it uses a lot more memory.
  501. void RDPF::expand(size_t &aes_ops)
  502. {
  503. nbits_t depth = this->depth();
  504. size_t num_leaves = size_t(1)<<depth;
  505. if (expansion.size() == num_leaves) return;
  506. expansion.resize(num_leaves);
  507. address_t index = 0;
  508. address_t lastindex = 0;
  509. DPFnode *path = new DPFnode[depth];
  510. path[0] = seed;
  511. for (nbits_t i=1;i<depth;++i) {
  512. path[i] = descend(path[i-1], i-1, 0, aes_ops);
  513. }
  514. expansion[index++] = descend(path[depth-1], depth-1, 0, aes_ops);
  515. expansion[index++] = descend(path[depth-1], depth-1, 1, aes_ops);
  516. while(index < num_leaves) {
  517. // Invariant: lastindex and index will both be even, and
  518. // index=lastindex+2
  519. uint64_t index_xor = index ^ lastindex;
  520. nbits_t how_many_1_bits = __builtin_popcount(index_xor);
  521. // If lastindex -> index goes for example from (in binary)
  522. // 010010110 -> 010011000, then index_xor will be
  523. // 000001110 and how_many_1_bits will be 3.
  524. // That indicates that path[depth-3] was a left child, and now
  525. // we need to change it to a right child by descending right
  526. // from path[depth-4], and then filling the path after that with
  527. // left children.
  528. path[depth-how_many_1_bits] =
  529. descend(path[depth-how_many_1_bits-1],
  530. depth-how_many_1_bits-1, 1, aes_ops);
  531. for (nbits_t i = depth-how_many_1_bits; i < depth-1; ++i) {
  532. path[i+1] = descend(path[i], i, 0, aes_ops);
  533. }
  534. lastindex = index;
  535. expansion[index++] = descend(path[depth-1], depth-1, 0, aes_ops);
  536. expansion[index++] = descend(path[depth-1], depth-1, 1, aes_ops);
  537. }
  538. delete[] path;
  539. }
  540. // Construct three RDPFs of the given depth all with the same randomly
  541. // generated target index.
  542. RDPFTriple::RDPFTriple(MPCTIO &tio, yield_t &yield,
  543. nbits_t depth, bool save_expansion)
  544. {
  545. // Pick a random XOR share of the target
  546. xs_target.randomize(depth);
  547. // Now create three RDPFs with that target, and also convert the XOR
  548. // shares of the target to additive shares
  549. std::vector<coro_t> coroutines;
  550. for (int i=0;i<3;++i) {
  551. coroutines.emplace_back(
  552. [this, &tio, depth, i, save_expansion](yield_t &yield) {
  553. dpf[i] = RDPF(tio, yield, xs_target, depth,
  554. save_expansion);
  555. });
  556. }
  557. coroutines.emplace_back(
  558. [this, &tio, depth](yield_t &yield) {
  559. mpc_xs_to_as(tio, yield, as_target, xs_target, depth);
  560. });
  561. run_coroutines(yield, coroutines);
  562. }
  563. RDPFTriple::node RDPFTriple::descend(const RDPFTriple::node &parent,
  564. nbits_t parentdepth, bit_t whichchild,
  565. size_t &aes_ops) const
  566. {
  567. auto [P0, P1, P2] = parent;
  568. DPFnode C0, C1, C2;
  569. C0 = dpf[0].descend(P0, parentdepth, whichchild, aes_ops);
  570. C1 = dpf[1].descend(P1, parentdepth, whichchild, aes_ops);
  571. C2 = dpf[2].descend(P2, parentdepth, whichchild, aes_ops);
  572. return std::make_tuple(C0,C1,C2);
  573. }
  574. RDPFPair::node RDPFPair::descend(const RDPFPair::node &parent,
  575. nbits_t parentdepth, bit_t whichchild,
  576. size_t &aes_ops) const
  577. {
  578. auto [P0, P1] = parent;
  579. DPFnode C0, C1;
  580. C0 = dpf[0].descend(P0, parentdepth, whichchild, aes_ops);
  581. C1 = dpf[1].descend(P1, parentdepth, whichchild, aes_ops);
  582. return std::make_tuple(C0,C1);
  583. }