avl.cpp 122 KB

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