avl.cpp 122 KB

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