avl.cpp 115 KB

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