cell.cpp 6.6 KB

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