mpcops.cpp 6.4 KB

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