mpcops.cpp 8.1 KB

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