avl.cpp 126 KB

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