avl.cpp 124 KB

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