types.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. #ifndef __OBLIVDS_TYPES_HPP__
  2. #define __OBLIVDS_TYPES_HPP__
  3. #include <tuple>
  4. #include <vector>
  5. #include <array>
  6. #include <cstdint>
  7. #include <x86intrin.h> // SSE and AVX intrinsics
  8. #include <bsd/stdlib.h> // arc4random_buf
  9. // The number of bits in an MPC secret-shared memory word
  10. #ifndef VALUE_BITS
  11. #define VALUE_BITS 64
  12. #endif
  13. // Values in MPC secret-shared memory are of this type.
  14. // This is the type of the underlying shared value, not the types of the
  15. // shares themselves.
  16. #if VALUE_BITS == 64
  17. using value_t = uint64_t;
  18. #elif VALUE_BITS == 32
  19. using value_t = uint32_t;
  20. #else
  21. #error "Unsupported value of VALUE_BITS"
  22. #endif
  23. // Secret-shared bits are of this type. Note that it is standards
  24. // compliant to treat a bool as an unsigned integer type with values 0
  25. // and 1.
  26. using bit_t = bool;
  27. // Counts of the number of bits in a value are of this type, which must
  28. // be large enough to store the _value_ VALUE_BITS
  29. using nbits_t = uint8_t;
  30. // Convert a number of bits to the number of bytes required to store (or
  31. // more to the point, send) them.
  32. #define BITBYTES(nbits) (((nbits)+7)>>3)
  33. // A mask of this many bits; the test is to prevent 1<<nbits from
  34. // overflowing if nbits == VALUE_BITS
  35. #define MASKBITS(nbits) (((nbits) < VALUE_BITS) ? (value_t(1)<<(nbits))-1 : ~0)
  36. // The type of a register holding an additive share of a value
  37. struct RegAS {
  38. value_t ashare;
  39. RegAS() : ashare(0) {}
  40. inline value_t share() const { return ashare; }
  41. inline void set(value_t s) { ashare = s; }
  42. // Set each side's share to a random value nbits bits long
  43. inline void randomize(size_t nbits = VALUE_BITS) {
  44. value_t mask = MASKBITS(nbits);
  45. arc4random_buf(&ashare, sizeof(ashare));
  46. ashare &= mask;
  47. }
  48. inline RegAS &operator+=(const RegAS &rhs) {
  49. this->ashare += rhs.ashare;
  50. return *this;
  51. }
  52. inline RegAS operator+(const RegAS &rhs) const {
  53. RegAS res = *this;
  54. res += rhs;
  55. return res;
  56. }
  57. inline RegAS &operator-=(const RegAS &rhs) {
  58. this->ashare -= rhs.ashare;
  59. return *this;
  60. }
  61. inline RegAS operator-(const RegAS &rhs) const {
  62. RegAS res = *this;
  63. res -= rhs;
  64. return res;
  65. }
  66. inline RegAS operator-() const {
  67. RegAS res = *this;
  68. res.ashare = -res.ashare;
  69. return res;
  70. }
  71. inline RegAS &operator*=(value_t rhs) {
  72. this->ashare *= rhs;
  73. return *this;
  74. }
  75. inline RegAS operator*(value_t rhs) const {
  76. RegAS res = *this;
  77. res *= rhs;
  78. return res;
  79. }
  80. inline RegAS &operator&=(value_t mask) {
  81. this->ashare &= mask;
  82. return *this;
  83. }
  84. inline RegAS operator&(value_t mask) const {
  85. RegAS res = *this;
  86. res &= mask;
  87. return res;
  88. }
  89. // Multiply by the local share of the argument, not multiplcation of
  90. // two shared values (two versions)
  91. inline RegAS &mulshareeq(const RegAS &rhs) {
  92. *this *= rhs.ashare;
  93. return *this;
  94. }
  95. inline RegAS mulshare(const RegAS &rhs) const {
  96. RegAS res = *this;
  97. res *= rhs.ashare;
  98. return res;
  99. }
  100. inline void dump() const {
  101. printf("%016lx", ashare);
  102. }
  103. };
  104. inline value_t combine(const RegAS &A, const RegAS &B,
  105. nbits_t nbits = VALUE_BITS) {
  106. value_t mask = ~0;
  107. if (nbits < VALUE_BITS) {
  108. mask = (value_t(1)<<nbits)-1;
  109. }
  110. return (A.ashare + B.ashare) & mask;
  111. }
  112. // The type of a register holding a bit share
  113. struct RegBS {
  114. bit_t bshare;
  115. RegBS() : bshare(0) {}
  116. inline bit_t share() const { return bshare; }
  117. inline void set(bit_t s) { bshare = s; }
  118. // Set each side's share to a random bit
  119. inline void randomize() {
  120. unsigned char randb;
  121. arc4random_buf(&randb, sizeof(randb));
  122. bshare = randb & 1;
  123. }
  124. inline RegBS &operator^=(const RegBS &rhs) {
  125. this->bshare ^= rhs.bshare;
  126. return *this;
  127. }
  128. inline RegBS operator^(const RegBS &rhs) const {
  129. RegBS res = *this;
  130. res ^= rhs;
  131. return res;
  132. }
  133. inline RegBS &operator^=(const bit_t &rhs) {
  134. this->bshare ^= rhs;
  135. return *this;
  136. }
  137. inline RegBS operator^(const bit_t &rhs) const {
  138. RegBS res = *this;
  139. res ^= rhs;
  140. return res;
  141. }
  142. };
  143. // The type of a register holding an XOR share of a value
  144. struct RegXS {
  145. value_t xshare;
  146. RegXS() : xshare(0) {}
  147. RegXS(const RegBS &b) { xshare = b.bshare ? ~0 : 0; }
  148. inline value_t share() const { return xshare; }
  149. inline void set(value_t s) { xshare = s; }
  150. // Set each side's share to a random value nbits bits long
  151. inline void randomize(size_t nbits = VALUE_BITS) {
  152. value_t mask = MASKBITS(nbits);
  153. arc4random_buf(&xshare, sizeof(xshare));
  154. xshare &= mask;
  155. }
  156. // For RegXS, + and * should be interpreted bitwise; that is, + is
  157. // really XOR and * is really AND. - is also XOR (the same as +).
  158. // We also include actual XOR operators for convenience
  159. inline RegXS &operator+=(const RegXS &rhs) {
  160. this->xshare ^= rhs.xshare;
  161. return *this;
  162. }
  163. inline RegXS operator+(const RegXS &rhs) const {
  164. RegXS res = *this;
  165. res += rhs;
  166. return res;
  167. }
  168. inline RegXS &operator-=(const RegXS &rhs) {
  169. this->xshare ^= rhs.xshare;
  170. return *this;
  171. }
  172. inline RegXS operator-(const RegXS &rhs) const {
  173. RegXS res = *this;
  174. res -= rhs;
  175. return res;
  176. }
  177. inline RegXS operator-() const {
  178. RegXS res = *this;
  179. return res;
  180. }
  181. inline RegXS &operator*=(value_t rhs) {
  182. this->xshare &= rhs;
  183. return *this;
  184. }
  185. inline RegXS operator*(value_t rhs) const {
  186. RegXS res = *this;
  187. res *= rhs;
  188. return res;
  189. }
  190. inline RegXS &operator^=(const RegXS &rhs) {
  191. this->xshare ^= rhs.xshare;
  192. return *this;
  193. }
  194. inline RegXS operator^(const RegXS &rhs) const {
  195. RegXS res = *this;
  196. res ^= rhs;
  197. return res;
  198. }
  199. inline RegXS &operator&=(value_t mask) {
  200. this->xshare &= mask;
  201. return *this;
  202. }
  203. inline RegXS operator&(value_t mask) const {
  204. RegXS res = *this;
  205. res &= mask;
  206. return res;
  207. }
  208. // Multiply by the local share of the argument, not multiplcation of
  209. // two shared values (two versions)
  210. inline RegXS &mulshareeq(const RegXS &rhs) {
  211. *this *= rhs.xshare;
  212. return *this;
  213. }
  214. inline RegXS mulshare(const RegXS &rhs) const {
  215. RegXS res = *this;
  216. res *= rhs.xshare;
  217. return res;
  218. }
  219. inline void dump() const {
  220. printf("%016lx", xshare);
  221. }
  222. // Extract a bit share of bit bitnum of the XOR-shared register
  223. inline RegBS bit(nbits_t bitnum) const {
  224. RegBS bs;
  225. bs.bshare = !!(xshare & (value_t(1)<<bitnum));
  226. return bs;
  227. }
  228. };
  229. inline value_t combine(const RegXS &A, const RegXS &B,
  230. nbits_t nbits = VALUE_BITS) {
  231. value_t mask = ~0;
  232. if (nbits < VALUE_BITS) {
  233. mask = (value_t(1)<<nbits)-1;
  234. }
  235. return (A.xshare ^ B.xshare) & mask;
  236. }
  237. // Enable templates to specialize on just the basic types RegAS and
  238. // RegXS. Technique from
  239. // https://stackoverflow.com/questions/2430039/one-template-specialization-for-multiple-classes
  240. template <bool B> struct prac_template_bool_type {};
  241. using prac_template_true = prac_template_bool_type<true>;
  242. using prac_template_false = prac_template_bool_type<false>;
  243. template <typename T>
  244. struct prac_basic_Reg_S : prac_template_false
  245. {
  246. static const bool value = false;
  247. };
  248. template<>
  249. struct prac_basic_Reg_S<RegAS>: prac_template_true
  250. {
  251. static const bool value = true;
  252. };
  253. template<>
  254. struct prac_basic_Reg_S<RegXS>: prac_template_true
  255. {
  256. static const bool value = true;
  257. };
  258. // Some useful operations on tuples, vectors, and arrays of the above
  259. // types
  260. template <typename T>
  261. std::tuple<T,T> operator+=(std::tuple<T,T> &A,
  262. const std::tuple<T,T> &B)
  263. {
  264. std::get<0>(A) += std::get<0>(B);
  265. std::get<1>(A) += std::get<1>(B);
  266. return A;
  267. }
  268. template <typename T>
  269. std::tuple<T,T> operator+=(const std::tuple<T&,T&> &A,
  270. const std::tuple<T,T> &B)
  271. {
  272. std::get<0>(A) += std::get<0>(B);
  273. std::get<1>(A) += std::get<1>(B);
  274. return A;
  275. }
  276. template <typename T>
  277. std::tuple<T,T> operator+(const std::tuple<T,T> &A,
  278. const std::tuple<T,T> &B)
  279. {
  280. auto res = A;
  281. res += B;
  282. return res;
  283. }
  284. template <typename T>
  285. std::tuple<T,T> operator-=(const std::tuple<T&,T&> &A,
  286. const std::tuple<T,T> &B)
  287. {
  288. std::get<0>(A) -= std::get<0>(B);
  289. std::get<1>(A) -= std::get<1>(B);
  290. return A;
  291. }
  292. template <typename T>
  293. std::tuple<T,T> operator-=(std::tuple<T,T> &A,
  294. const std::tuple<T,T> &B)
  295. {
  296. std::get<0>(A) -= std::get<0>(B);
  297. std::get<1>(A) -= std::get<1>(B);
  298. return A;
  299. }
  300. template <typename T>
  301. std::tuple<T,T> operator-(const std::tuple<T,T> &A,
  302. const std::tuple<T,T> &B)
  303. {
  304. auto res = A;
  305. res -= B;
  306. return res;
  307. }
  308. template <typename T>
  309. std::tuple<T,T> operator*=(const std::tuple<T&,T&> &A,
  310. const std::tuple<value_t,value_t> &B)
  311. {
  312. std::get<0>(A) *= std::get<0>(B);
  313. std::get<1>(A) *= std::get<1>(B);
  314. return A;
  315. }
  316. template <typename T>
  317. std::tuple<T,T> operator*=(std::tuple<T,T> &A,
  318. const std::tuple<value_t,value_t> &B)
  319. {
  320. std::get<0>(A) *= std::get<0>(B);
  321. std::get<1>(A) *= std::get<1>(B);
  322. return A;
  323. }
  324. template <typename T>
  325. std::tuple<T,T> operator*(const std::tuple<T,T> &A,
  326. const std::tuple<value_t,value_t> &B)
  327. {
  328. auto res = A;
  329. res *= B;
  330. return res;
  331. }
  332. template <typename T>
  333. inline std::tuple<value_t,value_t> combine(
  334. const std::tuple<T,T> &A, const std::tuple<T,T> &B,
  335. nbits_t nbits = VALUE_BITS) {
  336. return std::make_tuple(
  337. combine(std::get<0>(A), std::get<0>(B), nbits),
  338. combine(std::get<1>(A), std::get<1>(B), nbits));
  339. }
  340. template <typename T>
  341. std::tuple<T,T,T> operator+=(const std::tuple<T&,T&,T&> &A,
  342. const std::tuple<T,T,T> &B)
  343. {
  344. std::get<0>(A) += std::get<0>(B);
  345. std::get<1>(A) += std::get<1>(B);
  346. std::get<2>(A) += std::get<2>(B);
  347. return A;
  348. }
  349. template <typename T>
  350. std::tuple<T,T,T> operator+=(std::tuple<T,T,T> &A,
  351. const std::tuple<T,T,T> &B)
  352. {
  353. std::get<0>(A) += std::get<0>(B);
  354. std::get<1>(A) += std::get<1>(B);
  355. std::get<2>(A) += std::get<2>(B);
  356. return A;
  357. }
  358. template <typename T>
  359. std::tuple<T,T,T> operator+(const std::tuple<T,T,T> &A,
  360. const std::tuple<T,T,T> &B)
  361. {
  362. auto res = A;
  363. res += B;
  364. return res;
  365. }
  366. template <typename T>
  367. std::tuple<T,T,T> operator-=(const std::tuple<T&,T&,T&> &A,
  368. const std::tuple<T,T,T> &B)
  369. {
  370. std::get<0>(A) -= std::get<0>(B);
  371. std::get<1>(A) -= std::get<1>(B);
  372. std::get<2>(A) -= std::get<2>(B);
  373. return A;
  374. }
  375. template <typename T>
  376. std::tuple<T,T,T> operator-=(std::tuple<T,T,T> &A,
  377. const std::tuple<T,T,T> &B)
  378. {
  379. std::get<0>(A) -= std::get<0>(B);
  380. std::get<1>(A) -= std::get<1>(B);
  381. std::get<2>(A) -= std::get<2>(B);
  382. return A;
  383. }
  384. template <typename T>
  385. std::tuple<T,T,T> operator-(const std::tuple<T,T,T> &A,
  386. const std::tuple<T,T,T> &B)
  387. {
  388. auto res = A;
  389. res -= B;
  390. return res;
  391. }
  392. template <typename T>
  393. std::tuple<T,T,T> operator*=(const std::tuple<T&,T&,T&> &A,
  394. const std::tuple<value_t,value_t,value_t> &B)
  395. {
  396. std::get<0>(A) *= std::get<0>(B);
  397. std::get<1>(A) *= std::get<1>(B);
  398. std::get<2>(A) *= std::get<2>(B);
  399. return A;
  400. }
  401. template <typename T>
  402. std::tuple<T,T,T> operator*=(std::tuple<T,T,T> &A,
  403. const std::tuple<value_t,value_t,value_t> &B)
  404. {
  405. std::get<0>(A) *= std::get<0>(B);
  406. std::get<1>(A) *= std::get<1>(B);
  407. std::get<2>(A) *= std::get<2>(B);
  408. return A;
  409. }
  410. template <typename T>
  411. std::tuple<T,T,T> operator*(const std::tuple<T,T,T> &A,
  412. const std::tuple<value_t,value_t,value_t> &B)
  413. {
  414. auto res = A;
  415. res *= B;
  416. return res;
  417. }
  418. inline std::vector<RegAS> operator-(const std::vector<RegAS> &A)
  419. {
  420. std::vector<RegAS> res;
  421. for (const auto &v : A) {
  422. res.push_back(-v);
  423. }
  424. return res;
  425. }
  426. inline std::vector<RegXS> operator-(const std::vector<RegXS> &A)
  427. {
  428. return A;
  429. }
  430. inline std::vector<RegBS> operator-(const std::vector<RegBS> &A)
  431. {
  432. return A;
  433. }
  434. template <size_t N>
  435. inline std::vector<RegAS> operator-(const std::array<RegAS,N> &A)
  436. {
  437. std::vector<RegAS> res;
  438. for (const auto &v : A) {
  439. res.push_back(-v);
  440. }
  441. return res;
  442. }
  443. template <size_t N>
  444. inline std::array<RegXS,N> operator-(const std::array<RegXS,N> &A)
  445. {
  446. return A;
  447. }
  448. template <size_t N>
  449. inline std::array<RegBS,N> operator-(const std::array<RegBS,N> &A)
  450. {
  451. return A;
  452. }
  453. template <typename T>
  454. inline std::tuple<value_t,value_t,value_t> combine(
  455. const std::tuple<T,T,T> &A, const std::tuple<T,T,T> &B,
  456. nbits_t nbits = VALUE_BITS) {
  457. return std::make_tuple(
  458. combine(std::get<0>(A), std::get<0>(B), nbits),
  459. combine(std::get<1>(A), std::get<1>(B), nbits),
  460. combine(std::get<2>(A), std::get<2>(B), nbits));
  461. }
  462. // The _maximum_ number of bits in an MPC address; the actual size of
  463. // the memory will typically be set at runtime, but it cannot exceed
  464. // this value. It is more efficient (in terms of communication) in some
  465. // places for this value to be at most 32.
  466. #ifndef ADDRESS_MAX_BITS
  467. #define ADDRESS_MAX_BITS 32
  468. #endif
  469. // Addresses of MPC secret-shared memory are of this type
  470. #if ADDRESS_MAX_BITS <= 32
  471. using address_t = uint32_t;
  472. #elif ADDRESS_MAX_BITS <= 64
  473. using address_t = uint64_t;
  474. #else
  475. #error "Unsupported value of ADDRESS_MAX_BITS"
  476. #endif
  477. #if ADDRESS_MAX_BITS > VALUE_BITS
  478. #error "VALUE_BITS must be at least as large as ADDRESS_MAX_BITS"
  479. #endif
  480. // A multiplication triple is a triple (X0,Y0,Z0) held by P0 (and
  481. // correspondingly (X1,Y1,Z1) held by P1), with all values random,
  482. // but subject to the relation that X0*Y1 + Y0*X1 = Z0+Z1
  483. using MultTriple = std::tuple<value_t, value_t, value_t>;
  484. // The *Name structs are a way to get strings representing the names of
  485. // the types as would be given to preprocessing to create them in
  486. // advance.
  487. struct MultTripleName { static constexpr const char *name = "t"; };
  488. // A half-triple is (X0,Z0) held by P0 (and correspondingly (Y1,Z1) held
  489. // by P1), with all values random, but subject to the relation that
  490. // X0*Y1 = Z0+Z1
  491. using HalfTriple = std::tuple<value_t, value_t>;
  492. struct HalfTripleName { static constexpr const char *name = "h"; };
  493. // The type of nodes in a DPF. This must be at least as many bits as
  494. // the security parameter, and at least twice as many bits as value_t.
  495. using DPFnode = __m128i;
  496. // A Select triple is a triple of (X0,Y0,Z0) where X0 is a bit and Y0
  497. // and Z0 are DPFnodes held by P0 (and correspondingly (X1,Y1,Z1) held
  498. // by P1), with all values random, but subject to the relation that
  499. // (X0*Y1) ^ (Y0*X1) = Z0^Z1. These are only used while creating RDPFs
  500. // in the preprocessing phase, so we never need to store them. This is
  501. // a struct instead of a tuple for alignment reasons.
  502. struct SelectTriple {
  503. bit_t X;
  504. DPFnode Y, Z;
  505. };
  506. // These are defined in rdpf.hpp, but declared here to avoid cyclic
  507. // header dependencies.
  508. struct RDPFPair;
  509. struct RDPFPairName { static constexpr const char *name = "r"; };
  510. struct RDPFTriple;
  511. struct RDPFTripleName { static constexpr const char *name = "r"; };
  512. struct CDPF;
  513. struct CDPFName { static constexpr const char *name = "c"; };
  514. // We want the I/O (using << and >>) for many classes
  515. // to just be a common thing: write out the bytes
  516. // straight from memory
  517. #define DEFAULT_IO(CLASSNAME) \
  518. template <typename T> \
  519. T& operator>>(T& is, CLASSNAME &x) \
  520. { \
  521. is.read((char *)&x, sizeof(x)); \
  522. return is; \
  523. } \
  524. \
  525. template <typename T> \
  526. T& operator<<(T& os, const CLASSNAME &x) \
  527. { \
  528. os.write((const char *)&x, sizeof(x)); \
  529. return os; \
  530. }
  531. // Default I/O for various types
  532. DEFAULT_IO(DPFnode)
  533. DEFAULT_IO(RegBS)
  534. DEFAULT_IO(RegAS)
  535. DEFAULT_IO(RegXS)
  536. DEFAULT_IO(MultTriple)
  537. DEFAULT_IO(HalfTriple)
  538. // And for pairs and triples
  539. #define DEFAULT_TUPLE_IO(CLASSNAME) \
  540. template <typename T> \
  541. T& operator>>(T& is, std::tuple<CLASSNAME, CLASSNAME> &x) \
  542. { \
  543. is >> std::get<0>(x) >> std::get<1>(x); \
  544. return is; \
  545. } \
  546. \
  547. template <typename T> \
  548. T& operator<<(T& os, const std::tuple<CLASSNAME, CLASSNAME> &x) \
  549. { \
  550. os << std::get<0>(x) << std::get<1>(x); \
  551. return os; \
  552. } \
  553. \
  554. template <typename T> \
  555. T& operator>>(T& is, std::tuple<CLASSNAME, CLASSNAME, CLASSNAME> &x) \
  556. { \
  557. is >> std::get<0>(x) >> std::get<1>(x) >> std::get<2>(x); \
  558. return is; \
  559. } \
  560. \
  561. template <typename T> \
  562. T& operator<<(T& os, const std::tuple<CLASSNAME, CLASSNAME, CLASSNAME> &x) \
  563. { \
  564. os << std::get<0>(x) << std::get<1>(x) << std::get<2>(x); \
  565. return os; \
  566. }
  567. DEFAULT_TUPLE_IO(RegAS)
  568. DEFAULT_TUPLE_IO(RegXS)
  569. enum ProcessingMode {
  570. MODE_ONLINE, // Online mode, after preprocessing has been done
  571. MODE_PREPROCESSING, // Preprocessing mode
  572. MODE_ONLINEONLY // Online-only mode, where all computations are
  573. }; // done online
  574. #endif