mpcops.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #ifndef __MPCOPS_HPP__
  2. #define __MPCOPS_HPP__
  3. #include "types.hpp"
  4. #include "mpcio.hpp"
  5. #include "coroutine.hpp"
  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. RegAS &z, RegAS x, RegAS y,
  15. nbits_t nbits = VALUE_BITS);
  16. // P0 and P1 both hold additive shares of x (shares are x0 and x1) and y
  17. // (shares are y0 and y1); compute additive shares of z = x0*y1 + y0*x1.
  18. // x, y, and z are each at most nbits bits long.
  19. //
  20. // Cost:
  21. // 2 words sent in 1 message
  22. // consumes 1 MultTriple
  23. void mpc_cross(MPCTIO &tio, yield_t &yield,
  24. RegAS &z, RegAS x, RegAS y,
  25. nbits_t nbits = VALUE_BITS);
  26. // P0 holds the (complete) value x, P1 holds the (complete) value y;
  27. // compute additive shares of z = x*y. x, y, and z are each at most
  28. // nbits bits long. The parameter is called x, but P1 will pass y
  29. // there. When called by another task during preprocessing, set tally
  30. // to false so that the required halftriples aren't accounted for
  31. // separately from the main preprocessing task.
  32. //
  33. // Cost:
  34. // 1 word sent in 1 message
  35. // consumes 1 HalfTriple
  36. void mpc_valuemul(MPCTIO &tio, yield_t &yield,
  37. RegAS &z, value_t x,
  38. nbits_t nbits = VALUE_BITS, bool tally = true);
  39. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and additive
  40. // shares y0 and y1 of the value y; compute additive shares of
  41. // z = f * y = (f0 XOR f1) * (y0 + y1). y and z are each at most nbits
  42. // bits long.
  43. //
  44. // Cost:
  45. // 2 words sent in 1 message
  46. // consumes 1 MultTriple
  47. void mpc_flagmult(MPCTIO &tio, yield_t &yield,
  48. RegAS &z, RegBS f, RegAS y,
  49. nbits_t nbits = VALUE_BITS);
  50. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and additive
  51. // shares of the values x and y; compute additive shares of z, where
  52. // z = x if f=0 and z = y if f=1. x, y, and z are each at most nbits
  53. // bits long.
  54. //
  55. // Cost:
  56. // 2 words sent in 1 message
  57. // consumes 1 MultTriple
  58. void mpc_select(MPCTIO &tio, yield_t &yield,
  59. RegAS &z, RegBS f, RegAS x, RegAS y,
  60. nbits_t nbits = VALUE_BITS);
  61. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and XOR
  62. // shares of the values x and y; compute XOR shares of z, where z = x if
  63. // f=0 and z = y if f=1. x, y, and z are each at most nbits bits long.
  64. //
  65. // Cost:
  66. // 2 words sent in 1 message
  67. // consumes 1 SelectTriple
  68. void mpc_select(MPCTIO &tio, yield_t &yield,
  69. RegXS &z, RegBS f, RegXS x, RegXS y,
  70. nbits_t nbits = VALUE_BITS);
  71. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and bit
  72. // shares of the values x and y; compute bit shares of z, where
  73. // z = x if f=0 and z = y if f=1.
  74. //
  75. // Cost:
  76. // 1 byte sent in 1 message
  77. // consumes 1/64 AndTriple
  78. void mpc_select(MPCTIO &tio, yield_t &yield,
  79. RegBS &z, RegBS f, RegBS x, RegBS y);
  80. // P0 and P1 hold bit shares f0 and f1 of the single bit f, and additive
  81. // shares of the values x and y. Obliviously swap x and y; that is,
  82. // replace x and y with new additive sharings of x and y respectively
  83. // (if f=0) or y and x respectively (if f=1). x and y are each at most
  84. // nbits bits long.
  85. //
  86. // Cost:
  87. // 2 words sent in 1 message
  88. // consumes 1 MultTriple
  89. void mpc_oswap(MPCTIO &tio, yield_t &yield,
  90. RegAS &x, RegAS &y, RegBS f,
  91. nbits_t nbits = VALUE_BITS);
  92. // P0 and P1 hold XOR shares of x. Compute additive shares of the same
  93. // x. x is at most nbits bits long. When called by another task during
  94. // preprocessing, set tally to false so that the required halftriples
  95. // aren't accounted for separately from the main preprocessing task.
  96. //
  97. // Cost:
  98. // nbits-1 words sent in 1 message
  99. // consumes nbits-1 HalfTriples
  100. void mpc_xs_to_as(MPCTIO &tio, yield_t &yield,
  101. RegAS &as_x, RegXS xs_x,
  102. nbits_t nbits = VALUE_BITS, bool tally = true);
  103. // P0 and P1 hold XOR shares x0 and x1 of x. x is at most nbits bits
  104. // long. Return x to P0 and P1 (and 0 to P2).
  105. //
  106. // Cost: 1 word sent in 1 message
  107. value_t mpc_reconstruct(MPCTIO &tio, yield_t &yield,
  108. RegXS f, nbits_t nbits = VALUE_BITS);
  109. // P0 and P1 hold additive shares x0 and x1 of x. x is at most nbits
  110. // bits long. Return x to P0 and P1 (and 0 to P2).
  111. //
  112. // Cost: 1 word sent in 1 message
  113. value_t mpc_reconstruct(MPCTIO &tio, yield_t &yield,
  114. RegAS x, nbits_t nbits = VALUE_BITS);
  115. // P0 and P1 hold bit shares f0 and f1 of f. Return f to P0 and P1 (and
  116. // 0 to P2).
  117. //
  118. // Cost: 1 word sent in 1 message
  119. bool mpc_reconstruct(MPCTIO &tio, yield_t &yield, RegBS x);
  120. // P0 and P1 hold bit shares of f, and DPFnode XOR shares x0,y0 and
  121. // x1,y1 of x and y. Set z to x=x0^x1 if f=0 and to y=y0^y1 if f=1.
  122. //
  123. // Cost:
  124. // 6 64-bit words sent in 2 messages
  125. // consumes one AndTriple
  126. void mpc_reconstruct_choice(MPCTIO &tio, yield_t &yield,
  127. DPFnode &z, RegBS f, DPFnode x, DPFnode y);
  128. // As above, but for arrays of DPFnode
  129. //
  130. // Cost:
  131. // 6*LWIDTH 64-bit words sent in 2 messages
  132. // consumes LWIDTH AndTriples
  133. template <size_t LWIDTH>
  134. void mpc_reconstruct_choice(MPCTIO &tio, yield_t &yield,
  135. std::array<DPFnode,LWIDTH> &z, RegBS f,
  136. const std::array<DPFnode,LWIDTH> &x,
  137. const std::array<DPFnode,LWIDTH> &y);
  138. // P0 and P1 hold bit shares of x and y. Set z to bit shares of x & y.
  139. //
  140. // Cost:
  141. // 1 byte sent in 1 message
  142. // consumes 1/64 AndTriple
  143. void mpc_and(MPCTIO &tio, yield_t &yield,
  144. RegBS &z, RegBS x, RegBS y);
  145. // P0 and P1 hold bit shares of x and y. Set z to bit shares of x | y.
  146. //
  147. // Cost:
  148. // 1 byte sent in 1 message
  149. // consumes 1/64 AndTriple
  150. void mpc_or(MPCTIO &tio, yield_t &yield,
  151. RegBS &z, RegBS x, RegBS y);
  152. #include "mpcops.tcc"
  153. #endif