bst.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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;
  261. RegXS insert_address;
  262. int TTL = num_items++;
  263. bool insertAtEmptyLocation = (numEmptyLocations() > 0);
  264. if(insertAtEmptyLocation) {
  265. insert_address = empty_locations.back();
  266. empty_locations.pop_back();
  267. A[insert_address] = node;
  268. } else {
  269. new_id = 1 + num_items;
  270. A[new_id] = node;
  271. insert_address.set(new_id * tio.player());
  272. }
  273. RegBS isDummy;
  274. //Do a recursive insert
  275. auto [wptr, direction] = insert(tio, yield, root, node, A, TTL, isDummy);
  276. //Complete the insertion by reading wptr and updating its pointers
  277. RegXS pointers = A[wptr].NODE_POINTERS;
  278. RegXS left_ptr = extractLeftPtr(pointers);
  279. RegXS right_ptr = extractRightPtr(pointers);
  280. RegXS new_right_ptr, new_left_ptr;
  281. mpc_select(tio, yield, new_right_ptr, direction, right_ptr, insert_address);
  282. if(player0) {
  283. direction^=1;
  284. }
  285. mpc_select(tio, yield, new_left_ptr, direction, left_ptr, insert_address);
  286. setLeftPtr(pointers, new_left_ptr);
  287. setRightPtr(pointers, new_right_ptr);
  288. A[wptr].NODE_POINTERS = pointers;
  289. //printf("num_items == %ld!\n", num_items);
  290. }
  291. }
  292. void BST::insert(MPCTIO &tio, yield_t &yield, Node &node) {
  293. auto A = oram->flat(tio, yield);
  294. auto R = A.reconstruct();
  295. insert(tio, yield, node, A);
  296. /*
  297. // To visualize database and tree after each insert:
  298. if (tio.player() == 0) {
  299. for(size_t i=0;i<R.size();++i) {
  300. printf("\n%04lx ", i);
  301. R[i].dump();
  302. }
  303. printf("\n");
  304. }
  305. pretty_print(R, 1);
  306. */
  307. }
  308. bool BST::lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key, Duoram<Node>::Flat &A,
  309. int TTL, RegBS isDummy, Node *ret_node) {
  310. if(TTL==0) {
  311. // Reconstruct and return isDummy
  312. // If we found the key, then isDummy will be true
  313. bool found = reconstruct_RegBS(tio, yield, isDummy);
  314. return found;
  315. }
  316. RegBS isNotDummy = isDummy ^ (tio.player());
  317. Node cnode = A[ptr];
  318. // Compare key
  319. CDPF cdpf = tio.cdpf(yield);
  320. auto [lt, eq, gt] = cdpf.compare(tio, yield, key - cnode.key, tio.aes_ops());
  321. // Depending on [lteq, gt] select the next ptr/index as
  322. // upper 32 bits of cnode.pointers if lteq
  323. // lower 32 bits of cnode.pointers if gt
  324. RegXS left = extractLeftPtr(cnode.pointers);
  325. RegXS right = extractRightPtr(cnode.pointers);
  326. RegXS next_ptr;
  327. mpc_select(tio, yield, next_ptr, gt, left, right, 32);
  328. RegBS F_found;
  329. // If we haven't found the key yet, and the lookup matches the current node key,
  330. // then we found the node to return
  331. mpc_and(tio, yield, F_found, isNotDummy, eq);
  332. mpc_select(tio, yield, ret_node->key, eq, ret_node->key, cnode.key);
  333. mpc_select(tio, yield, ret_node->value, eq, ret_node->value, cnode.value);
  334. isDummy^=F_found;
  335. bool found = lookup(tio, yield, next_ptr, key, A, TTL-1, isDummy, ret_node);
  336. return found;
  337. }
  338. bool BST::lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node) {
  339. auto A = oram->flat(tio, yield);
  340. auto R = A.reconstruct();
  341. RegBS isDummy;
  342. bool found = lookup(tio, yield, root, key, A, num_items, isDummy, ret_node);
  343. /*
  344. // To visualize database and tree after each lookup:
  345. if (tio.player() == 0) {
  346. for(size_t i=0;i<R.size();++i) {
  347. printf("\n%04lx ", i);
  348. R[i].dump();
  349. }
  350. printf("\n");
  351. }
  352. pretty_print(R, 1);
  353. */
  354. return found;
  355. }
  356. bool BST::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  357. Duoram<Node>::Flat &A, RegBS af, RegBS fs, int TTL,
  358. del_return &ret_struct) {
  359. bool player0 = tio.player()==0;
  360. //printf("TTL = %d\n", TTL);
  361. if(TTL==0) {
  362. //Reconstruct and return af
  363. bool success = reconstruct_RegBS(tio, yield, af);
  364. //printf("Reconstructed flag = %d\n", success);
  365. if(player0)
  366. ret_struct.F_r^=1;
  367. return success;
  368. } else {
  369. bool player0 = tio.player()==0;
  370. Node node = A[ptr];
  371. // Compare key
  372. CDPF cdpf = tio.cdpf(yield);
  373. auto [lt, eq, gt] = cdpf.compare(tio, yield, del_key - node.key, tio.aes_ops());
  374. /*
  375. // Reconstruct and Debug Block 0
  376. bool lt_rec, eq_rec, gt_rec;
  377. lt_rec = reconstruct_RegBS(tio, yield, lt);
  378. eq_rec = reconstruct_RegBS(tio, yield, eq);
  379. gt_rec = reconstruct_RegBS(tio, yield, gt);
  380. size_t del_key_rec, node_key_rec;
  381. del_key_rec = reconstruct_RegAS(tio, yield, del_key);
  382. node_key_rec = reconstruct_RegAS(tio, yield, node.key);
  383. printf("node.key = %ld, del_key= %ld\n", node_key_rec, del_key_rec);
  384. printf("cdpf.compare results: lt = %d, eq = %d, gt = %d\n", lt_rec, eq_rec, gt_rec);
  385. */
  386. // c is the direction bit for next_ptr
  387. // (c=0: go left or c=1: go right)
  388. RegBS c = gt;
  389. // lf = local found. We found the key to delete in this level.
  390. RegBS lf = eq;
  391. // Depending on [lteq, gt] select the next ptr/index as
  392. // upper 32 bits of cnode.pointers if lteq
  393. // lower 32 bits of cnode.pointers if gt
  394. RegXS left = extractLeftPtr(node.pointers);
  395. RegXS right = extractRightPtr(node.pointers);
  396. CDPF dpf = tio.cdpf(yield);
  397. size_t &aes_ops = tio.aes_ops();
  398. // Check if left and right children are 0, and compute F_0, F_1, F_2
  399. RegBS l0 = dpf.is_zero(tio, yield, left, aes_ops);
  400. RegBS r0 = dpf.is_zero(tio, yield, right, aes_ops);
  401. RegBS F_0, F_1, F_2;
  402. // F_0 = l0 & r0
  403. mpc_and(tio, yield, F_0, l0, r0);
  404. // F_1 = l0 \xor r0
  405. F_1 = l0 ^ r0;
  406. // F_2 = !(F_0 + F_1) (Only 1 of F_0, F_1, and F_2 can be true)
  407. F_2 = F_0 ^ F_1;
  408. if(player0)
  409. F_2^=1;
  410. // We set next ptr based on c, but we need to handle three
  411. // edge cases where we do not go by just the comparison result
  412. RegXS next_ptr;
  413. RegBS c_prime;
  414. // Case 1: found the node here (lf): we traverse down the lone child path.
  415. // or we are finding successor (fs) and there is no left child.
  416. RegBS F_c1, F_c2, F_c3, F_c4;
  417. // Case 1: lf & F_1
  418. mpc_and(tio, yield, F_c1, lf, F_1);
  419. // Set c_prime for Case 1
  420. mpc_select(tio, yield, c_prime, F_c1, c, l0);
  421. /*
  422. // Reconstruct and Debug Block 1
  423. bool F_0_rec, F_1_rec, F_2_rec, c_prime_rec;
  424. F_0_rec = reconstruct_RegBS(tio, yield, F_0);
  425. F_1_rec = reconstruct_RegBS(tio, yield, F_1);
  426. F_2_rec = reconstruct_RegBS(tio, yield, F_2);
  427. c_prime_rec = reconstruct_RegBS(tio, yield, c_prime);
  428. 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);
  429. */
  430. // s1: shares of 1 bit, s0: shares of 0 bit
  431. RegBS s1, s0;
  432. s1.set(tio.player()==1);
  433. // Case 2: found the node here (lf) and node has both children (F_2)
  434. // In find successor case, so find inorder successor
  435. // (Go right and then find leftmost child.)
  436. mpc_and(tio, yield, F_c2, lf, F_2);
  437. mpc_select(tio, yield, c_prime, F_c2, c_prime, s1);
  438. /*
  439. // Reconstruct and Debug Block 2
  440. bool F_c2_rec, s1_rec;
  441. F_c2_rec = reconstruct_RegBS(tio, yield, F_c2);
  442. s1_rec = reconstruct_RegBS(tio, yield, s1);
  443. c_prime_rec = reconstruct_RegBS(tio, yield, c_prime);
  444. printf("c_prime = %d, F_c2 = %d, s1 = %d\n", c_prime_rec, F_c2_rec, s1_rec);
  445. */
  446. // Case 3: finding successor (fs) and node has both children (F_2)
  447. // Go left.
  448. mpc_and(tio, yield, F_c3, fs, F_2);
  449. mpc_select(tio, yield, c_prime, F_c3, c_prime, s0);
  450. // Case 4: finding successor (fs) and node has no more left children (l0)
  451. // This is the successor node then.
  452. // Go left (to end the traversal without triggering flags on the real path to the right.
  453. mpc_and(tio, yield, F_c4, fs, l0);
  454. mpc_select(tio, yield, c_prime, F_c4, c_prime, l0);
  455. // Set next_ptr
  456. mpc_select(tio, yield, next_ptr, c_prime, left, right, 32);
  457. RegBS af_prime, fs_prime;
  458. mpc_or(tio, yield, af_prime, af, lf);
  459. // If in Case 2, set fs. We are now finding successor
  460. mpc_or(tio, yield, fs_prime, fs, F_c2);
  461. // If in Case 3. Successor found here already. Toggle fs off
  462. fs_prime=fs_prime^F_c4;
  463. bool key_found = del(tio, yield, next_ptr, del_key, A, af_prime, fs_prime, TTL-1, ret_struct);
  464. // If we didn't find the key, we can end here.
  465. if(!key_found)
  466. return 0;
  467. //printf("TTL = %d\n", TTL);
  468. RegBS F_rs;
  469. // Flag here should be direction (c_prime) and F_r i.e. we need to swap return ptr in,
  470. // F_r needs to be returned in ret_struct
  471. mpc_and(tio, yield, F_rs, c_prime, ret_struct.F_r);
  472. mpc_select(tio, yield, right, F_rs, right, ret_struct.ret_ptr);
  473. if(player0)
  474. c_prime^=1;
  475. mpc_and(tio, yield, F_rs, c_prime, ret_struct.F_r);
  476. mpc_select(tio, yield, left, F_rs, left, ret_struct.ret_ptr);
  477. /*
  478. // Reconstruct and Debug Block 3
  479. bool F_rs_rec, F_ls_rec;
  480. size_t ret_ptr_rec;
  481. F_rs_rec = reconstruct_RegBS(tio, yield, F_rs);
  482. F_ls_rec = reconstruct_RegBS(tio, yield, F_rs);
  483. ret_ptr_rec = reconstruct_RegXS(tio, yield, ret_struct.ret_ptr);
  484. printf("F_rs_rec = %d, F_ls_rec = %d, ret_ptr_rec = %ld\n", F_rs_rec, F_ls_rec, ret_ptr_rec);
  485. */
  486. RegXS new_ptr;
  487. setLeftPtr(new_ptr, left);
  488. setRightPtr(new_ptr, right);
  489. A[ptr].NODE_POINTERS = new_ptr;
  490. // Update the return structure
  491. RegBS F_nd, F_ns, F_r;
  492. mpc_or(tio, yield, ret_struct.F_ss, ret_struct.F_ss, F_c2);
  493. if(player0)
  494. af^=1;
  495. mpc_and(tio, yield, F_nd, lf, af);
  496. // F_ns = fs & l0
  497. // Finding successor flag & no more left child
  498. F_ns = F_c4;
  499. // F_r = F_d.(!F_2)
  500. if(player0)
  501. F_2^=1;
  502. // If we have to delete here, and it doesn't have two children we have to
  503. // update child pointer in parent with the returned pointer
  504. mpc_and(tio, yield, F_r, F_nd, F_2);
  505. mpc_or(tio, yield, F_r, F_r, F_ns);
  506. ret_struct.F_r = F_r;
  507. mpc_select(tio, yield, ret_struct.N_d, F_nd, ret_struct.N_d, ptr);
  508. mpc_select(tio, yield, ret_struct.N_s, F_ns, ret_struct.N_s, ptr);
  509. mpc_select(tio, yield, ret_struct.ret_ptr, F_r, ptr, ret_struct.ret_ptr);
  510. //We don't empty the key and value of the node with del_key in the ORAM
  511. return 1;
  512. }
  513. }
  514. bool BST::del(MPCTIO &tio, yield_t &yield, RegAS del_key) {
  515. if(num_items==0)
  516. return 0;
  517. if(num_items==1) {
  518. //Delete root
  519. auto A = oram->flat(tio, yield);
  520. Node zero;
  521. empty_locations.emplace_back(root);
  522. A[root] = zero;
  523. num_items--;
  524. return 1;
  525. } else {
  526. int TTL = num_items;
  527. // Flags for already found (af) item to delete and find successor (fs)
  528. // if this deletion requires a successor swap
  529. RegBS af;
  530. RegBS fs;
  531. del_return ret_struct;
  532. auto A = oram->flat(tio, yield);
  533. int success = del(tio, yield, root, del_key, A, af, fs, TTL, ret_struct);
  534. printf ("Success = %d\n", success);
  535. if(!success){
  536. return 0;
  537. }
  538. else{
  539. num_items--;
  540. //Add deleted (empty) location into the empty_locations vector for reuse in next insert()
  541. empty_locations.emplace_back(ret_struct.N_d);
  542. /*
  543. printf("In delete's swap portion\n");
  544. Node del_node = A.reconstruct(A[ret_struct.N_d]);
  545. Node suc_node = A.reconstruct(A[ret_struct.N_s]);
  546. printf("del_node key = %ld, suc_node key = %ld\n",
  547. del_node.key.ashare, suc_node.key.ashare);
  548. printf("flag_s = %d\n", ret_struct.F_ss.bshare);
  549. */
  550. Node del_node = A[ret_struct.N_d];
  551. Node suc_node = A[ret_struct.N_s];
  552. RegAS zero_as; RegXS zero_xs;
  553. mpc_select(tio, yield, root, ret_struct.F_r, root, ret_struct.ret_ptr);
  554. mpc_select(tio, yield, del_node.key, ret_struct.F_ss, del_node.key, suc_node.key);
  555. mpc_select(tio, yield, del_node.value, ret_struct.F_ss, del_node.value, suc_node.value);
  556. A[ret_struct.N_d].NODE_KEY = del_node.key;
  557. A[ret_struct.N_d].NODE_VALUE = del_node.value;
  558. A[ret_struct.N_s].NODE_KEY = zero_as;
  559. A[ret_struct.N_s].NODE_VALUE = zero_xs;
  560. }
  561. return 1;
  562. }
  563. }
  564. // Now we use the node in various ways. This function is called by
  565. // online.cpp.
  566. void bst(MPCIO &mpcio,
  567. const PRACOptions &opts, char **args)
  568. {
  569. nbits_t depth=4;
  570. if (*args) {
  571. depth = atoi(*args);
  572. ++args;
  573. }
  574. size_t items = (size_t(1)<<depth)-1;
  575. if (*args) {
  576. items = atoi(*args);
  577. ++args;
  578. }
  579. MPCTIO tio(mpcio, 0, opts.num_threads);
  580. run_coroutines(tio, [&tio, depth, items] (yield_t &yield) {
  581. size_t size = size_t(1)<<depth;
  582. BST tree(tio.player(), size);
  583. int insert_array[] = {10, 10, 13, 11, 14, 8, 15, 20, 17, 19, 7, 12};
  584. //int insert_array[] = {1, 2, 3, 4, 5, 6};
  585. size_t insert_array_size = 11;
  586. Node node;
  587. for(size_t i = 0; i<=insert_array_size; i++) {
  588. newnode(node);
  589. node.key.set(insert_array[i] * tio.player());
  590. tree.insert(tio, yield, node);
  591. }
  592. tree.print_oram(tio, yield);
  593. tree.pretty_print(tio, yield);
  594. RegAS del_key;
  595. /*
  596. printf("\n\nDelete %x\n", 20);
  597. del_key.set(20 * tio.player());
  598. tree.del(tio, yield, del_key);
  599. tree.print_oram(tio, yield);
  600. tree.pretty_print(tio, yield);
  601. tree.check_bst(tio, yield);
  602. printf("\n\nDelete %x\n", 10);
  603. del_key.set(10 * tio.player());
  604. tree.del(tio, yield, del_key);
  605. tree.print_oram(tio, yield);
  606. tree.pretty_print(tio, yield);
  607. tree.check_bst(tio, yield);
  608. printf("\n\nDelete %x\n", 8);
  609. del_key.set(8 * tio.player());
  610. tree.del(tio, yield, del_key);
  611. tree.print_oram(tio, yield);
  612. tree.pretty_print(tio, yield);
  613. tree.check_bst(tio, yield);
  614. printf("\n\nDelete %x\n", 7);
  615. del_key.set(7 * tio.player());
  616. tree.del(tio, yield, del_key);
  617. tree.print_oram(tio, yield);
  618. tree.pretty_print(tio, yield);
  619. tree.check_bst(tio, yield);
  620. printf("\n\nDelete %x\n", 17);
  621. del_key.set(17 * tio.player());
  622. tree.del(tio, yield, del_key);
  623. tree.print_oram(tio, yield);
  624. tree.pretty_print(tio, yield);
  625. tree.check_bst(tio, yield);
  626. printf("\n\nDelete %x\n", 15);
  627. del_key.set(15 * tio.player());
  628. tree.del(tio, yield, del_key);
  629. tree.print_oram(tio, yield);
  630. tree.pretty_print(tio, yield);
  631. tree.check_bst(tio, yield);
  632. printf("Num empty_locations = %ld\n", tree.numEmptyLocations());
  633. printf("\n\nDelete %x\n", 5);
  634. del_key.set(5 * 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. */
  641. printf("\n\nInsert %x\n", 14);
  642. newnode(node);
  643. node.key.set(14 * tio.player());
  644. tree.insert(tio, yield, node);
  645. tree.print_oram(tio, yield);
  646. tree.pretty_print(tio, yield);
  647. tree.check_bst(tio, yield);
  648. printf("Num empty_locations = %ld\n", tree.numEmptyLocations());
  649. printf("\n\nLookup %x\n", 8);
  650. newnode(node);
  651. RegAS lookup_key;
  652. bool found;
  653. lookup_key.set(8 * tio.player());
  654. found = tree.lookup(tio, yield, lookup_key, &node);
  655. tree.print_oram(tio, yield);
  656. tree.pretty_print(tio, yield);
  657. if(found) {
  658. printf("Lookup Success\n");
  659. size_t value = reconstruct_RegXS(tio, yield, node.value);
  660. printf("value = %lx\n", value);
  661. } else {
  662. printf("Lookup Failed\n");
  663. }
  664. printf("\n\nLookup %x\n", 99);
  665. newnode(node);
  666. lookup_key.set(99 * tio.player());
  667. found = tree.lookup(tio, yield, lookup_key, &node);
  668. tree.print_oram(tio, yield);
  669. tree.pretty_print(tio, yield);
  670. if(found) {
  671. printf("Lookup Success\n");
  672. size_t value = reconstruct_RegXS(tio, yield, node.value);
  673. printf("value = %lx\n", value);
  674. } else {
  675. printf("Lookup Failed\n");
  676. }
  677. });
  678. }