avl.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #ifndef __AVL_HPP__
  2. #define __AVL_HPP__
  3. #include <optional>
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include <string>
  7. #include "types.hpp"
  8. #include "duoram.hpp"
  9. #include "cdpf.hpp"
  10. #include "mpcio.hpp"
  11. #include "options.hpp"
  12. #include "bst.hpp"
  13. /*
  14. Macro definitions:
  15. AVL_RANDOMIZE_INSERTS: Randomize keys of items inserted in the unit
  16. tests. When turned off, items with incremental keys are inserted
  17. AVL_DEBUG: General debug flag
  18. AVL_DEBUG_BB: Debug flag for balance bit computations
  19. */
  20. // #define AVL_RANDOMIZE_INSERTS
  21. // #define AVL_DEBUG
  22. // #define AVL_DEBUG_BB
  23. /*
  24. For AVL tree we'll treat the pointers fields as:
  25. < L_ptr (31 bits), R_ptr (31 bits), bal_L (1 bit), bal_R (1 bit)>
  26. Where L_ptr and R_ptr are pointers to the left and right child respectively,
  27. and bal_L and bal_R are the balance bits.
  28. Consequently AVL has its own versions of extract and set pointers for its children.
  29. */
  30. #define AVL_PTR_SIZE 31
  31. inline int AVL_TTL(size_t n) {
  32. if(n==0) {
  33. return 0;
  34. } else if (n==1) {
  35. return 1;
  36. } else {
  37. double logn = log2(n);
  38. double TTL = 1.44 * logn;
  39. return (int(ceil(TTL)));
  40. }
  41. }
  42. inline RegXS getAVLLeftPtr(RegXS pointer){
  43. return (pointer>>33);
  44. }
  45. inline RegXS getAVLRightPtr(RegXS pointer){
  46. return ((pointer&(0x00000001FFFFFFFC))>>2);
  47. }
  48. inline void setAVLLeftPtr(RegXS &pointer, RegXS new_ptr){
  49. pointer&=(0x00000001FFFFFFFF);
  50. pointer+=(new_ptr<<33);
  51. }
  52. inline void setAVLRightPtr(RegXS &pointer, RegXS new_ptr){
  53. pointer&=(0xFFFFFFFE00000003);
  54. pointer+=(new_ptr<<2);
  55. }
  56. inline RegBS getLeftBal(RegXS pointer){
  57. RegBS bal_l;
  58. bool bal_l_bit = ((pointer.share() & (0x0000000000000002))>>1);
  59. bal_l.set(bal_l_bit);
  60. return bal_l;
  61. }
  62. inline RegBS getRightBal(RegXS pointer){
  63. RegBS bal_r;
  64. bool bal_r_bit = (pointer.share() & (0x0000000000000001));
  65. bal_r.set(bal_r_bit);
  66. return bal_r;
  67. }
  68. inline void setLeftBal(RegXS &pointer, RegBS bal_l){
  69. value_t temp_ptr = pointer.share();
  70. temp_ptr&=(0xFFFFFFFFFFFFFFFD);
  71. temp_ptr^=((value_t)(bal_l.share()<<1));
  72. pointer.set(temp_ptr);
  73. }
  74. inline void setRightBal(RegXS &pointer, RegBS bal_r){
  75. value_t temp_ptr = pointer.share();
  76. temp_ptr&=(0xFFFFFFFFFFFFFFFE);
  77. temp_ptr^=((value_t)(bal_r.share()));
  78. pointer.set(temp_ptr);
  79. }
  80. inline void dumpAVL(Node n) {
  81. RegBS left_bal, right_bal;
  82. left_bal = getLeftBal(n.pointers);
  83. right_bal = getRightBal(n.pointers);
  84. printf("[%016lx %016lx(L:%ld, R:%ld) %d %d %016lx]", n.key.share(), n.pointers.share(),
  85. getAVLLeftPtr(n.pointers).xshare, getAVLRightPtr(n.pointers).xshare,
  86. left_bal.share(), right_bal.share(), n.value.share());
  87. }
  88. struct avl_del_return {
  89. // Flag to indicate if the key this deletion targets requires a successor swap
  90. RegBS F_ss;
  91. // Pointers to node to be deleted that would be replaced by successor node
  92. RegXS N_d;
  93. // Pointers to successor node that would replace deleted node
  94. RegXS N_s;
  95. // F_r: Flag for updating child pointer with returned pointer
  96. RegBS F_r;
  97. RegXS ret_ptr;
  98. };
  99. struct avl_insert_return {
  100. RegXS gp_node; // grandparent node
  101. RegXS p_node; // parent node
  102. RegXS c_node; // child node
  103. // Direction bits: 0 = Left, 1 = Right
  104. RegBS dir_gpp; // Direction bit from grandparent to parent node
  105. RegBS dir_pc; // Direction bit from p_node to c_node
  106. RegBS dir_cn; // Direction bit from c_node to new_node
  107. RegBS imbalance;
  108. };
  109. class AVL {
  110. private:
  111. Duoram<Node> oram;
  112. RegXS root;
  113. size_t num_items = 0;
  114. size_t cur_max_index = 0;
  115. size_t MAX_SIZE;
  116. int MAX_DEPTH;
  117. bool OPTIMIZED;
  118. std::vector<RegXS> empty_locations;
  119. std::tuple<RegBS, RegBS, RegXS, RegBS> insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  120. RegXS ins_addr, RegAS ins_key, Duoram<Node>::Flat &A, int TTL, RegBS isDummy,
  121. avl_insert_return &ret);
  122. void rotate(MPCTIO &tio, yield_t &yield, RegXS &gp_pointers, RegXS p_ptr,
  123. RegXS &p_pointers, RegXS c_ptr, RegXS &c_pointers, RegBS dir_gpp,
  124. RegBS dir_pc, RegBS isNotDummy, RegBS F_gp);
  125. std::tuple<RegBS, RegBS, RegBS, RegBS> updateBalanceIns(MPCTIO &tio, yield_t &yield,
  126. RegBS bal_l, RegBS bal_r, RegBS bal_upd, RegBS child_dir);
  127. void updateChildPointers(MPCTIO &tio, yield_t &yield, RegXS &left, RegXS &right,
  128. RegBS c_prime, const avl_del_return &ret_struct);
  129. void fixImbalance(MPCTIO &tio, yield_t &yield, Duoram<Node>::Flat &A,
  130. Duoram<Node>::OblivIndex<RegXS,1> oidx, RegXS oidx_oldptrs, RegXS ptr,
  131. RegXS nodeptrs, RegBS p_bal_l, RegBS p_bal_r, RegBS &bal_upd, RegBS c_prime,
  132. RegXS cs_ptr, RegBS imb, RegBS &F_ri, avl_del_return &ret_struct);
  133. void updateRetStruct(MPCTIO &tio, yield_t &yield, RegXS ptr, RegBS F_rs,
  134. RegBS F_dh, RegBS F_ri, RegBS &bal_upd, avl_del_return &ret_struct);
  135. std::tuple<bool, RegBS> del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  136. Duoram<Node>::Flat &A, RegBS F_af, RegBS F_fs, int TTL,
  137. avl_del_return &ret_struct);
  138. std::tuple<RegBS, RegBS, RegBS, RegBS> updateBalanceDel(MPCTIO &tio, yield_t &yield,
  139. RegBS bal_l, RegBS bal_r, RegBS bal_upd, RegBS child_dir);
  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. void pretty_print(const std::vector<Node> &R, value_t node,
  143. const std::string &prefix, bool is_left_child, bool is_right_child);
  144. std::tuple<bool, bool, bool, address_t> check_avl(const std::vector<Node> &R,
  145. value_t node, value_t min_key, value_t max_key);
  146. public:
  147. AVL(int num_players, size_t size, bool opt_flag = true) :
  148. oram(num_players, size), OPTIMIZED(opt_flag) {
  149. this->MAX_SIZE = size;
  150. MAX_DEPTH = 0;
  151. while(size>0) {
  152. MAX_DEPTH+=1;
  153. size=size>>1;
  154. }
  155. };
  156. void init(){
  157. num_items=0;
  158. cur_max_index=0;
  159. empty_locations.clear();
  160. }
  161. void insert(MPCTIO &tio, yield_t &yield, const Node &node);
  162. // Deletes the first node that matches del_key. If an item with del_key
  163. // does not exist in the tree, it results in an explicit (non-oblivious)
  164. // failure.
  165. bool del(MPCTIO &tio, yield_t &yield, RegAS del_key);
  166. // Returns the first node that matches key
  167. bool lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node);
  168. // Non-obliviously initialize an AVL tree of a particular size
  169. void initialize(MPCTIO &tio, yield_t &yield, size_t depth);
  170. // Display and correctness check functions
  171. void pretty_print(MPCTIO &tio, yield_t &yield);
  172. bool check_avl(MPCTIO &tio, yield_t &yield);
  173. void print_oram(MPCTIO &tio, yield_t &yield);
  174. // For test functions ONLY:
  175. Duoram<Node>* get_oram() {
  176. return &oram;
  177. };
  178. RegXS get_root() {
  179. return root;
  180. };
  181. };
  182. void avl(MPCIO &mpcio, const PRACOptions &opts, char **args);
  183. void avl_tests(MPCIO &mpcio, const PRACOptions &opts, char **args);
  184. #endif