avl.cpp 112 KB

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