bst.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #include <functional>
  2. #include "bst.hpp"
  3. // Helper functions to reconstruct shared RegBS, RegAS or RegXS
  4. bool reconstruct_RegBS(MPCTIO &tio, yield_t &yield, RegBS flag) {
  5. RegBS reconstructed_flag;
  6. if (tio.player() < 2) {
  7. RegBS peer_flag;
  8. tio.queue_peer(&flag, 1);
  9. tio.queue_server(&flag, 1);
  10. yield();
  11. tio.recv_peer(&peer_flag, 1);
  12. reconstructed_flag = flag;
  13. reconstructed_flag ^= peer_flag;
  14. } else {
  15. RegBS p0_flag, p1_flag;
  16. yield();
  17. tio.recv_p0(&p0_flag, 1);
  18. tio.recv_p1(&p1_flag, 1);
  19. reconstructed_flag = p0_flag;
  20. reconstructed_flag ^= p1_flag;
  21. }
  22. return reconstructed_flag.bshare;
  23. }
  24. size_t reconstruct_RegAS(MPCTIO &tio, yield_t &yield, RegAS variable) {
  25. RegAS reconstructed_var;
  26. if (tio.player() < 2) {
  27. RegAS peer_var;
  28. tio.queue_peer(&variable, sizeof(variable));
  29. tio.queue_server(&variable, sizeof(variable));
  30. yield();
  31. tio.recv_peer(&peer_var, sizeof(variable));
  32. reconstructed_var = variable;
  33. reconstructed_var += peer_var;
  34. } else {
  35. RegAS p0_var, p1_var;
  36. yield();
  37. tio.recv_p0(&p0_var, sizeof(variable));
  38. tio.recv_p1(&p1_var, sizeof(variable));
  39. reconstructed_var = p0_var;
  40. reconstructed_var += p1_var;
  41. }
  42. return reconstructed_var.ashare;
  43. }
  44. size_t reconstruct_RegXS(MPCTIO &tio, yield_t &yield, RegXS variable) {
  45. RegXS reconstructed_var;
  46. if (tio.player() < 2) {
  47. RegXS peer_var;
  48. tio.queue_peer(&variable, sizeof(variable));
  49. tio.queue_server(&variable, sizeof(variable));
  50. yield();
  51. tio.recv_peer(&peer_var, sizeof(variable));
  52. reconstructed_var = variable;
  53. reconstructed_var ^= peer_var;
  54. } else {
  55. RegXS p0_var, p1_var;
  56. yield();
  57. tio.recv_p0(&p0_var, sizeof(variable));
  58. tio.recv_p1(&p1_var, sizeof(variable));
  59. reconstructed_var = p0_var;
  60. reconstructed_var ^= p1_var;
  61. }
  62. return reconstructed_var.xshare;
  63. }
  64. std::tuple<RegBS, RegBS> compare_keys(MPCTIO tio, yield_t &yield, Node n1, Node n2) {
  65. CDPF cdpf = tio.cdpf(yield);
  66. auto [lt, eq, gt] = cdpf.compare(tio, yield, n2.key - n1.key, tio.aes_ops());
  67. RegBS lteq = lt^eq;
  68. return {lteq, gt};
  69. }
  70. // Assuming pointer of 64 bits is split as:
  71. // - 32 bits Left ptr
  72. // - 32 bits Right ptr
  73. // < Left, Right>
  74. inline RegXS extractLeftPtr(RegXS pointer){
  75. return ((pointer&(0xFFFFFFFF00000000))>>32);
  76. }
  77. inline RegXS extractRightPtr(RegXS pointer){
  78. return (pointer&(0x00000000FFFFFFFF));
  79. }
  80. inline void setLeftPtr(RegXS &pointer, RegXS new_ptr){
  81. pointer&=(0x00000000FFFFFFFF);
  82. pointer+=(new_ptr<<32);
  83. }
  84. inline void setRightPtr(RegXS &pointer, RegXS new_ptr){
  85. pointer&=(0xFFFFFFFF00000000);
  86. pointer+=(new_ptr);
  87. }
  88. // Pretty-print a reconstructed BST, rooted at node. is_left_child and
  89. // is_right_child indicate whether node is a left or right child of its
  90. // parent. They cannot both be true, but the root of the tree has both
  91. // of them false.
  92. void BST::pretty_print(const std::vector<Node> &R, value_t node,
  93. const std::string &prefix = "", bool is_left_child = false,
  94. bool is_right_child = false)
  95. {
  96. if (node == 0) {
  97. // NULL pointer
  98. if (is_left_child) {
  99. printf("%s\xE2\x95\xA7\n", prefix.c_str()); // ╧
  100. } else if (is_right_child) {
  101. printf("%s\xE2\x95\xA4\n", prefix.c_str()); // ╤
  102. } else {
  103. printf("%s\xE2\x95\xA2\n", prefix.c_str()); // ╢
  104. }
  105. return;
  106. }
  107. const Node &n = R[node];
  108. value_t left_ptr = extractLeftPtr(n.pointers).xshare;
  109. value_t right_ptr = extractRightPtr(n.pointers).xshare;
  110. std::string rightprefix(prefix), leftprefix(prefix),
  111. nodeprefix(prefix);
  112. if (is_left_child) {
  113. rightprefix.append("\xE2\x94\x82"); // │
  114. leftprefix.append(" ");
  115. nodeprefix.append("\xE2\x94\x94"); // └
  116. } else if (is_right_child) {
  117. rightprefix.append(" ");
  118. leftprefix.append("\xE2\x94\x82"); // │
  119. nodeprefix.append("\xE2\x94\x8C"); // ┌
  120. } else {
  121. rightprefix.append(" ");
  122. leftprefix.append(" ");
  123. nodeprefix.append("\xE2\x94\x80"); // ─
  124. }
  125. pretty_print(R, right_ptr, rightprefix, false, true);
  126. printf("%s\xE2\x94\xA4", nodeprefix.c_str()); // ┤
  127. n.dump();
  128. printf("\n");
  129. pretty_print(R, left_ptr, leftprefix, true, false);
  130. }
  131. void BST::print_oram(MPCTIO &tio, yield_t &yield) {
  132. auto A = oram->flat(tio, yield);
  133. auto R = A.reconstruct();
  134. for(size_t i=0;i<R.size();++i) {
  135. printf("\n%04lx ", i);
  136. R[i].dump();
  137. }
  138. printf("\n");
  139. }
  140. void BST::pretty_print(MPCTIO &tio, yield_t &yield) {
  141. RegXS peer_root;
  142. RegXS reconstructed_root = root;
  143. if (tio.player() == 1) {
  144. tio.queue_peer(&root, sizeof(root));
  145. } else {
  146. RegXS peer_root;
  147. tio.recv_peer(&peer_root, sizeof(peer_root));
  148. reconstructed_root += peer_root;
  149. }
  150. auto A = oram->flat(tio, yield);
  151. auto R = A.reconstruct();
  152. if(tio.player()==0) {
  153. pretty_print(R, reconstructed_root.xshare);
  154. }
  155. }
  156. // Check the BST invariant of the tree (that all keys to the left are
  157. // less than or equal to this key, all keys to the right are strictly
  158. // greater, and this is true recursively). Returns a
  159. // tuple<bool,address_t>, where the bool says whether the BST invariant
  160. // holds, and the address_t is the height of the tree (which will be
  161. // useful later when we check AVL trees).
  162. std::tuple<bool, address_t> BST::check_bst(const std::vector<Node> &R,
  163. value_t node, value_t min_key = 0, value_t max_key = ~0)
  164. {
  165. //printf("node = %ld\n", node);
  166. if (node == 0) {
  167. return { true, 0 };
  168. }
  169. const Node &n = R[node];
  170. value_t key = n.key.ashare;
  171. value_t left_ptr = extractLeftPtr(n.pointers).xshare;
  172. value_t right_ptr = extractRightPtr(n.pointers).xshare;
  173. auto [leftok, leftheight ] = check_bst(R, left_ptr, min_key, key);
  174. auto [rightok, rightheight ] = check_bst(R, right_ptr, key+1, max_key);
  175. address_t height = leftheight;
  176. if (rightheight > height) {
  177. height = rightheight;
  178. }
  179. height += 1;
  180. //printf("node = %ld, leftok = %d, rightok = %d\n", node, leftok, rightok);
  181. return { leftok && rightok && key >= min_key && key <= max_key,
  182. height };
  183. }
  184. void BST::check_bst(MPCTIO &tio, yield_t &yield) {
  185. auto A = oram->flat(tio, yield);
  186. auto R = A.reconstruct();
  187. RegXS rec_root = this->root;
  188. if (tio.player() == 1) {
  189. tio.queue_peer(&(this->root), sizeof(this->root));
  190. } else {
  191. RegXS peer_root;
  192. tio.recv_peer(&peer_root, sizeof(peer_root));
  193. rec_root+= peer_root;
  194. }
  195. if (tio.player() == 0) {
  196. auto [ ok, height ] = check_bst(R, rec_root.xshare);
  197. printf("BST structure %s\nBST height = %u\n",
  198. ok ? "ok" : "NOT OK", height);
  199. }
  200. }
  201. void newnode(Node &a) {
  202. a.key.randomize(8);
  203. a.pointers.set(0);
  204. a.value.randomize();
  205. }
  206. void BST::initialize(int num_players, size_t size) {
  207. this->MAX_SIZE = size;
  208. oram = new Duoram<Node>(num_players, size);
  209. }
  210. std::tuple<RegXS, RegBS> BST::insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  211. const Node &new_node, Duoram<Node>::Flat &A, int TTL, RegBS isDummy) {
  212. if(TTL==0) {
  213. RegBS zero;
  214. return {ptr, zero};
  215. }
  216. RegBS isNotDummy = isDummy ^ (tio.player());
  217. Node cnode = A[ptr];
  218. // Compare key
  219. auto [lteq, gt] = compare_keys(tio, yield, cnode, new_node);
  220. // Depending on [lteq, gt] select the next ptr/index as
  221. // upper 32 bits of cnode.pointers if lteq
  222. // lower 32 bits of cnode.pointers if gt
  223. RegXS left = extractLeftPtr(cnode.pointers);
  224. RegXS right = extractRightPtr(cnode.pointers);
  225. RegXS next_ptr;
  226. mpc_select(tio, yield, next_ptr, gt, left, right, 32);
  227. CDPF dpf = tio.cdpf(yield);
  228. size_t &aes_ops = tio.aes_ops();
  229. // F_z: Check if this is last node on path
  230. RegBS F_z = dpf.is_zero(tio, yield, next_ptr, aes_ops);
  231. RegBS F_i;
  232. // F_i: If this was last node on path (F_z), and isNotDummy insert.
  233. mpc_and(tio, yield, F_i, (isNotDummy), F_z);
  234. isDummy^=F_i;
  235. auto [wptr, direction] = insert(tio, yield, next_ptr, new_node, A, TTL-1, isDummy);
  236. RegXS ret_ptr;
  237. RegBS ret_direction;
  238. // If we insert here (F_i), return the ptr to this node as wptr
  239. // and update direction to the direction taken by compare_keys
  240. mpc_select(tio, yield, ret_ptr, F_i, wptr, ptr);
  241. //ret_direction = direction + F_p(direction - gt)
  242. mpc_and(tio, yield, ret_direction, F_i, direction^gt);
  243. ret_direction^=direction;
  244. return {ret_ptr, ret_direction};
  245. }
  246. // Insert(root, ptr, key, TTL, isDummy) -> (new_ptr, wptr, wnode, f_p)
  247. void BST::insert(MPCTIO &tio, yield_t &yield, const Node &node, Duoram<Node>::Flat &A) {
  248. bool player0 = tio.player()==0;
  249. // If there are no items in tree. Make this new item the root.
  250. if(num_items==0) {
  251. Node zero;
  252. A[0] = zero;
  253. A[1] = node;
  254. (root).set(1*tio.player());
  255. num_items++;
  256. //printf("num_items == %ld!\n", num_items);
  257. return;
  258. } else {
  259. // Insert node into next free slot in the ORAM
  260. int new_id = 1 + num_items;
  261. int TTL = num_items++;
  262. A[new_id] = node;
  263. RegXS new_addr;
  264. new_addr.set(new_id * tio.player());
  265. RegBS isDummy;
  266. //Do a recursive insert
  267. auto [wptr, direction] = insert(tio, yield, root, node, A, TTL, isDummy);
  268. //Complete the insertion by reading wptr and updating its pointers
  269. RegXS pointers = A[wptr].NODE_POINTERS;
  270. RegXS left_ptr = extractLeftPtr(pointers);
  271. RegXS right_ptr = extractRightPtr(pointers);
  272. RegXS new_right_ptr, new_left_ptr;
  273. mpc_select(tio, yield, new_right_ptr, direction, right_ptr, new_addr);
  274. if(player0) {
  275. direction^=1;
  276. }
  277. mpc_select(tio, yield, new_left_ptr, direction, left_ptr, new_addr);
  278. setLeftPtr(pointers, new_left_ptr);
  279. setRightPtr(pointers, new_right_ptr);
  280. A[wptr].NODE_POINTERS = pointers;
  281. //printf("num_items == %ld!\n", num_items);
  282. }
  283. }
  284. void BST::insert(MPCTIO &tio, yield_t &yield, Node &node) {
  285. auto A = oram->flat(tio, yield);
  286. auto R = A.reconstruct();
  287. insert(tio, yield, node, A);
  288. /*
  289. // To visualize database and tree after each insert:
  290. if (tio.player() == 0) {
  291. for(size_t i=0;i<R.size();++i) {
  292. printf("\n%04lx ", i);
  293. R[i].dump();
  294. }
  295. printf("\n");
  296. }
  297. pretty_print(R, 1);
  298. */
  299. }
  300. bool BST::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  301. Duoram<Node>::Flat &A, RegBS af, RegBS fs, int TTL,
  302. del_return &ret_struct) {
  303. bool player0 = tio.player()==0;
  304. //printf("TTL = %d\n", TTL);
  305. if(TTL==0) {
  306. //Reconstruct and return af
  307. bool success = reconstruct_RegBS(tio, yield, af);
  308. //printf("Reconstructed flag = %d\n", success);
  309. if(player0)
  310. ret_struct.F_r^=1;
  311. return success;
  312. } else {
  313. bool player0 = tio.player()==0;
  314. Node node = A[ptr];
  315. // Compare key
  316. CDPF cdpf = tio.cdpf(yield);
  317. auto [lt, eq, gt] = cdpf.compare(tio, yield, del_key - node.key, tio.aes_ops());
  318. /*
  319. // Reconstruct and Debug Block 0
  320. bool lt_rec, eq_rec, gt_rec;
  321. lt_rec = reconstruct_RegBS(tio, yield, lt);
  322. eq_rec = reconstruct_RegBS(tio, yield, eq);
  323. gt_rec = reconstruct_RegBS(tio, yield, gt);
  324. size_t del_key_rec, node_key_rec;
  325. del_key_rec = reconstruct_RegAS(tio, yield, del_key);
  326. node_key_rec = reconstruct_RegAS(tio, yield, node.key);
  327. printf("node.key = %ld, del_key= %ld\n", node_key_rec, del_key_rec);
  328. printf("cdpf.compare results: lt = %d, eq = %d, gt = %d\n", lt_rec, eq_rec, gt_rec);
  329. */
  330. // c is the direction bit for next_ptr
  331. // (c=0: go left or c=1: go right)
  332. RegBS c = gt;
  333. // lf = local found. We found the key to delete in this level.
  334. RegBS lf = eq;
  335. // Depending on [lteq, gt] select the next ptr/index as
  336. // upper 32 bits of cnode.pointers if lteq
  337. // lower 32 bits of cnode.pointers if gt
  338. RegXS left = extractLeftPtr(node.pointers);
  339. RegXS right = extractRightPtr(node.pointers);
  340. CDPF dpf = tio.cdpf(yield);
  341. size_t &aes_ops = tio.aes_ops();
  342. // Check if left and right children are 0, and compute F_0, F_1, F_2
  343. RegBS l0 = dpf.is_zero(tio, yield, left, aes_ops);
  344. RegBS r0 = dpf.is_zero(tio, yield, right, aes_ops);
  345. RegBS F_0, F_1, F_2;
  346. // F_0 = l0 & r0
  347. mpc_and(tio, yield, F_0, l0, r0);
  348. // F_1 = l0 \xor r0
  349. F_1 = l0 ^ r0;
  350. // F_2 = !(F_0 + F_1) (Only 1 of F_0, F_1, and F_2 can be true)
  351. F_2 = F_0 ^ F_1;
  352. if(player0)
  353. F_2^=1;
  354. // We set next ptr based on c, but we need to handle three
  355. // edge cases where we do not go by just the comparison result
  356. RegXS next_ptr;
  357. RegBS c_prime;
  358. // Case 1: found the node here (lf): we traverse down the lone child path.
  359. // or we are finding successor (fs) and there is no left child.
  360. RegBS F_c1, F_c2, F_c3, F_c4;
  361. // Case 1: lf & F_1
  362. mpc_and(tio, yield, F_c1, lf, F_1);
  363. // Set c_prime for Case 1
  364. mpc_select(tio, yield, c_prime, F_c1, c, l0);
  365. /*
  366. // Reconstruct and Debug Block 1
  367. bool F_0_rec, F_1_rec, F_2_rec, c_prime_rec;
  368. F_0_rec = reconstruct_RegBS(tio, yield, F_0);
  369. F_1_rec = reconstruct_RegBS(tio, yield, F_1);
  370. F_2_rec = reconstruct_RegBS(tio, yield, F_2);
  371. c_prime_rec = reconstruct_RegBS(tio, yield, c_prime);
  372. printf("F_0 = %d, F_1 = %d, F_2 = %d, c_prime = %d\n", F_0_rec, F_1_rec, F_2_rec, c_prime_rec);
  373. */
  374. // s1: shares of 1 bit, s0: shares of 0 bit
  375. RegBS s1, s0;
  376. s1.set(tio.player()==1);
  377. // Case 2: found the node here (lf) and node has both children (F_2)
  378. // In find successor case, so find inorder successor
  379. // (Go right and then find leftmost child.)
  380. mpc_and(tio, yield, F_c2, lf, F_2);
  381. mpc_select(tio, yield, c_prime, F_c2, c_prime, s1);
  382. /*
  383. // Reconstruct and Debug Block 2
  384. bool F_c2_rec, s1_rec;
  385. F_c2_rec = reconstruct_RegBS(tio, yield, F_c2);
  386. s1_rec = reconstruct_RegBS(tio, yield, s1);
  387. c_prime_rec = reconstruct_RegBS(tio, yield, c_prime);
  388. printf("c_prime = %d, F_c2 = %d, s1 = %d\n", c_prime_rec, F_c2_rec, s1_rec);
  389. */
  390. // Case 3: finding successor (fs) and node has both children (F_2)
  391. // Go left.
  392. mpc_and(tio, yield, F_c3, fs, F_2);
  393. mpc_select(tio, yield, c_prime, F_c3, c_prime, s0);
  394. // Case 4: finding successor (fs) and node has no more left children (l0)
  395. // This is the successor node then.
  396. // Go left (to end the traversal without triggering flags on the real path to the right.
  397. mpc_and(tio, yield, F_c4, fs, l0);
  398. mpc_select(tio, yield, c_prime, F_c4, c_prime, l0);
  399. // Set next_ptr
  400. mpc_select(tio, yield, next_ptr, c_prime, left, right, 32);
  401. RegBS af_prime, fs_prime;
  402. mpc_or(tio, yield, af_prime, af, lf);
  403. // If in Case 2, set fs. We are now finding successor
  404. mpc_or(tio, yield, fs_prime, fs, F_c2);
  405. // If in Case 3. Successor found here already. Toggle fs off
  406. fs_prime=fs_prime^F_c4;
  407. bool key_found = del(tio, yield, next_ptr, del_key, A, af_prime, fs_prime, TTL-1, ret_struct);
  408. // If we didn't find the key, we can end here.
  409. if(!key_found)
  410. return 0;
  411. //printf("TTL = %d\n", TTL);
  412. RegBS F_rs;
  413. // Flag here should be direction (c_prime) and F_r i.e. we need to swap return ptr in,
  414. // F_r needs to be returned in ret_struct
  415. mpc_and(tio, yield, F_rs, c_prime, ret_struct.F_r);
  416. mpc_select(tio, yield, right, F_rs, right, ret_struct.ret_ptr);
  417. if(player0)
  418. c_prime^=1;
  419. mpc_and(tio, yield, F_rs, c_prime, ret_struct.F_r);
  420. mpc_select(tio, yield, left, F_rs, left, ret_struct.ret_ptr);
  421. /*
  422. // Reconstruct and Debug Block 3
  423. bool F_rs_rec, F_ls_rec;
  424. size_t ret_ptr_rec;
  425. F_rs_rec = reconstruct_RegBS(tio, yield, F_rs);
  426. F_ls_rec = reconstruct_RegBS(tio, yield, F_rs);
  427. ret_ptr_rec = reconstruct_RegXS(tio, yield, ret_struct.ret_ptr);
  428. printf("F_rs_rec = %d, F_ls_rec = %d, ret_ptr_rec = %ld\n", F_rs_rec, F_ls_rec, ret_ptr_rec);
  429. */
  430. RegXS new_ptr;
  431. setLeftPtr(new_ptr, left);
  432. setRightPtr(new_ptr, right);
  433. A[ptr].NODE_POINTERS = new_ptr;
  434. // Update the return structure
  435. RegBS F_nd, F_ns, F_r;
  436. mpc_or(tio, yield, ret_struct.F_ss, ret_struct.F_ss, F_c2);
  437. if(player0)
  438. af^=1;
  439. mpc_and(tio, yield, F_nd, lf, af);
  440. // F_ns = fs & l0
  441. // Finding successor flag & no more left child
  442. F_ns = F_c4;
  443. // F_r = F_d.(!F_2)
  444. if(player0)
  445. F_2^=1;
  446. // If we have to delete here, and it doesn't have two children we have to
  447. // update child pointer in parent with the returned pointer
  448. mpc_and(tio, yield, F_r, F_nd, F_2);
  449. mpc_or(tio, yield, F_r, F_r, F_ns);
  450. ret_struct.F_r = F_r;
  451. mpc_select(tio, yield, ret_struct.N_d, F_nd, ret_struct.N_d, ptr);
  452. mpc_select(tio, yield, ret_struct.N_s, F_ns, ret_struct.N_s, ptr);
  453. mpc_select(tio, yield, ret_struct.ret_ptr, F_r, ptr, ret_struct.ret_ptr);
  454. //We don't empty the key and value of the node with del_key in the ORAM
  455. return 1;
  456. }
  457. }
  458. bool BST::del(MPCTIO &tio, yield_t &yield, RegAS del_key) {
  459. if(num_items==0)
  460. return 0;
  461. if(num_items==1) {
  462. //Delete root
  463. auto A = oram->flat(tio, yield);
  464. Node zero;
  465. empty_locations.emplace_back(root);
  466. A[root] = zero;
  467. num_items--;
  468. return 1;
  469. } else {
  470. int TTL = num_items;
  471. // Flags for already found (af) item to delete and find successor (fs)
  472. // if this deletion requires a successor swap
  473. RegBS af;
  474. RegBS fs;
  475. del_return ret_struct;
  476. auto A = oram->flat(tio, yield);
  477. int success = del(tio, yield, root, del_key, A, af, fs, TTL, ret_struct);
  478. printf ("Success = %d\n", success);
  479. if(!success){
  480. return 0;
  481. }
  482. else{
  483. num_items--;
  484. //Add deleted (empty) location into the empty_locations vector for reuse in next insert()
  485. empty_locations.emplace_back(ret_struct.N_d);
  486. /*
  487. printf("In delete's swap portion\n");
  488. Node del_node = A.reconstruct(A[ret_struct.N_d]);
  489. Node suc_node = A.reconstruct(A[ret_struct.N_s]);
  490. printf("del_node key = %ld, suc_node key = %ld\n",
  491. del_node.key.ashare, suc_node.key.ashare);
  492. printf("flag_s = %d\n", ret_struct.F_ss.bshare);
  493. */
  494. Node del_node = A[ret_struct.N_d];
  495. Node suc_node = A[ret_struct.N_s];
  496. RegAS zero_as; RegXS zero_xs;
  497. mpc_select(tio, yield, root, ret_struct.F_r, root, ret_struct.ret_ptr);
  498. mpc_select(tio, yield, del_node.key, ret_struct.F_ss, del_node.key, suc_node.key);
  499. mpc_select(tio, yield, del_node.value, ret_struct.F_ss, del_node.value, suc_node.value);
  500. A[ret_struct.N_d].NODE_KEY = del_node.key;
  501. A[ret_struct.N_d].NODE_VALUE = del_node.value;
  502. A[ret_struct.N_s].NODE_KEY = zero_as;
  503. A[ret_struct.N_s].NODE_VALUE = zero_xs;
  504. }
  505. return 1;
  506. }
  507. }
  508. // Now we use the node in various ways. This function is called by
  509. // online.cpp.
  510. void bst(MPCIO &mpcio,
  511. const PRACOptions &opts, char **args)
  512. {
  513. nbits_t depth=4;
  514. if (*args) {
  515. depth = atoi(*args);
  516. ++args;
  517. }
  518. size_t items = (size_t(1)<<depth)-1;
  519. if (*args) {
  520. items = atoi(*args);
  521. ++args;
  522. }
  523. MPCTIO tio(mpcio, 0, opts.num_threads);
  524. run_coroutines(tio, [&tio, depth, items] (yield_t &yield) {
  525. size_t size = size_t(1)<<depth;
  526. BST tree(tio.player(), size);
  527. int insert_array[] = {10, 10, 13, 11, 14, 8, 15, 20, 17, 19, 7, 12};
  528. //int insert_array[] = {1, 2, 3, 4, 5, 6};
  529. size_t insert_array_size = 11;
  530. Node node;
  531. for(size_t i = 0; i<=insert_array_size; i++) {
  532. newnode(node);
  533. node.key.set(insert_array[i] * tio.player());
  534. tree.insert(tio, yield, node);
  535. }
  536. tree.print_oram(tio, yield);
  537. tree.pretty_print(tio, yield);
  538. RegAS del_key;
  539. printf("\n\nDelete %x\n", 20);
  540. del_key.set(20 * tio.player());
  541. tree.del(tio, yield, del_key);
  542. tree.print_oram(tio, yield);
  543. tree.pretty_print(tio, yield);
  544. tree.check_bst(tio, yield);
  545. printf("\n\nDelete %x\n", 10);
  546. del_key.set(10 * tio.player());
  547. tree.del(tio, yield, del_key);
  548. tree.print_oram(tio, yield);
  549. tree.pretty_print(tio, yield);
  550. tree.check_bst(tio, yield);
  551. printf("\n\nDelete %x\n", 8);
  552. del_key.set(8 * tio.player());
  553. tree.del(tio, yield, del_key);
  554. tree.print_oram(tio, yield);
  555. tree.pretty_print(tio, yield);
  556. tree.check_bst(tio, yield);
  557. printf("\n\nDelete %x\n", 7);
  558. del_key.set(7 * tio.player());
  559. tree.del(tio, yield, del_key);
  560. tree.print_oram(tio, yield);
  561. tree.pretty_print(tio, yield);
  562. tree.check_bst(tio, yield);
  563. printf("\n\nDelete %x\n", 17);
  564. del_key.set(17 * tio.player());
  565. tree.del(tio, yield, del_key);
  566. tree.print_oram(tio, yield);
  567. tree.pretty_print(tio, yield);
  568. tree.check_bst(tio, yield);
  569. printf("\n\nDelete %x\n", 15);
  570. del_key.set(15 * tio.player());
  571. tree.del(tio, yield, del_key);
  572. tree.print_oram(tio, yield);
  573. tree.pretty_print(tio, yield);
  574. tree.check_bst(tio, yield);
  575. printf("\n\nDelete %x\n", 5);
  576. del_key.set(5 * tio.player());
  577. tree.del(tio, yield, del_key);
  578. tree.print_oram(tio, yield);
  579. tree.pretty_print(tio, yield);
  580. tree.check_bst(tio, yield);
  581. });
  582. }