rdpf.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include <bsd/stdlib.h> // arc4random_buf
  2. #include "rdpf.hpp"
  3. #include "bitutils.hpp"
  4. #include "mpcops.hpp"
  5. #include "aes.hpp"
  6. #include "prg.hpp"
  7. // Don't warn if we never actually use these functions
  8. static void dump_node(DPFnode node, const char *label = NULL)
  9. __attribute__ ((unused));
  10. static void dump_level(DPFnode *nodes, size_t num, const char *label = NULL)
  11. __attribute__ ((unused));
  12. static void dump_node(DPFnode node, const char *label)
  13. {
  14. if (label) printf("%s: ", label);
  15. for(int i=0;i<16;++i) { printf("%02x", ((unsigned char *)&node)[15-i]); } printf("\n");
  16. }
  17. static void dump_level(DPFnode *nodes, size_t num, const char *label)
  18. {
  19. if (label) printf("%s:\n", label);
  20. for (size_t i=0;i<num;++i) {
  21. dump_node(nodes[i]);
  22. }
  23. printf("\n");
  24. }
  25. // Compute the multiplicative inverse of x mod 2^{VALUE_BITS}
  26. // This is the same as computing x to the power of
  27. // 2^{VALUE_BITS-1}-1.
  28. static value_t inverse_value_t(value_t x)
  29. {
  30. int expon = 1;
  31. value_t xe = x;
  32. // Invariant: xe = x^(2^expon - 1) mod 2^{VALUE_BITS}
  33. // Goal: compute x^(2^{VALUE_BITS-1} - 1)
  34. while (expon < VALUE_BITS-1) {
  35. xe = xe * xe * x;
  36. ++expon;
  37. }
  38. return xe;
  39. }
  40. // Construct a DPF with the given (XOR-shared) target location, and
  41. // of the given depth, to be used for random-access memory reads and
  42. // writes. The DPF is construction collaboratively by P0 and P1,
  43. // with the server P2 helping by providing various kinds of
  44. // correlated randomness, such as MultTriples and AndTriples.
  45. //
  46. // This algorithm is based on Appendix C from the Duoram paper, with a
  47. // small optimization noted below.
  48. RDPF::RDPF(MPCTIO &tio, yield_t &yield,
  49. RegXS target, nbits_t depth, bool save_expansion)
  50. {
  51. int player = tio.player();
  52. size_t &aesops = tio.aes_ops();
  53. // Choose a random seed
  54. arc4random_buf(&seed, sizeof(seed));
  55. // Ensure the flag bits (the lsb of each node) are different
  56. seed = set_lsb(seed, !!player);
  57. cfbits = 0;
  58. whichhalf = (player == 1);
  59. // The root level is just the seed
  60. nbits_t level = 0;
  61. DPFnode *curlevel = NULL;
  62. DPFnode *nextlevel = new DPFnode[1];
  63. nextlevel[0] = seed;
  64. // Construct each intermediate level
  65. while(level < depth) {
  66. delete[] curlevel;
  67. curlevel = nextlevel;
  68. if (save_expansion && level == depth-1) {
  69. expansion.resize(1<<depth);
  70. nextlevel = expansion.data();
  71. } else {
  72. nextlevel = new DPFnode[1<<(level+1)];
  73. }
  74. // Invariant: curlevel has 2^level elements; nextlevel has
  75. // 2^{level+1} elements
  76. // The bit-shared choice bit is bit (depth-level-1) of the
  77. // XOR-shared target index
  78. RegBS bs_choice = target.bit(depth-level-1);
  79. size_t curlevel_size = (size_t(1)<<level);
  80. DPFnode L = _mm_setzero_si128();
  81. DPFnode R = _mm_setzero_si128();
  82. // The server doesn't need to do this computation, but it does
  83. // need to execute mpc_reconstruct_choice so that it sends
  84. // the AndTriples at the appropriate time.
  85. if (player < 2) {
  86. for(size_t i=0;i<curlevel_size;++i) {
  87. DPFnode lchild, rchild;
  88. prgboth(lchild, rchild, curlevel[i], aesops);
  89. L = (L ^ lchild);
  90. R = (R ^ rchild);
  91. if (nextlevel) {
  92. nextlevel[2*i] = lchild;
  93. nextlevel[2*i+1] = rchild;
  94. }
  95. }
  96. }
  97. // If we're going left (bs_choice = 0), we want the correction
  98. // word to be the XOR of our right side and our peer's right
  99. // side; if bs_choice = 1, it should be the XOR or our left side
  100. // and our peer's left side.
  101. // We also have to ensure that the flag bits (the lsb) of the
  102. // side that will end up the same be of course the same, but
  103. // also that the flag bits (the lsb) of the side that will end
  104. // up different _must_ be different. That is, it's not enough
  105. // for the nodes of the child selected by choice to be different
  106. // as 128-bit values; they also have to be different in their
  107. // lsb.
  108. // This is where we make a small optimization over Appendix C of
  109. // the Duoram paper: instead of keeping separate correction flag
  110. // bits for the left and right children, we observe that the low
  111. // bit of the overall correction word effectively serves as one
  112. // of those bits, so we just need to store one extra bit per
  113. // level, not two. (We arbitrarily choose the one for the right
  114. // child.)
  115. // Note that the XOR of our left and right child before and
  116. // after applying the correction word won't change, since the
  117. // correction word is applied to either both children or
  118. // neither, depending on the value of the parent's flag. So in
  119. // particular, the XOR of the flag bits won't change, and if our
  120. // children's flag's XOR equals our peer's children's flag's
  121. // XOR, then we won't have different flag bits even for the
  122. // children that have different 128-bit values.
  123. // So we compute our_parity = lsb(L^R)^player, and we XOR that
  124. // into the R value in the correction word computation. At the
  125. // same time, we exchange these parity values to compute the
  126. // combined parity, which we store in the DPF. Then when the
  127. // DPF is evaluated, if the parent's flag is set, not only apply
  128. // the correction work to both children, but also apply the
  129. // (combined) parity bit to just the right child. Then for
  130. // unequal nodes (where the flag bit is different), exactly one
  131. // of the four children (two for P0 and two for P1) will have
  132. // the parity bit applied, which will set the XOR of the lsb of
  133. // those four nodes to just L0^R0^L1^R1^our_parity^peer_parity
  134. // = 1 because everything cancels out except player (for which
  135. // one player is 0 and the other is 1).
  136. bool our_parity_bit = get_lsb(L ^ R) ^ !!player;
  137. DPFnode our_parity = lsb128_mask[our_parity_bit];
  138. DPFnode CW;
  139. bool peer_parity_bit;
  140. // Exchange the parities and do mpc_reconstruct_choice at the
  141. // same time (bundled into the same rounds)
  142. std::vector<coro_t> coroutines;
  143. coroutines.emplace_back(
  144. [&](yield_t &yield) {
  145. tio.queue_peer(&our_parity_bit, 1);
  146. yield();
  147. uint8_t peer_parity_byte;
  148. tio.recv_peer(&peer_parity_byte, 1);
  149. peer_parity_bit = peer_parity_byte & 1;
  150. });
  151. coroutines.emplace_back(
  152. [&](yield_t &yield) {
  153. mpc_reconstruct_choice(tio, yield, CW, bs_choice,
  154. (R ^ our_parity), L);
  155. });
  156. run_coroutines(yield, coroutines);
  157. bool parity_bit = our_parity_bit ^ peer_parity_bit;
  158. cfbits |= (size_t(parity_bit)<<level);
  159. DPFnode CWR = CW ^ lsb128_mask[parity_bit];
  160. if (player < 2) {
  161. if (level < depth-1) {
  162. for(size_t i=0;i<curlevel_size;++i) {
  163. bool flag = get_lsb(curlevel[i]);
  164. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  165. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  166. }
  167. } else {
  168. // Recall there are four potentially useful vectors that
  169. // can come out of a DPF:
  170. // - (single-bit) bitwise unit vector
  171. // - additive-shared unit vector
  172. // - XOR-shared scaled unit vector
  173. // - additive-shared scaled unit vector
  174. //
  175. // (No single DPF should be used for both of the first
  176. // two or both of the last two, though, since they're
  177. // correlated; you _can_ use one of the first two and
  178. // one of the last two.)
  179. //
  180. // For each 128-bit leaf, the low bit is the flag bit,
  181. // and we're guaranteed that the flag bits (and indeed
  182. // the whole 128-bit value) for P0 and P1 are the same
  183. // for every leaf except the target, and that the flag
  184. // bits definitely differ for the target (and the other
  185. // 127 bits are independently random on each side).
  186. //
  187. // We divide the 128-bit leaf into a low 64-bit word and
  188. // a high 64-bit word. We use the low word for the unit
  189. // vector and the high word for the scaled vector; this
  190. // choice is not arbitrary: the flag bit in the low word
  191. // means that the sum of all the low words (with P1's
  192. // low words negated) across both P0 and P1 is
  193. // definitely odd, so we can compute that sum's inverse
  194. // mod 2^64, and store it now during precomputation. At
  195. // evaluation time for the additive-shared unit vector,
  196. // we will output this global inverse times the low word
  197. // of each leaf, which will make the sum of all of those
  198. // values 1. (This technique replaces the protocol in
  199. // Appendix D of the Duoram paper.)
  200. //
  201. // For the scaled vector, we just have to compute shares
  202. // of what the scaled vector is a sharing _of_, but
  203. // that's just XORing or adding all of each party's
  204. // local high words; no communication needed.
  205. value_t low_sum = 0;
  206. value_t high_sum = 0;
  207. value_t high_xor = 0;
  208. for(size_t i=0;i<curlevel_size;++i) {
  209. bool flag = get_lsb(curlevel[i]);
  210. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  211. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  212. if (save_expansion) {
  213. nextlevel[2*i] = leftchild;
  214. nextlevel[2*i+1] = rightchild;
  215. }
  216. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  217. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  218. value_t lefthigh =
  219. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  220. value_t righthigh =
  221. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  222. low_sum += (leftlow + rightlow);
  223. high_sum += (lefthigh + righthigh);
  224. high_xor ^= (lefthigh ^ righthigh);
  225. }
  226. if (player == 1) {
  227. low_sum = -low_sum;
  228. high_sum = -high_sum;
  229. }
  230. scaled_sum.ashare = high_sum;
  231. scaled_xor.xshare = high_xor;
  232. // Exchange low_sum and add them up
  233. tio.queue_peer(&low_sum, sizeof(low_sum));
  234. yield();
  235. value_t peer_low_sum;
  236. tio.recv_peer(&peer_low_sum, sizeof(peer_low_sum));
  237. low_sum += peer_low_sum;
  238. // The low_sum had better be odd
  239. assert(low_sum & 1);
  240. unit_sum_inverse = inverse_value_t(low_sum);
  241. }
  242. cw.push_back(CW);
  243. }
  244. ++level;
  245. }
  246. delete[] curlevel;
  247. if (!save_expansion) {
  248. delete[] nextlevel;
  249. }
  250. }
  251. // The number of bytes it will take to store a RDPF of the given depth
  252. size_t RDPF::size(nbits_t depth)
  253. {
  254. return sizeof(seed) + depth*sizeof(DPFnode) + BITBYTES(depth) +
  255. sizeof(unit_sum_inverse) + sizeof(scaled_sum) +
  256. sizeof(scaled_xor);
  257. }
  258. // The number of bytes it will take to store this RDPF
  259. size_t RDPF::size() const
  260. {
  261. uint8_t depth = cw.size();
  262. return size(depth);
  263. }
  264. // Descend from a node at depth parentdepth to one of its children
  265. // whichchild = 0: left child
  266. // whichchild = 1: right child
  267. DPFnode RDPF::descend(const DPFnode parent, nbits_t parentdepth,
  268. bit_t whichchild, size_t &op_counter) const
  269. {
  270. DPFnode prgout;
  271. bool flag = get_lsb(parent);
  272. prg(prgout, parent, whichchild, op_counter);
  273. if (flag) {
  274. DPFnode CW = cw[parentdepth];
  275. bit_t cfbit = !!(cfbits & (value_t(1)<<parentdepth));
  276. DPFnode CWR = CW ^ lsb128_mask[cfbit];
  277. prgout ^= (whichchild ? CWR : CW);
  278. }
  279. return prgout;
  280. }
  281. // Get the leaf node for the given input
  282. DPFnode RDPF::leaf(address_t input, size_t &op_counter) const
  283. {
  284. // If we have a precomputed expansion, just use it
  285. if (expansion.size()) {
  286. return expansion[input];
  287. }
  288. nbits_t totdepth = depth();
  289. DPFnode node = seed;
  290. for (nbits_t d=0;d<totdepth;++d) {
  291. bit_t dir = !!(input & (address_t(1)<<(totdepth-d-1)));
  292. node = descend(node, d, dir, op_counter);
  293. }
  294. return node;
  295. }
  296. // Expand the DPF if it's not already expanded
  297. void RDPF::expand(size_t &op_counter)
  298. {
  299. nbits_t depth = this->depth();
  300. size_t num_leaves = size_t(1)<<depth;
  301. if (expansion.size() == num_leaves) return;
  302. expansion.resize(num_leaves);
  303. address_t index = 0;
  304. address_t lastindex = 0;
  305. DPFnode *path = new DPFnode[depth];
  306. path[0] = seed;
  307. for (nbits_t i=1;i<depth;++i) {
  308. path[i] = descend(path[i-1], i-1, 0, op_counter);
  309. }
  310. expansion[index++] = descend(path[depth-1], depth-1, 0, op_counter);
  311. expansion[index++] = descend(path[depth-1], depth-1, 1, op_counter);
  312. while(index < num_leaves) {
  313. // Invariant: lastindex and index will both be even, and
  314. // index=lastindex+2
  315. uint64_t index_xor = index ^ lastindex;
  316. nbits_t how_many_1_bits = __builtin_popcount(index_xor);
  317. // If lastindex -> index goes for example from (in binary)
  318. // 010010110 -> 010011000, then index_xor will be
  319. // 000001110 and how_many_1_bits will be 3.
  320. // That indicates that path[depth-3] was a left child, and now
  321. // we need to change it to a right child by descending right
  322. // from path[depth-4], and then filling the path after that with
  323. // left children.
  324. path[depth-how_many_1_bits] =
  325. descend(path[depth-how_many_1_bits-1],
  326. depth-how_many_1_bits-1, 1, op_counter);
  327. for (nbits_t i = depth-how_many_1_bits; i < depth-1; ++i) {
  328. path[i+1] = descend(path[i], i, 0, op_counter);
  329. }
  330. lastindex = index;
  331. expansion[index++] = descend(path[depth-1], depth-1, 0, op_counter);
  332. expansion[index++] = descend(path[depth-1], depth-1, 1, op_counter);
  333. }
  334. delete[] path;
  335. }
  336. // Construct three RDPFs of the given depth all with the same randomly
  337. // generated target index.
  338. RDPFTriple::RDPFTriple(MPCTIO &tio, yield_t &yield,
  339. nbits_t depth, bool save_expansion)
  340. {
  341. // Pick a random XOR share of the target
  342. xs_target.randomize(depth);
  343. // Now create three RDPFs with that target, and also convert the XOR
  344. // shares of the target to additive shares
  345. std::vector<coro_t> coroutines;
  346. for (int i=0;i<3;++i) {
  347. coroutines.emplace_back(
  348. [&, i](yield_t &yield) {
  349. dpf[i] = RDPF(tio, yield, xs_target, depth,
  350. save_expansion);
  351. });
  352. }
  353. coroutines.emplace_back(
  354. [&](yield_t &yield) {
  355. mpc_xs_to_as(tio, yield, as_target, xs_target, depth);
  356. });
  357. run_coroutines(yield, coroutines);
  358. }