rdpf.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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)
  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. // The root level is just the seed
  59. nbits_t level = 0;
  60. DPFnode *curlevel = NULL;
  61. DPFnode *nextlevel = new DPFnode[1];
  62. nextlevel[0] = seed;
  63. // Construct each intermediate level
  64. while(level < depth) {
  65. delete[] curlevel;
  66. curlevel = nextlevel;
  67. nextlevel = new DPFnode[1<<(level+1)];
  68. // Invariant: curlevel has 2^level elements; nextlevel has
  69. // 2^{level+1} elements
  70. // The bit-shared choice bit is bit (depth-level-1) of the
  71. // XOR-shared target index
  72. RegBS bs_choice = target.bit(depth-level-1);
  73. size_t curlevel_size = (size_t(1)<<level);
  74. DPFnode L = _mm_setzero_si128();
  75. DPFnode R = _mm_setzero_si128();
  76. // The server doesn't need to do this computation, but it does
  77. // need to execute mpc_reconstruct_choice so that it sends
  78. // the AndTriples at the appropriate time.
  79. if (player < 2) {
  80. for(size_t i=0;i<curlevel_size;++i) {
  81. DPFnode lchild, rchild;
  82. prgboth(lchild, rchild, curlevel[i], aesops);
  83. L = (L ^ lchild);
  84. R = (R ^ rchild);
  85. if (nextlevel) {
  86. nextlevel[2*i] = lchild;
  87. nextlevel[2*i+1] = rchild;
  88. }
  89. }
  90. }
  91. // If we're going left (bs_choice = 0), we want the correction
  92. // word to be the XOR of our right side and our peer's right
  93. // side; if bs_choice = 1, it should be the XOR or our left side
  94. // and our peer's left side.
  95. // We also have to ensure that the flag bits (the lsb) of the
  96. // side that will end up the same be of course the same, but
  97. // also that the flag bits (the lsb) of the side that will end
  98. // up different _must_ be different. That is, it's not enough
  99. // for the nodes of the child selected by choice to be different
  100. // as 128-bit values; they also have to be different in their
  101. // lsb.
  102. // This is where we make a small optimization over Appendix C of
  103. // the Duoram paper: instead of keeping separate correction flag
  104. // bits for the left and right children, we observe that the low
  105. // bit of the overall correction word effectively serves as one
  106. // of those bits, so we just need to store one extra bit per
  107. // level, not two. (We arbitrarily choose the one for the right
  108. // child.)
  109. // Note that the XOR of our left and right child before and
  110. // after applying the correction word won't change, since the
  111. // correction word is applied to either both children or
  112. // neither, depending on the value of the parent's flag. So in
  113. // particular, the XOR of the flag bits won't change, and if our
  114. // children's flag's XOR equals our peer's children's flag's
  115. // XOR, then we won't have different flag bits even for the
  116. // children that have different 128-bit values.
  117. // So we compute our_parity = lsb(L^R)^player, and we XOR that
  118. // into the R value in the correction word computation. At the
  119. // same time, we exchange these parity values to compute the
  120. // combined parity, which we store in the DPF. Then when the
  121. // DPF is evaluated, if the parent's flag is set, not only apply
  122. // the correction work to both children, but also apply the
  123. // (combined) parity bit to just the right child. Then for
  124. // unequal nodes (where the flag bit is different), exactly one
  125. // of the four children (two for P0 and two for P1) will have
  126. // the parity bit applied, which will set the XOR of the lsb of
  127. // those four nodes to just L0^R0^L1^R1^our_parity^peer_parity
  128. // = 1 because everything cancels out except player (for which
  129. // one player is 0 and the other is 1).
  130. bool our_parity_bit = get_lsb(L ^ R) ^ !!player;
  131. DPFnode our_parity = lsb128_mask[our_parity_bit];
  132. DPFnode CW;
  133. bool peer_parity_bit;
  134. // Exchange the parities and do mpc_reconstruct_choice at the
  135. // same time (bundled into the same rounds)
  136. std::vector<coro_t> coroutines;
  137. coroutines.emplace_back(
  138. [&](yield_t &yield) {
  139. tio.queue_peer(&our_parity_bit, 1);
  140. yield();
  141. tio.recv_peer(&peer_parity_bit, 1);
  142. });
  143. coroutines.emplace_back(
  144. [&](yield_t &yield) {
  145. mpc_reconstruct_choice(tio, yield, CW, bs_choice,
  146. (R ^ our_parity), L);
  147. });
  148. run_coroutines(yield, coroutines);
  149. bool parity_bit = our_parity_bit ^ peer_parity_bit;
  150. cfbits |= (size_t(parity_bit)<<level);
  151. DPFnode CWR = CW ^ lsb128_mask[parity_bit];
  152. if (player < 2) {
  153. if (level < depth-1) {
  154. for(size_t i=0;i<curlevel_size;++i) {
  155. bool flag = get_lsb(curlevel[i]);
  156. nextlevel[2*i] = xor_if(nextlevel[2*i], CW, flag);
  157. nextlevel[2*i+1] = xor_if(nextlevel[2*i+1], CWR, flag);
  158. }
  159. } else {
  160. // Recall there are four potentially useful vectors that
  161. // can come out of a DPF:
  162. // - (single-bit) bitwise unit vector
  163. // - additive-shared unit vector
  164. // - XOR-shared scaled unit vector
  165. // - additive-shared scaled unit vector
  166. //
  167. // (No single DPF should be used for both of the first
  168. // two or both of the last two, though, since they're
  169. // correlated; you _can_ use one of the first two and
  170. // one of the last two.)
  171. //
  172. // For each 128-bit leaf, the low bit is the flag bit,
  173. // and we're guaranteed that the flag bits (and indeed
  174. // the whole 128-bit value) for P0 and P1 are the same
  175. // for every leaf except the target, and that the flag
  176. // bits definitely differ for the target (and the other
  177. // 127 bits are independently random on each side).
  178. //
  179. // We divide the 128-bit leaf into a low 64-bit word and
  180. // a high 64-bit word. We use the low word for the unit
  181. // vector and the high word for the scaled vector; this
  182. // choice is not arbitrary: the flag bit in the low word
  183. // means that the sum of all the low words (with P1's
  184. // low words negated) across both P0 and P1 is
  185. // definitely odd, so we can compute that sum's inverse
  186. // mod 2^64, and store it now during precomputation. At
  187. // evaluation time for the additive-shared unit vector,
  188. // we will output this global inverse times the low word
  189. // of each leaf, which will make the sum of all of those
  190. // values 1.
  191. //
  192. // For the scaled vector, we just have to compute shares
  193. // of what the scaled vector is a sharing _of_, but
  194. // that's just XORing or adding all of each party's
  195. // local high words; no communication needed.
  196. value_t low_sum = 0;
  197. value_t high_sum = 0;
  198. value_t high_xor = 0;
  199. for(size_t i=0;i<curlevel_size;++i) {
  200. bool flag = get_lsb(curlevel[i]);
  201. DPFnode leftchild = xor_if(nextlevel[2*i], CW, flag);
  202. DPFnode rightchild = xor_if(nextlevel[2*i+1], CWR, flag);
  203. value_t leftlow = value_t(_mm_cvtsi128_si64x(leftchild));
  204. value_t rightlow = value_t(_mm_cvtsi128_si64x(rightchild));
  205. value_t lefthigh =
  206. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(leftchild,8)));
  207. value_t righthigh =
  208. value_t(_mm_cvtsi128_si64x(_mm_srli_si128(rightchild,8)));
  209. low_sum += (leftlow + rightlow);
  210. high_sum += (lefthigh + righthigh);
  211. high_xor ^= (lefthigh ^ righthigh);
  212. }
  213. if (player == 1) {
  214. low_sum = -low_sum;
  215. high_sum = -high_sum;
  216. }
  217. scaled_sum.ashare = high_sum;
  218. scaled_xor.xshare = high_xor;
  219. // Exchange low_sum and add them up
  220. tio.queue_peer(&low_sum, sizeof(low_sum));
  221. yield();
  222. value_t peer_low_sum;
  223. tio.recv_peer(&peer_low_sum, sizeof(peer_low_sum));
  224. low_sum += peer_low_sum;
  225. // The low_sum had better be odd
  226. assert(low_sum & 1);
  227. unit_sum_inverse = inverse_value_t(low_sum);
  228. }
  229. cw.push_back(CW);
  230. }
  231. ++level;
  232. }
  233. delete[] curlevel;
  234. delete[] nextlevel;
  235. }