rdpf.tcc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. // Templated method implementations for rdpf.hpp
  2. #include "mpcops.hpp"
  3. // Compute the multiplicative inverse of x mod 2^{VALUE_BITS}
  4. // This is the same as computing x to the power of
  5. // 2^{VALUE_BITS-1}-1.
  6. static value_t inverse_value_t(value_t x)
  7. {
  8. int expon = 1;
  9. value_t xe = x;
  10. // Invariant: xe = x^(2^expon - 1) mod 2^{VALUE_BITS}
  11. // Goal: compute x^(2^{VALUE_BITS-1} - 1)
  12. while (expon < VALUE_BITS-1) {
  13. xe = xe * xe * x;
  14. ++expon;
  15. }
  16. return xe;
  17. }
  18. // Create a StreamEval object that will start its output at index start.
  19. // It will wrap around to 0 when it hits 2^depth. If use_expansion
  20. // is true, then if the DPF has been expanded, just output values
  21. // from that. If use_expansion=false or if the DPF has not been
  22. // expanded, compute the values on the fly. If xor_offset is non-zero,
  23. // then the outputs are actually
  24. // DPF(start XOR xor_offset)
  25. // DPF((start+1) XOR xor_offset)
  26. // DPF((start+2) XOR xor_offset)
  27. // etc.
  28. template <typename T>
  29. StreamEval<T>::StreamEval(const T &rdpf, address_t start,
  30. address_t xor_offset, size_t &aes_ops,
  31. bool use_expansion) : rdpf(rdpf), aes_ops(aes_ops),
  32. use_expansion(use_expansion), counter_xor_offset(xor_offset)
  33. {
  34. depth = rdpf.depth();
  35. // Prevent overflow of 1<<depth
  36. if (depth < ADDRESS_MAX_BITS) {
  37. indexmask = (address_t(1)<<depth)-1;
  38. } else {
  39. indexmask = ~0;
  40. }
  41. start &= indexmask;
  42. counter_xor_offset &= indexmask;
  43. // Record that we haven't actually output the leaf for index start
  44. // itself yet
  45. nextindex = start;
  46. if (use_expansion && rdpf.has_expansion()) {
  47. // We just need to keep the counter, not compute anything
  48. return;
  49. }
  50. path.resize(depth);
  51. pathindex = start;
  52. path[0] = rdpf.get_seed();
  53. for (nbits_t i=1;i<depth;++i) {
  54. bool dir = !!(pathindex & (address_t(1)<<(depth-i)));
  55. bool xor_offset_bit =
  56. !!(counter_xor_offset & (address_t(1)<<(depth-i)));
  57. path[i] = rdpf.descend(path[i-1], i-1,
  58. dir ^ xor_offset_bit, aes_ops);
  59. }
  60. }
  61. template <typename T>
  62. typename T::node StreamEval<T>::next()
  63. {
  64. if (use_expansion && rdpf.has_expansion()) {
  65. // Just use the precomputed values
  66. typename T::node leaf =
  67. rdpf.get_expansion(nextindex ^ counter_xor_offset);
  68. nextindex = (nextindex + 1) & indexmask;
  69. return leaf;
  70. }
  71. // Invariant: in the first call to next(), nextindex = pathindex.
  72. // Otherwise, nextindex = pathindex+1.
  73. // Get the XOR of nextindex and pathindex, and strip the low bit.
  74. // If nextindex and pathindex are equal, or pathindex is even
  75. // and nextindex is the consecutive odd number, index_xor will be 0,
  76. // indicating that we don't have to update the path, but just
  77. // compute the appropriate leaf given by the low bit of nextindex.
  78. //
  79. // Otherwise, say for example pathindex is 010010111 and nextindex
  80. // is 010011000. Then their XOR is 000001111, and stripping the low
  81. // bit yields 000001110, so how_many_1_bits will be 3.
  82. // That indicates (typically) that path[depth-3] was a left child,
  83. // and now we need to change it to a right child by descending right
  84. // from path[depth-4], and then filling the path after that with
  85. // left children.
  86. //
  87. // When we wrap around, however, index_xor will be 111111110 (after
  88. // we strip the low bit), and how_many_1_bits will be depth-1, but
  89. // the new top child (of the root seed) we have to compute will be a
  90. // left, not a right, child.
  91. uint64_t index_xor = (nextindex ^ pathindex) & ~1;
  92. nbits_t how_many_1_bits = __builtin_popcount(index_xor);
  93. if (how_many_1_bits > 0) {
  94. // This will almost always be 1, unless we've just wrapped
  95. // around from the right subtree back to the left, in which case
  96. // it will be 0.
  97. bool top_changed_bit =
  98. !!(nextindex & (address_t(1) << how_many_1_bits));
  99. bool xor_offset_bit =
  100. !!(counter_xor_offset & (address_t(1) << how_many_1_bits));
  101. path[depth-how_many_1_bits] =
  102. rdpf.descend(path[depth-how_many_1_bits-1],
  103. depth-how_many_1_bits-1,
  104. top_changed_bit ^ xor_offset_bit, aes_ops);
  105. for (nbits_t i = depth-how_many_1_bits; i < depth-1; ++i) {
  106. bool xor_offset_bit =
  107. !!(counter_xor_offset & (address_t(1) << (depth-i-1)));
  108. path[i+1] = rdpf.descend(path[i], i, xor_offset_bit, aes_ops);
  109. }
  110. }
  111. bool xor_offset_bit = counter_xor_offset & 1;
  112. typename T::node leaf = rdpf.descend(path[depth-1], depth-1,
  113. (nextindex & 1) ^ xor_offset_bit, aes_ops);
  114. pathindex = nextindex;
  115. nextindex = (nextindex + 1) & indexmask;
  116. return leaf;
  117. }
  118. // Run the parallel evaluator. The type V is the type of the
  119. // accumulator; init should be the "zero" value of the accumulator.
  120. // The type W (process) is a lambda type with the signature
  121. // (int, address_t, const T::node &) -> V
  122. // which will be called like this for each i from 0 to num_evals-1,
  123. // across num_thread threads:
  124. // value_i = process(t, i, DPF((start+i) XOR xor_offset))
  125. // t is the thread number (0 <= t < num_threads).
  126. // The resulting num_evals values will be combined using V's +=
  127. // operator, first accumulating the values within each thread
  128. // (starting with the init value), and then accumulating the totals
  129. // from each thread together (again starting with the init value):
  130. //
  131. // total = init
  132. // for each thread t:
  133. // accum_t = init
  134. // for each accum_i generated by thread t:
  135. // accum_t += value_i
  136. // total += accum_t
  137. template <typename T> template <typename V, typename W>
  138. inline V ParallelEval<T>::reduce(V init, W process)
  139. {
  140. size_t thread_aes_ops[num_threads];
  141. V accums[num_threads];
  142. boost::asio::thread_pool pool(num_threads);
  143. address_t threadstart = start;
  144. address_t threadchunk = num_evals / num_threads;
  145. address_t threadextra = num_evals % num_threads;
  146. nbits_t depth = rdpf.depth();
  147. address_t indexmask = (depth < ADDRESS_MAX_BITS ?
  148. ((address_t(1)<<depth)-1) : ~0);
  149. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  150. address_t threadsize = threadchunk + (address_t(thread_num) < threadextra);
  151. boost::asio::post(pool,
  152. [this, &init, &thread_aes_ops, &accums, &process,
  153. thread_num, threadstart, threadsize, indexmask] {
  154. size_t local_aes_ops = 0;
  155. auto ev = StreamEval(rdpf, (start+threadstart)&indexmask,
  156. xor_offset, local_aes_ops);
  157. V accum = init;
  158. for (address_t x=0;x<threadsize;++x) {
  159. typename T::node leaf = ev.next();
  160. accum += process(thread_num,
  161. (threadstart+x)&indexmask, leaf);
  162. }
  163. accums[thread_num] = accum;
  164. thread_aes_ops[thread_num] = local_aes_ops;
  165. });
  166. threadstart = (threadstart + threadsize) & indexmask;
  167. }
  168. pool.join();
  169. V total = init;
  170. for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
  171. total += accums[thread_num];
  172. aes_ops += thread_aes_ops[thread_num];
  173. }
  174. return total;
  175. }
  176. // I/O for RDPFs
  177. template <typename T, nbits_t WIDTH>
  178. T& operator>>(T &is, RDPF<WIDTH> &rdpf)
  179. {
  180. is.read((char *)&rdpf.seed, sizeof(rdpf.seed));
  181. rdpf.whichhalf = get_lsb(rdpf.seed);
  182. uint8_t depth;
  183. // Add 64 to depth to indicate an expanded RDPF
  184. is.read((char *)&depth, sizeof(depth));
  185. bool read_expanded = false;
  186. if (depth > 64) {
  187. read_expanded = true;
  188. depth -= 64;
  189. }
  190. assert(depth <= ADDRESS_MAX_BITS);
  191. rdpf.cw.clear();
  192. for (uint8_t i=0; i<depth; ++i) {
  193. DPFnode cw;
  194. is.read((char *)&cw, sizeof(cw));
  195. rdpf.cw.push_back(cw);
  196. }
  197. if (read_expanded) {
  198. rdpf.expansion.resize(1<<depth);
  199. is.read((char *)rdpf.expansion.data(),
  200. sizeof(rdpf.expansion[0])<<depth);
  201. }
  202. value_t cfbits = 0;
  203. is.read((char *)&cfbits, BITBYTES(depth));
  204. rdpf.cfbits = cfbits;
  205. is.read((char *)&rdpf.unit_sum_inverse, sizeof(rdpf.unit_sum_inverse));
  206. is.read((char *)&rdpf.scaled_sum, sizeof(rdpf.scaled_sum));
  207. is.read((char *)&rdpf.scaled_xor, sizeof(rdpf.scaled_xor));
  208. return is;
  209. }
  210. // Write the DPF to the output stream. If expanded=true, then include
  211. // the expansion _if_ the DPF is itself already expanded. You can use
  212. // this to write DPFs to files.
  213. template <typename T, nbits_t WIDTH>
  214. T& write_maybe_expanded(T &os, const RDPF<WIDTH> &rdpf,
  215. bool expanded = true)
  216. {
  217. os.write((const char *)&rdpf.seed, sizeof(rdpf.seed));
  218. uint8_t depth = rdpf.cw.size();
  219. assert(depth <= ADDRESS_MAX_BITS);
  220. // If we're writing an expansion, add 64 to depth
  221. uint8_t expanded_depth = depth;
  222. bool write_expansion = false;
  223. if (expanded && rdpf.expansion.size() == (size_t(1)<<depth)) {
  224. write_expansion = true;
  225. expanded_depth += 64;
  226. }
  227. os.write((const char *)&expanded_depth, sizeof(expanded_depth));
  228. for (uint8_t i=0; i<depth; ++i) {
  229. os.write((const char *)&rdpf.cw[i], sizeof(rdpf.cw[i]));
  230. }
  231. if (write_expansion) {
  232. os.write((const char *)rdpf.expansion.data(),
  233. sizeof(rdpf.expansion[0])<<depth);
  234. }
  235. os.write((const char *)&rdpf.cfbits, BITBYTES(depth));
  236. os.write((const char *)&rdpf.unit_sum_inverse, sizeof(rdpf.unit_sum_inverse));
  237. os.write((const char *)&rdpf.scaled_sum, sizeof(rdpf.scaled_sum));
  238. os.write((const char *)&rdpf.scaled_xor, sizeof(rdpf.scaled_xor));
  239. return os;
  240. }
  241. // The ordinary << version never writes the expansion, since this is
  242. // what we use to send DPFs over the network.
  243. template <typename T, nbits_t WIDTH>
  244. T& operator<<(T &os, const RDPF<WIDTH> &rdpf)
  245. {
  246. return write_maybe_expanded(os, rdpf, false);
  247. }
  248. // I/O for RDPF Triples
  249. // We never write RDPFTriples over the network, so always write
  250. // the DPF expansions if they're available.
  251. template <typename T, nbits_t WIDTH>
  252. T& operator<<(T &os, const RDPFTriple<WIDTH> &rdpftrip)
  253. {
  254. write_maybe_expanded(os, rdpftrip.dpf[0], true);
  255. write_maybe_expanded(os, rdpftrip.dpf[1], true);
  256. write_maybe_expanded(os, rdpftrip.dpf[2], true);
  257. nbits_t depth = rdpftrip.dpf[0].depth();
  258. os.write((const char *)&rdpftrip.as_target.ashare, BITBYTES(depth));
  259. os.write((const char *)&rdpftrip.xs_target.xshare, BITBYTES(depth));
  260. return os;
  261. }
  262. template <typename T, nbits_t WIDTH>
  263. T& operator>>(T &is, RDPFTriple<WIDTH> &rdpftrip)
  264. {
  265. is >> rdpftrip.dpf[0] >> rdpftrip.dpf[1] >> rdpftrip.dpf[2];
  266. nbits_t depth = rdpftrip.dpf[0].depth();
  267. rdpftrip.as_target.ashare = 0;
  268. is.read((char *)&rdpftrip.as_target.ashare, BITBYTES(depth));
  269. rdpftrip.xs_target.xshare = 0;
  270. is.read((char *)&rdpftrip.xs_target.xshare, BITBYTES(depth));
  271. return is;
  272. }
  273. // I/O for RDPF Pairs
  274. // We never write RDPFPairs over the network, so always write
  275. // the DPF expansions if they're available.
  276. template <typename T, nbits_t WIDTH>
  277. T& operator<<(T &os, const RDPFPair<WIDTH> &rdpfpair)
  278. {
  279. write_maybe_expanded(os, rdpfpair.dpf[0], true);
  280. write_maybe_expanded(os, rdpfpair.dpf[1], true);
  281. return os;
  282. }
  283. template <typename T, nbits_t WIDTH>
  284. T& operator>>(T &is, RDPFPair<WIDTH> &rdpfpair)
  285. {
  286. is >> rdpfpair.dpf[0] >> rdpfpair.dpf[1];
  287. return is;
  288. }
  289. // Construct a DPF with the given (XOR-shared) target location, and
  290. // of the given depth, to be used for random-access memory reads and
  291. // writes. The DPF is construction collaboratively by P0 and P1,
  292. // with the server P2 helping by providing various kinds of
  293. // correlated randomness, such as MultTriples and AndTriples.
  294. //
  295. // This algorithm is based on Appendix C from the Duoram paper, with a
  296. // small optimization noted below.
  297. template <nbits_t WIDTH>
  298. RDPF<WIDTH>::RDPF(MPCTIO &tio, yield_t &yield,
  299. RegXS target, nbits_t depth, bool save_expansion)
  300. {
  301. int player = tio.player();
  302. size_t &aes_ops = tio.aes_ops();
  303. // Choose a random seed
  304. arc4random_buf(&seed, sizeof(seed));
  305. // Ensure the flag bits (the lsb of each node) are different
  306. seed = set_lsb(seed, !!player);
  307. cfbits = 0;
  308. whichhalf = (player == 1);
  309. // The root level is just the seed
  310. nbits_t level = 0;
  311. DPFnode *curlevel = NULL;
  312. DPFnode *nextlevel = new DPFnode[1];
  313. nextlevel[0] = seed;
  314. // Construct each intermediate level
  315. while(level < depth) {
  316. if (player < 2) {
  317. delete[] curlevel;
  318. curlevel = nextlevel;
  319. if (save_expansion && level == depth-1) {
  320. expansion.resize(1<<depth);
  321. nextlevel = expansion.data();
  322. } else {
  323. nextlevel = new DPFnode[1<<(level+1)];
  324. }
  325. }
  326. // Invariant: curlevel has 2^level elements; nextlevel has
  327. // 2^{level+1} elements
  328. // The bit-shared choice bit is bit (depth-level-1) of the
  329. // XOR-shared target index
  330. RegBS bs_choice = target.bit(depth-level-1);
  331. size_t curlevel_size = (size_t(1)<<level);
  332. DPFnode L = _mm_setzero_si128();
  333. DPFnode R = _mm_setzero_si128();
  334. // The server doesn't need to do this computation, but it does
  335. // need to execute mpc_reconstruct_choice so that it sends
  336. // the AndTriples at the appropriate time.
  337. if (player < 2) {
  338. #ifdef RDPF_MTGEN_TIMING_1
  339. if (player == 0) {
  340. mtgen_timetest_1(level, 0, (1<<23)>>level, curlevel,
  341. nextlevel, aes_ops);
  342. size_t niters = 2048;
  343. if (level > 8) niters = (1<<20)>>level;
  344. for(int t=1;t<=8;++t) {
  345. mtgen_timetest_1(level, t, niters, curlevel,
  346. nextlevel, aes_ops);
  347. }
  348. mtgen_timetest_1(level, 0, (1<<23)>>level, curlevel,
  349. nextlevel, aes_ops);
  350. }
  351. #endif
  352. // Using the timing results gathered above, decide whether
  353. // to multithread, and if so, how many threads to use.
  354. // tio.cpu_nthreads() is the maximum number we have
  355. // available.
  356. int max_nthreads = tio.cpu_nthreads();
  357. if (max_nthreads == 1 || level < 19) {
  358. // No threading
  359. size_t laes_ops = 0;
  360. for(size_t i=0;i<curlevel_size;++i) {
  361. DPFnode lchild, rchild;
  362. prgboth(lchild, rchild, curlevel[i], laes_ops);
  363. L = (L ^ lchild);
  364. R = (R ^ rchild);
  365. nextlevel[2*i] = lchild;
  366. nextlevel[2*i+1] = rchild;
  367. }
  368. aes_ops += laes_ops;
  369. } else {
  370. size_t curlevel_size = size_t(1)<<level;
  371. int nthreads =
  372. int(ceil(sqrt(double(curlevel_size/6000))));
  373. if (nthreads > max_nthreads) {
  374. nthreads = max_nthreads;
  375. }
  376. DPFnode tL[nthreads];
  377. DPFnode tR[nthreads];
  378. size_t taes_ops[nthreads];
  379. size_t threadstart = 0;
  380. size_t threadchunk = curlevel_size / nthreads;
  381. size_t threadextra = curlevel_size % nthreads;
  382. boost::asio::thread_pool pool(nthreads);
  383. for (int t=0;t<nthreads;++t) {
  384. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  385. size_t threadend = threadstart + threadsize;
  386. boost::asio::post(pool,
  387. [t, &tL, &tR, &taes_ops, threadstart, threadend,
  388. &curlevel, &nextlevel] {
  389. DPFnode L = _mm_setzero_si128();
  390. DPFnode R = _mm_setzero_si128();
  391. size_t aes_ops = 0;
  392. for(size_t i=threadstart;i<threadend;++i) {
  393. DPFnode lchild, rchild;
  394. prgboth(lchild, rchild, curlevel[i], aes_ops);
  395. L = (L ^ lchild);
  396. R = (R ^ rchild);
  397. nextlevel[2*i] = lchild;
  398. nextlevel[2*i+1] = rchild;
  399. }
  400. tL[t] = L;
  401. tR[t] = R;
  402. taes_ops[t] = aes_ops;
  403. });
  404. threadstart = threadend;
  405. }
  406. pool.join();
  407. for (int t=0;t<nthreads;++t) {
  408. L ^= tL[t];
  409. R ^= tR[t];
  410. aes_ops += taes_ops[t];
  411. }
  412. }
  413. }
  414. // If we're going left (bs_choice = 0), we want the correction
  415. // word to be the XOR of our right side and our peer's right
  416. // side; if bs_choice = 1, it should be the XOR or our left side
  417. // and our peer's left side.
  418. // We also have to ensure that the flag bits (the lsb) of the
  419. // side that will end up the same be of course the same, but
  420. // also that the flag bits (the lsb) of the side that will end
  421. // up different _must_ be different. That is, it's not enough
  422. // for the nodes of the child selected by choice to be different
  423. // as 128-bit values; they also have to be different in their
  424. // lsb.
  425. // This is where we make a small optimization over Appendix C of
  426. // the Duoram paper: instead of keeping separate correction flag
  427. // bits for the left and right children, we observe that the low
  428. // bit of the overall correction word effectively serves as one
  429. // of those bits, so we just need to store one extra bit per
  430. // level, not two. (We arbitrarily choose the one for the right
  431. // child.)
  432. // Note that the XOR of our left and right child before and
  433. // after applying the correction word won't change, since the
  434. // correction word is applied to either both children or
  435. // neither, depending on the value of the parent's flag. So in
  436. // particular, the XOR of the flag bits won't change, and if our
  437. // children's flag's XOR equals our peer's children's flag's
  438. // XOR, then we won't have different flag bits even for the
  439. // children that have different 128-bit values.
  440. // So we compute our_parity = lsb(L^R)^player, and we XOR that
  441. // into the R value in the correction word computation. At the
  442. // same time, we exchange these parity values to compute the
  443. // combined parity, which we store in the DPF. Then when the
  444. // DPF is evaluated, if the parent's flag is set, not only apply
  445. // the correction work to both children, but also apply the
  446. // (combined) parity bit to just the right child. Then for
  447. // unequal nodes (where the flag bit is different), exactly one
  448. // of the four children (two for P0 and two for P1) will have
  449. // the parity bit applied, which will set the XOR of the lsb of
  450. // those four nodes to just L0^R0^L1^R1^our_parity^peer_parity
  451. // = 1 because everything cancels out except player (for which
  452. // one player is 0 and the other is 1).
  453. bool our_parity_bit = get_lsb(L ^ R) ^ !!player;
  454. DPFnode our_parity = lsb128_mask[our_parity_bit];
  455. DPFnode CW;
  456. bool peer_parity_bit;
  457. // Exchange the parities and do mpc_reconstruct_choice at the
  458. // same time (bundled into the same rounds)
  459. run_coroutines(yield,
  460. [this, &tio, &our_parity_bit, &peer_parity_bit](yield_t &yield) {
  461. tio.queue_peer(&our_parity_bit, 1);
  462. yield();
  463. uint8_t peer_parity_byte;
  464. tio.recv_peer(&peer_parity_byte, 1);
  465. peer_parity_bit = peer_parity_byte & 1;
  466. },
  467. [this, &tio, &CW, &L, &R, &bs_choice, &our_parity](yield_t &yield) {
  468. mpc_reconstruct_choice(tio, yield, CW, bs_choice,
  469. (R ^ our_parity), L);
  470. });
  471. bool parity_bit = our_parity_bit ^ peer_parity_bit;
  472. cfbits |= (value_t(parity_bit)<<level);
  473. DPFnode CWR = CW ^ lsb128_mask[parity_bit];
  474. if (player < 2) {
  475. // The timing of each iteration of the inner loop is
  476. // comparable to the above, so just use the same
  477. // computations. All of this could be tuned, of course.
  478. if (level < depth-1) {
  479. // Using the timing results gathered above, decide whether
  480. // to multithread, and if so, how many threads to use.
  481. // tio.cpu_nthreads() is the maximum number we have
  482. // available.
  483. int max_nthreads = tio.cpu_nthreads();
  484. if (max_nthreads == 1 || level < 19) {
  485. // No threading
  486. for(size_t i=0;i<curlevel_size;++i) {
  487. bool flag = get_lsb(curlevel[i]);
  488. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  489. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  490. }
  491. } else {
  492. int nthreads =
  493. int(ceil(sqrt(double(curlevel_size/6000))));
  494. if (nthreads > max_nthreads) {
  495. nthreads = max_nthreads;
  496. }
  497. size_t threadstart = 0;
  498. size_t threadchunk = curlevel_size / nthreads;
  499. size_t threadextra = curlevel_size % nthreads;
  500. boost::asio::thread_pool pool(nthreads);
  501. for (int t=0;t<nthreads;++t) {
  502. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  503. size_t threadend = threadstart + threadsize;
  504. boost::asio::post(pool, [CW, CWR, threadstart, threadend,
  505. &curlevel, &nextlevel] {
  506. for(size_t i=threadstart;i<threadend;++i) {
  507. bool flag = get_lsb(curlevel[i]);
  508. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  509. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  510. }
  511. });
  512. threadstart = threadend;
  513. }
  514. pool.join();
  515. }
  516. } else {
  517. // Recall there are four potentially useful vectors that
  518. // can come out of a DPF:
  519. // - (single-bit) bitwise unit vector
  520. // - additive-shared unit vector
  521. // - XOR-shared scaled unit vector
  522. // - additive-shared scaled unit vector
  523. //
  524. // (No single DPF should be used for both of the first
  525. // two or both of the last two, though, since they're
  526. // correlated; you _can_ use one of the first two and
  527. // one of the last two.)
  528. //
  529. // For each 128-bit leaf, the low bit is the flag bit,
  530. // and we're guaranteed that the flag bits (and indeed
  531. // the whole 128-bit value) for P0 and P1 are the same
  532. // for every leaf except the target, and that the flag
  533. // bits definitely differ for the target (and the other
  534. // 127 bits are independently random on each side).
  535. //
  536. // We divide the 128-bit leaf into a low 64-bit word and
  537. // a high 64-bit word. We use the low word for the unit
  538. // vector and the high word for the scaled vector; this
  539. // choice is not arbitrary: the flag bit in the low word
  540. // means that the sum of all the low words (with P1's
  541. // low words negated) across both P0 and P1 is
  542. // definitely odd, so we can compute that sum's inverse
  543. // mod 2^64, and store it now during precomputation. At
  544. // evaluation time for the additive-shared unit vector,
  545. // we will output this global inverse times the low word
  546. // of each leaf, which will make the sum of all of those
  547. // values 1. (This technique replaces the protocol in
  548. // Appendix D of the Duoram paper.)
  549. //
  550. // For the scaled vector, we just have to compute shares
  551. // of what the scaled vector is a sharing _of_, but
  552. // that's just XORing or adding all of each party's
  553. // local high words; no communication needed.
  554. value_t low_sum = 0;
  555. value_t high_sum = 0;
  556. value_t high_xor = 0;
  557. // Using the timing results gathered above, decide whether
  558. // to multithread, and if so, how many threads to use.
  559. // tio.cpu_nthreads() is the maximum number we have
  560. // available.
  561. int max_nthreads = tio.cpu_nthreads();
  562. if (max_nthreads == 1 || level < 19) {
  563. // No threading
  564. for(size_t i=0;i<curlevel_size;++i) {
  565. bool flag = get_lsb(curlevel[i]);
  566. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  567. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  568. if (save_expansion) {
  569. nextlevel[2*i] = leftchild;
  570. nextlevel[2*i+1] = rightchild;
  571. }
  572. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  573. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  574. value_t lefthigh =
  575. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  576. value_t righthigh =
  577. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  578. low_sum += (leftlow + rightlow);
  579. high_sum += (lefthigh + righthigh);
  580. high_xor ^= (lefthigh ^ righthigh);
  581. }
  582. } else {
  583. int nthreads =
  584. int(ceil(sqrt(double(curlevel_size/6000))));
  585. if (nthreads > max_nthreads) {
  586. nthreads = max_nthreads;
  587. }
  588. value_t tlow_sum[nthreads];
  589. value_t thigh_sum[nthreads];
  590. value_t thigh_xor[nthreads];
  591. size_t threadstart = 0;
  592. size_t threadchunk = curlevel_size / nthreads;
  593. size_t threadextra = curlevel_size % nthreads;
  594. boost::asio::thread_pool pool(nthreads);
  595. for (int t=0;t<nthreads;++t) {
  596. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  597. size_t threadend = threadstart + threadsize;
  598. boost::asio::post(pool,
  599. [t, &tlow_sum, &thigh_sum, &thigh_xor, threadstart, threadend,
  600. &curlevel, &nextlevel, CW, CWR, save_expansion] {
  601. value_t low_sum = 0;
  602. value_t high_sum = 0;
  603. value_t high_xor = 0;
  604. for(size_t i=threadstart;i<threadend;++i) {
  605. bool flag = get_lsb(curlevel[i]);
  606. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  607. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  608. if (save_expansion) {
  609. nextlevel[2*i] = leftchild;
  610. nextlevel[2*i+1] = rightchild;
  611. }
  612. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  613. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  614. value_t lefthigh =
  615. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  616. value_t righthigh =
  617. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  618. low_sum += (leftlow + rightlow);
  619. high_sum += (lefthigh + righthigh);
  620. high_xor ^= (lefthigh ^ righthigh);
  621. }
  622. tlow_sum[t] = low_sum;
  623. thigh_sum[t] = high_sum;
  624. thigh_xor[t] = high_xor;
  625. });
  626. threadstart = threadend;
  627. }
  628. pool.join();
  629. for (int t=0;t<nthreads;++t) {
  630. low_sum += tlow_sum[t];
  631. high_sum += thigh_sum[t];
  632. high_xor ^= thigh_xor[t];
  633. }
  634. }
  635. if (player == 1) {
  636. low_sum = -low_sum;
  637. high_sum = -high_sum;
  638. }
  639. scaled_sum.ashare = high_sum;
  640. scaled_xor.xshare = high_xor;
  641. // Exchange low_sum and add them up
  642. tio.queue_peer(&low_sum, sizeof(low_sum));
  643. yield();
  644. value_t peer_low_sum;
  645. tio.recv_peer(&peer_low_sum, sizeof(peer_low_sum));
  646. low_sum += peer_low_sum;
  647. // The low_sum had better be odd
  648. assert(low_sum & 1);
  649. unit_sum_inverse = inverse_value_t(low_sum);
  650. }
  651. cw.push_back(CW);
  652. } else if (level == depth-1) {
  653. yield();
  654. }
  655. ++level;
  656. }
  657. delete[] curlevel;
  658. if (!save_expansion || player == 2) {
  659. delete[] nextlevel;
  660. }
  661. }
  662. // Get the leaf node for the given input
  663. template <nbits_t WIDTH>
  664. DPFnode RDPF<WIDTH>::leaf(address_t input, size_t &aes_ops) const
  665. {
  666. // If we have a precomputed expansion, just use it
  667. if (expansion.size()) {
  668. return expansion[input];
  669. }
  670. nbits_t totdepth = depth();
  671. DPFnode node = seed;
  672. for (nbits_t d=0;d<totdepth;++d) {
  673. bit_t dir = !!(input & (address_t(1)<<(totdepth-d-1)));
  674. node = descend(node, d, dir, aes_ops);
  675. }
  676. return node;
  677. }
  678. // Expand the DPF if it's not already expanded
  679. //
  680. // This routine is slightly more efficient than repeatedly calling
  681. // StreamEval::next(), but it uses a lot more memory.
  682. template <nbits_t WIDTH>
  683. void RDPF<WIDTH>::expand(size_t &aes_ops)
  684. {
  685. nbits_t depth = this->depth();
  686. size_t num_leaves = size_t(1)<<depth;
  687. if (expansion.size() == num_leaves) return;
  688. expansion.resize(num_leaves);
  689. address_t index = 0;
  690. address_t lastindex = 0;
  691. DPFnode *path = new DPFnode[depth];
  692. path[0] = seed;
  693. for (nbits_t i=1;i<depth;++i) {
  694. path[i] = descend(path[i-1], i-1, 0, aes_ops);
  695. }
  696. expansion[index++] = descend(path[depth-1], depth-1, 0, aes_ops);
  697. expansion[index++] = descend(path[depth-1], depth-1, 1, aes_ops);
  698. while(index < num_leaves) {
  699. // Invariant: lastindex and index will both be even, and
  700. // index=lastindex+2
  701. uint64_t index_xor = index ^ lastindex;
  702. nbits_t how_many_1_bits = __builtin_popcount(index_xor);
  703. // If lastindex -> index goes for example from (in binary)
  704. // 010010110 -> 010011000, then index_xor will be
  705. // 000001110 and how_many_1_bits will be 3.
  706. // That indicates that path[depth-3] was a left child, and now
  707. // we need to change it to a right child by descending right
  708. // from path[depth-4], and then filling the path after that with
  709. // left children.
  710. path[depth-how_many_1_bits] =
  711. descend(path[depth-how_many_1_bits-1],
  712. depth-how_many_1_bits-1, 1, aes_ops);
  713. for (nbits_t i = depth-how_many_1_bits; i < depth-1; ++i) {
  714. path[i+1] = descend(path[i], i, 0, aes_ops);
  715. }
  716. lastindex = index;
  717. expansion[index++] = descend(path[depth-1], depth-1, 0, aes_ops);
  718. expansion[index++] = descend(path[depth-1], depth-1, 1, aes_ops);
  719. }
  720. delete[] path;
  721. }
  722. // Construct three RDPFs of the given depth all with the same randomly
  723. // generated target index.
  724. template <nbits_t WIDTH>
  725. RDPFTriple<WIDTH>::RDPFTriple(MPCTIO &tio, yield_t &yield,
  726. nbits_t depth, bool save_expansion)
  727. {
  728. // Pick a random XOR share of the target
  729. xs_target.randomize(depth);
  730. // Now create three RDPFs with that target, and also convert the XOR
  731. // shares of the target to additive shares
  732. std::vector<coro_t> coroutines;
  733. for (int i=0;i<3;++i) {
  734. coroutines.emplace_back(
  735. [this, &tio, depth, i, save_expansion](yield_t &yield) {
  736. dpf[i] = RDPF<WIDTH>(tio, yield, xs_target, depth,
  737. save_expansion);
  738. });
  739. }
  740. coroutines.emplace_back(
  741. [this, &tio, depth](yield_t &yield) {
  742. mpc_xs_to_as(tio, yield, as_target, xs_target, depth, false);
  743. });
  744. run_coroutines(yield, coroutines);
  745. }
  746. template <nbits_t WIDTH>
  747. typename RDPFTriple<WIDTH>::node RDPFTriple<WIDTH>::descend(
  748. const RDPFTriple<WIDTH>::node &parent,
  749. nbits_t parentdepth, bit_t whichchild,
  750. size_t &aes_ops) const
  751. {
  752. auto [P0, P1, P2] = parent;
  753. DPFnode C0, C1, C2;
  754. C0 = dpf[0].descend(P0, parentdepth, whichchild, aes_ops);
  755. C1 = dpf[1].descend(P1, parentdepth, whichchild, aes_ops);
  756. C2 = dpf[2].descend(P2, parentdepth, whichchild, aes_ops);
  757. return std::make_tuple(C0,C1,C2);
  758. }
  759. template <nbits_t WIDTH>
  760. typename RDPFPair<WIDTH>::node RDPFPair<WIDTH>::descend(
  761. const RDPFPair<WIDTH>::node &parent,
  762. nbits_t parentdepth, bit_t whichchild,
  763. size_t &aes_ops) const
  764. {
  765. auto [P0, P1] = parent;
  766. DPFnode C0, C1;
  767. C0 = dpf[0].descend(P0, parentdepth, whichchild, aes_ops);
  768. C1 = dpf[1].descend(P1, parentdepth, whichchild, aes_ops);
  769. return std::make_tuple(C0,C1);
  770. }