types.hpp 15 KB

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