types.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. arc4random_buf(&bshare, sizeof(bshare));
  86. bshare &= 1;
  87. }
  88. };
  89. // The type of a register holding an XOR share of a value
  90. struct RegXS {
  91. value_t xshare;
  92. // Set each side's share to a random value nbits bits long
  93. inline void randomize(size_t nbits = VALUE_BITS) {
  94. value_t mask = MASKBITS(nbits);
  95. arc4random_buf(&xshare, sizeof(xshare));
  96. xshare &= mask;
  97. }
  98. inline RegXS &operator^=(RegXS &rhs) {
  99. this->xshare ^= rhs.xshare;
  100. return *this;
  101. }
  102. inline RegXS operator^(RegXS &rhs) const {
  103. RegXS res = *this;
  104. res ^= rhs;
  105. return res;
  106. }
  107. inline RegXS &operator&=(value_t mask) {
  108. this->xshare &= mask;
  109. return *this;
  110. }
  111. inline RegXS operator&(value_t mask) const {
  112. RegXS res = *this;
  113. res &= mask;
  114. return res;
  115. }
  116. // Extract a bit share of bit bitnum of the XOR-shared register
  117. inline RegBS bit(nbits_t bitnum) const {
  118. RegBS bs;
  119. bs.bshare = !!(xshare & (value_t(1)<<bitnum));
  120. return bs;
  121. }
  122. };
  123. // The _maximum_ number of bits in an MPC address; the actual size of
  124. // the memory will typically be set at runtime, but it cannot exceed
  125. // this value. It is more efficient (in terms of communication) in some
  126. // places for this value to be at most 32.
  127. #ifndef ADDRESS_MAX_BITS
  128. #define ADDRESS_MAX_BITS 32
  129. #endif
  130. // Addresses of MPC secret-shared memory are of this type
  131. #if ADDRESS_MAX_BITS <= 32
  132. using address_t = uint32_t;
  133. #elif ADDRESS_MAX_BITS <= 64
  134. using address_t = uint64_t;
  135. #else
  136. #error "Unsupported value of ADDRESS_MAX_BITS"
  137. #endif
  138. #if ADDRESS_MAX_BITS > VALUE_BITS
  139. #error "VALUE_BITS must be at least as large as ADDRESS_MAX_BITS"
  140. #endif
  141. // A multiplication triple is a triple (X0,Y0,Z0) held by P0 (and
  142. // correspondingly (X1,Y1,Z1) held by P1), with all values random,
  143. // but subject to the relation that X0*Y1 + Y0*X1 = Z0+Z1
  144. using MultTriple = std::tuple<value_t, value_t, value_t>;
  145. // A half-triple is (X0,Z0) held by P0 (and correspondingly (Y1,Z1) held
  146. // by P1), with all values random, but subject to the relation that
  147. // X0*Y1 = Z0+Z1
  148. using HalfTriple = std::tuple<value_t, value_t>;
  149. // The type of nodes in a DPF. This must be at least as many bits as
  150. // the security parameter, and at least twice as many bits as value_t.
  151. using DPFnode = __m128i;
  152. // A Select triple is a triple of (X0,Y0,Z0) where X0 is a bit and Y0
  153. // and Z0 are DPFnodes held by P0 (and correspondingly (X1,Y1,Z1) held
  154. // by P1), with all values random, but subject to the relation that
  155. // (X0*Y1) ^ (Y0*X1) = Z0^Z1. These are only used while creating RDPFs
  156. // in the preprocessing phase, so we never need to store them. This is
  157. // a struct instead of a tuple for alignment reasons.
  158. struct SelectTriple {
  159. bit_t X;
  160. DPFnode Y, Z;
  161. };
  162. #endif