bst.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #ifndef __NODE_HPP__
  2. #define __NODE_HPP__
  3. #include "types.hpp"
  4. #include "duoram.hpp"
  5. #include "cdpf.hpp"
  6. #include "mpcio.hpp"
  7. #include "options.hpp"
  8. struct Node {
  9. RegAS key;
  10. RegXS pointers;
  11. RegXS value;
  12. // Field-access macros so we can write A[i].NODE_KEY instead of
  13. // A[i].field(&Node::key)
  14. #define NODE_KEY field(&Node::key)
  15. #define NODE_POINTERS field(&Node::pointers)
  16. #define NODE_VALUE field(&Node::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 Node &operator+=(const Node &rhs) {
  31. this->key += rhs.key;
  32. this->pointers += rhs.pointers;
  33. this->value += rhs.value;
  34. return *this;
  35. }
  36. inline Node operator+(const Node &rhs) const {
  37. Node res = *this;
  38. res += rhs;
  39. return res;
  40. }
  41. inline Node &operator-=(const Node &rhs) {
  42. this->key -= rhs.key;
  43. this->pointers -= rhs.pointers;
  44. this->value -= rhs.value;
  45. return *this;
  46. }
  47. inline Node operator-(const Node &rhs) const {
  48. Node res = *this;
  49. res -= rhs;
  50. return res;
  51. }
  52. inline Node operator-() const {
  53. Node 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 Node mulshare(const Node &rhs) const {
  62. Node 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 Node &M) {
  84. run_coroutines(shyield,
  85. [&shape, &idx, &M] (yield_t &yield) {
  86. Sh Sh_coro = shape.context(yield);
  87. Sh_coro[idx].NODE_KEY += M.key;
  88. },
  89. [&shape, &idx, &M] (yield_t &yield) {
  90. Sh Sh_coro = shape.context(yield);
  91. Sh_coro[idx].NODE_POINTERS += M.pointers;
  92. },
  93. [&shape, &idx, &M] (yield_t &yield) {
  94. Sh Sh_coro = shape.context(yield);
  95. Sh_coro[idx].NODE_VALUE += M.value;
  96. });
  97. }
  98. };
  99. // I/O operations (for sending over the network)
  100. template <typename T>
  101. T& operator>>(T& is, Node &x)
  102. {
  103. is >> x.key >> x.pointers >> x.value;
  104. return is;
  105. }
  106. template <typename T>
  107. T& operator<<(T& os, const Node &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 node type
  113. DEFAULT_TUPLE_IO(Node)
  114. struct del_return {
  115. // Flag to indicate if the key to delete was found in tree
  116. RegBS F_f;
  117. RegXS ret_ptr;
  118. // Flag to indicate if the key this deletion requires a successor swap
  119. RegBS F_ss;
  120. // Pointers to node to delete and successor node that would replace
  121. // deleted node
  122. RegXS N_d;
  123. RegXS N_s;
  124. };
  125. class BST {
  126. private:
  127. Duoram<Node> *oram;
  128. RegXS root;
  129. size_t num_items = 0;
  130. size_t MAX_SIZE;
  131. std::tuple<RegXS, RegBS> insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  132. const Node &new_node, Duoram<Node>::Flat &A, int TTL, RegBS isDummy);
  133. void insert(MPCTIO &tio, yield_t &yield, const Node &node, Duoram<Node>::Flat &A);
  134. int del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  135. Duoram<Node>::Flat &A, RegBS F_af, RegBS F_fs, int TTL,
  136. del_return &ret_struct);
  137. public:
  138. BST(int num_players, size_t size) {
  139. this->initialize(num_players, size);
  140. };
  141. ~BST() {
  142. if(oram)
  143. delete oram;
  144. };
  145. void initialize(int num_players, size_t size);
  146. void insert(MPCTIO &tio, yield_t &yield, Node &node);
  147. int del(MPCTIO &tio, yield_t &yield, RegAS del_key);
  148. // Display and correctness check functions
  149. void pretty_print(MPCTIO &tio, yield_t &yield);
  150. void pretty_print(const std::vector<Node> &R, value_t node,
  151. const std::string &prefix, bool is_left_child, bool is_right_child);
  152. void check_bst(MPCTIO &tio, yield_t &yield);
  153. std::tuple<bool, address_t> check_bst(const std::vector<Node> &R,
  154. value_t node, value_t min_key, value_t max_key);
  155. };
  156. /*
  157. class BST_OP {
  158. private:
  159. MPCTIO tio;
  160. yield_t yield;
  161. BST *ptr;
  162. public:
  163. BST_OP* init(MPCTIO &tio, yield_t &yield, BST *bst_ptr) {
  164. this->tio = tio;
  165. this->yield = yield;
  166. this->ptr = bst_ptr;
  167. return this;
  168. }
  169. };
  170. */
  171. void bst(MPCIO &mpcio,
  172. const PRACOptions &opts, char **args);
  173. #endif