types.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 an XOR share of a value
  81. struct RegXS {
  82. value_t xshare;
  83. // Set each side's share to a random value nbits bits long
  84. inline void randomize(size_t nbits = VALUE_BITS) {
  85. value_t mask = MASKBITS(nbits);
  86. arc4random_buf(&xshare, sizeof(xshare));
  87. xshare &= mask;
  88. }
  89. inline RegXS &operator^=(RegXS &rhs) {
  90. this->xshare ^= rhs.xshare;
  91. return *this;
  92. }
  93. inline RegXS operator^(RegXS &rhs) const {
  94. RegXS res = *this;
  95. res ^= rhs;
  96. return res;
  97. }
  98. inline RegXS &operator&=(value_t mask) {
  99. this->xshare &= mask;
  100. return *this;
  101. }
  102. inline RegXS operator&(value_t mask) const {
  103. RegXS res = *this;
  104. res &= mask;
  105. return res;
  106. }
  107. };
  108. // The type of a register holding a bit share
  109. struct RegBS {
  110. bit_t bshare;
  111. // Set each side's share to a random bit
  112. inline void randomize() {
  113. arc4random_buf(&bshare, sizeof(bshare));
  114. bshare &= 1;
  115. }
  116. };
  117. // The _maximum_ number of bits in an MPC address; the actual size of
  118. // the memory will typically be set at runtime, but it cannot exceed
  119. // this value. It is more efficient (in terms of communication) in some
  120. // places for this value to be at most 32.
  121. #ifndef ADDRESS_MAX_BITS
  122. #define ADDRESS_MAX_BITS 32
  123. #endif
  124. // Addresses of MPC secret-shared memory are of this type
  125. #if ADDRESS_MAX_BITS <= 32
  126. using address_t = uint32_t;
  127. #elif ADDRESS_MAX_BITS <= 64
  128. using address_t = uint64_t;
  129. #else
  130. #error "Unsupported value of ADDRESS_MAX_BITS"
  131. #endif
  132. #if ADDRESS_MAX_BITS > VALUE_BITS
  133. #error "VALUE_BITS must be at least as large as ADDRESS_MAX_BITS"
  134. #endif
  135. // A multiplication triple is a triple (X0,Y0,Z0) held by P0 (and
  136. // correspondingly (X1,Y1,Z1) held by P1), with all values random,
  137. // but subject to the relation that X0*Y1 + Y0*X1 = Z0+Z1
  138. using MultTriple = std::tuple<value_t, value_t, value_t>;
  139. // A half-triple is (X0,Z0) held by P0 (and correspondingly (Y1,Z1) held
  140. // by P1), with all values random, but subject to the relation that
  141. // X0*Y1 = Z0+Z1
  142. using HalfTriple = std::tuple<value_t, value_t>;
  143. // The type of nodes in a DPF
  144. using DPFnode = __m128i;
  145. struct RDPF {
  146. DPFnode seed;
  147. };
  148. #endif