avl.hpp 6.5 KB

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