cell.cpp 6.2 KB

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