types.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. RegAS() : ashare(0) {}
  38. inline value_t share() const { return ashare; }
  39. inline void set(value_t s) { ashare = s; }
  40. // Set each side's share to a random value nbits bits long
  41. inline void randomize(size_t nbits = VALUE_BITS) {
  42. value_t mask = MASKBITS(nbits);
  43. arc4random_buf(&ashare, sizeof(ashare));
  44. ashare &= mask;
  45. }
  46. inline RegAS &operator+=(const RegAS &rhs) {
  47. this->ashare += rhs.ashare;
  48. return *this;
  49. }
  50. inline RegAS operator+(const RegAS &rhs) const {
  51. RegAS res = *this;
  52. res += rhs;
  53. return res;
  54. }
  55. inline RegAS &operator-=(const RegAS &rhs) {
  56. this->ashare -= rhs.ashare;
  57. return *this;
  58. }
  59. inline RegAS operator-(const RegAS &rhs) const {
  60. RegAS res = *this;
  61. res -= rhs;
  62. return res;
  63. }
  64. inline RegAS &operator*=(value_t rhs) {
  65. this->ashare *= rhs;
  66. return *this;
  67. }
  68. inline RegAS operator*(value_t rhs) const {
  69. RegAS res = *this;
  70. res *= rhs;
  71. return res;
  72. }
  73. inline RegAS &operator&=(value_t mask) {
  74. this->ashare &= mask;
  75. return *this;
  76. }
  77. inline RegAS operator&(value_t mask) const {
  78. RegAS res = *this;
  79. res &= mask;
  80. return res;
  81. }
  82. };
  83. inline value_t combine(const RegAS &A, const RegAS &B,
  84. nbits_t nbits = VALUE_BITS) {
  85. value_t mask = ~0;
  86. if (nbits < VALUE_BITS) {
  87. mask = (value_t(1)<<nbits)-1;
  88. }
  89. return (A.ashare + B.ashare) & mask;
  90. }
  91. // The type of a register holding a bit share
  92. struct RegBS {
  93. bit_t bshare;
  94. RegBS() : bshare(0) {}
  95. inline bit_t share() const { return bshare; }
  96. inline void set(bit_t s) { bshare = s; }
  97. // Set each side's share to a random bit
  98. inline void randomize() {
  99. unsigned char randb;
  100. arc4random_buf(&randb, sizeof(randb));
  101. bshare = randb & 1;
  102. }
  103. inline RegBS &operator^=(const RegBS &rhs) {
  104. this->bshare ^= rhs.bshare;
  105. return *this;
  106. }
  107. inline RegBS operator^(const RegBS &rhs) const {
  108. RegBS res = *this;
  109. res ^= rhs;
  110. return res;
  111. }
  112. };
  113. // The type of a register holding an XOR share of a value
  114. struct RegXS {
  115. value_t xshare;
  116. RegXS() : xshare(0) {}
  117. RegXS(const RegBS &b) { xshare = b.bshare ? ~0 : 0; }
  118. inline value_t share() const { return xshare; }
  119. inline void set(value_t s) { xshare = s; }
  120. // Set each side's share to a random value nbits bits long
  121. inline void randomize(size_t nbits = VALUE_BITS) {
  122. value_t mask = MASKBITS(nbits);
  123. arc4random_buf(&xshare, sizeof(xshare));
  124. xshare &= mask;
  125. }
  126. // For RegXS, + and * should be interpreted bitwise; that is, + is
  127. // really XOR and * is really AND. - is also XOR (the same as +).
  128. // We also include actual XOR operators for convenience
  129. inline RegXS &operator+=(const RegXS &rhs) {
  130. this->xshare ^= rhs.xshare;
  131. return *this;
  132. }
  133. inline RegXS operator+(const RegXS &rhs) const {
  134. RegXS res = *this;
  135. res += rhs;
  136. return res;
  137. }
  138. inline RegXS &operator-=(const RegXS &rhs) {
  139. this->xshare ^= rhs.xshare;
  140. return *this;
  141. }
  142. inline RegXS operator-(const RegXS &rhs) const {
  143. RegXS res = *this;
  144. res -= rhs;
  145. return res;
  146. }
  147. inline RegXS &operator*=(value_t rhs) {
  148. this->xshare &= rhs;
  149. return *this;
  150. }
  151. inline RegXS operator*(value_t rhs) const {
  152. RegXS res = *this;
  153. res *= rhs;
  154. return res;
  155. }
  156. inline RegXS &operator^=(const RegXS &rhs) {
  157. this->xshare ^= rhs.xshare;
  158. return *this;
  159. }
  160. inline RegXS operator^(const RegXS &rhs) const {
  161. RegXS res = *this;
  162. res ^= rhs;
  163. return res;
  164. }
  165. inline RegXS &operator&=(value_t mask) {
  166. this->xshare &= mask;
  167. return *this;
  168. }
  169. inline RegXS operator&(value_t mask) const {
  170. RegXS res = *this;
  171. res &= mask;
  172. return res;
  173. }
  174. // Extract a bit share of bit bitnum of the XOR-shared register
  175. inline RegBS bit(nbits_t bitnum) const {
  176. RegBS bs;
  177. bs.bshare = !!(xshare & (value_t(1)<<bitnum));
  178. return bs;
  179. }
  180. };
  181. inline value_t combine(const RegXS &A, const RegXS &B,
  182. nbits_t nbits = VALUE_BITS) {
  183. value_t mask = ~0;
  184. if (nbits < VALUE_BITS) {
  185. mask = (value_t(1)<<nbits)-1;
  186. }
  187. return (A.xshare ^ B.xshare) & mask;
  188. }
  189. // Some useful operations on tuples of the above types
  190. template <typename T>
  191. std::tuple<T,T> operator+=(std::tuple<T,T> &A,
  192. const std::tuple<T,T> &B)
  193. {
  194. std::get<0>(A) += std::get<0>(B);
  195. std::get<1>(A) += std::get<1>(B);
  196. return A;
  197. }
  198. template <typename T>
  199. std::tuple<T,T> operator+=(const std::tuple<T&,T&> &A,
  200. const std::tuple<T,T> &B)
  201. {
  202. std::get<0>(A) += std::get<0>(B);
  203. std::get<1>(A) += std::get<1>(B);
  204. return A;
  205. }
  206. template <typename T>
  207. std::tuple<T,T> operator+(const std::tuple<T,T> &A,
  208. const std::tuple<T,T> &B)
  209. {
  210. auto res = A;
  211. res += B;
  212. return res;
  213. }
  214. template <typename T>
  215. std::tuple<T,T> operator-=(const std::tuple<T&,T&> &A,
  216. const std::tuple<T,T> &B)
  217. {
  218. std::get<0>(A) -= std::get<0>(B);
  219. std::get<1>(A) -= std::get<1>(B);
  220. return A;
  221. }
  222. template <typename T>
  223. std::tuple<T,T> operator-=(std::tuple<T,T> &A,
  224. const std::tuple<T,T> &B)
  225. {
  226. std::get<0>(A) -= std::get<0>(B);
  227. std::get<1>(A) -= std::get<1>(B);
  228. return A;
  229. }
  230. template <typename T>
  231. std::tuple<T,T> operator-(const std::tuple<T,T> &A,
  232. const std::tuple<T,T> &B)
  233. {
  234. auto res = A;
  235. res -= B;
  236. return res;
  237. }
  238. template <typename T>
  239. std::tuple<T,T> operator*=(const std::tuple<T&,T&> &A,
  240. const std::tuple<value_t,value_t> &B)
  241. {
  242. std::get<0>(A) *= std::get<0>(B);
  243. std::get<1>(A) *= std::get<1>(B);
  244. return A;
  245. }
  246. template <typename T>
  247. std::tuple<T,T> operator*=(std::tuple<T,T> &A,
  248. const std::tuple<value_t,value_t> &B)
  249. {
  250. std::get<0>(A) *= std::get<0>(B);
  251. std::get<1>(A) *= std::get<1>(B);
  252. return A;
  253. }
  254. template <typename T>
  255. std::tuple<T,T> operator*(const std::tuple<T,T> &A,
  256. const std::tuple<value_t,value_t> &B)
  257. {
  258. auto res = A;
  259. res *= B;
  260. return res;
  261. }
  262. template <typename T>
  263. inline std::tuple<value_t,value_t> combine(
  264. const std::tuple<T,T> &A, const std::tuple<T,T> &B,
  265. nbits_t nbits = VALUE_BITS) {
  266. return std::make_tuple(
  267. combine(std::get<0>(A), std::get<0>(B), nbits),
  268. combine(std::get<1>(A), std::get<1>(B), nbits));
  269. }
  270. template <typename T>
  271. std::tuple<T,T,T> operator+=(const std::tuple<T&,T&,T&> &A,
  272. const std::tuple<T,T,T> &B)
  273. {
  274. std::get<0>(A) += std::get<0>(B);
  275. std::get<1>(A) += std::get<1>(B);
  276. std::get<2>(A) += std::get<2>(B);
  277. return A;
  278. }
  279. template <typename T>
  280. std::tuple<T,T,T> operator+=(std::tuple<T,T,T> &A,
  281. const std::tuple<T,T,T> &B)
  282. {
  283. std::get<0>(A) += std::get<0>(B);
  284. std::get<1>(A) += std::get<1>(B);
  285. std::get<2>(A) += std::get<2>(B);
  286. return A;
  287. }
  288. template <typename T>
  289. std::tuple<T,T,T> operator+(const std::tuple<T,T,T> &A,
  290. const std::tuple<T,T,T> &B)
  291. {
  292. auto res = A;
  293. res += B;
  294. return res;
  295. }
  296. template <typename T>
  297. std::tuple<T,T,T> operator-=(const std::tuple<T&,T&,T&> &A,
  298. const std::tuple<T,T,T> &B)
  299. {
  300. std::get<0>(A) -= std::get<0>(B);
  301. std::get<1>(A) -= std::get<1>(B);
  302. std::get<2>(A) -= std::get<2>(B);
  303. return A;
  304. }
  305. template <typename T>
  306. std::tuple<T,T,T> operator-=(std::tuple<T,T,T> &A,
  307. const std::tuple<T,T,T> &B)
  308. {
  309. std::get<0>(A) -= std::get<0>(B);
  310. std::get<1>(A) -= std::get<1>(B);
  311. std::get<2>(A) -= std::get<2>(B);
  312. return A;
  313. }
  314. template <typename T>
  315. std::tuple<T,T,T> operator-(const std::tuple<T,T,T> &A,
  316. const std::tuple<T,T,T> &B)
  317. {
  318. auto res = A;
  319. res -= B;
  320. return res;
  321. }
  322. template <typename T>
  323. std::tuple<T,T,T> operator*=(const std::tuple<T&,T&,T&> &A,
  324. const std::tuple<value_t,value_t,value_t> &B)
  325. {
  326. std::get<0>(A) *= std::get<0>(B);
  327. std::get<1>(A) *= std::get<1>(B);
  328. std::get<2>(A) *= std::get<2>(B);
  329. return A;
  330. }
  331. template <typename T>
  332. std::tuple<T,T,T> operator*=(std::tuple<T,T,T> &A,
  333. const std::tuple<value_t,value_t,value_t> &B)
  334. {
  335. std::get<0>(A) *= std::get<0>(B);
  336. std::get<1>(A) *= std::get<1>(B);
  337. std::get<2>(A) *= std::get<2>(B);
  338. return A;
  339. }
  340. template <typename T>
  341. std::tuple<T,T,T> operator*(const std::tuple<T,T,T> &A,
  342. const std::tuple<value_t,value_t,value_t> &B)
  343. {
  344. auto res = A;
  345. res *= B;
  346. return res;
  347. }
  348. template <typename T>
  349. inline std::tuple<value_t,value_t,value_t> combine(
  350. const std::tuple<T,T,T> &A, const std::tuple<T,T,T> &B,
  351. nbits_t nbits = VALUE_BITS) {
  352. return std::make_tuple(
  353. combine(std::get<0>(A), std::get<0>(B), nbits),
  354. combine(std::get<1>(A), std::get<1>(B), nbits),
  355. combine(std::get<2>(A), std::get<2>(B), nbits));
  356. }
  357. // The _maximum_ number of bits in an MPC address; the actual size of
  358. // the memory will typically be set at runtime, but it cannot exceed
  359. // this value. It is more efficient (in terms of communication) in some
  360. // places for this value to be at most 32.
  361. #ifndef ADDRESS_MAX_BITS
  362. #define ADDRESS_MAX_BITS 32
  363. #endif
  364. // Addresses of MPC secret-shared memory are of this type
  365. #if ADDRESS_MAX_BITS <= 32
  366. using address_t = uint32_t;
  367. #elif ADDRESS_MAX_BITS <= 64
  368. using address_t = uint64_t;
  369. #else
  370. #error "Unsupported value of ADDRESS_MAX_BITS"
  371. #endif
  372. #if ADDRESS_MAX_BITS > VALUE_BITS
  373. #error "VALUE_BITS must be at least as large as ADDRESS_MAX_BITS"
  374. #endif
  375. // A multiplication triple is a triple (X0,Y0,Z0) held by P0 (and
  376. // correspondingly (X1,Y1,Z1) held by P1), with all values random,
  377. // but subject to the relation that X0*Y1 + Y0*X1 = Z0+Z1
  378. using MultTriple = std::tuple<value_t, value_t, value_t>;
  379. // The *Name structs are a way to get strings representing the names of
  380. // the types as would be given to preprocessing to create them in
  381. // advance.
  382. struct MultTripleName { static constexpr const char *name = "t"; };
  383. // A half-triple is (X0,Z0) held by P0 (and correspondingly (Y1,Z1) held
  384. // by P1), with all values random, but subject to the relation that
  385. // X0*Y1 = Z0+Z1
  386. using HalfTriple = std::tuple<value_t, value_t>;
  387. struct HalfTripleName { static constexpr const char *name = "h"; };
  388. // The type of nodes in a DPF. This must be at least as many bits as
  389. // the security parameter, and at least twice as many bits as value_t.
  390. using DPFnode = __m128i;
  391. // A Select triple is a triple of (X0,Y0,Z0) where X0 is a bit and Y0
  392. // and Z0 are DPFnodes held by P0 (and correspondingly (X1,Y1,Z1) held
  393. // by P1), with all values random, but subject to the relation that
  394. // (X0*Y1) ^ (Y0*X1) = Z0^Z1. These are only used while creating RDPFs
  395. // in the preprocessing phase, so we never need to store them. This is
  396. // a struct instead of a tuple for alignment reasons.
  397. struct SelectTriple {
  398. bit_t X;
  399. DPFnode Y, Z;
  400. };
  401. // These are defined in rdpf.hpp, but declared here to avoid cyclic
  402. // header dependencies.
  403. struct RDPFPair;
  404. struct RDPFPairName { static constexpr const char *name = "r"; };
  405. struct RDPFTriple;
  406. struct RDPFTripleName { static constexpr const char *name = "r"; };
  407. struct CDPF;
  408. struct CDPFName { static constexpr const char *name = "c"; };
  409. // We want the I/O (using << and >>) for many classes
  410. // to just be a common thing: write out the bytes
  411. // straight from memory
  412. #define DEFAULT_IO(CLASSNAME) \
  413. template <typename T> \
  414. T& operator>>(T& is, CLASSNAME &x) \
  415. { \
  416. is.read((char *)&x, sizeof(x)); \
  417. return is; \
  418. } \
  419. \
  420. template <typename T> \
  421. T& operator<<(T& os, const CLASSNAME &x) \
  422. { \
  423. os.write((const char *)&x, sizeof(x)); \
  424. return os; \
  425. }
  426. // Default I/O for various types
  427. DEFAULT_IO(RegBS)
  428. DEFAULT_IO(RegAS)
  429. DEFAULT_IO(RegXS)
  430. DEFAULT_IO(MultTriple)
  431. DEFAULT_IO(HalfTriple)
  432. // And for pairs and triples
  433. #define DEFAULT_TUPLE_IO(CLASSNAME) \
  434. template <typename T> \
  435. T& operator>>(T& is, std::tuple<CLASSNAME, CLASSNAME> &x) \
  436. { \
  437. is >> std::get<0>(x) >> std::get<1>(x); \
  438. return is; \
  439. } \
  440. \
  441. template <typename T> \
  442. T& operator<<(T& os, const std::tuple<CLASSNAME, CLASSNAME> &x) \
  443. { \
  444. os << std::get<0>(x) << std::get<1>(x); \
  445. return os; \
  446. } \
  447. \
  448. template <typename T> \
  449. T& operator>>(T& is, std::tuple<CLASSNAME, CLASSNAME, CLASSNAME> &x) \
  450. { \
  451. is >> std::get<0>(x) >> std::get<1>(x) >> std::get<2>(x); \
  452. return is; \
  453. } \
  454. \
  455. template <typename T> \
  456. T& operator<<(T& os, const std::tuple<CLASSNAME, CLASSNAME, CLASSNAME> &x) \
  457. { \
  458. os << std::get<0>(x) << std::get<1>(x) << std::get<2>(x); \
  459. return os; \
  460. }
  461. DEFAULT_TUPLE_IO(RegAS)
  462. DEFAULT_TUPLE_IO(RegXS)
  463. #endif