bst.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. template <nbits_t WIDTH>
  75. inline void unit(const RDPF<WIDTH> &dpf,
  76. typename RDPF<WIDTH>::LeafNode leaf) {
  77. key = dpf.unit_as(leaf);
  78. pointers = dpf.unit_bs(leaf);
  79. value = dpf.unit_bs(leaf);
  80. }
  81. // Perform an update on each of the fields, using field-specific
  82. // MemRefs constructed from the Shape shape and the index idx
  83. template <typename Sh, typename U>
  84. inline static void update(Sh &shape, yield_t &shyield, U idx,
  85. const Node &M) {
  86. run_coroutines(shyield,
  87. [&shape, &idx, &M] (yield_t &yield) {
  88. Sh Sh_coro = shape.context(yield);
  89. Sh_coro[idx].NODE_KEY += M.key;
  90. },
  91. [&shape, &idx, &M] (yield_t &yield) {
  92. Sh Sh_coro = shape.context(yield);
  93. Sh_coro[idx].NODE_POINTERS += M.pointers;
  94. },
  95. [&shape, &idx, &M] (yield_t &yield) {
  96. Sh Sh_coro = shape.context(yield);
  97. Sh_coro[idx].NODE_VALUE += M.value;
  98. });
  99. }
  100. };
  101. // I/O operations (for sending over the network)
  102. template <typename T>
  103. T& operator>>(T& is, Node &x)
  104. {
  105. is >> x.key >> x.pointers >> x.value;
  106. return is;
  107. }
  108. template <typename T>
  109. T& operator<<(T& os, const Node &x)
  110. {
  111. os << x.key << x.pointers << x.value;
  112. return os;
  113. }
  114. // This macro will define I/O on tuples of two or three of the node type
  115. DEFAULT_TUPLE_IO(Node)
  116. struct del_return {
  117. // Flag to indicate if the key this deletion requires a successor swap
  118. RegBS F_ss;
  119. // Pointers to node to delete and successor node that would replace
  120. // deleted node
  121. RegXS N_d;
  122. RegXS N_s;
  123. // Flag for updating child pointer with returned pointer
  124. RegBS F_r;
  125. RegXS ret_ptr;
  126. };
  127. class BST {
  128. private:
  129. Duoram<Node> *oram;
  130. RegXS root;
  131. size_t num_items = 0;
  132. size_t MAX_SIZE;
  133. std::vector<RegXS> empty_locations;
  134. std::tuple<RegXS, RegBS> insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  135. const Node &new_node, Duoram<Node>::Flat &A, int TTL, RegBS isDummy);
  136. void insert(MPCTIO &tio, yield_t &yield, const Node &node, Duoram<Node>::Flat &A);
  137. bool del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  138. Duoram<Node>::Flat &A, RegBS F_af, RegBS F_fs, int TTL,
  139. del_return &ret_struct);
  140. bool lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key,
  141. Duoram<Node>::Flat &A, int TTL, RegBS isDummy, Node *ret_node);
  142. public:
  143. BST(int num_players, size_t size) {
  144. this->initialize(num_players, size);
  145. };
  146. ~BST() {
  147. if(oram)
  148. delete oram;
  149. };
  150. void initialize(int num_players, size_t size);
  151. void insert(MPCTIO &tio, yield_t &yield, Node &node);
  152. // Deletes the first node that matches del_key
  153. bool del(MPCTIO &tio, yield_t &yield, RegAS del_key);
  154. // Returns the first node that matches key
  155. bool lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node);
  156. // Display and correctness check functions
  157. void pretty_print(MPCTIO &tio, yield_t &yield);
  158. void pretty_print(const std::vector<Node> &R, value_t node,
  159. const std::string &prefix, bool is_left_child, bool is_right_child);
  160. void check_bst(MPCTIO &tio, yield_t &yield);
  161. std::tuple<bool, address_t> check_bst(const std::vector<Node> &R,
  162. value_t node, value_t min_key, value_t max_key);
  163. void print_oram(MPCTIO &tio, yield_t &yield);
  164. size_t numEmptyLocations(){
  165. return(empty_locations.size());
  166. };
  167. };
  168. /*
  169. class BST_OP {
  170. private:
  171. MPCTIO tio;
  172. yield_t yield;
  173. BST *ptr;
  174. public:
  175. BST_OP* init(MPCTIO &tio, yield_t &yield, BST *bst_ptr) {
  176. this->tio = tio;
  177. this->yield = yield;
  178. this->ptr = bst_ptr;
  179. return this;
  180. }
  181. };
  182. */
  183. void bst(MPCIO &mpcio,
  184. const PRACOptions &opts, char **args);
  185. #endif