cell.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. inline void unit(const RDPF &dpf, DPFnode leaf) {
  80. key = dpf.unit_as(leaf);
  81. pointers = dpf.unit_bs(leaf);
  82. value = dpf.unit_bs(leaf);
  83. }
  84. // Perform an update on each of the fields, using field-specific
  85. // MemRefs constructed from the Shape shape and the index idx
  86. template <typename Sh, typename U>
  87. inline static void update(Sh &shape, yield_t &shyield, U idx,
  88. const Cell &M) {
  89. run_coroutines(shyield,
  90. [&shape, &idx, &M] (yield_t &yield) {
  91. Sh Sh_coro = shape.context(yield);
  92. Sh_coro[idx].CELL_KEY += M.key;
  93. },
  94. [&shape, &idx, &M] (yield_t &yield) {
  95. Sh Sh_coro = shape.context(yield);
  96. Sh_coro[idx].CELL_POINTERS += M.pointers;
  97. },
  98. [&shape, &idx, &M] (yield_t &yield) {
  99. Sh Sh_coro = shape.context(yield);
  100. Sh_coro[idx].CELL_VALUE += M.value;
  101. });
  102. }
  103. };
  104. // I/O operations (for sending over the network)
  105. template <typename T>
  106. T& operator>>(T& is, Cell &x)
  107. {
  108. is >> x.key >> x.pointers >> x.value;
  109. return is;
  110. }
  111. template <typename T>
  112. T& operator<<(T& os, const Cell &x)
  113. {
  114. os << x.key << x.pointers << x.value;
  115. return os;
  116. }
  117. // This macro will define I/O on tuples of two or three of the cell type
  118. DEFAULT_TUPLE_IO(Cell)
  119. // Now we use the cell in various ways. This function is called by
  120. // online.cpp.
  121. void cell(MPCIO &mpcio,
  122. const PRACOptions &opts, char **args)
  123. {
  124. nbits_t depth=4;
  125. if (*args) {
  126. depth = atoi(*args);
  127. ++args;
  128. }
  129. MPCTIO tio(mpcio, 0, opts.num_threads);
  130. run_coroutines(tio, [&tio, depth] (yield_t &yield) {
  131. size_t size = size_t(1)<<depth;
  132. Duoram<Cell> oram(tio.player(), size);
  133. auto A = oram.flat(tio, yield);
  134. Cell c;
  135. c.key.set(0x0102030405060708);
  136. c.pointers.set(0x1112131415161718);
  137. c.value.set(0x2122232425262728);
  138. // Explicit write
  139. A[0] = c;
  140. RegAS idx;
  141. // Explicit read
  142. Cell expl_read_c = A[0];
  143. printf("expl_read_c = ");
  144. expl_read_c.dump();
  145. printf("\n");
  146. // ORAM read
  147. Cell oram_read_c = A[idx];
  148. printf("oram_read_c = ");
  149. oram_read_c.dump();
  150. printf("\n");
  151. RegXS valueupdate;
  152. valueupdate.set(0x4040404040404040 * tio.player());
  153. RegXS pointersset;
  154. pointersset.set(0x123456789abcdef0 * tio.player());
  155. // Explicit update and write of individual fields
  156. A[1].CELL_VALUE += valueupdate;
  157. A[3].CELL_POINTERS = pointersset;
  158. // Explicit read of individual field
  159. RegXS pointval = A[0].CELL_POINTERS;
  160. printf("pointval = ");
  161. pointval.dump();
  162. printf("\n");
  163. idx.set(1 * tio.player());
  164. // ORAM read of individual field
  165. RegXS oram_value_read = A[idx].CELL_VALUE;
  166. printf("oram_value_read = ");
  167. oram_value_read.dump();
  168. printf("\n");
  169. valueupdate.set(0x8080808080808080 * tio.player());
  170. // ORAM update of individual field
  171. A[idx].CELL_VALUE += valueupdate;
  172. idx.set(2 * tio.player());
  173. // ORAM write of individual field
  174. A[idx].CELL_VALUE = valueupdate;
  175. c.key.set(0x0102030405060708 * tio.player());
  176. c.pointers.set(0x1112131415161718 * tio.player());
  177. c.value.set(0x2122232425262728 * tio.player());
  178. // ORAM update of full Cell
  179. A[idx] += c;
  180. idx.set(3 * tio.player());
  181. // ORAM write of full Cell
  182. A[idx] = c;
  183. printf("\n");
  184. if (depth < 10) {
  185. oram.dump();
  186. auto R = A.reconstruct();
  187. if (tio.player() == 0) {
  188. for(size_t i=0;i<R.size();++i) {
  189. printf("\n%04lx ", i);
  190. R[i].dump();
  191. }
  192. printf("\n");
  193. }
  194. }
  195. });
  196. }