cell.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #ifndef __CELL_HPP__
  2. #define __CELL_HPP__
  3. #include "mpcio.hpp"
  4. #include "options.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,
  81. typename RDPF<WIDTH>::LeafNode leaf) {
  82. key = dpf.unit_as(leaf);
  83. pointers = dpf.unit_bs(leaf);
  84. value = dpf.unit_bs(leaf);
  85. }
  86. // Perform an update on each of the fields, using field-specific
  87. // MemRefs constructed from the Shape shape and the index idx
  88. template <typename Sh, typename U>
  89. inline static void update(Sh &shape, yield_t &shyield, U idx,
  90. const Cell &M) {
  91. run_coroutines(shyield,
  92. [&shape, &idx, &M] (yield_t &yield) {
  93. Sh Sh_coro = shape.context(yield);
  94. Sh_coro[idx].CELL_KEY += M.key;
  95. },
  96. [&shape, &idx, &M] (yield_t &yield) {
  97. Sh Sh_coro = shape.context(yield);
  98. Sh_coro[idx].CELL_POINTERS += M.pointers;
  99. },
  100. [&shape, &idx, &M] (yield_t &yield) {
  101. Sh Sh_coro = shape.context(yield);
  102. Sh_coro[idx].CELL_VALUE += M.value;
  103. });
  104. }
  105. };
  106. // I/O operations (for sending over the network)
  107. template <typename T>
  108. T& operator>>(T& is, Cell &x)
  109. {
  110. is >> x.key >> x.pointers >> x.value;
  111. return is;
  112. }
  113. template <typename T>
  114. T& operator<<(T& os, const Cell &x)
  115. {
  116. os << x.key << x.pointers << x.value;
  117. return os;
  118. }
  119. // This macro will define I/O on tuples of two or three of the cell type
  120. DEFAULT_TUPLE_IO(Cell)
  121. void cell(MPCIO &mpcio,
  122. const PRACOptions &opts, char **args);
  123. #endif