mpcops.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "mpcops.hpp"
  2. #include "bitutils.hpp"
  3. // P0 and P1 both hold additive shares of x (shares are x0 and x1) and y
  4. // (shares are y0 and y1); compute additive shares of z = x*y =
  5. // (x0+x1)*(y0+y1). x, y, and z are each at most nbits bits long.
  6. //
  7. // Cost:
  8. // 2 words sent in 1 message
  9. // consumes 1 MultTriple
  10. void mpc_mul(MPCTIO &tio, yield_t &yield,
  11. RegAS &z, RegAS x, RegAS y,
  12. nbits_t nbits)
  13. {
  14. const value_t mask = MASKBITS(nbits);
  15. // Compute z to be an additive share of (x0*y1+y0*x1)
  16. mpc_cross(tio, yield, z, x, y, nbits);
  17. // Add x0*y0 (the peer will add x1*y1)
  18. z.ashare = (z.ashare + x.ashare * y.ashare) & mask;
  19. }
  20. // P0 and P1 both hold additive shares of x (shares are x0 and x1) and y
  21. // (shares are y0 and y1); compute additive shares of z = x0*y1 + y0*x1.
  22. // x, y, and z are each at most nbits bits long.
  23. //
  24. // Cost:
  25. // 2 words sent in 1 message
  26. // consumes 1 MultTriple
  27. void mpc_cross(MPCTIO &tio, yield_t &yield,
  28. RegAS &z, RegAS x, RegAS y,
  29. nbits_t nbits)
  30. {
  31. const value_t mask = MASKBITS(nbits);
  32. size_t nbytes = BITBYTES(nbits);
  33. auto [X, Y, Z] = tio.triple();
  34. // Send x+X and y+Y
  35. value_t blind_x = (x.ashare + X) & mask;
  36. value_t blind_y = (y.ashare + Y) & mask;
  37. tio.queue_peer(&blind_x, nbytes);
  38. tio.queue_peer(&blind_y, nbytes);
  39. yield();
  40. // Read the peer's x+X and y+Y
  41. value_t peer_blind_x=0, peer_blind_y=0;
  42. tio.recv_peer(&peer_blind_x, nbytes);
  43. tio.recv_peer(&peer_blind_y, nbytes);
  44. z.ashare = ((x.ashare * peer_blind_y) - (Y * peer_blind_x) + Z) & mask;
  45. }
  46. // P0 holds the (complete) value x, P1 holds the (complete) value y;
  47. // compute additive shares of z = x*y. x, y, and z are each at most
  48. // nbits bits long. The parameter is called x, but P1 will pass y
  49. // there.
  50. //
  51. // Cost:
  52. // 1 word sent in 1 message
  53. // consumes 1 HalfTriple
  54. void mpc_valuemul(MPCTIO &tio, yield_t &yield,
  55. RegAS &z, value_t x,
  56. nbits_t nbits)
  57. {
  58. const value_t mask = MASKBITS(nbits);
  59. size_t nbytes = BITBYTES(nbits);
  60. auto [X, Z] = tio.halftriple();
  61. // Send x+X
  62. value_t blind_x = (x + X) & mask;
  63. tio.queue_peer(&blind_x, nbytes);
  64. yield();
  65. // Read the peer's y+Y
  66. value_t peer_blind_y=0;
  67. tio.recv_peer(&peer_blind_y, nbytes);
  68. if (tio.player() == 0) {
  69. z.ashare = ((x * peer_blind_y) + Z) & mask;
  70. } else if (tio.player() == 1) {
  71. z.ashare = ((-X * peer_blind_y) + Z) & mask;
  72. }
  73. }
  74. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and additive
  75. // shares y0 and y1 of the value y; compute additive shares of
  76. // z = f * y = (f0 XOR f1) * (y0 + y1). y and z are each at most nbits
  77. // bits long.
  78. //
  79. // Cost:
  80. // 2 words sent in 1 message
  81. // consumes 1 MultTriple
  82. void mpc_flagmult(MPCTIO &tio, yield_t &yield,
  83. RegAS &z, RegBS f, RegAS y,
  84. nbits_t nbits)
  85. {
  86. const value_t mask = MASKBITS(nbits);
  87. // Compute additive shares of [(1-2*f0)*y0]*f1 + [(1-2*f1)*y1]*f0
  88. value_t bs_fval = value_t(f.bshare);
  89. RegAS fval;
  90. fval.ashare = bs_fval;
  91. mpc_cross(tio, yield, z, y*(1-2*bs_fval), fval, nbits);
  92. // Add f0*y0 (and the peer will add f1*y1)
  93. z.ashare = (z.ashare + bs_fval*y.ashare) & mask;
  94. // Now the shares add up to:
  95. // [(1-2*f0)*y0]*f1 + [(1-2*f1)*y1]*f0 + f0*y0 + f1*y1
  96. // which you can rearrange to see that it's equal to the desired
  97. // (f0 + f1 - 2*f0*f1)*(y0+y1), since f0 XOR f1 = (f0 + f1 - 2*f0*f1).
  98. }
  99. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and additive
  100. // shares of the values x and y; compute additive shares of z, where
  101. // z = x if f=0 and z = y if f=1. x, y, and z are each at most nbits
  102. // bits long.
  103. //
  104. // Cost:
  105. // 2 words sent in 1 message
  106. // consumes 1 MultTriple
  107. void mpc_select(MPCTIO &tio, yield_t &yield,
  108. RegAS &z, RegBS f, RegAS x, RegAS y,
  109. nbits_t nbits)
  110. {
  111. const value_t mask = MASKBITS(nbits);
  112. // The desired result is z = x + f * (y-x)
  113. mpc_flagmult(tio, yield, z, f, y-x, nbits);
  114. z.ashare = (z.ashare + x.ashare) & mask;
  115. }
  116. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and additive
  117. // shares of the values x and y. Obliviously swap x and y; that is,
  118. // replace x and y with new additive sharings of x and y respectively
  119. // (if f=0) or y and x respectively (if f=1). x and y are each at most
  120. // nbits bits long.
  121. //
  122. // Cost:
  123. // 2 words sent in 1 message
  124. // consumes 1 MultTriple
  125. void mpc_oswap(MPCTIO &tio, yield_t &yield,
  126. RegAS &x, RegAS &y, RegBS f,
  127. nbits_t nbits)
  128. {
  129. const value_t mask = MASKBITS(nbits);
  130. // Let s = f*(y-x). Then the desired result is
  131. // x <- x + s, y <- y - s.
  132. RegAS s;
  133. mpc_flagmult(tio, yield, s, f, y-x, nbits);
  134. x.ashare = (x.ashare + s.ashare) & mask;
  135. y.ashare = (y.ashare - s.ashare) & mask;
  136. }
  137. // P0 and P1 hold XOR shares of x. Compute additive shares of the same
  138. // x. x is at most nbits bits long.
  139. //
  140. // Cost:
  141. // nbits-1 words sent in 1 message
  142. // consumes nbits-1 HalfTriples
  143. void mpc_xs_to_as(MPCTIO &tio, yield_t &yield,
  144. RegAS &as_x, RegXS xs_x,
  145. nbits_t nbits)
  146. {
  147. const value_t mask = MASKBITS(nbits);
  148. // We use the fact that for any nbits-bit A and B,
  149. // A+B = (A XOR B) + 2*(A AND B) mod 2^nbits
  150. // so if we have additive shares C0 and C1 of 2*(A AND B)
  151. // (so C0 + C1 = 2*(A AND B)), then (A-C0) and (B-C1) are
  152. // additive shares of (A XOR B).
  153. // To get additive shares of 2*(A AND B) (mod 2^nbits), we first
  154. // note that we can ignore the top bits of A and B, since the
  155. // multiplication by 2 will shift it out of the nbits-bit range.
  156. // For the other bits, use valuemult to get the product of the
  157. // corresponding bit i of A and B (i=0..nbits-2), and compute
  158. // C = \sum_i 2^{i+1} * (A_i * B_i).
  159. // This can all be done in a single message, using the coroutine
  160. // mechanism to have all nbits-1 instances of valuemult queue their
  161. // message, then yield, so that all of their messages get sent at
  162. // once, then each will read their results.
  163. RegAS as_bitand[nbits-1];
  164. std::vector<coro_t> coroutines;
  165. for (nbits_t i=0; i<nbits-1; ++i) {
  166. coroutines.emplace_back(
  167. [&](yield_t &yield) {
  168. mpc_valuemul(tio, yield, as_bitand[i], (xs_x.xshare>>i)&1, nbits);
  169. });
  170. }
  171. run_coroutines(yield, coroutines);
  172. value_t as_C = 0;
  173. for (nbits_t i=0; i<nbits-1; ++i) {
  174. as_C += (as_bitand[i].ashare<<(i+1));
  175. }
  176. as_x.ashare = (xs_x.xshare - as_C) & mask;
  177. }
  178. // P0 and P1 hold bit shares of f, and DPFnode XOR shares x0,y0 and
  179. // x1,y1 of x and y. Set z to x=x0^x1 if f=0 and to y=y0^y1 if f=1.
  180. //
  181. // Cost:
  182. // 6 64-bit words sent in 2 messages
  183. // consumes one AndTriple
  184. void mpc_reconstruct_choice(MPCTIO &tio, yield_t &yield,
  185. DPFnode &z, RegBS f, DPFnode x, DPFnode y)
  186. {
  187. // Sign-extend f (so 0 -> 0000...0; 1 -> 1111...1)
  188. DPFnode fext = if128_mask[f.bshare];
  189. // Compute XOR shares of f & (x ^ y)
  190. auto [X, Y, Z] = tio.selecttriple();
  191. bit_t blind_f = f.bshare ^ X;
  192. DPFnode d = x ^ y;
  193. DPFnode blind_d = d ^ Y;
  194. // Send the blinded values
  195. tio.queue_peer(&blind_f, sizeof(blind_f));
  196. tio.queue_peer(&blind_d, sizeof(blind_d));
  197. yield();
  198. // Read the peer's values
  199. bit_t peer_blind_f = 0;
  200. DPFnode peer_blind_d;
  201. tio.recv_peer(&peer_blind_f, sizeof(peer_blind_f));
  202. tio.recv_peer(&peer_blind_d, sizeof(peer_blind_d));
  203. // Compute _our share_ of f ? x : y = (f * (x ^ y))^x
  204. DPFnode peer_blind_fext = if128_mask[peer_blind_f];
  205. DPFnode zshare =
  206. (fext & peer_blind_d) ^ (Y & peer_blind_fext) ^
  207. (fext & d) ^ (Z ^ x);
  208. // Now exchange shares
  209. tio.queue_peer(&zshare, sizeof(zshare));
  210. yield();
  211. DPFnode peer_zshare;
  212. tio.recv_peer(&peer_zshare, sizeof(peer_zshare));
  213. z = zshare ^ peer_zshare;
  214. }