rdpf.tcc 36 KB

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