avl.cpp 97 KB

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