bst.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifndef __BST_HPP__
  2. #define __BST_HPP__
  3. #include "types.hpp"
  4. #include "duoram.hpp"
  5. #include "cdpf.hpp"
  6. #include "mpcio.hpp"
  7. #include "options.hpp"
  8. // Some simple utility functions:
  9. bool reconstruct_RegBS(MPCTIO &tio, yield_t &yield, RegBS flag);
  10. size_t reconstruct_RegAS(MPCTIO &tio, yield_t &yield, RegAS variable);
  11. size_t reconstruct_RegXS(MPCTIO &tio, yield_t &yield, RegXS variable);
  12. struct Node {
  13. RegAS key;
  14. RegXS pointers;
  15. RegXS value;
  16. // Field-access macros so we can write A[i].NODE_KEY instead of
  17. // A[i].field(&Node::key)
  18. #define NODE_KEY field(&Node::key)
  19. #define NODE_POINTERS field(&Node::pointers)
  20. #define NODE_VALUE field(&Node::value)
  21. // For debugging and checking answers
  22. void dump() const {
  23. printf("[%016lx %016lx %016lx]", key.share(), pointers.share(),
  24. value.share());
  25. }
  26. // You'll need to be able to create a random element, and do the
  27. // operations +=, +, -=, - (binary and unary). Note that for
  28. // XOR-shared fields, + and - are both really XOR.
  29. inline void randomize() {
  30. key.randomize();
  31. pointers.randomize();
  32. value.randomize();
  33. }
  34. inline Node &operator+=(const Node &rhs) {
  35. this->key += rhs.key;
  36. this->pointers += rhs.pointers;
  37. this->value += rhs.value;
  38. return *this;
  39. }
  40. inline Node operator+(const Node &rhs) const {
  41. Node res = *this;
  42. res += rhs;
  43. return res;
  44. }
  45. inline Node &operator-=(const Node &rhs) {
  46. this->key -= rhs.key;
  47. this->pointers -= rhs.pointers;
  48. this->value -= rhs.value;
  49. return *this;
  50. }
  51. inline Node operator-(const Node &rhs) const {
  52. Node res = *this;
  53. res -= rhs;
  54. return res;
  55. }
  56. inline Node operator-() const {
  57. Node res;
  58. res.key = -this->key;
  59. res.pointers = -this->pointers;
  60. res.value = -this->value;
  61. return res;
  62. }
  63. // Multiply each field by the local share of the corresponding field
  64. // in the argument
  65. inline Node mulshare(const Node &rhs) const {
  66. Node res = *this;
  67. res.key.mulshareeq(rhs.key);
  68. res.pointers.mulshareeq(rhs.pointers);
  69. res.value.mulshareeq(rhs.value);
  70. return res;
  71. }
  72. // You need a method to turn a leaf node of a DPF into a share of a
  73. // unit element of your type. Typically set each RegAS to
  74. // dpf.unit_as(leaf) and each RegXS or RegBS to dpf.unit_bs(leaf).
  75. // Note that RegXS will extend a RegBS of 1 to the all-1s word, not
  76. // the word with value 1. This is used for ORAM reads, where the
  77. // same DPF is used for all the fields.
  78. template <nbits_t WIDTH>
  79. inline void unit(const RDPF<WIDTH> &dpf,
  80. typename RDPF<WIDTH>::LeafNode leaf) {
  81. key = dpf.unit_as(leaf);
  82. pointers = dpf.unit_bs(leaf);
  83. value = dpf.unit_bs(leaf);
  84. }
  85. // Perform an update on each of the fields, using field-specific
  86. // MemRefs constructed from the Shape shape and the index idx
  87. template <typename Sh, typename U>
  88. inline static void update(Sh &shape, yield_t &shyield, U idx,
  89. const Node &M) {
  90. run_coroutines(shyield,
  91. [&shape, &idx, &M] (yield_t &yield) {
  92. Sh Sh_coro = shape.context(yield);
  93. Sh_coro[idx].NODE_KEY += M.key;
  94. },
  95. [&shape, &idx, &M] (yield_t &yield) {
  96. Sh Sh_coro = shape.context(yield);
  97. Sh_coro[idx].NODE_POINTERS += M.pointers;
  98. },
  99. [&shape, &idx, &M] (yield_t &yield) {
  100. Sh Sh_coro = shape.context(yield);
  101. Sh_coro[idx].NODE_VALUE += M.value;
  102. });
  103. }
  104. };
  105. void newnode(Node &a);
  106. std::tuple<RegBS, RegBS> compare_keys(MPCTIO tio, yield_t &yield, Node n1, Node n2);
  107. std::tuple<RegBS, RegBS> compare_keys(MPCTIO tio, yield_t &yield, RegAS k1, RegAS k2);
  108. // I/O operations (for sending over the network)
  109. template <typename T>
  110. T& operator>>(T& is, Node &x)
  111. {
  112. is >> x.key >> x.pointers >> x.value;
  113. return is;
  114. }
  115. template <typename T>
  116. T& operator<<(T& os, const Node &x)
  117. {
  118. os << x.key << x.pointers << x.value;
  119. return os;
  120. }
  121. // This macro will define I/O on tuples of two or three of the node type
  122. DEFAULT_TUPLE_IO(Node)
  123. struct del_return {
  124. // Flag to indicate if the key this deletion targets requires a successor swap
  125. RegBS F_ss;
  126. // Pointers to node to delete and successor node that would replace
  127. // deleted node
  128. RegXS N_d;
  129. RegXS N_s;
  130. // Flag for updating child pointer with returned pointer
  131. RegBS F_r;
  132. RegXS ret_ptr;
  133. };
  134. class BST {
  135. private:
  136. Duoram<Node> *oram;
  137. RegXS root;
  138. size_t num_items = 0;
  139. size_t MAX_SIZE;
  140. std::vector<RegXS> empty_locations;
  141. std::tuple<RegXS, RegBS> insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  142. const Node &new_node, Duoram<Node>::Flat &A, int TTL, RegBS isDummy);
  143. void insert(MPCTIO &tio, yield_t &yield, const Node &node, Duoram<Node>::Flat &A);
  144. bool del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  145. Duoram<Node>::Flat &A, RegBS F_af, RegBS F_fs, int TTL,
  146. del_return &ret_struct);
  147. bool lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key,
  148. Duoram<Node>::Flat &A, int TTL, RegBS isDummy, Node *ret_node);
  149. public:
  150. BST(int num_players, size_t size) {
  151. this->initialize(num_players, size);
  152. };
  153. ~BST() {
  154. if(oram)
  155. delete oram;
  156. };
  157. size_t numEmptyLocations(){
  158. return(empty_locations.size());
  159. };
  160. void initialize(int num_players, size_t size);
  161. void insert(MPCTIO &tio, yield_t &yield, Node &node);
  162. // Deletes the first node that matches del_key
  163. bool del(MPCTIO &tio, yield_t &yield, RegAS del_key);
  164. // Returns the first node that matches key
  165. bool lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node);
  166. // Display and correctness check functions
  167. void pretty_print(MPCTIO &tio, yield_t &yield);
  168. void pretty_print(const std::vector<Node> &R, value_t node,
  169. const std::string &prefix, bool is_left_child, bool is_right_child);
  170. void check_bst(MPCTIO &tio, yield_t &yield);
  171. std::tuple<bool, address_t> check_bst(const std::vector<Node> &R,
  172. value_t node, value_t min_key, value_t max_key);
  173. void print_oram(MPCTIO &tio, yield_t &yield);
  174. };
  175. /*
  176. class BST_OP {
  177. private:
  178. MPCTIO tio;
  179. yield_t yield;
  180. BST *ptr;
  181. public:
  182. BST_OP* init(MPCTIO &tio, yield_t &yield, BST *bst_ptr) {
  183. this->tio = tio;
  184. this->yield = yield;
  185. this->ptr = bst_ptr;
  186. return this;
  187. }
  188. };
  189. */
  190. void bst(MPCIO &mpcio,
  191. const PRACOptions &opts, char **args);
  192. #endif