cell.cpp 5.5 KB

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