avl.cpp 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736
  1. #include <functional>
  2. #include "avl.hpp"
  3. void print_green(std::string line) {
  4. printf("%s%s%s", KGRN, line.c_str(), KNRM);
  5. }
  6. void print_red(std::string line) {
  7. printf("%s%s%s", KRED, line.c_str(), KNRM);
  8. }
  9. // Pretty-print a reconstructed BST, rooted at node. is_left_child and
  10. // is_right_child indicate whether node is a left or right child of its
  11. // parent. They cannot both be true, but the root of the tree has both
  12. // of them false.
  13. void AVL::pretty_print(const std::vector<Node> &R, value_t node,
  14. const std::string &prefix = "", bool is_left_child = false,
  15. bool is_right_child = false)
  16. {
  17. if (node == 0) {
  18. // NULL pointer
  19. if (is_left_child) {
  20. printf("%s\xE2\x95\xA7\n", prefix.c_str()); // ╧
  21. } else if (is_right_child) {
  22. printf("%s\xE2\x95\xA4\n", prefix.c_str()); // ╤
  23. } else {
  24. printf("%s\xE2\x95\xA2\n", prefix.c_str()); // ╢
  25. }
  26. return;
  27. }
  28. const Node &n = R[node];
  29. value_t left_ptr = getAVLLeftPtr(n.pointers).xshare;
  30. value_t right_ptr = getAVLRightPtr(n.pointers).xshare;
  31. std::string rightprefix(prefix), leftprefix(prefix),
  32. nodeprefix(prefix);
  33. if (is_left_child) {
  34. rightprefix.append("\xE2\x94\x82"); // │
  35. leftprefix.append(" ");
  36. nodeprefix.append("\xE2\x94\x94"); // └
  37. } else if (is_right_child) {
  38. rightprefix.append(" ");
  39. leftprefix.append("\xE2\x94\x82"); // │
  40. nodeprefix.append("\xE2\x94\x8C"); // ┌
  41. } else {
  42. rightprefix.append(" ");
  43. leftprefix.append(" ");
  44. nodeprefix.append("\xE2\x94\x80"); // ─
  45. }
  46. pretty_print(R, right_ptr, rightprefix, false, true);
  47. printf("%s\xE2\x94\xA4", nodeprefix.c_str()); // ┤
  48. dumpAVL(n);
  49. printf("\n");
  50. pretty_print(R, left_ptr, leftprefix, true, false);
  51. }
  52. void AVL::print_oram(MPCTIO &tio, yield_t &yield) {
  53. auto A = oram.flat(tio, yield);
  54. auto R = A.reconstruct();
  55. for(size_t i=0;i<R.size();++i) {
  56. printf("\n%04lx ", i);
  57. R[i].dump();
  58. }
  59. printf("\n");
  60. }
  61. void AVL::pretty_print(MPCTIO &tio, yield_t &yield) {
  62. RegXS peer_root;
  63. RegXS reconstructed_root = root;
  64. if (tio.player() == 1) {
  65. tio.queue_peer(&root, sizeof(root));
  66. } else {
  67. RegXS peer_root;
  68. tio.recv_peer(&peer_root, sizeof(peer_root));
  69. reconstructed_root += peer_root;
  70. }
  71. auto A = oram.flat(tio, yield);
  72. auto R = A.reconstruct();
  73. if(tio.player()==0) {
  74. pretty_print(R, reconstructed_root.xshare);
  75. }
  76. }
  77. // Check the BST invariant of the tree (that all keys to the left are
  78. // less than or equal to this key, all keys to the right are strictly
  79. // greater, and this is true recursively). Returns a
  80. // tuple<bool,address_t>, where the bool says whether the BST invariant
  81. // holds, and the address_t is the height of the tree (which will be
  82. // useful later when we check AVL trees).
  83. std::tuple<bool, bool, address_t> AVL::check_avl(const std::vector<Node> &R,
  84. value_t node, value_t min_key = 0, value_t max_key = ~0)
  85. {
  86. if (node == 0) {
  87. return { true, true, 0 };
  88. }
  89. const Node &n = R[node];
  90. value_t key = n.key.ashare;
  91. value_t left_ptr = getAVLLeftPtr(n.pointers).xshare;
  92. value_t right_ptr = getAVLRightPtr(n.pointers).xshare;
  93. auto [leftok, leftavlok, leftheight ] = check_avl(R, left_ptr, min_key, key);
  94. auto [rightok, rightavlok, rightheight ] = check_avl(R, right_ptr, key+1, max_key);
  95. address_t height = leftheight;
  96. if (rightheight > height) {
  97. height = rightheight;
  98. }
  99. height += 1;
  100. int heightgap = leftheight - rightheight;
  101. bool avlok = (abs(heightgap)<2);
  102. //printf("node = %ld, leftok = %d, rightok = %d\n", node, leftok, rightok);
  103. return { leftok && rightok && key >= min_key && key <= max_key,
  104. avlok && leftavlok && rightavlok, height};
  105. }
  106. void AVL::check_avl(MPCTIO &tio, yield_t &yield) {
  107. auto A = oram.flat(tio, yield);
  108. auto R = A.reconstruct();
  109. RegXS rec_root = this->root;
  110. if (tio.player() == 1) {
  111. tio.queue_peer(&(this->root), sizeof(this->root));
  112. } else {
  113. RegXS peer_root;
  114. tio.recv_peer(&peer_root, sizeof(peer_root));
  115. rec_root+= peer_root;
  116. }
  117. if (tio.player() == 0) {
  118. auto [ bst_ok, avl_ok, height ] = check_avl(R, rec_root.xshare);
  119. printf("BST structure %s\nAVL structure %s\nTree height = %u\n",
  120. bst_ok ? "ok" : "NOT OK", avl_ok ? "ok" : "NOT OK", height);
  121. }
  122. }
  123. /*
  124. Rotate: (gp = grandparent (if exists), p = parent, c = child)
  125. This rotates the p -> c link.
  126. gp gp
  127. \ \
  128. p --- Left rotate ---> c
  129. \ /
  130. c p
  131. gp gp
  132. \ \
  133. p --- Right rotate ---> c
  134. / \
  135. c p
  136. */
  137. void AVL::rotate(MPCTIO &tio, yield_t &yield, RegXS &gp_pointers, RegXS p_ptr,
  138. RegXS &p_pointers, RegXS c_ptr, RegXS &c_pointers, RegBS dir_gpp,
  139. RegBS dir_pc, RegBS isReal, RegBS F_gp) {
  140. bool player0 = tio.player()==0;
  141. RegXS gp_left = getAVLLeftPtr(gp_pointers);
  142. RegXS gp_right = getAVLRightPtr(gp_pointers);
  143. RegXS p_left = getAVLLeftPtr(p_pointers);
  144. RegXS p_right = getAVLRightPtr(p_pointers);
  145. RegXS c_left = getAVLLeftPtr(c_pointers);
  146. RegXS c_right = getAVLRightPtr(c_pointers);
  147. RegXS ptr_upd;
  148. // F_gpp: Flag to update gp -> p link, F_pc: Flag to update p -> c link
  149. RegBS F_gpp, F_pc, F_gppr, F_gppl;
  150. // We care about !F_gp. If !F_gp, then we do the gp->p link updates.
  151. // Otherwise, we do NOT do any updates to gp-> p link;
  152. // since F_gp==1, implies gp does not exist and parent is root.
  153. if(player0)
  154. F_gp^=1;
  155. mpc_and(tio, yield, F_gpp, F_gp, isReal);
  156. // i) gp[dir_gpp] <-- c_ptr
  157. mpc_select(tio, yield, ptr_upd, F_gpp, p_ptr, c_ptr);
  158. mpc_and(tio, yield, F_gppr, F_gpp, dir_gpp);
  159. mpc_select(tio, yield, gp_right, F_gppr, gp_right, ptr_upd);
  160. if(player0)
  161. dir_gpp^=1;
  162. mpc_and(tio, yield, F_gppl, F_gpp, dir_gpp);
  163. mpc_select(tio, yield, gp_left, F_gppl, gp_left, ptr_upd);
  164. setAVLLeftPtr(gp_pointers, gp_left);
  165. setAVLRightPtr(gp_pointers, gp_right);
  166. // ii) p[dir_pc] <-- c[!dir_pc] and iii) c[!dir_pc] <-- p_ptr
  167. RegBS not_dir_pc = dir_pc;
  168. if(player0)
  169. not_dir_pc^=1;
  170. RegXS c_not_dir_pc; //c[!dir_pc]
  171. // ndpc_right: if not_dir_pc is right
  172. // ndpc_left: if not_dir_pc is left
  173. RegBS F_ndpc_right, F_ndpc_left;
  174. mpc_and(tio, yield, F_ndpc_right, isReal, not_dir_pc);
  175. mpc_select(tio, yield, c_not_dir_pc, F_ndpc_right, c_not_dir_pc, c_right, AVL_PTR_SIZE);
  176. // Negating not_dir_pc to handle left case
  177. if(player0)
  178. not_dir_pc^=1;
  179. mpc_and(tio, yield, F_ndpc_left, isReal, not_dir_pc);
  180. mpc_select(tio, yield, c_not_dir_pc, F_ndpc_left, c_not_dir_pc, c_left, AVL_PTR_SIZE);
  181. // Now c_not_dir_pc = c[!dir_pc]
  182. // ii) p[dir_pc] <-- c[!dir_pc]
  183. mpc_select(tio, yield, p_left, F_ndpc_right, p_left, c_not_dir_pc, AVL_PTR_SIZE);
  184. mpc_select(tio, yield, p_right, F_ndpc_left, p_right, c_not_dir_pc, AVL_PTR_SIZE);
  185. setAVLLeftPtr(p_pointers, p_left);
  186. setAVLRightPtr(p_pointers, p_right);
  187. // iii): c[!dir_pc] <-- p_ptr
  188. mpc_select(tio, yield, ptr_upd, isReal, c_not_dir_pc, p_ptr, AVL_PTR_SIZE);
  189. mpc_and(tio, yield, F_pc, dir_pc, isReal);
  190. mpc_select(tio, yield, c_left, F_pc, c_left, ptr_upd, AVL_PTR_SIZE);
  191. if(player0)
  192. dir_pc^=1;
  193. // dir_pc <-- !dir_pc
  194. mpc_and(tio, yield, F_pc, dir_pc, isReal);
  195. mpc_select(tio, yield, c_right, F_pc, c_right, ptr_upd, AVL_PTR_SIZE);
  196. setAVLLeftPtr(c_pointers, c_left);
  197. setAVLRightPtr(c_pointers, c_right);
  198. }
  199. /*
  200. In updateBalanceDel, the position of imbalance, and shift direction for both
  201. cases are inverted, since a bal_upd on a child implies it reduced height.
  202. If F_rs: (bal_upd & right_child)
  203. imbalance, bal_l, balanced, bal_r
  204. And then left shift to get imbalance bit, and new bal_l, bal_r bits
  205. else if F_ls: (bal_upd & left_child)
  206. bal_l, balanced, bal_r, imbalance
  207. And then right shift to get imbalance bit, and new bal_l, bal_r bits
  208. */
  209. std::tuple<RegBS, RegBS, RegBS, RegBS> AVL::updateBalanceDel(MPCTIO &tio, yield_t &yield,
  210. RegBS bal_l, RegBS bal_r, RegBS bal_upd, RegBS child_dir) {
  211. bool player0 = tio.player()==0;
  212. RegBS s0;
  213. RegBS F_rs, F_ls, balanced, imbalance;
  214. /*
  215. bool rec_bal_l, rec_bal_r, rec_bal_upd;
  216. rec_bal_l = reconstruct_RegBS(tio, yield, bal_l);
  217. rec_bal_r = reconstruct_RegBS(tio, yield, bal_r);
  218. rec_bal_upd = reconstruct_RegBS(tio, yield, bal_upd);
  219. printf("In updateBalanceDel, beforeBalance: rec_bal_l = %d, rec_bal_r = %d, rec_bal_upd = %d\n",
  220. rec_bal_l, rec_bal_r, rec_bal_upd);
  221. */
  222. // balanced = is the node currently balanced
  223. balanced = bal_l ^ bal_r;
  224. //F_ls (Flag left shift) <- child_dir & bal_upd
  225. mpc_and(tio, yield, F_ls, child_dir, bal_upd);
  226. if(player0) {
  227. child_dir^=1;
  228. balanced^=1;
  229. }
  230. //F_rs (Flag right shift) <- !child_dir & bal_upd
  231. mpc_and(tio, yield, F_rs, child_dir, bal_upd);
  232. /*
  233. bool rec_F_ls, rec_F_rs;
  234. rec_F_ls = reconstruct_RegBS(tio, yield, F_ls);
  235. rec_F_rs = reconstruct_RegBS(tio, yield, F_rs);
  236. printf("In updateBalanceDel: rec_F_ls = %d, rec_F_rs = %d\n",
  237. rec_F_ls, rec_F_rs);
  238. */
  239. // Left shift if F_ls
  240. mpc_select(tio, yield, imbalance, F_ls, imbalance, bal_l);
  241. mpc_select(tio, yield, bal_l, F_ls, bal_l, balanced);
  242. mpc_select(tio, yield, balanced, F_ls, balanced, bal_r);
  243. mpc_select(tio, yield, bal_r, F_ls, bal_r, s0);
  244. // Right shift if F_rs
  245. mpc_select(tio, yield, imbalance, F_rs, imbalance, bal_r);
  246. mpc_select(tio, yield, bal_r, F_rs, bal_r, balanced);
  247. mpc_select(tio, yield, balanced, F_rs, balanced, bal_l);
  248. mpc_select(tio, yield, bal_l, F_rs, bal_l, s0);
  249. /*
  250. rec_bal_l = reconstruct_RegBS(tio, yield, bal_l);
  251. rec_bal_r = reconstruct_RegBS(tio, yield, bal_r);
  252. rec_bal_upd = reconstruct_RegBS(tio, yield, bal_upd);
  253. printf("In updateBalanceDel, foundterBalance: rec_bal_l = %d, rec_bal_r = %d, rec_bal_upd = %d\n",
  254. rec_bal_l, rec_bal_r, rec_bal_upd);
  255. */
  256. // if(bal_upd) and not imbalance bal_upd<-0
  257. RegBS bu0;
  258. if(player0){
  259. imbalance^=1;
  260. }
  261. mpc_and(tio, yield, bu0, bal_upd, imbalance);
  262. mpc_select(tio, yield, bal_upd, bu0, bal_upd, s0);
  263. if(player0){
  264. imbalance^=1;
  265. }
  266. // Any bal_upd, propogates all the way up to root
  267. return {bal_l, bal_r, bal_upd, imbalance};
  268. }
  269. /*
  270. If F_rs: (bal_upd & right_child)
  271. bal_l, balanced, bal_r, imbalance
  272. And then right shift to get imbalance bit, and new bal_l, bal_r bits
  273. else if F_ls: (bal_upd & left_child)
  274. imbalance, bal_l, balanced, bal_r
  275. And then left shift to get imbalance bit, and new bal_l, bal_r bits
  276. */
  277. std::tuple<RegBS, RegBS, RegBS, RegBS> AVL::updateBalanceIns(MPCTIO &tio, yield_t &yield,
  278. RegBS bal_l, RegBS bal_r, RegBS bal_upd, RegBS child_dir) {
  279. bool player0 = tio.player()==0;
  280. RegBS s1, s0;
  281. s1.set(tio.player()==1);
  282. RegBS F_rs, F_ls, balanced, imbalance;
  283. // balanced = is the node currently balanced
  284. balanced = bal_l ^ bal_r;
  285. //F_rs (Flag right shift) <- child_dir & bal_upd
  286. mpc_and(tio, yield, F_rs, child_dir, bal_upd);
  287. if(player0) {
  288. child_dir^=1;
  289. balanced^=1;
  290. }
  291. //F_ls (Flag left shift) <- !child_dir & bal_upd
  292. mpc_and(tio, yield, F_ls, child_dir, bal_upd);
  293. // Right shift if child_dir = 1 & bal_upd = 1
  294. mpc_select(tio, yield, imbalance, F_rs, imbalance, bal_r);
  295. mpc_select(tio, yield, bal_r, F_rs, bal_r, balanced);
  296. mpc_select(tio, yield, balanced, F_rs, balanced, bal_l);
  297. mpc_select(tio, yield, bal_l, F_rs, bal_l, s0);
  298. // Left shift if child_dir = 0 & bal_upd = 1
  299. mpc_select(tio, yield, imbalance, F_ls, imbalance, bal_l);
  300. mpc_select(tio, yield, bal_l, F_ls, bal_l, balanced);
  301. mpc_select(tio, yield, balanced, F_ls, balanced, bal_r);
  302. mpc_select(tio, yield, bal_r, F_ls, bal_r, s0);
  303. // bal_upd' <- bal_upd ^ imbalance
  304. RegBS F_bu0;
  305. mpc_and(tio, yield, F_bu0, bal_upd, balanced);
  306. mpc_select(tio, yield, bal_upd, F_bu0, bal_upd, s0);
  307. mpc_select(tio, yield, bal_upd, imbalance, bal_upd, s0);
  308. return {bal_l, bal_r, bal_upd, imbalance};
  309. }
  310. std::tuple<RegBS, RegBS, RegXS, RegBS> AVL::insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  311. RegAS insert_key, Duoram<Node>::Flat &A, int TTL, RegBS isDummy, avl_insert_return *ret) {
  312. if(TTL==0) {
  313. RegBS z;
  314. return {z, z, z, z};
  315. }
  316. RegBS isReal = isDummy ^ (tio.player());
  317. Node cnode = A[ptr];
  318. // Compare key
  319. auto [lteq, gt] = compare_keys(tio, yield, cnode.key, insert_key);
  320. // Depending on [lteq, gt] select the next_ptr
  321. RegXS next_ptr;
  322. RegXS left = getAVLLeftPtr(cnode.pointers);
  323. RegXS right = getAVLRightPtr(cnode.pointers);
  324. RegBS bal_l = getLeftBal(cnode.pointers);
  325. RegBS bal_r = getRightBal(cnode.pointers);
  326. /*
  327. size_t rec_left = reconstruct_RegXS(tio, yield, left);
  328. size_t rec_right = reconstruct_RegXS(tio, yield, right);
  329. size_t rec_key = reconstruct_RegAS(tio, yield, cnode.key);
  330. printf("\n\nKey = %ld\n", rec_key);
  331. printf("rec_left = %ld, rec_right = %ld\n", rec_left, rec_right);
  332. */
  333. mpc_select(tio, yield, next_ptr, gt, left, right, AVL_PTR_SIZE);
  334. /*
  335. size_t rec_next_ptr = reconstruct_RegXS(tio, yield, next_ptr);
  336. printf("rec_next_ptr = %ld\n", rec_next_ptr);
  337. */
  338. CDPF dpf = tio.cdpf(yield);
  339. size_t &aes_ops = tio.aes_ops();
  340. // F_z: Check if this is last node on path
  341. RegBS F_z = dpf.is_zero(tio, yield, next_ptr, aes_ops);
  342. RegBS F_i;
  343. // F_i: If this was last node on path (F_z), and isReal insert.
  344. mpc_and(tio, yield, F_i, (isReal), F_z);
  345. isDummy^=F_i;
  346. auto [bal_upd, F_gp, prev_node, prev_dir] = insert(tio, yield,
  347. next_ptr, insert_key, A, TTL-1, isDummy, ret);
  348. /*
  349. rec_bal_upd = reconstruct_RegBS(tio, yield, bal_upd);
  350. rec_F_gp = reconstruct_RegBS(tio, yield, F_gp);
  351. printf("Insert returns: rec_bal_upd = %d, rec_F_gp = %d\n",
  352. rec_bal_upd, rec_F_gp);
  353. size_t rec_ptr = reconstruct_RegXS(tio, yield, ptr);
  354. printf("\nrec_ptr = %ld\n", rec_ptr);
  355. */
  356. // Save insertion pointer and direction
  357. mpc_select(tio, yield, ret->i_node, F_i, ret->i_node, ptr, AVL_PTR_SIZE);
  358. mpc_select(tio, yield, ret->dir_i, F_i, ret->dir_i, gt);
  359. // Update balance
  360. // If we inserted at this level (F_i), bal_upd is set to 1
  361. mpc_or(tio, yield, bal_upd, bal_upd, F_i);
  362. auto [new_bal_l, new_bal_r, new_bal_upd, imbalance] = updateBalanceIns(tio, yield, bal_l, bal_r, bal_upd, gt);
  363. // Store if this insert triggers an imbalance
  364. ret->imbalance ^= imbalance;
  365. // Save grandparent pointer
  366. mpc_select(tio, yield, ret->gp_node, F_gp, ret->gp_node, ptr, AVL_PTR_SIZE);
  367. mpc_select(tio, yield, ret->dir_gpp, F_gp, ret->dir_gpp, gt);
  368. // Save parent pointer
  369. mpc_select(tio, yield, ret->p_node, imbalance, ret->p_node, ptr, AVL_PTR_SIZE);
  370. mpc_select(tio, yield, ret->dir_pc, imbalance, ret->dir_pc, gt);
  371. // Save child pointer
  372. mpc_select(tio, yield, ret->c_node, imbalance, ret->c_node, prev_node, AVL_PTR_SIZE);
  373. mpc_select(tio, yield, ret->dir_cn, imbalance, ret->dir_cn, prev_dir);
  374. // Store new_bal_l and new_bal_r for this node
  375. setLeftBal(cnode.pointers, new_bal_l);
  376. setRightBal(cnode.pointers, new_bal_r);
  377. // We have to write the node pointers anyway to resolve balance updates
  378. // We still defer the actual insertion to post recursion.
  379. A[ptr].NODE_POINTERS = cnode.pointers;
  380. // s0 = shares of 0
  381. RegBS s0;
  382. // Update F_gp flag: If there was an imbalance then we set this to store
  383. // the grandparent node (node in the level above) into the ret_struct
  384. mpc_select(tio, yield, F_gp, imbalance, s0, imbalance);
  385. return {new_bal_upd, F_gp, ptr, gt};
  386. }
  387. // Insert(root, ptr, key, TTL, isDummy) -> (new_ptr, wptr, wnode, f_p)
  388. void AVL::insert(MPCTIO &tio, yield_t &yield, const Node &node) {
  389. bool player0 = tio.player()==0;
  390. auto A = oram.flat(tio, yield);
  391. // If there are no items in tree. Make this new item the root.
  392. if(num_items==0) {
  393. Node zero;
  394. A[0] = zero;
  395. A[1] = node;
  396. (root).set(1*tio.player());
  397. num_items++;
  398. return;
  399. } else {
  400. // Insert node into next free slot in the ORAM
  401. int new_id;
  402. RegXS insert_address;
  403. num_items++;
  404. int TTL = AVL_TTL(num_items);
  405. bool insertAtEmptyLocation = (numEmptyLocations() > 0);
  406. if(insertAtEmptyLocation) {
  407. insert_address = empty_locations.back();
  408. empty_locations.pop_back();
  409. A[insert_address] = node;
  410. } else {
  411. new_id = num_items;
  412. A[new_id] = node;
  413. insert_address.set(new_id * tio.player());
  414. }
  415. RegBS isDummy;
  416. avl_insert_return ret;
  417. RegAS insert_key = node.key;
  418. // Recursive insert function
  419. auto [bal_upd, F_gp, prev_node, prev_dir] = insert(tio, yield, root, insert_key, A, TTL, isDummy, &ret);
  420. /*
  421. // Debug code
  422. bool rec_bal_upd, rec_F_gp, ret_dir_pc, ret_dir_cn;
  423. rec_bal_upd = reconstruct_RegBS(tio, yield, bal_upd);
  424. rec_F_gp = reconstruct_RegBS(tio, yield, F_gp);
  425. ret_dir_pc = reconstruct_RegBS(tio, yield, ret.dir_pc);
  426. ret_dir_cn = reconstruct_RegBS(tio, yield, ret.dir_cn);
  427. printf("(Top level) Insert returns: rec_bal_upd = %d, rec_F_gp = %d\n",
  428. rec_bal_upd, rec_F_gp);
  429. printf("(Top level) Insert returns: ret.dir_pc = %d, rt.dir_cn = %d\n",
  430. ret_dir_pc, ret_dir_cn);
  431. */
  432. // Perform actual insertion
  433. RegXS ins_pointers = A[ret.i_node].NODE_POINTERS;
  434. RegXS left_ptr = getAVLLeftPtr(ins_pointers);
  435. RegXS right_ptr = getAVLRightPtr(ins_pointers);
  436. mpc_select(tio, yield, right_ptr, ret.dir_i, right_ptr, insert_address, AVL_PTR_SIZE);
  437. // ret.dir_i -> !(ret.dir_i)
  438. if(player0) {
  439. ret.dir_i^=1;
  440. }
  441. mpc_select(tio, yield, left_ptr, ret.dir_i, left_ptr, insert_address, AVL_PTR_SIZE);
  442. // We never use ret.dir_i again, so don't bother reverting the negation above.
  443. setAVLLeftPtr(ins_pointers, left_ptr);
  444. setAVLRightPtr(ins_pointers, right_ptr);
  445. A[ret.i_node].NODE_POINTERS = ins_pointers;
  446. // Perform balance procedure
  447. RegXS gp_pointers = A[ret.gp_node].NODE_POINTERS;
  448. RegXS parent_pointers = A[ret.p_node].NODE_POINTERS;
  449. RegXS child_pointers = A[ret.c_node].NODE_POINTERS;
  450. // n_node (child's next node)
  451. RegXS child_left = getAVLLeftPtr(child_pointers);
  452. RegXS child_right = getAVLRightPtr(child_pointers);
  453. RegXS n_node;
  454. mpc_select(tio, yield, n_node, ret.dir_cn, n_node, child_right, AVL_PTR_SIZE);
  455. // dir_cn -> !(dir_cn); to handle left case
  456. if(player0) {
  457. ret.dir_cn^=1;
  458. }
  459. mpc_select(tio, yield, n_node, ret.dir_cn, n_node, child_left, AVL_PTR_SIZE);
  460. // Undo dir_cn negation
  461. if(player0) {
  462. ret.dir_cn^=1;
  463. }
  464. RegXS n_pointers = A[n_node].NODE_POINTERS;
  465. // F_dr = (dir_pc != dir_cn) : i.e., double rotation case if
  466. // (parent->child) and (child->new_node) are not in the same direction
  467. RegBS F_dr = (ret.dir_pc) ^ (ret.dir_cn);
  468. /* Flags: F_cn_rot = child->node rotate
  469. F_ur = update root.
  470. In case of an imbalance we have to always rotate p->c link. (L or R case)
  471. In case of an imbalance where p->c and c->n are in different directions, we have
  472. to perform a double rotation (LR or RL case). In such cases, first rotate
  473. c->n link, and then p->c link
  474. (Note: in the second rotation c is actually n, since the the first rotation
  475. swaps their positions)
  476. */
  477. RegBS F_cn_rot, F_ur;
  478. mpc_and(tio, yield, F_ur, F_gp, ret.imbalance);
  479. mpc_and(tio, yield, F_cn_rot, ret.imbalance, F_dr);
  480. RegBS s0;
  481. // Get the n children information for 2nd rotate fix before rotations happen.
  482. RegBS n_bal_l, n_bal_r;
  483. RegXS n_l = getAVLLeftPtr(n_pointers);
  484. RegXS n_r = getAVLRightPtr(n_pointers);
  485. n_bal_l = getLeftBal(n_pointers);
  486. n_bal_r = getRightBal(n_pointers);
  487. // First rotation: c->n link
  488. rotate(tio, yield, parent_pointers, ret.c_node, child_pointers, n_node,
  489. n_pointers, ret.dir_pc, ret.dir_cn, F_cn_rot, s0);
  490. // If F_cn_rot, i.e. we did first rotation. Then c and n need to swap before the second rotate.
  491. RegXS new_child_pointers, new_child;
  492. mpc_select(tio, yield, new_child_pointers, F_cn_rot, child_pointers, n_pointers);
  493. mpc_select(tio, yield, new_child, F_cn_rot, ret.c_node, n_node, AVL_PTR_SIZE);
  494. // Second rotation: p->c link
  495. rotate(tio, yield, gp_pointers, ret.p_node, parent_pointers, new_child,
  496. new_child_pointers, ret.dir_gpp, ret.dir_pc, ret.imbalance, F_gp);
  497. // Set parent and child balances to 0 if there was an imbalance.
  498. // parent balances are already set to 0 from updateBalanceIns
  499. RegBS temp_bal, p_bal_l, p_bal_r, p_bal_ndpc;
  500. RegBS c_bal_l, c_bal_r, c_bal_dpc, n_bal_dpc, n_bal_ndpc;
  501. p_bal_l = getLeftBal(parent_pointers);
  502. p_bal_r = getRightBal(parent_pointers);
  503. mpc_select(tio, yield, child_pointers, F_cn_rot, new_child_pointers, child_pointers);
  504. mpc_select(tio, yield, n_pointers, F_cn_rot, n_pointers, new_child_pointers);
  505. c_bal_l = getLeftBal(child_pointers);
  506. c_bal_r = getRightBal(child_pointers);
  507. mpc_select(tio, yield, c_bal_l, ret.imbalance, c_bal_l, s0);
  508. mpc_select(tio, yield, c_bal_r, ret.imbalance, c_bal_r, s0);
  509. /* In the double rotation case: balance of c and p have a tweak
  510. p_bal_ndpc <- !(n_bal_ndpc)
  511. c_bal_dpc <- !(n_bal_dpc) */
  512. CDPF cdpf = tio.cdpf(yield);
  513. size_t &aes_ops = tio.aes_ops();
  514. RegBS n_l0 = cdpf.is_zero(tio, yield, n_l, aes_ops);
  515. RegBS n_r0 = cdpf.is_zero(tio, yield, n_r, aes_ops);
  516. RegBS p_c_update, n_has_children;
  517. // n_has_children = !(n_l0 & n_r0)
  518. mpc_and(tio, yield, n_has_children, n_l0, n_r0);
  519. if(player0) {
  520. n_has_children^=1;
  521. }
  522. /*
  523. bool rec_n_l0, rec_n_r0, rec_n_hc;
  524. rec_n_l0 = reconstruct_RegBS(tio, yield, n_l0);
  525. rec_n_r0 = reconstruct_RegBS(tio, yield, n_r0);
  526. rec_n_hc = reconstruct_RegBS(tio, yield, n_has_children);
  527. printf("n_l0 = %d, n_r0 = %d, n_has_children = %d\n", rec_n_l0, rec_n_r0, rec_n_hc);
  528. */
  529. mpc_and(tio, yield, p_c_update, F_cn_rot, n_has_children);
  530. mpc_select(tio, yield, n_bal_ndpc, ret.dir_pc, n_bal_r, n_bal_l);
  531. mpc_select(tio, yield, n_bal_dpc, ret.dir_pc, n_bal_l, n_bal_r);
  532. mpc_select(tio, yield, p_bal_ndpc, ret.dir_pc, p_bal_r, p_bal_l);
  533. // !n_bal_ndpc, !n_bal_dpc
  534. if(player0) {
  535. n_bal_ndpc^=1;
  536. n_bal_dpc^=1;
  537. }
  538. mpc_select(tio, yield, p_bal_ndpc, p_c_update, p_bal_ndpc, n_bal_ndpc);
  539. mpc_select(tio, yield, c_bal_dpc, p_c_update, c_bal_dpc, n_bal_dpc);
  540. mpc_select(tio, yield, p_bal_r, ret.dir_pc, p_bal_ndpc, p_bal_r);
  541. mpc_select(tio, yield, p_bal_l, ret.dir_pc, p_bal_l, p_bal_ndpc);
  542. mpc_select(tio, yield, c_bal_r, ret.dir_pc, c_bal_r, c_bal_dpc);
  543. mpc_select(tio, yield, c_bal_l, ret.dir_pc, c_bal_dpc, c_bal_l);
  544. setLeftBal(parent_pointers, p_bal_l);
  545. setRightBal(parent_pointers, p_bal_r);
  546. setLeftBal(child_pointers, c_bal_l);
  547. setRightBal(child_pointers, c_bal_r);
  548. // Write back update pointers and balances into gp, p, c, and n
  549. A[ret.c_node].NODE_POINTERS = child_pointers;
  550. A[ret.p_node].NODE_POINTERS = parent_pointers;
  551. A[ret.gp_node].NODE_POINTERS = gp_pointers;
  552. A[n_node].NODE_POINTERS = n_pointers;
  553. // Handle root pointer switch (if F_gp is true in the return from insert())
  554. // If F_gp and we did a double rotation: root <-- new node
  555. // If F_gp and we did a single rotation: root <-- child node
  556. mpc_select(tio, yield, root, F_ur, root, ret.c_node, AVL_PTR_SIZE);
  557. mpc_and(tio, yield, F_ur, F_gp, F_dr);
  558. mpc_select(tio, yield, root, F_ur, root, n_node, AVL_PTR_SIZE);
  559. }
  560. }
  561. bool AVL::lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key, Duoram<Node>::Flat &A,
  562. int TTL, RegBS isDummy, Node *ret_node) {
  563. if(TTL==0) {
  564. // Reconstruct and return isDummy
  565. // If we found the key, then isDummy will be true
  566. bool found = reconstruct_RegBS(tio, yield, isDummy);
  567. return found;
  568. }
  569. RegBS isNotDummy = isDummy ^ (tio.player());
  570. Node cnode = A[ptr];
  571. // Compare key
  572. CDPF cdpf = tio.cdpf(yield);
  573. auto [lt, eq, gt] = cdpf.compare(tio, yield, key - cnode.key, tio.aes_ops());
  574. // Depending on [lteq, gt] select the next ptr/index as
  575. // upper 32 bits of cnode.pointers if lteq
  576. // lower 32 bits of cnode.pointers if gt
  577. RegXS left = getAVLLeftPtr(cnode.pointers);
  578. RegXS right = getAVLRightPtr(cnode.pointers);
  579. RegXS next_ptr;
  580. mpc_select(tio, yield, next_ptr, gt, left, right, 32);
  581. RegBS F_found;
  582. // If we haven't found the key yet, and the lookup matches the current node key,
  583. // then we found the node to return
  584. mpc_and(tio, yield, F_found, isNotDummy, eq);
  585. mpc_select(tio, yield, ret_node->key, eq, ret_node->key, cnode.key);
  586. mpc_select(tio, yield, ret_node->value, eq, ret_node->value, cnode.value);
  587. isDummy^=F_found;
  588. bool found = lookup(tio, yield, next_ptr, key, A, TTL-1, isDummy, ret_node);
  589. return found;
  590. }
  591. bool AVL::lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node) {
  592. auto A = oram.flat(tio, yield);
  593. RegBS isDummy;
  594. bool found = lookup(tio, yield, root, key, A, num_items, isDummy, ret_node);
  595. return found;
  596. }
  597. std::tuple<bool, RegBS> AVL::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  598. Duoram<Node>::Flat &A, RegBS found, RegBS find_successor, int TTL,
  599. avl_del_return &ret_struct) {
  600. bool player0 = tio.player()==0;
  601. if(TTL==0) {
  602. //Reconstruct and return found
  603. bool success = reconstruct_RegBS(tio, yield, found);
  604. RegBS zero;
  605. if(player0)
  606. ret_struct.F_r^=1;
  607. return {success, zero};
  608. } else {
  609. Node node = A[ptr];
  610. // Compare key
  611. CDPF cdpf = tio.cdpf(yield);
  612. auto [lt, eq, gt] = cdpf.compare(tio, yield, del_key - node.key, tio.aes_ops());
  613. // c is the direction bit for next_ptr
  614. // (c=0: go left or c=1: go right)
  615. RegBS c = gt;
  616. // lf = local found. We found the key to delete in this level.
  617. RegBS lf = eq;
  618. // Select the next ptr
  619. RegXS left = getAVLLeftPtr(node.pointers);
  620. RegXS right = getAVLRightPtr(node.pointers);
  621. size_t &aes_ops = tio.aes_ops();
  622. // Check if left and right children are 0, and compute F_0, F_1, F_2
  623. RegBS l0 = cdpf.is_zero(tio, yield, left, aes_ops);
  624. RegBS r0 = cdpf.is_zero(tio, yield, right, aes_ops);
  625. RegBS F_0, F_1, F_2;
  626. // F_0 = l0 & r0
  627. mpc_and(tio, yield, F_0, l0, r0);
  628. // F_1 = l0 \xor r0
  629. F_1 = l0 ^ r0;
  630. // F_2 = !(F_0 + F_1) (Only 1 of F_0, F_1, and F_2 can be true)
  631. F_2 = F_0 ^ F_1;
  632. if(player0)
  633. F_2^=1;
  634. // We set next ptr based on c, but we need to handle three
  635. // edge cases where we do not pick next_ptr by just the comparison result
  636. RegXS next_ptr, cs_ptr;
  637. RegBS c_prime;
  638. // Case 1: found the node here (lf), and node has only one child.
  639. // Then we iterate down the only child.
  640. RegBS F_c1, F_c2, F_c3, F_c4;
  641. // Case 1: lf & F_1
  642. mpc_and(tio, yield, F_c1, lf, F_1);
  643. // Set c_prime for Case 1
  644. mpc_select(tio, yield, c_prime, F_c1, c, l0);
  645. // s1: shares of 1 bit, s0: shares of 0 bit
  646. RegBS s1, s0;
  647. s1.set(tio.player()==1);
  648. // Case 2: found the node here (lf) and node has both children (F_2)
  649. // In find successor case, so we find inorder successor for node to be deleted
  650. // (inorder successor = go right and then find leftmost child.)
  651. mpc_and(tio, yield, F_c2, lf, F_2);
  652. mpc_select(tio, yield, c_prime, F_c2, c_prime, s1);
  653. /*
  654. // Reconstruct and Debug Block 2
  655. bool F_c2_rec, s1_rec;
  656. F_c2_rec = reconstruct_RegBS(tio, yield, F_c2);
  657. s1_rec = reconstruct_RegBS(tio, yield, s1);
  658. c_prime_rec = reconstruct_RegBS(tio, yield, c_prime);
  659. printf("c_prime = %d, F_c2 = %d, s1 = %d\n", c_prime_rec, F_c2_rec, s1_rec);
  660. */
  661. // Case 3: finding successor (find_successor) and node has both children (F_2)
  662. // Go left.
  663. mpc_and(tio, yield, F_c3, find_successor, F_2);
  664. mpc_select(tio, yield, c_prime, F_c3, c_prime, s0);
  665. // Case 4: finding successor (find_successor) and node has no more left children (l0)
  666. // This is the successor node then.
  667. // Go right (since no more left)
  668. mpc_and(tio, yield, F_c4, find_successor, l0);
  669. mpc_select(tio, yield, c_prime, F_c4, c_prime, l0);
  670. // Set next_ptr
  671. mpc_select(tio, yield, next_ptr, c_prime, left, right, AVL_PTR_SIZE);
  672. // cs_ptr: child's sibling pointer
  673. mpc_select(tio, yield, cs_ptr, c_prime, right, left, AVL_PTR_SIZE);
  674. RegBS found_prime, find_successor_prime;
  675. mpc_or(tio, yield, found_prime, found, lf);
  676. // If in Case 2, set find_successor. We are now finding successor
  677. mpc_or(tio, yield, find_successor_prime, find_successor, F_c2);
  678. // If in Case 4. Successor found here already. Toggle find_successor off
  679. find_successor_prime=find_successor_prime^F_c4;
  680. TTL-=1;
  681. auto [key_found, bal_upd] = del(tio, yield, next_ptr, del_key, A, found_prime, find_successor_prime, TTL, ret_struct);
  682. // If we didn't find the key, we can end here.
  683. if(!key_found) {
  684. return {0, s0};
  685. }
  686. /* F_rs: Flag for updating the correct child pointer of this node
  687. This happens if F_r is set in ret_struct. F_r indicates if we need
  688. to update a child pointer at this level by skipping the current
  689. child in the direction of traversal. We do this in two cases:
  690. i) F_d & (!F_2) : If we delete here, and this node does not have
  691. 2 children (;i.e., we are not in the finding successor case)
  692. ii) F_ns: Found the successor (no more left children while
  693. traversing to find successor)
  694. In cases i and ii we skip the next node, and make the current node
  695. point to the node foundter the next node.
  696. iii) We did rotation(s) at the lower level, changing the child in
  697. that position. So we update it to the correct node in that
  698. position now.
  699. Whether skip happens or just update happens is handled by how
  700. ret_struct.ret_ptr is set.
  701. */
  702. RegBS F_rr; // Flag to resolve F_r by updating correct child ptr
  703. mpc_and(tio, yield, F_rr, c_prime, ret_struct.F_r);
  704. mpc_select(tio, yield, right, F_rr, right, ret_struct.ret_ptr);
  705. if(player0)
  706. c_prime^=1;
  707. mpc_and(tio, yield, F_rr, c_prime, ret_struct.F_r);
  708. mpc_select(tio, yield, left, F_rr, left, ret_struct.ret_ptr);
  709. if(player0)
  710. c_prime^=1;
  711. setAVLLeftPtr(node.pointers, left);
  712. setAVLRightPtr(node.pointers, right);
  713. // Delay storing pointers back until balance updates are done as well.
  714. // Since we resolved the F_r flag returned, we set it back to 0.
  715. ret_struct.F_r = s0;
  716. RegBS p_bal_l, p_bal_r;
  717. p_bal_l = getLeftBal(node.pointers);
  718. p_bal_r = getRightBal(node.pointers);
  719. auto [new_p_bal_l, new_p_bal_r, new_bal_upd, imb] =
  720. updateBalanceDel(tio, yield, p_bal_l, p_bal_r, bal_upd, c_prime);
  721. /*
  722. // Reconstruct and Debug Block
  723. bool rec_new_bal_upd, rec_imb, rec_bal_upd;
  724. size_t rec_ckey;
  725. rec_new_bal_upd = reconstruct_RegBS(tio, yield, new_bal_upd);
  726. rec_imb = reconstruct_RegBS(tio, yield, imb);
  727. rec_bal_upd = reconstruct_RegBS(tio, yield, bal_upd);
  728. rec_ckey = reconstruct_RegAS(tio, yield, node.key);
  729. bool rec_F_c1, rec_F_c2, rec_F_c3, rec_F_c4;
  730. rec_F_c1 = reconstruct_RegBS(tio, yield, F_c1);
  731. rec_F_c2 = reconstruct_RegBS(tio, yield, F_c2);
  732. rec_F_c3 = reconstruct_RegBS(tio, yield, F_c3);
  733. rec_F_c4 = reconstruct_RegBS(tio, yield, F_c4);
  734. printf("Current Key = %lu\n", rec_ckey);
  735. size_t rec_p_left_0, rec_p_right_0;
  736. rec_p_left_0 = reconstruct_RegXS(tio, yield, getAVLLeftPtr(node.pointers));
  737. rec_p_right_0 = reconstruct_RegXS(tio, yield, getAVLRightPtr(node.pointers));
  738. printf("parent_ptrs (foundter read): left = %lu, right = %lu\n", rec_p_left_0, rec_p_right_0);
  739. printf("F_c1 = %d, F_c2 = %d, F_c3 = %d, F_c4 = %d\n", rec_F_c1, rec_F_c2, rec_F_c3, rec_F_c4);
  740. printf("bal_upd = %d, new_bal_upd = %d, imb= %d\n", rec_bal_upd, rec_new_bal_upd, rec_imb);
  741. */
  742. // F_ri: subflag for F_r. F_ri = returned flag set to 1 from imbalance fix.
  743. RegBS F_ri;
  744. // Perform rotations if imbalance (else dummy rotations)
  745. /*
  746. For capturing both the symmetric L and R cases of rotations, we'll capture directions with
  747. dpc = dir_pc = direction from parent to child, and
  748. ndpc = not(dir_pc)
  749. When we travelled down the stack, we went from p->c. But in deletions to handle any imbalance
  750. we look at c's sibling cs (child's sibling). And the rotation is between p and cs if there
  751. was an imbalance at p, and perhaps even cs and it's child (the child in dir_pc, as that's the
  752. only case that results in a double rotation when deleting).
  753. In case of an imbalance we have to always rotate p->cs link. (L or R case)
  754. If cs.bal_(dir_pc), then we have a double rotation (LR or RL) case.
  755. In such cases, first rotate cs->gcs link, and then p->cs link. gcs = grandchild on cs path
  756. Layout: In the R (or LR) case:
  757. p
  758. / \
  759. cs c
  760. / \
  761. a gcs
  762. / \
  763. x y
  764. - One of x or y must exist for it to be an LR case,
  765. since then cs.bal_(dir_pc) = cs.bal_r = 1
  766. Layout: In the L (or RL) case:
  767. p
  768. / \
  769. c cs
  770. / \
  771. gcs a
  772. / \
  773. x y
  774. - One of x or y must exist for it to be an RL case,
  775. since then cs.bal_(dir_pc) = cs.bal_l = 1
  776. (Note: if double rotation case, in the second rotation cs is actually gcs,
  777. since the the first rotation swaps their positions)
  778. */
  779. Node cs_node = A[cs_ptr];
  780. //dirpc = dir_pc = dpc = c_prime
  781. RegBS cs_bal_l, cs_bal_r, cs_bal_dpc, cs_bal_ndpc, F_dr, not_c_prime;
  782. RegXS gcs_ptr, cs_left, cs_right, cs_dpc, cs_ndpc, null;
  783. // child's sibling node's balances in dir_pc (dpc), and not_dir_pc (ndpc)
  784. cs_bal_l = getLeftBal(cs_node.pointers);
  785. cs_bal_r = getRightBal(cs_node.pointers);
  786. cs_left = getAVLLeftPtr(cs_node.pointers);
  787. cs_right = getAVLRightPtr(cs_node.pointers);
  788. mpc_select(tio, yield, cs_bal_dpc, c_prime, cs_bal_l, cs_bal_r);
  789. mpc_select(tio, yield, cs_bal_ndpc, c_prime, cs_bal_r, cs_bal_l);
  790. mpc_select(tio, yield, cs_dpc, c_prime, cs_left, cs_right);
  791. mpc_select(tio, yield, cs_ndpc, c_prime, cs_right, cs_left);
  792. // We need to double rotate (LR or RL case) if cs_bal_dpc is 1
  793. mpc_and(tio, yield, F_dr, imb, cs_bal_dpc);
  794. mpc_select(tio, yield, gcs_ptr, cs_bal_dpc, cs_ndpc, cs_dpc, AVL_PTR_SIZE);
  795. Node gcs_node = A[gcs_ptr];
  796. not_c_prime = c_prime;
  797. if(player0) {
  798. not_c_prime^=1;
  799. }
  800. // First rotation: cs->gcs link
  801. rotate(tio, yield, node.pointers, cs_ptr, cs_node.pointers, gcs_ptr,
  802. gcs_node.pointers, not_c_prime, c_prime, F_dr, s0);
  803. // If F_dr, we did first rotation. Then cs and gcs need to swap before the second rotate.
  804. RegXS new_cs_pointers, new_cs, new_ptr;
  805. mpc_select(tio, yield, new_cs_pointers, F_dr, cs_node.pointers, gcs_node.pointers);
  806. mpc_select(tio, yield, new_cs, F_dr, cs_ptr, gcs_ptr, AVL_PTR_SIZE);
  807. // Second rotation: p->cs link
  808. // Since we don't have access to gp node here we just send a null and s0
  809. // for gp_pointers and dir_gpp. Instead this pointer fix is handled by F_r
  810. // and ret_struct.ret_ptr.
  811. rotate(tio, yield, null, ptr, node.pointers, new_cs,
  812. new_cs_pointers, s0, not_c_prime, imb, s1);
  813. /*
  814. size_t rec_p_left_1, rec_p_right_1;
  815. bool rec_flag_imb, rec_flag_dr;
  816. rec_flag_imb = reconstruct_RegBS(tio, yield, imb);
  817. rec_flag_dr = reconstruct_RegBS(tio, yield, F_dr);
  818. rec_p_left_1 = reconstruct_RegXS(tio, yield, getAVLLeftPtr(node.pointers));
  819. rec_p_right_1 = reconstruct_RegXS(tio, yield, getAVLRightPtr(node.pointers));
  820. printf("flag_imb = %d, flag_dr = %d\n", rec_flag_imb, rec_flag_dr);
  821. printf("parent_ptrs (foundter rotations): left = %lu, right = %lu\n", rec_p_left_1, rec_p_right_1);
  822. */
  823. // If imb (we do some rotation), then update F_r, and ret_ptr, to
  824. // fix the gp->p link (The F_r clauses later, and this are mutually
  825. // exclusive events. They will never trigger together.)
  826. mpc_select(tio, yield, new_ptr, F_dr, cs_ptr, gcs_ptr);
  827. mpc_select(tio, yield, F_ri, imb, s0, s1);
  828. mpc_select(tio, yield, ret_struct.ret_ptr, imb, ret_struct.ret_ptr, new_ptr);
  829. // Write back new_cs_pointers correctly to (cs_node/gcs_node).pointers
  830. // and then balance the nodes
  831. mpc_select(tio, yield, cs_node.pointers, F_dr, new_cs_pointers, cs_node.pointers);
  832. mpc_select(tio, yield, gcs_node.pointers, F_dr, gcs_node.pointers, new_cs_pointers);
  833. /*
  834. Update balances based on imbalance and type of rotations that happen.
  835. In the case of an imbalance, updateBalance() sets bal_l and bal_r of p to 0.
  836. */
  837. RegBS IC1, IC2, IC3; // Imbalance Case 1, 2 or 3
  838. // IC1 = Single rotation (L/R). L/R = dpc
  839. mpc_and(tio, yield, IC1, imb, cs_bal_ndpc);
  840. // IC3 = Double rotation (LR/RL). 1st rotate direction = ndpc, 2nd direction = dpc
  841. mpc_and(tio, yield, IC3, imb, cs_bal_dpc);
  842. // IC2 = Single rotation (L/R).
  843. IC2 = IC1 ^ IC3;
  844. if(player0) {
  845. IC2^=1;
  846. }
  847. mpc_and(tio, yield, IC2, imb, IC2);
  848. /*
  849. bool rec_IC1, rec_IC2, rec_IC3;
  850. rec_IC1 = reconstruct_RegBS(tio, yield, IC1);
  851. rec_IC2 = reconstruct_RegBS(tio, yield, IC2);
  852. rec_IC3 = reconstruct_RegBS(tio, yield, IC3);
  853. printf("rec_IC1 = %d, rec_IC2 = %d, rec_IC3 = %d\n", rec_IC1, rec_IC2, rec_IC3);
  854. */
  855. // IC1, IC2, IC3: CS.bal = 0 0
  856. mpc_select(tio, yield, cs_bal_dpc, imb, cs_bal_dpc, s0);
  857. mpc_select(tio, yield, cs_bal_ndpc, imb, cs_bal_ndpc, s0);
  858. mpc_select(tio, yield, cs_bal_r, c_prime, cs_bal_ndpc, cs_bal_dpc);
  859. mpc_select(tio, yield, cs_bal_l, c_prime, cs_bal_dpc, cs_bal_ndpc);
  860. // IC2: p.bal_ndpc = 1, cs.bal_dpc = 1
  861. // (IC2 & not_c_prime)
  862. cs_bal_dpc^=IC2;
  863. RegBS p_bal_dpc, p_bal_ndpc;
  864. mpc_select(tio, yield, p_bal_ndpc, c_prime, new_p_bal_r, new_p_bal_l);
  865. p_bal_ndpc^=IC2;
  866. RegBS IC2_ndpc_l, IC2_ndpc_r, IC2_dpc_l, IC2_dpc_r;
  867. mpc_and(tio, yield, IC2_ndpc_l, IC2, c_prime);
  868. mpc_and(tio, yield, IC2_ndpc_r, IC2, not_c_prime);
  869. mpc_and(tio, yield, IC2_dpc_l, IC2, not_c_prime);
  870. mpc_and(tio, yield, IC2_dpc_r, IC2, c_prime);
  871. mpc_select(tio, yield, new_p_bal_l, IC2_ndpc_l, new_p_bal_l, p_bal_ndpc);
  872. mpc_select(tio, yield, new_p_bal_r, IC2_ndpc_r, new_p_bal_r, p_bal_ndpc);
  873. mpc_select(tio, yield, cs_bal_l, IC2_dpc_l, cs_bal_l, cs_bal_dpc);
  874. mpc_select(tio, yield, cs_bal_r, IC2_dpc_r, cs_bal_r, cs_bal_dpc);
  875. // In the IC2 case bal_upd = 0 (The rotation doesn't end up
  876. // decreasing height of this subtree.
  877. mpc_select(tio, yield, bal_upd, IC2, bal_upd, s0);
  878. // IC3:
  879. // To set balance in this case we need to know if gcs.dpc child exists
  880. // and similarly if gcs.ndpc child exitst.
  881. // if(gcs.ndpc child exists): cs.bal_ndpc = 1
  882. // if(gcs.dpc child exists): p.bal_dpc = 1
  883. RegBS gcs_dpc_exists, gcs_ndpc_exists;
  884. RegXS gcs_l = getAVLLeftPtr(gcs_node.pointers);
  885. RegXS gcs_r = getAVLRightPtr(gcs_node.pointers);
  886. RegBS gcs_bal_l = getLeftBal(gcs_node.pointers);
  887. RegBS gcs_bal_r = getRightBal(gcs_node.pointers);
  888. RegXS gcs_dpc, gcs_ndpc;
  889. mpc_select(tio, yield, gcs_dpc, c_prime, gcs_l, gcs_r);
  890. mpc_select(tio, yield, gcs_ndpc, not_c_prime, gcs_l, gcs_r);
  891. gcs_dpc_exists = cdpf.is_zero(tio, yield, gcs_dpc, aes_ops);
  892. gcs_ndpc_exists = cdpf.is_zero(tio, yield, gcs_ndpc, aes_ops);
  893. cs_bal_ndpc^=IC3;
  894. RegBS IC3_ndpc_l, IC3_ndpc_r, IC3_dpc_l, IC3_dpc_r;
  895. mpc_and(tio, yield, IC3_ndpc_l, IC3, c_prime);
  896. mpc_and(tio, yield, IC3_ndpc_r, IC3, not_c_prime);
  897. mpc_and(tio, yield, IC3_dpc_l, IC3, not_c_prime);
  898. mpc_and(tio, yield, IC3_dpc_r, IC3, c_prime);
  899. RegBS f0, f1, f2, f3;
  900. mpc_and(tio, yield, f0, IC3_dpc_l, gcs_dpc_exists);
  901. mpc_and(tio, yield, f1, IC3_dpc_r, gcs_dpc_exists);
  902. mpc_and(tio, yield, f2, IC3_ndpc_l, gcs_ndpc_exists);
  903. mpc_and(tio, yield, f3, IC3_ndpc_r, gcs_ndpc_exists);
  904. mpc_select(tio, yield, new_p_bal_l, f0, new_p_bal_l, IC3);
  905. mpc_select(tio, yield, new_p_bal_r, f1, new_p_bal_r, IC3);
  906. mpc_select(tio, yield, cs_bal_l, f2, cs_bal_l, IC3);
  907. mpc_select(tio, yield, cs_bal_r, f3, cs_bal_r, IC3);
  908. // In IC3 gcs.bal = 0 0
  909. mpc_select(tio, yield, gcs_bal_l, IC3, gcs_bal_l, s0);
  910. mpc_select(tio, yield, gcs_bal_r, IC3, gcs_bal_r, s0);
  911. // Write back <cs_bal_dpc, cs_bal_ndpc> and <gcs_bal_l, gcs_bal_r>
  912. setLeftBal(gcs_node.pointers, gcs_bal_l);
  913. setRightBal(gcs_node.pointers, gcs_bal_r);
  914. setLeftBal(cs_node.pointers, cs_bal_l);
  915. setRightBal(cs_node.pointers, cs_bal_r);
  916. A[cs_ptr].NODE_POINTERS = cs_node.pointers;
  917. A[gcs_ptr].NODE_POINTERS = gcs_node.pointers;
  918. // Write back updated pointers correctly accounting for rotations
  919. setLeftBal(node.pointers, new_p_bal_l);
  920. setRightBal(node.pointers, new_p_bal_r);
  921. A[ptr].NODE_POINTERS = node.pointers;
  922. // Update the return structure
  923. // F_dh = Delete Here flag,
  924. // F_sf = successor found (no more left children while trying to find successor)
  925. // F_rs = subflag for F_r. F_rs = flag for F_r set to 1 from handling a skip fix
  926. // (deleting a node with single child, or found successor cases)
  927. RegBS F_dh, F_sf, F_rs;
  928. mpc_or(tio, yield, ret_struct.F_ss, ret_struct.F_ss, F_c2);
  929. if(player0)
  930. found^=1;
  931. mpc_and(tio, yield, F_dh, lf, found);
  932. mpc_select(tio, yield, ret_struct.N_d, F_dh, ret_struct.N_d, ptr);
  933. // F_sf = Successor found = F_c4 = Finding successor & no more left child
  934. F_sf = F_c4;
  935. if(player0)
  936. F_2^=1;
  937. // If we have to i) delete here, and it doesn't have two children
  938. // we have to update child pointer in parent with the returned pointer
  939. mpc_and(tio, yield, F_rs, F_dh, F_2);
  940. // ii) if we found successor here
  941. mpc_or(tio, yield, F_rs, F_rs, F_sf);
  942. mpc_select(tio, yield, ret_struct.N_s, F_sf, ret_struct.N_s, ptr);
  943. // F_rs and F_ri will never trigger together. So the line below
  944. // set ret_ptr to the correct pointer to handle either case
  945. // If neither F_rs nor F_ri, we set the ret_ptr to current ptr.
  946. RegBS F_nr;
  947. mpc_or(tio, yield, F_nr, F_rs, F_ri);
  948. // F_nr = F_rs || F_ri
  949. ret_struct.F_r = F_nr;
  950. /*
  951. bool rec_ret_F_r, rec_F_rs, rec_F_ri;
  952. rec_ret_F_r = reconstruct_RegBS(tio, yield, ret_struct.F_r);
  953. rec_F_rs = reconstruct_RegBS(tio, yield, F_rs);
  954. rec_F_ri = reconstruct_RegBS(tio, yield, F_ri);
  955. printf("rec_ret_F_r = %d, rec_F_rs = %d, rec_F_ri = %d\n", rec_ret_F_r, rec_F_rs, rec_F_ri);
  956. */
  957. if(player0) {
  958. F_nr^=1;
  959. }
  960. // F_nr = !(F_rs || F_ri)
  961. mpc_select(tio, yield, ret_struct.ret_ptr, F_nr, ret_struct.ret_ptr, ptr);
  962. // If F_rs, we skipped a node, so update bal_upd to 1
  963. mpc_select(tio, yield, bal_upd, F_rs, bal_upd, s1);
  964. /*
  965. rec_F_rs = reconstruct_RegBS(tio, yield, F_rs);
  966. bool rec_bal_upd_set = reconstruct_RegBS(tio, yield, bal_upd);
  967. printf("foundter bal_upd select from rec_F_rs = %d, rec_bal_upd = %d\n",
  968. rec_F_rs, rec_bal_upd_set);
  969. */
  970. // Swap deletion node with successor node done outside of recursive traversal.
  971. return {key_found, bal_upd};
  972. }
  973. }
  974. bool AVL::del(MPCTIO &tio, yield_t &yield, RegAS del_key) {
  975. if(num_items==0)
  976. return 0;
  977. auto A = oram.flat(tio, yield);
  978. if(num_items==1) {
  979. //Delete root
  980. Node zero;
  981. empty_locations.emplace_back(root);
  982. A[root] = zero;
  983. num_items--;
  984. return 1;
  985. } else {
  986. int TTL = AVL_TTL(num_items);
  987. // Flags for already found (found) item to delete and find successor (find_successor)
  988. // if this deletion requires a successor swap
  989. RegBS found, find_successor;
  990. avl_del_return ret_struct;
  991. auto [success, bal_upd] = del(tio, yield, root, del_key, A, found, find_successor, TTL, ret_struct);
  992. printf ("Success = %d\n", success);
  993. if(!success){
  994. return 0;
  995. }
  996. else{
  997. num_items--;
  998. /*
  999. printf("In delete's swap portion\n");
  1000. Node rec_del_node = A.reconstruct(A[ret_struct.N_d]);
  1001. Node rec_suc_node = A.reconstruct(A[ret_struct.N_s]);
  1002. printf("del_node key = %ld, suc_node key = %ld\n",
  1003. rec_del_node.key.ashare, rec_suc_node.key.ashare);
  1004. printf("flag_s = %d\n", ret_struct.F_ss.bshare);
  1005. */
  1006. Node del_node = A[ret_struct.N_d];
  1007. Node suc_node = A[ret_struct.N_s];
  1008. RegAS zero_as; RegXS zero_xs;
  1009. // Update root if needed
  1010. mpc_select(tio, yield, root, ret_struct.F_r, root, ret_struct.ret_ptr);
  1011. /*
  1012. bool rec_F_ss = reconstruct_RegBS(tio, yield, ret_struct.F_ss);
  1013. size_t rec_del_key = reconstruct_RegAS(tio, yield, del_node.key);
  1014. size_t rec_suc_key = reconstruct_RegAS(tio, yield, suc_node.key);
  1015. printf("rec_F_ss = %d, del_node.key = %lu, suc_nod.key = %lu\n",
  1016. rec_F_ss, rec_del_key, rec_suc_key);
  1017. */
  1018. mpc_select(tio, yield, del_node.key, ret_struct.F_ss, del_node.key, suc_node.key);
  1019. mpc_select(tio, yield, del_node.value, ret_struct.F_ss, del_node.value, suc_node.value);
  1020. A[ret_struct.N_d].NODE_KEY = del_node.key;
  1021. A[ret_struct.N_d].NODE_VALUE = del_node.value;
  1022. A[ret_struct.N_s].NODE_KEY = zero_as;
  1023. A[ret_struct.N_s].NODE_VALUE = zero_xs;
  1024. RegXS empty_loc;
  1025. mpc_select(tio, yield, empty_loc, ret_struct.F_ss, ret_struct.N_d, ret_struct.N_s);
  1026. //Add deleted (empty) location into the empty_locations vector for reuse in next insert()
  1027. empty_locations.emplace_back(empty_loc);
  1028. }
  1029. return 1;
  1030. }
  1031. }
  1032. // Now we use the AVL class in various ways. This function is called by
  1033. // online.cpp.
  1034. void avl(MPCIO &mpcio,
  1035. const PRACOptions &opts, char **args)
  1036. {
  1037. nbits_t depth=4;
  1038. if (*args) {
  1039. depth = atoi(*args);
  1040. ++args;
  1041. }
  1042. size_t items = (size_t(1)<<depth)-1;
  1043. if (*args) {
  1044. items = atoi(*args);
  1045. ++args;
  1046. }
  1047. MPCTIO tio(mpcio, 0, opts.num_threads);
  1048. run_coroutines(tio, [&tio, depth, items] (yield_t &yield) {
  1049. size_t size = size_t(1)<<depth;
  1050. AVL tree(tio.player(), size);
  1051. // Insert a few elements
  1052. int insert_array[] = {10, 10, 13, 11, 14, 8, 15, 20, 17, 19, 7, 12};
  1053. size_t insert_array_size = 11;
  1054. //int insert_array[] = {10, 10, 13, 11, 14, 8, 15, 20, 17, 19, 7, 12};
  1055. //size_t insert_array_size = 11;
  1056. //int insert_array[] = {6, 3, 10, 1, 2};
  1057. //size_t insert_array_size = 4;
  1058. Node node;
  1059. for(size_t i = 0; i<=insert_array_size; i++) {
  1060. newnode(node);
  1061. node.key.set(insert_array[i] * tio.player());
  1062. printf("Insert %d\n", insert_array[i]);
  1063. tree.insert(tio, yield, node);
  1064. tree.print_oram(tio, yield);
  1065. tree.pretty_print(tio, yield);
  1066. tree.check_avl(tio, yield);
  1067. }
  1068. RegAS del_key;
  1069. del_key.set(10 * tio.player());
  1070. printf("Delete 10\n");
  1071. tree.del(tio, yield, del_key);
  1072. tree.print_oram(tio, yield);
  1073. tree.pretty_print(tio, yield);
  1074. tree.check_avl(tio, yield);
  1075. del_key.set(14 * tio.player());
  1076. printf("Delete 14\n");
  1077. tree.del(tio, yield, del_key);
  1078. tree.print_oram(tio, yield);
  1079. tree.pretty_print(tio, yield);
  1080. tree.check_avl(tio, yield);
  1081. tree.pretty_print(tio, yield);
  1082. del_key.set(12 * tio.player());
  1083. printf("Delete 12\n");
  1084. tree.del(tio, yield, del_key);
  1085. tree.print_oram(tio, yield);
  1086. tree.pretty_print(tio, yield);
  1087. tree.check_avl(tio, yield);
  1088. RegAS lookup_key;
  1089. Node lookup;
  1090. bool success;
  1091. lookup_key.set(8 * tio.player());
  1092. success = tree.lookup(tio, yield, lookup_key, &lookup);
  1093. if(success) {
  1094. printf("Lookup 8 success\n");
  1095. }
  1096. else {
  1097. printf("Lookup 8 failed\n");
  1098. }
  1099. lookup_key.set(12 * tio.player());
  1100. success = tree.lookup(tio, yield, lookup_key, &lookup);
  1101. if(success) {
  1102. printf("Lookup 12 success\n");
  1103. }
  1104. else {
  1105. printf("Lookup 12 failed\n");
  1106. }
  1107. });
  1108. }
  1109. void avl_tests(MPCIO &mpcio,
  1110. const PRACOptions &opts, char **args)
  1111. {
  1112. // Not taking arguments for tests
  1113. nbits_t depth=4;
  1114. size_t items = (size_t(1)<<depth)-1;
  1115. MPCTIO tio(mpcio, 0, opts.num_threads);
  1116. run_coroutines(tio, [&tio, depth, items] (yield_t &yield) {
  1117. size_t size = size_t(1)<<depth;
  1118. bool player0 = tio.player()==0;
  1119. AVL tree(tio.player(), size);
  1120. // (T1) : Test 1 : L rotation (root modified)
  1121. /*
  1122. Operation:
  1123. 5 7
  1124. \ / \
  1125. 7 ---> 5 9
  1126. \
  1127. 9
  1128. T1 checks:
  1129. - root is 7
  1130. - 5,7,9 in correct positions
  1131. - 5 and 9 have no children and 0 balances
  1132. */
  1133. {
  1134. bool success = 1;
  1135. int insert_array[] = {5, 7, 9};
  1136. size_t insert_array_size = 2;
  1137. Node node;
  1138. for(size_t i = 0; i<=insert_array_size; i++) {
  1139. newnode(node);
  1140. node.key.set(insert_array[i] * tio.player());
  1141. tree.insert(tio, yield, node);
  1142. tree.check_avl(tio, yield);
  1143. }
  1144. Duoram<Node>* oram = tree.get_oram();
  1145. RegXS root_xs = tree.get_root();
  1146. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1147. auto A = oram->flat(tio, yield);
  1148. auto R = A.reconstruct();
  1149. Node root_node, left_node, right_node;
  1150. size_t left_index, right_index;
  1151. root_node = R[root];
  1152. if((root_node.key).share()!=7) {
  1153. success = false;
  1154. }
  1155. left_index = (getAVLLeftPtr(root_node.pointers)).share();
  1156. right_index = (getAVLRightPtr(root_node.pointers)).share();
  1157. left_node = R[left_index];
  1158. right_node = R[right_index];
  1159. if(left_node.key.share()!=5 || right_node.key.share()!=9) {
  1160. success = false;
  1161. }
  1162. //To check that left and right have no children and 0 balances
  1163. size_t sum = left_node.pointers.share() + right_node.pointers.share();
  1164. if(sum!=0) {
  1165. success = false;
  1166. }
  1167. if(player0) {
  1168. if(success) {
  1169. print_green("T1 : SUCCESS\n");
  1170. } else {
  1171. print_red("T1 : FAIL\n");
  1172. }
  1173. }
  1174. A.init();
  1175. tree.init();
  1176. }
  1177. // (T2) : Test 2 : L rotation (root unmodified)
  1178. /*
  1179. Operation:
  1180. 5 5
  1181. / \ / \
  1182. 3 7 3 9
  1183. \ ---> / \
  1184. 9 7 7 12
  1185. \
  1186. 12
  1187. T2 checks:
  1188. - root is 5
  1189. - 3, 7, 9, 12 in expected positions
  1190. - Nodes 3, 7, 12 have 0 balance and no children
  1191. - 5's bal = 0 1
  1192. */
  1193. {
  1194. bool success = 1;
  1195. int insert_array[] = {5, 3, 7, 9, 12};
  1196. size_t insert_array_size = 4;
  1197. Node node;
  1198. for(size_t i = 0; i<=insert_array_size; i++) {
  1199. newnode(node);
  1200. node.key.set(insert_array[i] * tio.player());
  1201. tree.insert(tio, yield, node);
  1202. tree.check_avl(tio, yield);
  1203. }
  1204. Duoram<Node>* oram = tree.get_oram();
  1205. RegXS root_xs = tree.get_root();
  1206. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1207. auto A = oram->flat(tio, yield);
  1208. auto R = A.reconstruct();
  1209. Node root_node, n3, n7, n9, n12;
  1210. size_t n3_index, n7_index, n9_index, n12_index;
  1211. root_node = R[root];
  1212. if((root_node.key).share()!=5) {
  1213. success = false;
  1214. }
  1215. n3_index = (getAVLLeftPtr(root_node.pointers)).share();
  1216. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  1217. n3 = R[n3_index];
  1218. n9 = R[n9_index];
  1219. n7_index = getAVLLeftPtr(n9.pointers).share();
  1220. n12_index = getAVLRightPtr(n9.pointers).share();
  1221. n7 = R[n7_index];
  1222. n12 = R[n12_index];
  1223. // Node value checks
  1224. if(n3.key.share()!=3 || n9.key.share()!=9) {
  1225. success = false;
  1226. }
  1227. if(n7.key.share()!=7 || n12.key.share()!=12) {
  1228. success = false;
  1229. }
  1230. // Node children and balance checks
  1231. size_t zero = 0;
  1232. zero+=(n3.pointers.share());
  1233. zero+=(n7.pointers.share());
  1234. zero+=(n12.pointers.share());
  1235. zero+=(getLeftBal(root_node.pointers).share());
  1236. zero+=(getLeftBal(n9.pointers).share());
  1237. zero+=(getRightBal(n9.pointers).share());
  1238. if(zero!=0) {
  1239. success = false;
  1240. }
  1241. int one = (getRightBal(root_node.pointers).share());
  1242. if(one!=1) {
  1243. success = false;
  1244. }
  1245. if(player0) {
  1246. if(success) {
  1247. print_green("T2 : SUCCESS\n");
  1248. } else {
  1249. print_red("T2 : FAIL\n");
  1250. }
  1251. }
  1252. A.init();
  1253. tree.init();
  1254. }
  1255. // (T3) : Test 3 : R rotation (root modified)
  1256. /*
  1257. Operation:
  1258. 9 7
  1259. / / \
  1260. 7 ---> 5 9
  1261. /
  1262. 5
  1263. T3 checks:
  1264. - root is 7
  1265. - 5,7,9 in correct positions
  1266. - 5 and 9 have no children
  1267. */
  1268. {
  1269. bool success = 1;
  1270. int insert_array[] = {9, 7, 5};
  1271. size_t insert_array_size = 2;
  1272. Node node;
  1273. for(size_t i = 0; i<=insert_array_size; i++) {
  1274. newnode(node);
  1275. node.key.set(insert_array[i] * tio.player());
  1276. tree.insert(tio, yield, node);
  1277. tree.check_avl(tio, yield);
  1278. }
  1279. Duoram<Node>* oram = tree.get_oram();
  1280. RegXS root_xs = tree.get_root();
  1281. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1282. auto A = oram->flat(tio, yield);
  1283. auto R = A.reconstruct();
  1284. Node root_node, left_node, right_node;
  1285. size_t left_index, right_index;
  1286. root_node = R[root];
  1287. if((root_node.key).share()!=7) {
  1288. success = false;
  1289. }
  1290. left_index = (getAVLLeftPtr(root_node.pointers)).share();
  1291. right_index = (getAVLRightPtr(root_node.pointers)).share();
  1292. left_node = R[left_index];
  1293. right_node = R[right_index];
  1294. if(left_node.key.share()!=5 || right_node.key.share()!=9) {
  1295. success = false;
  1296. }
  1297. //To check that left and right have no children and 0 balances
  1298. size_t sum = left_node.pointers.share() + right_node.pointers.share();
  1299. if(sum!=0) {
  1300. success = false;
  1301. }
  1302. if(player0) {
  1303. if(success) {
  1304. print_green("T3 : SUCCESS\n");
  1305. } else{
  1306. print_red("T3 : FAIL\n");
  1307. }
  1308. }
  1309. A.init();
  1310. tree.init();
  1311. }
  1312. // (T4) : Test 4 : R rotation (root unmodified)
  1313. /*
  1314. Operation:
  1315. 9 9
  1316. / \ / \
  1317. 7 12 5 12
  1318. / ---> / \
  1319. 5 7 3 7
  1320. /
  1321. 3
  1322. T4 checks:
  1323. - root is 9
  1324. - 3,5,7,12 are in correct positions
  1325. - Nodes 3,7,12 have 0 balance
  1326. - Nodes 3,7,12 have no children
  1327. - 9's bal = 1 0
  1328. */
  1329. {
  1330. bool success = 1;
  1331. int insert_array[] = {9, 12, 7, 5, 3};
  1332. size_t insert_array_size = 4;
  1333. Node node;
  1334. for(size_t i = 0; i<=insert_array_size; i++) {
  1335. newnode(node);
  1336. node.key.set(insert_array[i] * tio.player());
  1337. tree.insert(tio, yield, node);
  1338. tree.check_avl(tio, yield);
  1339. }
  1340. Duoram<Node>* oram = tree.get_oram();
  1341. RegXS root_xs = tree.get_root();
  1342. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1343. auto A = oram->flat(tio, yield);
  1344. auto R = A.reconstruct();
  1345. Node root_node, n3, n7, n5, n12;
  1346. size_t n3_index, n7_index, n5_index, n12_index;
  1347. root_node = R[root];
  1348. if((root_node.key).share()!=9) {
  1349. success = false;
  1350. }
  1351. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  1352. n12_index = (getAVLRightPtr(root_node.pointers)).share();
  1353. n5 = R[n5_index];
  1354. n12 = R[n12_index];
  1355. n3_index = getAVLLeftPtr(n5.pointers).share();
  1356. n7_index = getAVLRightPtr(n5.pointers).share();
  1357. n7 = R[n7_index];
  1358. n3 = R[n3_index];
  1359. // Node value checks
  1360. if(n12.key.share()!=12 || n5.key.share()!=5) {
  1361. success = false;
  1362. }
  1363. if(n3.key.share()!=3 || n7.key.share()!=7) {
  1364. success = false;
  1365. }
  1366. // Node balance checks
  1367. size_t zero = 0;
  1368. zero+=(n3.pointers.share());
  1369. zero+=(n7.pointers.share());
  1370. zero+=(n12.pointers.share());
  1371. zero+=(getRightBal(root_node.pointers).share());
  1372. zero+=(getLeftBal(n5.pointers).share());
  1373. zero+=(getRightBal(n5.pointers).share());
  1374. if(zero!=0) {
  1375. success = false;
  1376. }
  1377. int one = (getLeftBal(root_node.pointers).share());
  1378. if(one!=1) {
  1379. success = false;
  1380. }
  1381. if(player0) {
  1382. if(success) {
  1383. print_green("T4 : SUCCESS\n");
  1384. } else {
  1385. print_red("T4 : FAIL\n");
  1386. }
  1387. }
  1388. A.init();
  1389. tree.init();
  1390. }
  1391. // (T5) : Test 5 : LR rotation (root modified)
  1392. /*
  1393. Operation:
  1394. 9 9 7
  1395. / / / \
  1396. 5 --> 7 --> 5 9
  1397. \ /
  1398. 7 5
  1399. T5 checks:
  1400. - root is 7
  1401. - 9,5,7 are in correct positions
  1402. - Nodes 5,7,9 have 0 balance
  1403. - Nodes 5,9 have no children
  1404. */
  1405. {
  1406. bool success = 1;
  1407. int insert_array[] = {9, 5, 7};
  1408. size_t insert_array_size = 2;
  1409. Node node;
  1410. for(size_t i = 0; i<=insert_array_size; i++) {
  1411. newnode(node);
  1412. node.key.set(insert_array[i] * tio.player());
  1413. tree.insert(tio, yield, node);
  1414. tree.check_avl(tio, yield);
  1415. }
  1416. Duoram<Node>* oram = tree.get_oram();
  1417. RegXS root_xs = tree.get_root();
  1418. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1419. auto A = oram->flat(tio, yield);
  1420. auto R = A.reconstruct();
  1421. Node root_node, n9, n5;
  1422. size_t n9_index, n5_index;
  1423. root_node = R[root];
  1424. if((root_node.key).share()!=7) {
  1425. success = false;
  1426. }
  1427. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  1428. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  1429. n5 = R[n5_index];
  1430. n9 = R[n9_index];
  1431. // Node value checks
  1432. if(n9.key.share()!=9 || n5.key.share()!=5) {
  1433. success = false;
  1434. }
  1435. // Node balance checks
  1436. size_t zero = 0;
  1437. zero+=(n5.pointers.share());
  1438. zero+=(n9.pointers.share());
  1439. zero+=(getRightBal(root_node.pointers).share());
  1440. zero+=(getLeftBal(n5.pointers).share());
  1441. zero+=(getRightBal(n5.pointers).share());
  1442. zero+=(getLeftBal(n5.pointers).share());
  1443. zero+=(getRightBal(n9.pointers).share());
  1444. zero+=(getLeftBal(n9.pointers).share());
  1445. if(zero!=0) {
  1446. success = false;
  1447. }
  1448. if(player0) {
  1449. if(success) {
  1450. print_green("T5 : SUCCESS\n");
  1451. } else {
  1452. print_red("T5 : FAIL\n");
  1453. }
  1454. }
  1455. A.init();
  1456. tree.init();
  1457. }
  1458. // (T6) : Test 6 : LR rotation (root unmodified)
  1459. /*
  1460. Operation:
  1461. 9 9 9
  1462. / \ / \ / \
  1463. 7 12 7 12 5 12
  1464. / ---> / ---> / \
  1465. 3 5 3 7
  1466. \ /
  1467. 5 3
  1468. T6 checks:
  1469. - root is 9
  1470. - 3,5,7,12 are in correct positions
  1471. - Nodes 3,7,12 have 0 balance
  1472. - Nodes 3,7,12 have no children
  1473. - 9's bal = 1 0
  1474. */
  1475. {
  1476. bool success = 1;
  1477. int insert_array[] = {9, 12, 7, 3, 5};
  1478. size_t insert_array_size = 4;
  1479. Node node;
  1480. for(size_t i = 0; i<=insert_array_size; i++) {
  1481. newnode(node);
  1482. node.key.set(insert_array[i] * tio.player());
  1483. tree.insert(tio, yield, node);
  1484. tree.check_avl(tio, yield);
  1485. }
  1486. Duoram<Node>* oram = tree.get_oram();
  1487. RegXS root_xs = tree.get_root();
  1488. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1489. auto A = oram->flat(tio, yield);
  1490. auto R = A.reconstruct();
  1491. Node root_node, n3, n7, n5, n12;
  1492. size_t n3_index, n7_index, n5_index, n12_index;
  1493. root_node = R[root];
  1494. if((root_node.key).share()!=9) {
  1495. success = false;
  1496. }
  1497. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  1498. n12_index = (getAVLRightPtr(root_node.pointers)).share();
  1499. n5 = R[n5_index];
  1500. n12 = R[n12_index];
  1501. n3_index = getAVLLeftPtr(n5.pointers).share();
  1502. n7_index = getAVLRightPtr(n5.pointers).share();
  1503. n7 = R[n7_index];
  1504. n3 = R[n3_index];
  1505. // Node value checks
  1506. if(n5.key.share()!=5 || n12.key.share()!=12) {
  1507. success = false;
  1508. }
  1509. if(n3.key.share()!=3 || n7.key.share()!=7) {
  1510. success = false;
  1511. }
  1512. // Node balance checks
  1513. size_t zero = 0;
  1514. zero+=(n3.pointers.share());
  1515. zero+=(n7.pointers.share());
  1516. zero+=(n12.pointers.share());
  1517. zero+=(getRightBal(root_node.pointers).share());
  1518. zero+=(getLeftBal(n5.pointers).share());
  1519. zero+=(getRightBal(n5.pointers).share());
  1520. if(zero!=0) {
  1521. success = false;
  1522. }
  1523. int one = (getLeftBal(root_node.pointers).share());
  1524. if(one!=1) {
  1525. success = false;
  1526. }
  1527. if(player0) {
  1528. if(success) {
  1529. print_green("T6 : SUCCESS\n");
  1530. } else {
  1531. print_red("T6 : FAIL\n");
  1532. }
  1533. }
  1534. A.init();
  1535. tree.init();
  1536. }
  1537. // (T7) : Test 7 : RL rotation (root modified)
  1538. /*
  1539. Operation:
  1540. 5 5 7
  1541. \ \ / \
  1542. 9 --> 7 --> 5 9
  1543. / \
  1544. 7 9
  1545. T7 checks:
  1546. - root is 7
  1547. - 9,5,7 are in correct positions
  1548. - Nodes 5,7,9 have 0 balance
  1549. - Nodes 5,9 have no children
  1550. */
  1551. {
  1552. bool success = 1;
  1553. int insert_array[] = {5, 9, 7};
  1554. size_t insert_array_size = 2;
  1555. Node node;
  1556. for(size_t i = 0; i<=insert_array_size; i++) {
  1557. newnode(node);
  1558. node.key.set(insert_array[i] * tio.player());
  1559. tree.insert(tio, yield, node);
  1560. tree.check_avl(tio, yield);
  1561. }
  1562. Duoram<Node>* oram = tree.get_oram();
  1563. RegXS root_xs = tree.get_root();
  1564. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1565. auto A = oram->flat(tio, yield);
  1566. auto R = A.reconstruct();
  1567. Node root_node, n9, n5;
  1568. size_t n9_index, n5_index;
  1569. root_node = R[root];
  1570. if((root_node.key).share()!=7) {
  1571. success = false;
  1572. }
  1573. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  1574. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  1575. n5 = R[n5_index];
  1576. n9 = R[n9_index];
  1577. // Node value checks
  1578. if(n9.key.share()!=9 || n5.key.share()!=5) {
  1579. success = false;
  1580. }
  1581. // Node balance checks
  1582. size_t zero = 0;
  1583. zero+=(n5.pointers.share());
  1584. zero+=(n9.pointers.share());
  1585. zero+=(getRightBal(root_node.pointers).share());
  1586. zero+=(getLeftBal(n5.pointers).share());
  1587. zero+=(getRightBal(n5.pointers).share());
  1588. zero+=(getLeftBal(n5.pointers).share());
  1589. zero+=(getRightBal(n9.pointers).share());
  1590. zero+=(getLeftBal(n9.pointers).share());
  1591. if(zero!=0) {
  1592. success = false;
  1593. }
  1594. if(player0) {
  1595. if(success) {
  1596. print_green("T7 : SUCCESS\n");
  1597. } else {
  1598. print_red("T7 : FAIL\n");
  1599. }
  1600. }
  1601. A.init();
  1602. tree.init();
  1603. }
  1604. // (T8) : Test 8 : RL rotation (root unmodified)
  1605. /*
  1606. Operation:
  1607. 5 5 5
  1608. / \ / \ / \
  1609. 3 12 3 12 3 9
  1610. / ---> / ---> / \
  1611. 7 9 7 12
  1612. \ /
  1613. 9 7
  1614. T8 checks:
  1615. - root is 5
  1616. - 3,9,7,12 are in correct positions
  1617. - Nodes 3,7,12 have 0 balance
  1618. - Nodes 3,7,12 have no children
  1619. - 5's bal = 0 1
  1620. */
  1621. {
  1622. bool success = 1;
  1623. int insert_array[] = {5, 3, 12, 7, 9};
  1624. size_t insert_array_size = 4;
  1625. Node node;
  1626. for(size_t i = 0; i<=insert_array_size; i++) {
  1627. newnode(node);
  1628. node.key.set(insert_array[i] * tio.player());
  1629. tree.insert(tio, yield, node);
  1630. tree.check_avl(tio, yield);
  1631. }
  1632. Duoram<Node>* oram = tree.get_oram();
  1633. RegXS root_xs = tree.get_root();
  1634. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1635. auto A = oram->flat(tio, yield);
  1636. auto R = A.reconstruct();
  1637. Node root_node, n3, n7, n9, n12;
  1638. size_t n3_index, n7_index, n9_index, n12_index;
  1639. root_node = R[root];
  1640. if((root_node.key).share()!=5) {
  1641. success = false;
  1642. }
  1643. n3_index = (getAVLLeftPtr(root_node.pointers)).share();
  1644. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  1645. n3 = R[n3_index];
  1646. n9 = R[n9_index];
  1647. n7_index = getAVLLeftPtr(n9.pointers).share();
  1648. n12_index = getAVLRightPtr(n9.pointers).share();
  1649. n7 = R[n7_index];
  1650. n12 = R[n12_index];
  1651. // Node value checks
  1652. if(n3.key.share()!=3 || n9.key.share()!=9) {
  1653. success = false;
  1654. }
  1655. if(n7.key.share()!=7 || n12.key.share()!=12) {
  1656. success = false;
  1657. }
  1658. // Node balance checks
  1659. size_t zero = 0;
  1660. zero+=(n3.pointers.share());
  1661. zero+=(n7.pointers.share());
  1662. zero+=(n12.pointers.share());
  1663. zero+=(getLeftBal(root_node.pointers).share());
  1664. zero+=(getLeftBal(n9.pointers).share());
  1665. zero+=(getRightBal(n9.pointers).share());
  1666. if(zero!=0) {
  1667. success = false;
  1668. }
  1669. int one = (getRightBal(root_node.pointers).share());
  1670. if(one!=1) {
  1671. success = false;
  1672. }
  1673. if(player0) {
  1674. if(success) {
  1675. print_green("T8 : SUCCESS\n");
  1676. } else {
  1677. print_red("T8 : FAIL\n");
  1678. }
  1679. }
  1680. A.init();
  1681. tree.init();
  1682. }
  1683. // Deletion Tests:
  1684. // (T9) : Test 9 : L rotation (root modified)
  1685. /*
  1686. Operation:
  1687. 5 7
  1688. / \ Del 3 / \
  1689. 3 7 ------> 5 9
  1690. \
  1691. 9
  1692. T9 checks:
  1693. - root is 7
  1694. - 5,7,9 in correct positions
  1695. - 5 and 9 have no children and 0 balances
  1696. - 7 has 0 balances
  1697. */
  1698. {
  1699. bool success = 1;
  1700. int insert_array[] = {5, 3, 7, 9};
  1701. size_t insert_array_size = 3;
  1702. Node node;
  1703. for(size_t i = 0; i<=insert_array_size; i++) {
  1704. newnode(node);
  1705. node.key.set(insert_array[i] * tio.player());
  1706. tree.insert(tio, yield, node);
  1707. tree.check_avl(tio, yield);
  1708. }
  1709. RegAS del_key;
  1710. del_key.set(3 * tio.player());
  1711. tree.del(tio, yield, del_key);
  1712. tree.check_avl(tio, yield);
  1713. Duoram<Node>* oram = tree.get_oram();
  1714. RegXS root_xs = tree.get_root();
  1715. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1716. auto A = oram->flat(tio, yield);
  1717. auto R = A.reconstruct();
  1718. Node root_node, left_node, right_node;
  1719. size_t left_index, right_index;
  1720. root_node = R[root];
  1721. if((root_node.key).share()!=7) {
  1722. success = false;
  1723. }
  1724. left_index = (getAVLLeftPtr(root_node.pointers)).share();
  1725. right_index = (getAVLRightPtr(root_node.pointers)).share();
  1726. left_node = R[left_index];
  1727. right_node = R[right_index];
  1728. if(left_node.key.share()!=5 || right_node.key.share()!=9) {
  1729. success = false;
  1730. }
  1731. //To check that left and right have no children and 0 balances
  1732. size_t sum = left_node.pointers.share() + right_node.pointers.share();
  1733. if(sum!=0) {
  1734. success = false;
  1735. }
  1736. if(player0) {
  1737. if(success) {
  1738. print_green("T9 : SUCCESS\n");
  1739. } else {
  1740. print_red("T9 : FAIL\n");
  1741. }
  1742. }
  1743. A.init();
  1744. tree.init();
  1745. }
  1746. // (T10) : Test 10 : L rotation (root unmodified)
  1747. /*
  1748. Operation:
  1749. 5 5
  1750. / \ / \
  1751. 3 7 Del 6 3 9
  1752. / / \ ------> / / \
  1753. 1 6 9 1 7 12
  1754. \
  1755. 12
  1756. T10 checks:
  1757. - root is 5
  1758. - 3, 7, 9, 12 in expected positions
  1759. - Nodes 3, 7, 12 have 0 balance and no children
  1760. - 5's bal = 0 1
  1761. */
  1762. {
  1763. bool success = 1;
  1764. int insert_array[] = {5, 3, 7, 9, 6, 1, 12};
  1765. size_t insert_array_size = 6;
  1766. Node node;
  1767. for(size_t i = 0; i<=insert_array_size; i++) {
  1768. newnode(node);
  1769. node.key.set(insert_array[i] * tio.player());
  1770. tree.insert(tio, yield, node);
  1771. tree.check_avl(tio, yield);
  1772. }
  1773. RegAS del_key;
  1774. del_key.set(6 * tio.player());
  1775. tree.del(tio, yield, del_key);
  1776. tree.check_avl(tio, yield);
  1777. Duoram<Node>* oram = tree.get_oram();
  1778. RegXS root_xs = tree.get_root();
  1779. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1780. auto A = oram->flat(tio, yield);
  1781. auto R = A.reconstruct();
  1782. Node root_node, n1, n3, n7, n9, n12;
  1783. size_t n1_index, n3_index, n7_index, n9_index, n12_index;
  1784. root_node = R[root];
  1785. if((root_node.key).share()!=5) {
  1786. success = false;
  1787. }
  1788. n3_index = (getAVLLeftPtr(root_node.pointers)).share();
  1789. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  1790. n3 = R[n3_index];
  1791. n9 = R[n9_index];
  1792. n7_index = getAVLLeftPtr(n9.pointers).share();
  1793. n12_index = getAVLRightPtr(n9.pointers).share();
  1794. n7 = R[n7_index];
  1795. n12 = R[n12_index];
  1796. n1_index = getAVLLeftPtr(n3.pointers).share();
  1797. n1 = R[n1_index];
  1798. // Node value checks
  1799. if(n3.key.share()!=3 || n9.key.share()!=9) {
  1800. success = false;
  1801. }
  1802. if(n7.key.share()!=7 || n12.key.share()!=12 || n1.key.share()!=1) {
  1803. success = false;
  1804. }
  1805. // Node children and balance checks
  1806. size_t zero = 0;
  1807. zero+=(n1.pointers.share());
  1808. zero+=(n7.pointers.share());
  1809. zero+=(n12.pointers.share());
  1810. zero+=(getLeftBal(root_node.pointers).share());
  1811. zero+=(getRightBal(root_node.pointers).share());
  1812. zero+=(getLeftBal(n9.pointers).share());
  1813. zero+=(getRightBal(n9.pointers).share());
  1814. zero+=(getRightBal(n3.pointers).share());
  1815. if(zero!=0) {
  1816. success = false;
  1817. }
  1818. int one = (getLeftBal(n3.pointers).share());
  1819. if(one!=1) {
  1820. success = false;
  1821. }
  1822. if(player0) {
  1823. if(success) {
  1824. print_green("T10 : SUCCESS\n");
  1825. } else {
  1826. print_red("T10 : FAIL\n");
  1827. }
  1828. }
  1829. A.init();
  1830. tree.init();
  1831. }
  1832. // (T11) : Test 11 : R rotation (root modified)
  1833. /*
  1834. Operation:
  1835. 9 7
  1836. / \ Del 12 / \
  1837. 7 12 -------> 5 9
  1838. /
  1839. 5
  1840. T11 checks:
  1841. - root is 7
  1842. - 5,7,9 in correct positions and balances to 0
  1843. - 5 and 9 have no children
  1844. */
  1845. {
  1846. bool success = 1;
  1847. int insert_array[] = {9, 7, 12, 5};
  1848. size_t insert_array_size = 3;
  1849. Node node;
  1850. for(size_t i = 0; i<=insert_array_size; i++) {
  1851. newnode(node);
  1852. node.key.set(insert_array[i] * tio.player());
  1853. tree.insert(tio, yield, node);
  1854. tree.check_avl(tio, yield);
  1855. }
  1856. RegAS del_key;
  1857. del_key.set(12 * tio.player());
  1858. tree.del(tio, yield, del_key);
  1859. tree.check_avl(tio, yield);
  1860. Duoram<Node>* oram = tree.get_oram();
  1861. RegXS root_xs = tree.get_root();
  1862. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1863. auto A = oram->flat(tio, yield);
  1864. auto R = A.reconstruct();
  1865. Node root_node, left_node, right_node;
  1866. size_t left_index, right_index;
  1867. root_node = R[root];
  1868. if((root_node.key).share()!=7) {
  1869. success = false;
  1870. }
  1871. left_index = (getAVLLeftPtr(root_node.pointers)).share();
  1872. right_index = (getAVLRightPtr(root_node.pointers)).share();
  1873. left_node = R[left_index];
  1874. right_node = R[right_index];
  1875. if(left_node.key.share()!=5 || right_node.key.share()!=9) {
  1876. success = false;
  1877. }
  1878. //To check that left and right have no children and 0 balances
  1879. size_t zero = left_node.pointers.share() + right_node.pointers.share();
  1880. zero+=(getLeftBal(left_node.pointers).share());
  1881. zero+=(getRightBal(left_node.pointers).share());
  1882. zero+=(getLeftBal(right_node.pointers).share());
  1883. zero+=(getRightBal(right_node.pointers).share());
  1884. if(zero!=0) {
  1885. success = false;
  1886. }
  1887. if(player0) {
  1888. if(success) {
  1889. print_green("T11 : SUCCESS\n");
  1890. } else{
  1891. print_red("T11 : FAIL\n");
  1892. }
  1893. }
  1894. A.init();
  1895. tree.init();
  1896. }
  1897. // (T12) : Test 12 : R rotation (root unmodified)
  1898. /*
  1899. Operation:
  1900. 9 9
  1901. / \ / \
  1902. 7 12 Del 8 5 12
  1903. / \ \ ------> / \ \
  1904. 5 8 15 3 7 15
  1905. /
  1906. 3
  1907. T4 checks:
  1908. - root is 9
  1909. - 3,5,7,12,15 are in correct positions
  1910. - Nodes 3,7,15 have 0 balance
  1911. - Nodes 3,7,15 have no children
  1912. - 9,5 bal = 0 0
  1913. - 12 bal = 0 1
  1914. */
  1915. {
  1916. bool success = 1;
  1917. int insert_array[] = {9, 12, 7, 5, 8, 15, 3};
  1918. size_t insert_array_size = 6;
  1919. Node node;
  1920. for(size_t i = 0; i<=insert_array_size; i++) {
  1921. newnode(node);
  1922. node.key.set(insert_array[i] * tio.player());
  1923. tree.insert(tio, yield, node);
  1924. tree.check_avl(tio, yield);
  1925. }
  1926. RegAS del_key;
  1927. del_key.set(8 * tio.player());
  1928. tree.del(tio, yield, del_key);
  1929. tree.check_avl(tio, yield);
  1930. Duoram<Node>* oram = tree.get_oram();
  1931. RegXS root_xs = tree.get_root();
  1932. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  1933. auto A = oram->flat(tio, yield);
  1934. auto R = A.reconstruct();
  1935. Node root_node, n3, n7, n5, n12, n15;
  1936. size_t n3_index, n7_index, n5_index, n12_index, n15_index;
  1937. root_node = R[root];
  1938. if((root_node.key).share()!=9) {
  1939. success = false;
  1940. }
  1941. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  1942. n12_index = (getAVLRightPtr(root_node.pointers)).share();
  1943. n5 = R[n5_index];
  1944. n12 = R[n12_index];
  1945. n3_index = getAVLLeftPtr(n5.pointers).share();
  1946. n7_index = getAVLRightPtr(n5.pointers).share();
  1947. n7 = R[n7_index];
  1948. n3 = R[n3_index];
  1949. n15_index = getAVLRightPtr(n12.pointers).share();
  1950. n15 = R[n15_index];
  1951. // Node value checks
  1952. if(n12.key.share()!=12 || n5.key.share()!=5) {
  1953. success = false;
  1954. }
  1955. if(n3.key.share()!=3 || n7.key.share()!=7 || n15.key.share()!=15) {
  1956. success = false;
  1957. }
  1958. // Node balance checks
  1959. size_t zero = 0;
  1960. zero+=(n3.pointers.share());
  1961. zero+=(n7.pointers.share());
  1962. zero+=(n15.pointers.share());
  1963. zero+=(getRightBal(root_node.pointers).share());
  1964. zero+=(getLeftBal(root_node.pointers).share());
  1965. zero+=(getLeftBal(n5.pointers).share());
  1966. zero+=(getRightBal(n5.pointers).share());
  1967. if(zero!=0) {
  1968. success = false;
  1969. }
  1970. int one = (getRightBal(n12.pointers).share());
  1971. if(one!=1) {
  1972. success = false;
  1973. }
  1974. if(player0) {
  1975. if(success) {
  1976. print_green("T12 : SUCCESS\n");
  1977. } else {
  1978. print_red("T12 : FAIL\n");
  1979. }
  1980. }
  1981. A.init();
  1982. tree.init();
  1983. }
  1984. // (T13) : Test 13 : LR rotation (root modified)
  1985. /*
  1986. Operation:
  1987. 9 9 7
  1988. / \ Del 12 / / \
  1989. 5 12 -------> 7 --> 5 9
  1990. \ /
  1991. 7 5
  1992. T5 checks:
  1993. - root is 7
  1994. - 9,5,7 are in correct positions
  1995. - Nodes 5,7,9 have 0 balance
  1996. - Nodes 5,9 have no children
  1997. */
  1998. {
  1999. bool success = 1;
  2000. int insert_array[] = {9, 5, 12, 7};
  2001. size_t insert_array_size = 3;
  2002. Node node;
  2003. for(size_t i = 0; i<=insert_array_size; i++) {
  2004. newnode(node);
  2005. node.key.set(insert_array[i] * tio.player());
  2006. tree.insert(tio, yield, node);
  2007. tree.check_avl(tio, yield);
  2008. }
  2009. RegAS del_key;
  2010. del_key.set(12 * tio.player());
  2011. tree.del(tio, yield, del_key);
  2012. tree.check_avl(tio, yield);
  2013. Duoram<Node>* oram = tree.get_oram();
  2014. RegXS root_xs = tree.get_root();
  2015. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  2016. auto A = oram->flat(tio, yield);
  2017. auto R = A.reconstruct();
  2018. Node root_node, n9, n5;
  2019. size_t n9_index, n5_index;
  2020. root_node = R[root];
  2021. if((root_node.key).share()!=7) {
  2022. success = false;
  2023. }
  2024. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  2025. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  2026. n5 = R[n5_index];
  2027. n9 = R[n9_index];
  2028. // Node value checks
  2029. if(n9.key.share()!=9 || n5.key.share()!=5) {
  2030. success = false;
  2031. }
  2032. // Node balance checks
  2033. size_t zero = 0;
  2034. zero+=(n5.pointers.share());
  2035. zero+=(n9.pointers.share());
  2036. zero+=(getRightBal(root_node.pointers).share());
  2037. zero+=(getLeftBal(n5.pointers).share());
  2038. zero+=(getRightBal(n5.pointers).share());
  2039. zero+=(getLeftBal(n5.pointers).share());
  2040. zero+=(getRightBal(n9.pointers).share());
  2041. zero+=(getLeftBal(n9.pointers).share());
  2042. if(zero!=0) {
  2043. success = false;
  2044. }
  2045. if(player0) {
  2046. if(success) {
  2047. print_green("T13 : SUCCESS\n");
  2048. } else {
  2049. print_red("T13 : FAIL\n");
  2050. }
  2051. }
  2052. A.init();
  2053. tree.init();
  2054. }
  2055. // (T14) : Test 14 : LR rotation (root unmodified)
  2056. /*
  2057. Operation:
  2058. 9 9 9
  2059. / \ / \ / \
  2060. 7 12 Del 8 7 12 5 12
  2061. / \ ------> / ---> / \
  2062. 3 8 5 3 7
  2063. \ /
  2064. 5 3
  2065. T6 checks:
  2066. - root is 9
  2067. - 3,5,7,12 are in correct positions
  2068. - Nodes 3,7,12 have 0 balance
  2069. - Nodes 3,7,12 have no children
  2070. - 9's bal = 1 0
  2071. */
  2072. {
  2073. bool success = 1;
  2074. int insert_array[] = {9, 12, 7, 3, 5};
  2075. size_t insert_array_size = 4;
  2076. Node node;
  2077. for(size_t i = 0; i<=insert_array_size; i++) {
  2078. newnode(node);
  2079. node.key.set(insert_array[i] * tio.player());
  2080. tree.insert(tio, yield, node);
  2081. tree.check_avl(tio, yield);
  2082. }
  2083. RegAS del_key;
  2084. del_key.set(8 * tio.player());
  2085. tree.del(tio, yield, del_key);
  2086. tree.check_avl(tio, yield);
  2087. Duoram<Node>* oram = tree.get_oram();
  2088. RegXS root_xs = tree.get_root();
  2089. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  2090. auto A = oram->flat(tio, yield);
  2091. auto R = A.reconstruct();
  2092. Node root_node, n3, n7, n5, n12;
  2093. size_t n3_index, n7_index, n5_index, n12_index;
  2094. root_node = R[root];
  2095. if((root_node.key).share()!=9) {
  2096. success = false;
  2097. }
  2098. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  2099. n12_index = (getAVLRightPtr(root_node.pointers)).share();
  2100. n5 = R[n5_index];
  2101. n12 = R[n12_index];
  2102. n3_index = getAVLLeftPtr(n5.pointers).share();
  2103. n7_index = getAVLRightPtr(n5.pointers).share();
  2104. n7 = R[n7_index];
  2105. n3 = R[n3_index];
  2106. // Node value checks
  2107. if(n5.key.share()!=5 || n12.key.share()!=12) {
  2108. success = false;
  2109. }
  2110. if(n3.key.share()!=3 || n7.key.share()!=7) {
  2111. success = false;
  2112. }
  2113. // Node balance checks
  2114. size_t zero = 0;
  2115. zero+=(n3.pointers.share());
  2116. zero+=(n7.pointers.share());
  2117. zero+=(n12.pointers.share());
  2118. zero+=(getRightBal(root_node.pointers).share());
  2119. zero+=(getLeftBal(n5.pointers).share());
  2120. zero+=(getRightBal(n5.pointers).share());
  2121. if(zero!=0) {
  2122. success = false;
  2123. }
  2124. int one = (getLeftBal(root_node.pointers).share());
  2125. if(one!=1) {
  2126. success = false;
  2127. }
  2128. if(player0) {
  2129. if(success) {
  2130. print_green("T14 : SUCCESS\n");
  2131. } else {
  2132. print_red("T14 : FAIL\n");
  2133. }
  2134. }
  2135. A.init();
  2136. tree.init();
  2137. }
  2138. // (T15) : Test 15 : RL rotation (root modified)
  2139. /*
  2140. Operation:
  2141. 5 5 7
  2142. / \ Del 3 \ / \
  2143. 3 9 -------> 7 --> 5 9
  2144. / \
  2145. 7 9
  2146. T15 checks:
  2147. - root is 7
  2148. - 9,5,7 are in correct positions
  2149. - Nodes 5,7,9 have 0 balance
  2150. - Nodes 5,9 have no children
  2151. */
  2152. {
  2153. bool success = 1;
  2154. int insert_array[] = {5, 9, 3, 7};
  2155. size_t insert_array_size = 3;
  2156. Node node;
  2157. for(size_t i = 0; i<=insert_array_size; i++) {
  2158. newnode(node);
  2159. node.key.set(insert_array[i] * tio.player());
  2160. tree.insert(tio, yield, node);
  2161. tree.check_avl(tio, yield);
  2162. }
  2163. RegAS del_key;
  2164. del_key.set(3 * tio.player());
  2165. tree.del(tio, yield, del_key);
  2166. tree.check_avl(tio, yield);
  2167. Duoram<Node>* oram = tree.get_oram();
  2168. RegXS root_xs = tree.get_root();
  2169. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  2170. auto A = oram->flat(tio, yield);
  2171. auto R = A.reconstruct();
  2172. Node root_node, n9, n5;
  2173. size_t n9_index, n5_index;
  2174. root_node = R[root];
  2175. if((root_node.key).share()!=7) {
  2176. success = false;
  2177. }
  2178. n5_index = (getAVLLeftPtr(root_node.pointers)).share();
  2179. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  2180. n5 = R[n5_index];
  2181. n9 = R[n9_index];
  2182. // Node value checks
  2183. if(n9.key.share()!=9 || n5.key.share()!=5) {
  2184. success = false;
  2185. }
  2186. // Node balance checks
  2187. size_t zero = 0;
  2188. zero+=(n5.pointers.share());
  2189. zero+=(n9.pointers.share());
  2190. zero+=(getRightBal(root_node.pointers).share());
  2191. zero+=(getLeftBal(n5.pointers).share());
  2192. zero+=(getRightBal(n5.pointers).share());
  2193. zero+=(getLeftBal(n5.pointers).share());
  2194. zero+=(getRightBal(n9.pointers).share());
  2195. zero+=(getLeftBal(n9.pointers).share());
  2196. if(zero!=0) {
  2197. success = false;
  2198. }
  2199. if(player0) {
  2200. if(success) {
  2201. print_green("T15 : SUCCESS\n");
  2202. } else {
  2203. print_red("T15 : FAIL\n");
  2204. }
  2205. }
  2206. A.init();
  2207. tree.init();
  2208. }
  2209. // (T16) : Test 16 : RL rotation (root unmodified)
  2210. /*
  2211. Operation:
  2212. 5 5 5
  2213. / \ / \ / \
  2214. 3 12 Del 1 3 12 3 9
  2215. / / ------> / ---> / \
  2216. 1 7 9 7 12
  2217. \ /
  2218. 9 7
  2219. T8 checks:
  2220. - root is 5
  2221. - 3,9,7,12 are in correct positions
  2222. - Nodes 3,7,12 have 0 balance
  2223. - Nodes 3,7,12 have no children
  2224. - 5's bal = 0 1
  2225. */
  2226. {
  2227. bool success = 1;
  2228. int insert_array[] = {5, 3, 12, 7, 1, 9};
  2229. size_t insert_array_size = 5;
  2230. Node node;
  2231. for(size_t i = 0; i<=insert_array_size; i++) {
  2232. newnode(node);
  2233. node.key.set(insert_array[i] * tio.player());
  2234. tree.insert(tio, yield, node);
  2235. tree.check_avl(tio, yield);
  2236. }
  2237. RegAS del_key;
  2238. del_key.set(1 * tio.player());
  2239. tree.del(tio, yield, del_key);
  2240. tree.check_avl(tio, yield);
  2241. Duoram<Node>* oram = tree.get_oram();
  2242. RegXS root_xs = tree.get_root();
  2243. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  2244. auto A = oram->flat(tio, yield);
  2245. auto R = A.reconstruct();
  2246. Node root_node, n3, n7, n9, n12;
  2247. size_t n3_index, n7_index, n9_index, n12_index;
  2248. root_node = R[root];
  2249. if((root_node.key).share()!=5) {
  2250. success = false;
  2251. }
  2252. n3_index = (getAVLLeftPtr(root_node.pointers)).share();
  2253. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  2254. n3 = R[n3_index];
  2255. n9 = R[n9_index];
  2256. n7_index = getAVLLeftPtr(n9.pointers).share();
  2257. n12_index = getAVLRightPtr(n9.pointers).share();
  2258. n7 = R[n7_index];
  2259. n12 = R[n12_index];
  2260. // Node value checks
  2261. if(n3.key.share()!=3 || n9.key.share()!=9) {
  2262. success = false;
  2263. }
  2264. if(n7.key.share()!=7 || n12.key.share()!=12) {
  2265. success = false;
  2266. }
  2267. // Node balance checks
  2268. size_t zero = 0;
  2269. zero+=(n3.pointers.share());
  2270. zero+=(n7.pointers.share());
  2271. zero+=(n12.pointers.share());
  2272. zero+=(getLeftBal(root_node.pointers).share());
  2273. zero+=(getLeftBal(n9.pointers).share());
  2274. zero+=(getRightBal(n9.pointers).share());
  2275. if(zero!=0) {
  2276. success = false;
  2277. }
  2278. int one = (getRightBal(root_node.pointers).share());
  2279. if(one!=1) {
  2280. success = false;
  2281. }
  2282. if(player0) {
  2283. if(success) {
  2284. print_green("T16 : SUCCESS\n");
  2285. } else {
  2286. print_red("T16 : FAIL\n");
  2287. }
  2288. }
  2289. A.init();
  2290. tree.init();
  2291. }
  2292. // (T17) : Test 17 : Double imbalance (root modified)
  2293. /*
  2294. Operation:
  2295. 9 9
  2296. / \ / \
  2297. 5 12 Del 10 5 15
  2298. / \ / \ --------> / \ / \
  2299. 3 7 10 15 3 7 12 20
  2300. / \ / \ \ / \ / \
  2301. 2 4 6 8 20 2 4 6 8
  2302. / /
  2303. 1 1
  2304. 5
  2305. / \
  2306. 3 9
  2307. -----> / \ / \
  2308. 2 4 7 15
  2309. / / \ / \
  2310. 1 6 8 10 20
  2311. T17 checks:
  2312. - root is 5
  2313. - all other nodes are in correct positions
  2314. - balances and children are correct
  2315. */
  2316. {
  2317. bool success = 1;
  2318. int insert_array[] = {9, 5, 12, 7, 3, 10, 15, 2, 4, 6, 8, 20, 1};
  2319. size_t insert_array_size = 12;
  2320. Node node;
  2321. for(size_t i = 0; i<=insert_array_size; i++) {
  2322. newnode(node);
  2323. node.key.set(insert_array[i] * tio.player());
  2324. tree.insert(tio, yield, node);
  2325. tree.check_avl(tio, yield);
  2326. }
  2327. RegAS del_key;
  2328. del_key.set(10 * tio.player());
  2329. tree.del(tio, yield, del_key);
  2330. tree.check_avl(tio, yield);
  2331. Duoram<Node>* oram = tree.get_oram();
  2332. RegXS root_xs = tree.get_root();
  2333. size_t root = reconstruct_RegXS(tio, yield, root_xs);
  2334. auto A = oram->flat(tio, yield);
  2335. auto R = A.reconstruct();
  2336. Node root_node, n3, n7, n9;
  2337. Node n1, n2, n4, n6, n8, n12, n15, n20;
  2338. size_t n3_index, n7_index, n9_index;
  2339. size_t n1_index, n2_index, n4_index, n6_index;
  2340. size_t n8_index, n12_index, n15_index, n20_index;
  2341. root_node = R[root];
  2342. if((root_node.key).share()!=5) {
  2343. success = false;
  2344. }
  2345. n3_index = (getAVLLeftPtr(root_node.pointers)).share();
  2346. n9_index = (getAVLRightPtr(root_node.pointers)).share();
  2347. n3 = R[n3_index];
  2348. n9 = R[n9_index];
  2349. n2_index = getAVLLeftPtr(n3.pointers).share();
  2350. n4_index = getAVLRightPtr(n3.pointers).share();
  2351. n7_index = getAVLLeftPtr(n9.pointers).share();
  2352. n15_index = getAVLRightPtr(n9.pointers).share();
  2353. n2 = R[n2_index];
  2354. n4 = R[n4_index];
  2355. n7 = R[n7_index];
  2356. n15 = R[n15_index];
  2357. n1_index = getAVLLeftPtr(n2.pointers).share();
  2358. n6_index = getAVLLeftPtr(n7.pointers).share();
  2359. n8_index = getAVLRightPtr(n7.pointers).share();
  2360. n12_index = getAVLLeftPtr(n15.pointers).share();
  2361. n20_index = getAVLRightPtr(n15.pointers).share();
  2362. n1 = R[n1_index];
  2363. n6 = R[n6_index];
  2364. n8 = R[n8_index];
  2365. n12 = R[n12_index];
  2366. n20 = R[n20_index];
  2367. // Node value checks
  2368. if(n3.key.share()!=3 || n9.key.share()!=9) {
  2369. success = false;
  2370. }
  2371. if(n2.key.share()!=2 || n4.key.share()!=4) {
  2372. success = false;
  2373. }
  2374. if(n7.key.share()!=7 || n15.key.share()!=15) {
  2375. success = false;
  2376. }
  2377. if(n1.key.share()!=1 || n6.key.share()!=6 || n8.key.share()!=8) {
  2378. success = false;
  2379. }
  2380. if(n12.key.share()!=12 || n20.key.share()!=20) {
  2381. success = false;
  2382. }
  2383. // Node balance checks
  2384. size_t zero = 0;
  2385. zero+=(n1.pointers.share());
  2386. zero+=(n4.pointers.share());
  2387. zero+=(n6.pointers.share());
  2388. zero+=(n8.pointers.share());
  2389. zero+=(n12.pointers.share());
  2390. zero+=(n20.pointers.share());
  2391. zero+=(getLeftBal(n7.pointers).share());
  2392. zero+=(getRightBal(n7.pointers).share());
  2393. zero+=(getLeftBal(n9.pointers).share());
  2394. zero+=(getRightBal(n9.pointers).share());
  2395. zero+=(getLeftBal(n15.pointers).share());
  2396. zero+=(getRightBal(n15.pointers).share());
  2397. zero+=(getRightBal(n3.pointers).share());
  2398. zero+=(getLeftBal(root_node.pointers).share());
  2399. zero+=(getRightBal(root_node.pointers).share());
  2400. if(zero!=0) {
  2401. success = false;
  2402. }
  2403. int one = (getLeftBal(n3.pointers).share());
  2404. if(one!=1) {
  2405. success = false;
  2406. }
  2407. if(player0) {
  2408. if(success) {
  2409. print_green("T17 : SUCCESS\n");
  2410. } else {
  2411. print_red("T17 : FAIL\n");
  2412. }
  2413. }
  2414. A.init();
  2415. tree.init();
  2416. }
  2417. });
  2418. }