cell.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <functional>
  2. #include "types.hpp"
  3. #include "duoram.hpp"
  4. #include "cell.hpp"
  5. // This file demonstrates how to implement custom ORAM wide cell types.
  6. // Such types can be structures of arbitrary numbers of RegAS and RegXS
  7. // fields. The example here imagines a cell of a binary search tree,
  8. // where you would want the key to be additively shared (so that you can
  9. // easily do comparisons), the pointers field to be XOR shared (so that
  10. // you can easily do bit operations to pack two pointers and maybe some
  11. // tree balancing information into one field) and the value doesn't
  12. // really matter, but XOR shared is usually slightly more efficient.
  13. struct Cell {
  14. RegAS key;
  15. RegXS pointers;
  16. RegXS value;
  17. // Field-access macros so we can write A[i].CELL_KEY instead of
  18. // A[i].field(&Cell::key)
  19. #define CELL_KEY field(&Cell::key)
  20. #define CELL_POINTERS field(&Cell::pointers)
  21. #define CELL_VALUE field(&Cell::value)
  22. // For debugging and checking answers
  23. void dump() const {
  24. printf("[%016lx %016lx %016lx]", key.share(), pointers.share(),
  25. value.share());
  26. }
  27. // You'll need to be able to create a random element, and do the
  28. // operations +=, +, -=, - (binary and unary). Note that for
  29. // XOR-shared fields, + and - are both really XOR.
  30. inline void randomize() {
  31. key.randomize();
  32. pointers.randomize();
  33. value.randomize();
  34. }
  35. inline Cell &operator+=(const Cell &rhs) {
  36. this->key += rhs.key;
  37. this->pointers += rhs.pointers;
  38. this->value += rhs.value;
  39. return *this;
  40. }
  41. inline Cell operator+(const Cell &rhs) const {
  42. Cell res = *this;
  43. res += rhs;
  44. return res;
  45. }
  46. inline Cell &operator-=(const Cell &rhs) {
  47. this->key -= rhs.key;
  48. this->pointers -= rhs.pointers;
  49. this->value -= rhs.value;
  50. return *this;
  51. }
  52. inline Cell operator-(const Cell &rhs) const {
  53. Cell res = *this;
  54. res -= rhs;
  55. return res;
  56. }
  57. inline Cell operator-() const {
  58. Cell res;
  59. res.key = -this->key;
  60. res.pointers = -this->pointers;
  61. res.value = -this->value;
  62. return res;
  63. }
  64. // Multiply each field by the local share of the corresponding field
  65. // in the argument
  66. inline Cell mulshare(const Cell &rhs) const {
  67. Cell res = *this;
  68. res.key.mulshareeq(rhs.key);
  69. res.pointers.mulshareeq(rhs.pointers);
  70. res.value.mulshareeq(rhs.value);
  71. return res;
  72. }
  73. // You need a method to turn a leaf node of a DPF into a share of a
  74. // unit element of your type. Typically set each RegAS to
  75. // dpf.unit_as(leaf) and each RegXS or RegBS to dpf.unit_bs(leaf).
  76. // Note that RegXS will extend a RegBS of 1 to the all-1s word, not
  77. // the word with value 1. This is used for ORAM reads, where the
  78. // same DPF is used for all the fields.
  79. template <nbits_t WIDTH>
  80. inline void unit(const RDPF<WIDTH> &dpf, DPFnode leaf) {
  81. key = dpf.unit_as(leaf);
  82. pointers = dpf.unit_bs(leaf);
  83. value = dpf.unit_bs(leaf);
  84. }
  85. // Perform an update on each of the fields, using field-specific
  86. // MemRefs constructed from the Shape shape and the index idx
  87. template <typename Sh, typename U>
  88. inline static void update(Sh &shape, yield_t &shyield, U idx,
  89. const Cell &M) {
  90. run_coroutines(shyield,
  91. [&shape, &idx, &M] (yield_t &yield) {
  92. Sh Sh_coro = shape.context(yield);
  93. Sh_coro[idx].CELL_KEY += M.key;
  94. },
  95. [&shape, &idx, &M] (yield_t &yield) {
  96. Sh Sh_coro = shape.context(yield);
  97. Sh_coro[idx].CELL_POINTERS += M.pointers;
  98. },
  99. [&shape, &idx, &M] (yield_t &yield) {
  100. Sh Sh_coro = shape.context(yield);
  101. Sh_coro[idx].CELL_VALUE += M.value;
  102. });
  103. }
  104. };
  105. // I/O operations (for sending over the network)
  106. template <typename T>
  107. T& operator>>(T& is, Cell &x)
  108. {
  109. is >> x.key >> x.pointers >> x.value;
  110. return is;
  111. }
  112. template <typename T>
  113. T& operator<<(T& os, const Cell &x)
  114. {
  115. os << x.key << x.pointers << x.value;
  116. return os;
  117. }
  118. // This macro will define I/O on tuples of two or three of the cell type
  119. DEFAULT_TUPLE_IO(Cell)
  120. // Now we use the cell in various ways. This function is called by
  121. // online.cpp.
  122. void cell(MPCIO &mpcio,
  123. const PRACOptions &opts, char **args)
  124. {
  125. nbits_t depth=4;
  126. if (*args) {
  127. depth = atoi(*args);
  128. ++args;
  129. }
  130. MPCTIO tio(mpcio, 0, opts.num_threads);
  131. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  132. size_t size = size_t(1)<<depth;
  133. Duoram<Cell> oram(tio.player(), size);
  134. auto A = oram.flat(tio, yield);
  135. Cell c;
  136. c.key.set(0x0102030405060708);
  137. c.pointers.set(0x1112131415161718);
  138. c.value.set(0x2122232425262728);
  139. // Explicit write
  140. A[0] = c;
  141. RegAS idx;
  142. // Explicit read
  143. Cell expl_read_c = A[0];
  144. printf("expl_read_c = ");
  145. expl_read_c.dump();
  146. printf("\n");
  147. // ORAM read
  148. Cell oram_read_c = A[idx];
  149. printf("oram_read_c = ");
  150. oram_read_c.dump();
  151. printf("\n");
  152. RegXS valueupdate;
  153. valueupdate.set(0x4040404040404040 * tio.player());
  154. RegXS pointersset;
  155. pointersset.set(0x123456789abcdef0 * tio.player());
  156. // Explicit update and write of individual fields
  157. A[1].CELL_VALUE += valueupdate;
  158. A[3].CELL_POINTERS = pointersset;
  159. // Explicit read of individual field
  160. RegXS pointval = A[0].CELL_POINTERS;
  161. printf("pointval = ");
  162. pointval.dump();
  163. printf("\n");
  164. idx.set(1 * tio.player());
  165. // ORAM read of individual field
  166. RegXS oram_value_read = A[idx].CELL_VALUE;
  167. printf("oram_value_read = ");
  168. oram_value_read.dump();
  169. printf("\n");
  170. valueupdate.set(0x8080808080808080 * tio.player());
  171. // ORAM update of individual field
  172. A[idx].CELL_VALUE += valueupdate;
  173. idx.set(2 * tio.player());
  174. // ORAM write of individual field
  175. A[idx].CELL_VALUE = valueupdate;
  176. c.key.set(0x0102030405060708 * tio.player());
  177. c.pointers.set(0x1112131415161718 * tio.player());
  178. c.value.set(0x2122232425262728 * tio.player());
  179. // ORAM update of full Cell
  180. A[idx] += c;
  181. idx.set(3 * tio.player());
  182. // ORAM write of full Cell
  183. A[idx] = c;
  184. printf("\n");
  185. if (depth < 10) {
  186. oram.dump();
  187. auto R = A.reconstruct();
  188. if (tio.player() == 0) {
  189. for(size_t i=0;i<R.size();++i) {
  190. printf("\n%04lx ", i);
  191. R[i].dump();
  192. }
  193. printf("\n");
  194. }
  195. }
  196. });
  197. }