types.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef __OBLIVDS_TYPES_HPP__
  2. #define __OBLIVDS_TYPES_HPP__
  3. #include <tuple>
  4. #include <cstdint>
  5. #include <bsd/stdlib.h> // arc4random_buf
  6. // The number of bits in an MPC secret-shared memory word
  7. #ifndef VALUE_BITS
  8. #define VALUE_BITS 64
  9. #endif
  10. // Values in MPC secret-shared memory are of this type.
  11. // This is the type of the underlying shared value, not the types of the
  12. // shares themselves.
  13. #if VALUE_BITS == 64
  14. using value_t = uint64_t;
  15. #elif VALUE_BITS == 32
  16. using value_t = uint32_t;
  17. #else
  18. #error "Unsupported value of VALUE_BITS"
  19. #endif
  20. // Secret-shared bits are of this type. Note that it is standards
  21. // compliant to treat a bool as an unsigned integer type with values 0
  22. // and 1.
  23. using bit_t = bool;
  24. // Counts of the number of bits in a value are of this type, which must
  25. // be large enough to store the _value_ VALUE_BITS
  26. using nbits_t = uint8_t;
  27. // Convert a number of bits to the number of bytes required to store (or
  28. // more to the point, send) them.
  29. #define BITBYTES(nbits) (((nbits)+7)>>3)
  30. // A mask of this many bits; the test is to prevent 1<<nbits from
  31. // overflowing if nbits == VALUE_BITS
  32. #define MASKBITS(nbits) (((nbits) < VALUE_BITS) ? (value_t(1)<<(nbits))-1 : ~0)
  33. // The type of a register holding an additive share of a value
  34. struct RegAS {
  35. value_t ashare;
  36. // Set each side's share to a random value nbits bits long
  37. inline void randomize(size_t nbits = VALUE_BITS) {
  38. value_t mask = MASKBITS(nbits);
  39. arc4random_buf(&ashare, sizeof(ashare));
  40. ashare &= mask;
  41. }
  42. inline RegAS &operator+=(RegAS &rhs) {
  43. this->ashare += rhs.ashare;
  44. return *this;
  45. }
  46. inline RegAS operator+(RegAS &rhs) const {
  47. RegAS res = *this;
  48. res += rhs;
  49. return res;
  50. }
  51. inline RegAS &operator-=(RegAS &rhs) {
  52. this->ashare -= rhs.ashare;
  53. return *this;
  54. }
  55. inline RegAS operator-(RegAS &rhs) const {
  56. RegAS res = *this;
  57. res -= rhs;
  58. return res;
  59. }
  60. inline RegAS &operator*=(value_t rhs) {
  61. this->ashare *= rhs;
  62. return *this;
  63. }
  64. inline RegAS operator*(value_t rhs) const {
  65. RegAS res = *this;
  66. res *= rhs;
  67. return res;
  68. }
  69. inline RegAS &operator&=(value_t mask) {
  70. this->ashare &= mask;
  71. return *this;
  72. }
  73. inline RegAS operator&(value_t mask) const {
  74. RegAS res = *this;
  75. res &= mask;
  76. return res;
  77. }
  78. };
  79. // The type of a register holding an XOR share of a value
  80. struct RegXS {
  81. value_t xshare;
  82. // Set each side's share to a random value nbits bits long
  83. inline void randomize(size_t nbits = VALUE_BITS) {
  84. value_t mask = MASKBITS(nbits);
  85. arc4random_buf(&xshare, sizeof(xshare));
  86. xshare &= mask;
  87. }
  88. inline RegXS &operator^=(RegXS &rhs) {
  89. this->xshare ^= rhs.xshare;
  90. return *this;
  91. }
  92. inline RegXS operator^(RegXS &rhs) const {
  93. RegXS res = *this;
  94. res ^= rhs;
  95. return res;
  96. }
  97. inline RegXS &operator&=(value_t mask) {
  98. this->xshare &= mask;
  99. return *this;
  100. }
  101. inline RegXS operator&(value_t mask) const {
  102. RegXS res = *this;
  103. res &= mask;
  104. return res;
  105. }
  106. };
  107. // The type of a register holding a bit share
  108. struct RegBS {
  109. bit_t bshare;
  110. // Set each side's share to a random bit
  111. inline void randomize() {
  112. arc4random_buf(&bshare, sizeof(bshare));
  113. bshare &= 1;
  114. }
  115. };
  116. // The _maximum_ number of bits in an MPC address; the actual size of
  117. // the memory will typically be set at runtime, but it cannot exceed
  118. // this value. It is more efficient (in terms of communication) in some
  119. // places for this value to be at most 32.
  120. #ifndef ADDRESS_MAX_BITS
  121. #define ADDRESS_MAX_BITS 32
  122. #endif
  123. // Addresses of MPC secret-shared memory are of this type
  124. #if ADDRESS_MAX_BITS <= 32
  125. using address_t = uint32_t;
  126. #elif ADDRESS_MAX_BITS <= 64
  127. using address_t = uint64_t;
  128. #else
  129. #error "Unsupported value of ADDRESS_MAX_BITS"
  130. #endif
  131. #if ADDRESS_MAX_BITS > VALUE_BITS
  132. #error "VALUE_BITS must be at least as large as ADDRESS_MAX_BITS"
  133. #endif
  134. // A multiplication triple is a triple (X0,Y0,Z0) held by P0 (and
  135. // correspondingly (X1,Y1,Z1) held by P1), with all values random,
  136. // but subject to the relation that X0*Y1 + Y0*X1 = Z0+Z1
  137. using MultTriple = std::tuple<value_t, value_t, value_t>;
  138. // A half-triple is (X0,Z0) held by P0 (and correspondingly (Y1,Z1) held
  139. // by P1), with all values random, but subject to the relation that
  140. // X0*Y1 = Z0+Z1
  141. using HalfTriple = std::tuple<value_t, value_t>;
  142. #endif