avl.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifndef __AVL_HPP__
  2. #define __AVL_HPP__
  3. #include <math.h>
  4. #include <stdio.h>
  5. #include <string>
  6. #include "types.hpp"
  7. #include "duoram.hpp"
  8. #include "cdpf.hpp"
  9. #include "mpcio.hpp"
  10. #include "options.hpp"
  11. #include "bst.hpp"
  12. #define KNRM "\x1B[0m"
  13. #define KRED "\x1B[31m"
  14. #define KGRN "\x1B[32m"
  15. #define KYEL "\x1B[33m"
  16. #define KBLU "\x1B[34m"
  17. #define KMAG "\x1B[35m"
  18. #define KCYN "\x1B[36m"
  19. #define KWHT "\x1B[37m"
  20. #define OPT_ON 0
  21. // #define RANDOMIZE 0
  22. // #define DEBUG 0
  23. // #define DEBUG_BB 0
  24. /*
  25. For AVL tree we'll treat the pointers fields as:
  26. < L_ptr (31 bits), R_ptr (31 bits), bal_L (1 bit), bal_R (1 bit)>
  27. Where L_ptr and R_ptr are pointers to the left and right child respectively,
  28. and bal_L and bal_R are the balance bits.
  29. Consequently AVL has its own versions of extract and set pointers for its children.
  30. */
  31. #define AVL_PTR_SIZE 31
  32. inline int AVL_TTL(size_t n) {
  33. if(n==0) {
  34. return 0;
  35. } else if (n==1) {
  36. return 1;
  37. } else {
  38. double logn = log2(n);
  39. double TTL = 1.44 * logn;
  40. return (int(ceil(TTL)));
  41. }
  42. }
  43. inline RegXS getAVLLeftPtr(RegXS pointer){
  44. return ((pointer&(0xFFFFFFFF00000000))>>33);
  45. }
  46. inline RegXS getAVLRightPtr(RegXS pointer){
  47. return ((pointer&(0x00000001FFFFFFFF))>>2);
  48. }
  49. inline void setAVLLeftPtr(RegXS &pointer, RegXS new_ptr){
  50. pointer&=(0x00000001FFFFFFFF);
  51. pointer+=(new_ptr<<33);
  52. }
  53. inline void setAVLRightPtr(RegXS &pointer, RegXS new_ptr){
  54. pointer&=(0xFFFFFFFE00000003);
  55. pointer+=(new_ptr<<2);
  56. }
  57. inline RegBS getLeftBal(RegXS pointer){
  58. RegBS bal_l;
  59. bool bal_l_bit = ((pointer.share() & (0x0000000000000002))>>1) & 1;
  60. bal_l.set(bal_l_bit);
  61. return bal_l;
  62. }
  63. inline RegBS getRightBal(RegXS pointer){
  64. RegBS bal_r;
  65. bool bal_r_bit = (pointer.share() & (0x0000000000000001)) & 1;
  66. bal_r.set(bal_r_bit);
  67. return bal_r;
  68. }
  69. inline void setLeftBal(RegXS &pointer, RegBS bal_l){
  70. value_t temp_ptr = pointer.share();
  71. temp_ptr&=(0xFFFFFFFFFFFFFFFD);
  72. temp_ptr^=((value_t)(bal_l.share()<<1));
  73. pointer.set(temp_ptr);
  74. }
  75. inline void setRightBal(RegXS &pointer, RegBS bal_r){
  76. value_t temp_ptr = pointer.share();
  77. temp_ptr&=(0xFFFFFFFFFFFFFFFE);
  78. temp_ptr^=((value_t)(bal_r.share()));
  79. pointer.set(temp_ptr);
  80. }
  81. inline void dumpAVL(Node n) {
  82. RegBS left_bal, right_bal;
  83. left_bal = getLeftBal(n.pointers);
  84. right_bal = getRightBal(n.pointers);
  85. printf("[%016lx %016lx(L:%ld, R:%ld) %d %d %016lx]", n.key.share(), n.pointers.share(),
  86. getAVLLeftPtr(n.pointers).xshare, getAVLRightPtr(n.pointers).xshare,
  87. left_bal.share(), right_bal.share(), n.value.share());
  88. }
  89. struct avl_del_return {
  90. // Flag to indicate if the key this deletion targets requires a successor swap
  91. RegBS F_ss;
  92. // Pointers to node to be deleted that would be replaced by successor node
  93. RegXS N_d;
  94. // Pointers to successor node that would replace deleted node
  95. RegXS N_s;
  96. // F_r: Flag for updating child pointer with returned pointer
  97. RegBS F_r;
  98. RegXS ret_ptr;
  99. };
  100. struct avl_insert_return {
  101. RegXS gp_node; // grandparent node
  102. RegXS p_node; // parent node
  103. RegXS c_node; // child node
  104. // Direction bits: 0 = Left, 1 = Right
  105. RegBS dir_gpp; // Direction bit from grandparent to parent node
  106. RegBS dir_pc; // Direction bit from p_node to c_node
  107. RegBS dir_cn; // Direction bit from c_node to new_node
  108. RegBS imbalance;
  109. };
  110. class AVL {
  111. private:
  112. Duoram<Node> oram;
  113. RegXS root;
  114. size_t num_items = 0;
  115. size_t cur_max_index = 0;
  116. size_t MAX_SIZE;
  117. int MAX_DEPTH;
  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, 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. public:
  143. AVL(int num_players, size_t size) : oram(num_players, size) {
  144. this->MAX_SIZE = size;
  145. MAX_DEPTH = 0;
  146. while(size>0) {
  147. MAX_DEPTH+=1;
  148. size=size>>1;
  149. }
  150. };
  151. void init(){
  152. num_items=0;
  153. cur_max_index=0;
  154. empty_locations.clear();
  155. }
  156. size_t numEmptyLocations(){
  157. return(empty_locations.size());
  158. };
  159. void insert(MPCTIO &tio, yield_t &yield, const Node &node);
  160. // Deletes the first node that matches del_key
  161. bool del(MPCTIO &tio, yield_t &yield, RegAS del_key);
  162. // Returns the first node that matches key
  163. bool lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node);
  164. // Non-obliviously initialize an AVL tree of a particular size
  165. void initialize(MPCTIO &tio, yield_t &yield, size_t depth);
  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_avl(MPCTIO &tio, yield_t &yield);
  171. std::tuple<bool, bool, bool, address_t> check_avl(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. // 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