avl.cpp 99 KB

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