mpcops.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 and y; compute additive
  7. // shares of z = x*y. x, y, and z are each at most nbits bits long.
  8. //
  9. // Cost:
  10. // 1 word sent in 1 message
  11. // consumes 1 MultTriple
  12. void mpc_mul(MPCIO &mpcio, size_t thread_num, yield_t &yield,
  13. value_t &as_z, value_t as_x, value_t as_y,
  14. MultTriple &T, nbits_t nbits)
  15. {
  16. value_t mask = MASKBITS(nbits);
  17. size_t nbytes = BITBYTES(nbits);
  18. auto [X, Y, Z] = T;
  19. // Send x+X and y+Y
  20. value_t blind_x = (as_x + X) & mask;
  21. value_t blind_y = (as_y + Y) & mask;
  22. mpcio.peerios[thread_num].queue(&blind_x, nbytes);
  23. mpcio.peerios[thread_num].queue(&blind_y, nbytes);
  24. yield();
  25. // Read the peer's x+X and y+Y
  26. value_t peer_blind_x, peer_blind_y;
  27. mpcio.peerios[thread_num].recv(&peer_blind_x, nbytes);
  28. mpcio.peerios[thread_num].recv(&peer_blind_y, nbytes);
  29. as_z = ((as_x * (as_y + peer_blind_y)) - Y * peer_blind_x + Z) & mask;
  30. }
  31. // P0 holds the (complete) value x, P1 holds the (complete) value y;
  32. // compute additive shares of z = x*y. x, y, and z are each at most
  33. // nbits bits long. The parameter is called x, but P1 will pass y
  34. // there.
  35. //
  36. // Cost:
  37. // 1 word sent in 1 message
  38. // consumes 1 HalfTriple
  39. void mpc_valuemul(MPCIO &mpcio, size_t thread_num, yield_t &yield,
  40. value_t &as_z, value_t x,
  41. HalfTriple &H, nbits_t nbits)
  42. {
  43. value_t mask = MASKBITS(nbits);
  44. size_t nbytes = BITBYTES(nbits);
  45. auto [X, Z] = H;
  46. // Send x+X
  47. value_t blind_x = (x + X) & mask;
  48. mpcio.peerios[thread_num].queue(&blind_x, nbytes);
  49. yield();
  50. // Read the peer's y+Y
  51. value_t peer_blind_y;
  52. mpcio.peerios[thread_num].recv(&peer_blind_y, nbytes);
  53. if (mpcio.player == 0) {
  54. as_z = ((x * peer_blind_y) + Z) & mask;
  55. } else if (mpcio.player == 1) {
  56. as_z = ((-X * peer_blind_y) + Z) & mask;
  57. }
  58. }