rdpf.tcc 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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::LeafNode StreamEval<T>::next()
  63. {
  64. if (use_expansion && rdpf.has_expansion()) {
  65. // Just use the precomputed values
  66. typename T::LeafNode 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::LeafNode leaf = rdpf.descend_to_leaf(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::LeafNode 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. // Descend from a node at depth parentdepth to one of its leaf children
  177. // whichchild = 0: left child
  178. // whichchild = 1: right child
  179. //
  180. // Cost: 1 AES operation
  181. template <nbits_t WIDTH>
  182. inline typename RDPF<WIDTH>::LeafNode RDPF<WIDTH>::descend_to_leaf(
  183. const DPFnode &parent, nbits_t parentdepth, bit_t whichchild,
  184. size_t &aes_ops) const
  185. {
  186. typename RDPF<WIDTH>::LeafNode prgout;
  187. bool flag = get_lsb(parent);
  188. prg(prgout[0], parent, whichchild, aes_ops);
  189. if (flag) {
  190. DPFnode CW = cw[parentdepth];
  191. bit_t cfbit = !!(cfbits & (value_t(1)<<parentdepth));
  192. DPFnode CWR = CW ^ lsb128_mask[cfbit];
  193. prgout[0] ^= (whichchild ? CWR : CW);
  194. }
  195. return prgout;
  196. }
  197. // I/O for RDPFs
  198. template <typename T, nbits_t WIDTH>
  199. T& operator>>(T &is, RDPF<WIDTH> &rdpf)
  200. {
  201. is.read((char *)&rdpf.seed, sizeof(rdpf.seed));
  202. rdpf.whichhalf = get_lsb(rdpf.seed);
  203. uint8_t depth;
  204. // Add 64 to depth to indicate an expanded RDPF
  205. is.read((char *)&depth, sizeof(depth));
  206. bool read_expanded = false;
  207. if (depth > 64) {
  208. read_expanded = true;
  209. depth -= 64;
  210. }
  211. assert(depth <= ADDRESS_MAX_BITS);
  212. rdpf.cw.clear();
  213. for (uint8_t i=0; i<depth; ++i) {
  214. DPFnode cw;
  215. is.read((char *)&cw, sizeof(cw));
  216. rdpf.cw.push_back(cw);
  217. }
  218. if (read_expanded) {
  219. rdpf.expansion.resize(1<<depth);
  220. is.read((char *)rdpf.expansion.data(),
  221. sizeof(rdpf.expansion[0])<<depth);
  222. }
  223. value_t cfbits = 0;
  224. is.read((char *)&cfbits, BITBYTES(depth));
  225. rdpf.cfbits = cfbits;
  226. rdpf.li.resize(1);
  227. is.read((char *)&rdpf.li[0].unit_sum_inverse,
  228. sizeof(rdpf.li[0].unit_sum_inverse));
  229. is.read((char *)&rdpf.li[0].scaled_sum,
  230. sizeof(rdpf.li[0].scaled_sum));
  231. is.read((char *)&rdpf.li[0].scaled_xor,
  232. sizeof(rdpf.li[0].scaled_xor));
  233. return is;
  234. }
  235. // Write the DPF to the output stream. If expanded=true, then include
  236. // the expansion _if_ the DPF is itself already expanded. You can use
  237. // this to write DPFs to files.
  238. template <typename T, nbits_t WIDTH>
  239. T& write_maybe_expanded(T &os, const RDPF<WIDTH> &rdpf,
  240. bool expanded = true)
  241. {
  242. os.write((const char *)&rdpf.seed, sizeof(rdpf.seed));
  243. uint8_t depth = rdpf.cw.size();
  244. assert(depth <= ADDRESS_MAX_BITS);
  245. // If we're writing an expansion, add 64 to depth
  246. uint8_t expanded_depth = depth;
  247. bool write_expansion = false;
  248. if (expanded && rdpf.expansion.size() == (size_t(1)<<depth)) {
  249. write_expansion = true;
  250. expanded_depth += 64;
  251. }
  252. os.write((const char *)&expanded_depth, sizeof(expanded_depth));
  253. for (uint8_t i=0; i<depth; ++i) {
  254. os.write((const char *)&rdpf.cw[i], sizeof(rdpf.cw[i]));
  255. }
  256. if (write_expansion) {
  257. os.write((const char *)rdpf.expansion.data(),
  258. sizeof(rdpf.expansion[0])<<depth);
  259. }
  260. os.write((const char *)&rdpf.cfbits, BITBYTES(depth));
  261. os.write((const char *)&rdpf.li[0].unit_sum_inverse,
  262. sizeof(rdpf.li[0].unit_sum_inverse));
  263. os.write((const char *)&rdpf.li[0].scaled_sum,
  264. sizeof(rdpf.li[0].scaled_sum));
  265. os.write((const char *)&rdpf.li[0].scaled_xor,
  266. sizeof(rdpf.li[0].scaled_xor));
  267. return os;
  268. }
  269. // The ordinary << version never writes the expansion, since this is
  270. // what we use to send DPFs over the network.
  271. template <typename T, nbits_t WIDTH>
  272. T& operator<<(T &os, const RDPF<WIDTH> &rdpf)
  273. {
  274. return write_maybe_expanded(os, rdpf, false);
  275. }
  276. // I/O for RDPF Triples
  277. // We never write RDPFTriples over the network, so always write
  278. // the DPF expansions if they're available.
  279. template <typename T, nbits_t WIDTH>
  280. T& operator<<(T &os, const RDPFTriple<WIDTH> &rdpftrip)
  281. {
  282. write_maybe_expanded(os, rdpftrip.dpf[0], true);
  283. write_maybe_expanded(os, rdpftrip.dpf[1], true);
  284. write_maybe_expanded(os, rdpftrip.dpf[2], true);
  285. nbits_t depth = rdpftrip.dpf[0].depth();
  286. os.write((const char *)&rdpftrip.as_target.ashare, BITBYTES(depth));
  287. os.write((const char *)&rdpftrip.xs_target.xshare, BITBYTES(depth));
  288. return os;
  289. }
  290. template <typename T, nbits_t WIDTH>
  291. T& operator>>(T &is, RDPFTriple<WIDTH> &rdpftrip)
  292. {
  293. is >> rdpftrip.dpf[0] >> rdpftrip.dpf[1] >> rdpftrip.dpf[2];
  294. nbits_t depth = rdpftrip.dpf[0].depth();
  295. rdpftrip.as_target.ashare = 0;
  296. is.read((char *)&rdpftrip.as_target.ashare, BITBYTES(depth));
  297. rdpftrip.xs_target.xshare = 0;
  298. is.read((char *)&rdpftrip.xs_target.xshare, BITBYTES(depth));
  299. return is;
  300. }
  301. // I/O for RDPF Pairs
  302. // We never write RDPFPairs over the network, so always write
  303. // the DPF expansions if they're available.
  304. template <typename T, nbits_t WIDTH>
  305. T& operator<<(T &os, const RDPFPair<WIDTH> &rdpfpair)
  306. {
  307. write_maybe_expanded(os, rdpfpair.dpf[0], true);
  308. write_maybe_expanded(os, rdpfpair.dpf[1], true);
  309. return os;
  310. }
  311. template <typename T, nbits_t WIDTH>
  312. T& operator>>(T &is, RDPFPair<WIDTH> &rdpfpair)
  313. {
  314. is >> rdpfpair.dpf[0] >> rdpfpair.dpf[1];
  315. return is;
  316. }
  317. // Construct a DPF with the given (XOR-shared) target location, and
  318. // of the given depth, to be used for random-access memory reads and
  319. // writes. The DPF is construction collaboratively by P0 and P1,
  320. // with the server P2 helping by providing various kinds of
  321. // correlated randomness, such as MultTriples and AndTriples.
  322. //
  323. // This algorithm is based on Appendix C from the Duoram paper, with a
  324. // small optimization noted below.
  325. template <nbits_t WIDTH>
  326. RDPF<WIDTH>::RDPF(MPCTIO &tio, yield_t &yield,
  327. RegXS target, nbits_t depth, bool save_expansion)
  328. {
  329. int player = tio.player();
  330. size_t &aes_ops = tio.aes_ops();
  331. // Choose a random seed
  332. arc4random_buf(&seed, sizeof(seed));
  333. // Ensure the flag bits (the lsb of each node) are different
  334. seed = set_lsb(seed, !!player);
  335. cfbits = 0;
  336. whichhalf = (player == 1);
  337. // The root level is just the seed
  338. nbits_t level = 0;
  339. DPFnode *curlevel = NULL;
  340. DPFnode *nextlevel = new DPFnode[1];
  341. nextlevel[0] = seed;
  342. li.resize(1);
  343. // Construct each intermediate level
  344. while(level < depth) {
  345. if (player < 2) {
  346. delete[] curlevel;
  347. curlevel = nextlevel;
  348. if (save_expansion && level == depth-1) {
  349. expansion.resize(1<<depth);
  350. nextlevel = (DPFnode *)expansion.data();
  351. } else {
  352. nextlevel = new DPFnode[1<<(level+1)];
  353. }
  354. }
  355. // Invariant: curlevel has 2^level elements; nextlevel has
  356. // 2^{level+1} elements
  357. // The bit-shared choice bit is bit (depth-level-1) of the
  358. // XOR-shared target index
  359. RegBS bs_choice = target.bit(depth-level-1);
  360. size_t curlevel_size = (size_t(1)<<level);
  361. DPFnode L = _mm_setzero_si128();
  362. DPFnode R = _mm_setzero_si128();
  363. // The server doesn't need to do this computation, but it does
  364. // need to execute mpc_reconstruct_choice so that it sends
  365. // the AndTriples at the appropriate time.
  366. if (player < 2) {
  367. #ifdef RDPF_MTGEN_TIMING_1
  368. if (player == 0) {
  369. mtgen_timetest_1(level, 0, (1<<23)>>level, curlevel,
  370. nextlevel, aes_ops);
  371. size_t niters = 2048;
  372. if (level > 8) niters = (1<<20)>>level;
  373. for(int t=1;t<=8;++t) {
  374. mtgen_timetest_1(level, t, niters, curlevel,
  375. nextlevel, aes_ops);
  376. }
  377. mtgen_timetest_1(level, 0, (1<<23)>>level, curlevel,
  378. nextlevel, aes_ops);
  379. }
  380. #endif
  381. // Using the timing results gathered above, decide whether
  382. // to multithread, and if so, how many threads to use.
  383. // tio.cpu_nthreads() is the maximum number we have
  384. // available.
  385. int max_nthreads = tio.cpu_nthreads();
  386. if (max_nthreads == 1 || level < 19) {
  387. // No threading
  388. size_t laes_ops = 0;
  389. for(size_t i=0;i<curlevel_size;++i) {
  390. DPFnode lchild, rchild;
  391. prgboth(lchild, rchild, curlevel[i], laes_ops);
  392. L = (L ^ lchild);
  393. R = (R ^ rchild);
  394. nextlevel[2*i] = lchild;
  395. nextlevel[2*i+1] = rchild;
  396. }
  397. aes_ops += laes_ops;
  398. } else {
  399. size_t curlevel_size = size_t(1)<<level;
  400. int nthreads =
  401. int(ceil(sqrt(double(curlevel_size/6000))));
  402. if (nthreads > max_nthreads) {
  403. nthreads = max_nthreads;
  404. }
  405. DPFnode tL[nthreads];
  406. DPFnode tR[nthreads];
  407. size_t taes_ops[nthreads];
  408. size_t threadstart = 0;
  409. size_t threadchunk = curlevel_size / nthreads;
  410. size_t threadextra = curlevel_size % nthreads;
  411. boost::asio::thread_pool pool(nthreads);
  412. for (int t=0;t<nthreads;++t) {
  413. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  414. size_t threadend = threadstart + threadsize;
  415. boost::asio::post(pool,
  416. [t, &tL, &tR, &taes_ops, threadstart, threadend,
  417. &curlevel, &nextlevel] {
  418. DPFnode L = _mm_setzero_si128();
  419. DPFnode R = _mm_setzero_si128();
  420. size_t aes_ops = 0;
  421. for(size_t i=threadstart;i<threadend;++i) {
  422. DPFnode lchild, rchild;
  423. prgboth(lchild, rchild, curlevel[i], aes_ops);
  424. L = (L ^ lchild);
  425. R = (R ^ rchild);
  426. nextlevel[2*i] = lchild;
  427. nextlevel[2*i+1] = rchild;
  428. }
  429. tL[t] = L;
  430. tR[t] = R;
  431. taes_ops[t] = aes_ops;
  432. });
  433. threadstart = threadend;
  434. }
  435. pool.join();
  436. for (int t=0;t<nthreads;++t) {
  437. L ^= tL[t];
  438. R ^= tR[t];
  439. aes_ops += taes_ops[t];
  440. }
  441. }
  442. }
  443. // If we're going left (bs_choice = 0), we want the correction
  444. // word to be the XOR of our right side and our peer's right
  445. // side; if bs_choice = 1, it should be the XOR or our left side
  446. // and our peer's left side.
  447. // We also have to ensure that the flag bits (the lsb) of the
  448. // side that will end up the same be of course the same, but
  449. // also that the flag bits (the lsb) of the side that will end
  450. // up different _must_ be different. That is, it's not enough
  451. // for the nodes of the child selected by choice to be different
  452. // as 128-bit values; they also have to be different in their
  453. // lsb.
  454. // This is where we make a small optimization over Appendix C of
  455. // the Duoram paper: instead of keeping separate correction flag
  456. // bits for the left and right children, we observe that the low
  457. // bit of the overall correction word effectively serves as one
  458. // of those bits, so we just need to store one extra bit per
  459. // level, not two. (We arbitrarily choose the one for the right
  460. // child.)
  461. // Note that the XOR of our left and right child before and
  462. // after applying the correction word won't change, since the
  463. // correction word is applied to either both children or
  464. // neither, depending on the value of the parent's flag. So in
  465. // particular, the XOR of the flag bits won't change, and if our
  466. // children's flag's XOR equals our peer's children's flag's
  467. // XOR, then we won't have different flag bits even for the
  468. // children that have different 128-bit values.
  469. // So we compute our_parity = lsb(L^R)^player, and we XOR that
  470. // into the R value in the correction word computation. At the
  471. // same time, we exchange these parity values to compute the
  472. // combined parity, which we store in the DPF. Then when the
  473. // DPF is evaluated, if the parent's flag is set, not only apply
  474. // the correction work to both children, but also apply the
  475. // (combined) parity bit to just the right child. Then for
  476. // unequal nodes (where the flag bit is different), exactly one
  477. // of the four children (two for P0 and two for P1) will have
  478. // the parity bit applied, which will set the XOR of the lsb of
  479. // those four nodes to just L0^R0^L1^R1^our_parity^peer_parity
  480. // = 1 because everything cancels out except player (for which
  481. // one player is 0 and the other is 1).
  482. bool our_parity_bit = get_lsb(L ^ R) ^ !!player;
  483. DPFnode our_parity = lsb128_mask[our_parity_bit];
  484. DPFnode CW;
  485. bool peer_parity_bit;
  486. // Exchange the parities and do mpc_reconstruct_choice at the
  487. // same time (bundled into the same rounds)
  488. run_coroutines(yield,
  489. [this, &tio, &our_parity_bit, &peer_parity_bit](yield_t &yield) {
  490. tio.queue_peer(&our_parity_bit, 1);
  491. yield();
  492. uint8_t peer_parity_byte;
  493. tio.recv_peer(&peer_parity_byte, 1);
  494. peer_parity_bit = peer_parity_byte & 1;
  495. },
  496. [this, &tio, &CW, &L, &R, &bs_choice, &our_parity](yield_t &yield) {
  497. mpc_reconstruct_choice(tio, yield, CW, bs_choice,
  498. (R ^ our_parity), L);
  499. });
  500. bool parity_bit = our_parity_bit ^ peer_parity_bit;
  501. cfbits |= (value_t(parity_bit)<<level);
  502. DPFnode CWR = CW ^ lsb128_mask[parity_bit];
  503. if (player < 2) {
  504. // The timing of each iteration of the inner loop is
  505. // comparable to the above, so just use the same
  506. // computations. All of this could be tuned, of course.
  507. if (level < depth-1) {
  508. // Using the timing results gathered above, decide whether
  509. // to multithread, and if so, how many threads to use.
  510. // tio.cpu_nthreads() is the maximum number we have
  511. // available.
  512. int max_nthreads = tio.cpu_nthreads();
  513. if (max_nthreads == 1 || level < 19) {
  514. // No threading
  515. for(size_t i=0;i<curlevel_size;++i) {
  516. bool flag = get_lsb(curlevel[i]);
  517. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  518. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  519. }
  520. } else {
  521. int nthreads =
  522. int(ceil(sqrt(double(curlevel_size/6000))));
  523. if (nthreads > max_nthreads) {
  524. nthreads = max_nthreads;
  525. }
  526. size_t threadstart = 0;
  527. size_t threadchunk = curlevel_size / nthreads;
  528. size_t threadextra = curlevel_size % nthreads;
  529. boost::asio::thread_pool pool(nthreads);
  530. for (int t=0;t<nthreads;++t) {
  531. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  532. size_t threadend = threadstart + threadsize;
  533. boost::asio::post(pool, [CW, CWR, threadstart, threadend,
  534. &curlevel, &nextlevel] {
  535. for(size_t i=threadstart;i<threadend;++i) {
  536. bool flag = get_lsb(curlevel[i]);
  537. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  538. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  539. }
  540. });
  541. threadstart = threadend;
  542. }
  543. pool.join();
  544. }
  545. } else {
  546. // Recall there are four potentially useful vectors that
  547. // can come out of a DPF:
  548. // - (single-bit) bitwise unit vector
  549. // - additive-shared unit vector
  550. // - XOR-shared scaled unit vector
  551. // - additive-shared scaled unit vector
  552. //
  553. // (No single DPF should be used for both of the first
  554. // two or both of the last two, though, since they're
  555. // correlated; you _can_ use one of the first two and
  556. // one of the last two.)
  557. //
  558. // For each 128-bit leaf, the low bit is the flag bit,
  559. // and we're guaranteed that the flag bits (and indeed
  560. // the whole 128-bit value) for P0 and P1 are the same
  561. // for every leaf except the target, and that the flag
  562. // bits definitely differ for the target (and the other
  563. // 127 bits are independently random on each side).
  564. //
  565. // We divide the 128-bit leaf into a low 64-bit word and
  566. // a high 64-bit word. We use the low word for the unit
  567. // vector and the high word for the scaled vector; this
  568. // choice is not arbitrary: the flag bit in the low word
  569. // means that the sum of all the low words (with P1's
  570. // low words negated) across both P0 and P1 is
  571. // definitely odd, so we can compute that sum's inverse
  572. // mod 2^64, and store it now during precomputation. At
  573. // evaluation time for the additive-shared unit vector,
  574. // we will output this global inverse times the low word
  575. // of each leaf, which will make the sum of all of those
  576. // values 1. (This technique replaces the protocol in
  577. // Appendix D of the Duoram paper.)
  578. //
  579. // For the scaled vector, we just have to compute shares
  580. // of what the scaled vector is a sharing _of_, but
  581. // that's just XORing or adding all of each party's
  582. // local high words; no communication needed.
  583. value_t low_sum = 0;
  584. value_t high_sum = 0;
  585. value_t high_xor = 0;
  586. // Using the timing results gathered above, decide whether
  587. // to multithread, and if so, how many threads to use.
  588. // tio.cpu_nthreads() is the maximum number we have
  589. // available.
  590. int max_nthreads = tio.cpu_nthreads();
  591. if (max_nthreads == 1 || level < 19) {
  592. // No threading
  593. for(size_t i=0;i<curlevel_size;++i) {
  594. bool flag = get_lsb(curlevel[i]);
  595. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  596. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  597. if (save_expansion) {
  598. nextlevel[2*i] = leftchild;
  599. nextlevel[2*i+1] = rightchild;
  600. }
  601. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  602. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  603. value_t lefthigh =
  604. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  605. value_t righthigh =
  606. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  607. low_sum += (leftlow + rightlow);
  608. high_sum += (lefthigh + righthigh);
  609. high_xor ^= (lefthigh ^ righthigh);
  610. }
  611. } else {
  612. int nthreads =
  613. int(ceil(sqrt(double(curlevel_size/6000))));
  614. if (nthreads > max_nthreads) {
  615. nthreads = max_nthreads;
  616. }
  617. value_t tlow_sum[nthreads];
  618. value_t thigh_sum[nthreads];
  619. value_t thigh_xor[nthreads];
  620. size_t threadstart = 0;
  621. size_t threadchunk = curlevel_size / nthreads;
  622. size_t threadextra = curlevel_size % nthreads;
  623. boost::asio::thread_pool pool(nthreads);
  624. for (int t=0;t<nthreads;++t) {
  625. size_t threadsize = threadchunk + (size_t(t) < threadextra);
  626. size_t threadend = threadstart + threadsize;
  627. boost::asio::post(pool,
  628. [t, &tlow_sum, &thigh_sum, &thigh_xor, threadstart, threadend,
  629. &curlevel, &nextlevel, CW, CWR, save_expansion] {
  630. value_t low_sum = 0;
  631. value_t high_sum = 0;
  632. value_t high_xor = 0;
  633. for(size_t i=threadstart;i<threadend;++i) {
  634. bool flag = get_lsb(curlevel[i]);
  635. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  636. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  637. if (save_expansion) {
  638. nextlevel[2*i] = leftchild;
  639. nextlevel[2*i+1] = rightchild;
  640. }
  641. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  642. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  643. value_t lefthigh =
  644. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  645. value_t righthigh =
  646. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  647. low_sum += (leftlow + rightlow);
  648. high_sum += (lefthigh + righthigh);
  649. high_xor ^= (lefthigh ^ righthigh);
  650. }
  651. tlow_sum[t] = low_sum;
  652. thigh_sum[t] = high_sum;
  653. thigh_xor[t] = high_xor;
  654. });
  655. threadstart = threadend;
  656. }
  657. pool.join();
  658. for (int t=0;t<nthreads;++t) {
  659. low_sum += tlow_sum[t];
  660. high_sum += thigh_sum[t];
  661. high_xor ^= thigh_xor[t];
  662. }
  663. }
  664. if (player == 1) {
  665. low_sum = -low_sum;
  666. high_sum = -high_sum;
  667. }
  668. li[0].scaled_sum[0].ashare = high_sum;
  669. li[0].scaled_xor[0].xshare = high_xor;
  670. // Exchange low_sum and add them up
  671. tio.queue_peer(&low_sum, sizeof(low_sum));
  672. yield();
  673. value_t peer_low_sum;
  674. tio.recv_peer(&peer_low_sum, sizeof(peer_low_sum));
  675. low_sum += peer_low_sum;
  676. // The low_sum had better be odd
  677. assert(low_sum & 1);
  678. li[0].unit_sum_inverse = inverse_value_t(low_sum);
  679. }
  680. cw.push_back(CW);
  681. } else if (level == depth-1) {
  682. yield();
  683. }
  684. ++level;
  685. }
  686. delete[] curlevel;
  687. if (!save_expansion || player == 2) {
  688. delete[] nextlevel;
  689. }
  690. }
  691. // Get the leaf node for the given input
  692. template <nbits_t WIDTH>
  693. typename RDPF<WIDTH>::LeafNode
  694. RDPF<WIDTH>::leaf(address_t input, size_t &aes_ops) const
  695. {
  696. // If we have a precomputed expansion, just use it
  697. if (expansion.size()) {
  698. return expansion[input];
  699. }
  700. nbits_t totdepth = depth();
  701. DPFnode node = seed;
  702. for (nbits_t d=0;d<totdepth;++d) {
  703. bit_t dir = !!(input & (address_t(1)<<(totdepth-d-1)));
  704. node = descend(node, d, dir, aes_ops);
  705. }
  706. LeafNode ln;
  707. ln[0] = node;
  708. return ln;
  709. }
  710. // Expand the DPF if it's not already expanded
  711. //
  712. // This routine is slightly more efficient than repeatedly calling
  713. // StreamEval::next(), but it uses a lot more memory.
  714. template <nbits_t WIDTH>
  715. void RDPF<WIDTH>::expand(size_t &aes_ops)
  716. {
  717. nbits_t depth = this->depth();
  718. size_t num_leaves = size_t(1)<<depth;
  719. if (expansion.size() == num_leaves) return;
  720. expansion.resize(num_leaves);
  721. address_t index = 0;
  722. address_t lastindex = 0;
  723. DPFnode *path = new DPFnode[depth];
  724. path[0] = seed;
  725. for (nbits_t i=1;i<depth;++i) {
  726. path[i] = descend(path[i-1], i-1, 0, aes_ops);
  727. }
  728. expansion[index++][0] = descend(path[depth-1], depth-1, 0, aes_ops);
  729. expansion[index++][0] = descend(path[depth-1], depth-1, 1, aes_ops);
  730. while(index < num_leaves) {
  731. // Invariant: lastindex and index will both be even, and
  732. // index=lastindex+2
  733. uint64_t index_xor = index ^ lastindex;
  734. nbits_t how_many_1_bits = __builtin_popcount(index_xor);
  735. // If lastindex -> index goes for example from (in binary)
  736. // 010010110 -> 010011000, then index_xor will be
  737. // 000001110 and how_many_1_bits will be 3.
  738. // That indicates that path[depth-3] was a left child, and now
  739. // we need to change it to a right child by descending right
  740. // from path[depth-4], and then filling the path after that with
  741. // left children.
  742. path[depth-how_many_1_bits] =
  743. descend(path[depth-how_many_1_bits-1],
  744. depth-how_many_1_bits-1, 1, aes_ops);
  745. for (nbits_t i = depth-how_many_1_bits; i < depth-1; ++i) {
  746. path[i+1] = descend(path[i], i, 0, aes_ops);
  747. }
  748. lastindex = index;
  749. expansion[index++][0] = descend(path[depth-1], depth-1, 0, aes_ops);
  750. expansion[index++][0] = descend(path[depth-1], depth-1, 1, aes_ops);
  751. }
  752. delete[] path;
  753. }
  754. // Construct three RDPFs of the given depth all with the same randomly
  755. // generated target index.
  756. template <nbits_t WIDTH>
  757. RDPFTriple<WIDTH>::RDPFTriple(MPCTIO &tio, yield_t &yield,
  758. nbits_t depth, bool save_expansion)
  759. {
  760. // Pick a random XOR share of the target
  761. xs_target.randomize(depth);
  762. // Now create three RDPFs with that target, and also convert the XOR
  763. // shares of the target to additive shares
  764. std::vector<coro_t> coroutines;
  765. for (int i=0;i<3;++i) {
  766. coroutines.emplace_back(
  767. [this, &tio, depth, i, save_expansion](yield_t &yield) {
  768. dpf[i] = RDPF<WIDTH>(tio, yield, xs_target, depth,
  769. save_expansion);
  770. });
  771. }
  772. coroutines.emplace_back(
  773. [this, &tio, depth](yield_t &yield) {
  774. mpc_xs_to_as(tio, yield, as_target, xs_target, depth, false);
  775. });
  776. run_coroutines(yield, coroutines);
  777. }
  778. template <nbits_t WIDTH>
  779. typename RDPFTriple<WIDTH>::node RDPFTriple<WIDTH>::descend(
  780. const RDPFTriple<WIDTH>::node &parent,
  781. nbits_t parentdepth, bit_t whichchild,
  782. size_t &aes_ops) const
  783. {
  784. auto [P0, P1, P2] = parent;
  785. DPFnode C0, C1, C2;
  786. C0 = dpf[0].descend(P0, parentdepth, whichchild, aes_ops);
  787. C1 = dpf[1].descend(P1, parentdepth, whichchild, aes_ops);
  788. C2 = dpf[2].descend(P2, parentdepth, whichchild, aes_ops);
  789. return std::make_tuple(C0,C1,C2);
  790. }
  791. template <nbits_t WIDTH>
  792. typename RDPFTriple<WIDTH>::LeafNode RDPFTriple<WIDTH>::descend_to_leaf(
  793. const RDPFTriple<WIDTH>::node &parent,
  794. nbits_t parentdepth, bit_t whichchild,
  795. size_t &aes_ops) const
  796. {
  797. auto [P0, P1, P2] = parent;
  798. typename RDPF<WIDTH>::LeafNode C0, C1, C2;
  799. C0 = dpf[0].descend_to_leaf(P0, parentdepth, whichchild, aes_ops);
  800. C1 = dpf[1].descend_to_leaf(P1, parentdepth, whichchild, aes_ops);
  801. C2 = dpf[2].descend_to_leaf(P2, parentdepth, whichchild, aes_ops);
  802. return std::make_tuple(C0,C1,C2);
  803. }
  804. template <nbits_t WIDTH>
  805. typename RDPFPair<WIDTH>::node RDPFPair<WIDTH>::descend(
  806. const RDPFPair<WIDTH>::node &parent,
  807. nbits_t parentdepth, bit_t whichchild,
  808. size_t &aes_ops) const
  809. {
  810. auto [P0, P1] = parent;
  811. DPFnode C0, C1;
  812. C0 = dpf[0].descend(P0, parentdepth, whichchild, aes_ops);
  813. C1 = dpf[1].descend(P1, parentdepth, whichchild, aes_ops);
  814. return std::make_tuple(C0,C1);
  815. }
  816. template <nbits_t WIDTH>
  817. typename RDPFPair<WIDTH>::LeafNode RDPFPair<WIDTH>::descend_to_leaf(
  818. const RDPFPair<WIDTH>::node &parent,
  819. nbits_t parentdepth, bit_t whichchild,
  820. size_t &aes_ops) const
  821. {
  822. auto [P0, P1] = parent;
  823. typename RDPF<WIDTH>::LeafNode C0, C1;
  824. C0 = dpf[0].descend_to_leaf(P0, parentdepth, whichchild, aes_ops);
  825. C1 = dpf[1].descend_to_leaf(P1, parentdepth, whichchild, aes_ops);
  826. return std::make_tuple(C0,C1);
  827. }