types.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #ifndef __OBLIVDS_TYPES_HPP__
  2. #define __OBLIVDS_TYPES_HPP__
  3. #include <tuple>
  4. #include <cstdint>
  5. #include <x86intrin.h> // SSE and AVX intrinsics
  6. #include <bsd/stdlib.h> // arc4random_buf
  7. // The number of bits in an MPC secret-shared memory word
  8. #ifndef VALUE_BITS
  9. #define VALUE_BITS 64
  10. #endif
  11. // Values in MPC secret-shared memory are of this type.
  12. // This is the type of the underlying shared value, not the types of the
  13. // shares themselves.
  14. #if VALUE_BITS == 64
  15. using value_t = uint64_t;
  16. #elif VALUE_BITS == 32
  17. using value_t = uint32_t;
  18. #else
  19. #error "Unsupported value of VALUE_BITS"
  20. #endif
  21. // Secret-shared bits are of this type. Note that it is standards
  22. // compliant to treat a bool as an unsigned integer type with values 0
  23. // and 1.
  24. using bit_t = bool;
  25. // Counts of the number of bits in a value are of this type, which must
  26. // be large enough to store the _value_ VALUE_BITS
  27. using nbits_t = uint8_t;
  28. // Convert a number of bits to the number of bytes required to store (or
  29. // more to the point, send) them.
  30. #define BITBYTES(nbits) (((nbits)+7)>>3)
  31. // A mask of this many bits; the test is to prevent 1<<nbits from
  32. // overflowing if nbits == VALUE_BITS
  33. #define MASKBITS(nbits) (((nbits) < VALUE_BITS) ? (value_t(1)<<(nbits))-1 : ~0)
  34. // The type of a register holding an additive share of a value
  35. struct RegAS {
  36. value_t ashare;
  37. // Set each side's share to a random value nbits bits long
  38. inline void randomize(size_t nbits = VALUE_BITS) {
  39. value_t mask = MASKBITS(nbits);
  40. arc4random_buf(&ashare, sizeof(ashare));
  41. ashare &= mask;
  42. }
  43. inline RegAS &operator+=(RegAS &rhs) {
  44. this->ashare += rhs.ashare;
  45. return *this;
  46. }
  47. inline RegAS operator+(RegAS &rhs) const {
  48. RegAS res = *this;
  49. res += rhs;
  50. return res;
  51. }
  52. inline RegAS &operator-=(RegAS &rhs) {
  53. this->ashare -= rhs.ashare;
  54. return *this;
  55. }
  56. inline RegAS operator-(RegAS &rhs) const {
  57. RegAS res = *this;
  58. res -= rhs;
  59. return res;
  60. }
  61. inline RegAS &operator*=(value_t rhs) {
  62. this->ashare *= rhs;
  63. return *this;
  64. }
  65. inline RegAS operator*(value_t rhs) const {
  66. RegAS res = *this;
  67. res *= rhs;
  68. return res;
  69. }
  70. inline RegAS &operator&=(value_t mask) {
  71. this->ashare &= mask;
  72. return *this;
  73. }
  74. inline RegAS operator&(value_t mask) const {
  75. RegAS res = *this;
  76. res &= mask;
  77. return res;
  78. }
  79. };
  80. // The type of a register holding a bit share
  81. struct RegBS {
  82. bit_t bshare;
  83. // Set each side's share to a random bit
  84. inline void randomize() {
  85. unsigned char randb;
  86. arc4random_buf(&randb, sizeof(randb));
  87. bshare = randb & 1;
  88. }
  89. };
  90. // The type of a register holding an XOR share of a value
  91. struct RegXS {
  92. value_t xshare;
  93. // Set each side's share to a random value nbits bits long
  94. inline void randomize(size_t nbits = VALUE_BITS) {
  95. value_t mask = MASKBITS(nbits);
  96. arc4random_buf(&xshare, sizeof(xshare));
  97. xshare &= mask;
  98. }
  99. inline RegXS &operator^=(RegXS &rhs) {
  100. this->xshare ^= rhs.xshare;
  101. return *this;
  102. }
  103. inline RegXS operator^(RegXS &rhs) const {
  104. RegXS res = *this;
  105. res ^= rhs;
  106. return res;
  107. }
  108. inline RegXS &operator&=(value_t mask) {
  109. this->xshare &= mask;
  110. return *this;
  111. }
  112. inline RegXS operator&(value_t mask) const {
  113. RegXS res = *this;
  114. res &= mask;
  115. return res;
  116. }
  117. // Extract a bit share of bit bitnum of the XOR-shared register
  118. inline RegBS bit(nbits_t bitnum) const {
  119. RegBS bs;
  120. bs.bshare = !!(xshare & (value_t(1)<<bitnum));
  121. return bs;
  122. }
  123. };
  124. // The _maximum_ number of bits in an MPC address; the actual size of
  125. // the memory will typically be set at runtime, but it cannot exceed
  126. // this value. It is more efficient (in terms of communication) in some
  127. // places for this value to be at most 32.
  128. #ifndef ADDRESS_MAX_BITS
  129. #define ADDRESS_MAX_BITS 32
  130. #endif
  131. // Addresses of MPC secret-shared memory are of this type
  132. #if ADDRESS_MAX_BITS <= 32
  133. using address_t = uint32_t;
  134. #elif ADDRESS_MAX_BITS <= 64
  135. using address_t = uint64_t;
  136. #else
  137. #error "Unsupported value of ADDRESS_MAX_BITS"
  138. #endif
  139. #if ADDRESS_MAX_BITS > VALUE_BITS
  140. #error "VALUE_BITS must be at least as large as ADDRESS_MAX_BITS"
  141. #endif
  142. // A multiplication triple is a triple (X0,Y0,Z0) held by P0 (and
  143. // correspondingly (X1,Y1,Z1) held by P1), with all values random,
  144. // but subject to the relation that X0*Y1 + Y0*X1 = Z0+Z1
  145. using MultTriple = std::tuple<value_t, value_t, value_t>;
  146. // A half-triple is (X0,Z0) held by P0 (and correspondingly (Y1,Z1) held
  147. // by P1), with all values random, but subject to the relation that
  148. // X0*Y1 = Z0+Z1
  149. using HalfTriple = std::tuple<value_t, value_t>;
  150. // The type of nodes in a DPF. This must be at least as many bits as
  151. // the security parameter, and at least twice as many bits as value_t.
  152. using DPFnode = __m128i;
  153. // A Select triple is a triple of (X0,Y0,Z0) where X0 is a bit and Y0
  154. // and Z0 are DPFnodes held by P0 (and correspondingly (X1,Y1,Z1) held
  155. // by P1), with all values random, but subject to the relation that
  156. // (X0*Y1) ^ (Y0*X1) = Z0^Z1. These are only used while creating RDPFs
  157. // in the preprocessing phase, so we never need to store them. This is
  158. // a struct instead of a tuple for alignment reasons.
  159. struct SelectTriple {
  160. bit_t X;
  161. DPFnode Y, Z;
  162. };
  163. // These are defined in rdpf.hpp, but declared here to avoid cyclic
  164. // header dependencies.
  165. struct RDPFPair;
  166. struct RDPFTriple;
  167. // We want the I/O (using << and >>) for many classes
  168. // to just be a common thing: write out the bytes
  169. // straight from memory
  170. #define DEFAULT_IO(CLASSNAME) \
  171. template <typename T> \
  172. T& operator>>(T& is, CLASSNAME &x) \
  173. { \
  174. is.read((char *)&x, sizeof(x)); \
  175. return is; \
  176. } \
  177. \
  178. template <typename T> \
  179. T& operator<<(T& os, const CLASSNAME &x) \
  180. { \
  181. os.write((const char *)&x, sizeof(x)); \
  182. return os; \
  183. }
  184. // Default I/O for various types
  185. DEFAULT_IO(RegBS)
  186. DEFAULT_IO(RegAS)
  187. DEFAULT_IO(RegXS)
  188. DEFAULT_IO(MultTriple)
  189. DEFAULT_IO(HalfTriple)
  190. #endif