bst.cpp 26 KB

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