mpcops.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 additive
  72. // shares of the values x and y. Obliviously swap x and y; that is,
  73. // replace x and y with new additive sharings of x and y respectively
  74. // (if f=0) or y and x respectively (if f=1). x and y are each at most
  75. // nbits bits long.
  76. //
  77. // Cost:
  78. // 2 words sent in 1 message
  79. // consumes 1 MultTriple
  80. void mpc_oswap(MPCTIO &tio, yield_t &yield,
  81. RegAS &x, RegAS &y, RegBS f,
  82. nbits_t nbits = VALUE_BITS);
  83. // P0 and P1 hold XOR shares of x. Compute additive shares of the same
  84. // x. x is at most nbits bits long. When called by another task during
  85. // preprocessing, set tally to false so that the required halftriples
  86. // aren't accounted for separately from the main preprocessing task.
  87. //
  88. // Cost:
  89. // nbits-1 words sent in 1 message
  90. // consumes nbits-1 HalfTriples
  91. void mpc_xs_to_as(MPCTIO &tio, yield_t &yield,
  92. RegAS &as_x, RegXS xs_x,
  93. nbits_t nbits = VALUE_BITS, bool tally = true);
  94. // P0 and P1 hold bit shares of f, and DPFnode XOR shares x0,y0 and
  95. // x1,y1 of x and y. Set z to x=x0^x1 if f=0 and to y=y0^y1 if f=1.
  96. //
  97. // Cost:
  98. // 6 64-bit words sent in 2 messages
  99. // consumes one AndTriple
  100. void mpc_reconstruct_choice(MPCTIO &tio, yield_t &yield,
  101. DPFnode &z, RegBS f, DPFnode x, DPFnode y);
  102. // As above, but for arrays of DPFnode
  103. //
  104. // Cost:
  105. // 6*LWIDTH 64-bit words sent in 2 messages
  106. // consumes LWIDTH AndTriples
  107. template <size_t LWIDTH>
  108. void mpc_reconstruct_choice(MPCTIO &tio, yield_t &yield,
  109. std::array<DPFnode,LWIDTH> &z, RegBS f,
  110. const std::array<DPFnode,LWIDTH> &x,
  111. const std::array<DPFnode,LWIDTH> &y);
  112. // P0 and P1 hold bit shares of x and y. Set z to bit shares of x & y.
  113. //
  114. // Cost:
  115. // 1 byte sent in 1 message
  116. // consumes 1/64 AndTriple
  117. void mpc_and(MPCTIO &tio, yield_t &yield,
  118. RegBS &z, RegBS x, RegBS y);
  119. // P0 and P1 hold bit shares of x and y. Set z to bit shares of x | y.
  120. //
  121. // Cost:
  122. // 1 byte sent in 1 message
  123. // consumes 1/64 AndTriple
  124. void mpc_or(MPCTIO &tio, yield_t &yield,
  125. RegBS &z, RegBS x, RegBS y);
  126. #include "mpcops.tcc"
  127. #endif