bst.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. #include <functional>
  2. #include "bst.hpp"
  3. // This file demonstrates how to implement custom ORAM wide cell types.
  4. // Such types can be structures of arbitrary numbers of RegAS and RegXS
  5. // fields. The example here imagines a node of a binary search tree,
  6. // where you would want the key to be additively shared (so that you can
  7. // easily do comparisons), the pointers field to be XOR shared (so that
  8. // you can easily do bit operations to pack two pointers and maybe some
  9. // tree balancing information into one field) and the value doesn't
  10. // really matter, but XOR shared is usually slightly more efficient.
  11. std::tuple<RegBS, RegBS> compare_keys(MPCTIO tio, yield_t &yield, Node n1, Node n2) {
  12. CDPF cdpf = tio.cdpf(yield);
  13. auto [lt, eq, gt] = cdpf.compare(tio, yield, n2.key - n1.key, tio.aes_ops());
  14. RegBS lteq = lt^eq;
  15. return {lteq, gt};
  16. }
  17. // Assuming pointer of 64 bits is split as:
  18. // - 32 bits Left ptr
  19. // - 32 bits Right ptr
  20. // < Left, Right>
  21. inline RegXS extractLeftPtr(RegXS pointer){
  22. return ((pointer&(0xFFFFFFFF00000000))>>32);
  23. }
  24. inline RegXS extractRightPtr(RegXS pointer){
  25. return (pointer&(0x00000000FFFFFFFF));
  26. }
  27. inline void setLeftPtr(RegXS &pointer, RegXS new_ptr){
  28. pointer&=(0x00000000FFFFFFFF);
  29. pointer+=(new_ptr<<32);
  30. }
  31. inline void setRightPtr(RegXS &pointer, RegXS new_ptr){
  32. pointer&=(0xFFFFFFFF00000000);
  33. pointer+=(new_ptr);
  34. }
  35. // Pretty-print a reconstructed BST, rooted at node. is_left_child and
  36. // is_right_child indicate whether node is a left or right child of its
  37. // parent. They cannot both be true, but the root of the tree has both
  38. // of them false.
  39. void BST::pretty_print(const std::vector<Node> &R, value_t node,
  40. const std::string &prefix = "", bool is_left_child = false,
  41. bool is_right_child = false)
  42. {
  43. if (node == 0) {
  44. // NULL pointer
  45. if (is_left_child) {
  46. printf("%s\xE2\x95\xA7\n", prefix.c_str()); // ╧
  47. } else if (is_right_child) {
  48. printf("%s\xE2\x95\xA4\n", prefix.c_str()); // ╤
  49. } else {
  50. printf("%s\xE2\x95\xA2\n", prefix.c_str()); // ╢
  51. }
  52. return;
  53. }
  54. const Node &n = R[node];
  55. value_t left_ptr = extractLeftPtr(n.pointers).xshare;
  56. value_t right_ptr = extractRightPtr(n.pointers).xshare;
  57. std::string rightprefix(prefix), leftprefix(prefix),
  58. nodeprefix(prefix);
  59. if (is_left_child) {
  60. rightprefix.append("\xE2\x94\x82"); // │
  61. leftprefix.append(" ");
  62. nodeprefix.append("\xE2\x94\x94"); // └
  63. } else if (is_right_child) {
  64. rightprefix.append(" ");
  65. leftprefix.append("\xE2\x94\x82"); // │
  66. nodeprefix.append("\xE2\x94\x8C"); // ┌
  67. } else {
  68. rightprefix.append(" ");
  69. leftprefix.append(" ");
  70. nodeprefix.append("\xE2\x94\x80"); // ─
  71. }
  72. pretty_print(R, right_ptr, rightprefix, false, true);
  73. printf("%s\xE2\x94\xA4", nodeprefix.c_str()); // ┤
  74. n.dump();
  75. printf("\n");
  76. pretty_print(R, left_ptr, leftprefix, true, false);
  77. }
  78. bool reconstruct_flag(MPCTIO &tio, yield_t &yield, RegBS flag) {
  79. RegBS peer_flag;
  80. RegBS reconstructed_flag;
  81. if (tio.player() == 1) {
  82. tio.queue_peer(&flag, sizeof(flag));
  83. } else {
  84. RegBS peer_flag;
  85. tio.recv_peer(&peer_flag, sizeof(peer_flag));
  86. reconstructed_flag ^= peer_flag;
  87. }
  88. if (tio.player() == 0) {
  89. tio.queue_peer(&flag, sizeof(flag));
  90. } else {
  91. RegBS peer_flag;
  92. tio.recv_peer(&peer_flag, sizeof(peer_flag));
  93. reconstructed_flag ^= peer_flag;
  94. }
  95. return reconstructed_flag.bshare;
  96. }
  97. void BST::pretty_print(MPCTIO &tio, yield_t &yield) {
  98. RegXS peer_root;
  99. RegXS reconstructed_root = root;
  100. if (tio.player() == 1) {
  101. tio.queue_peer(&root, sizeof(root));
  102. } else {
  103. RegXS peer_root;
  104. tio.recv_peer(&peer_root, sizeof(peer_root));
  105. reconstructed_root += peer_root;
  106. }
  107. auto A = oram->flat(tio, yield);
  108. auto R = A.reconstruct();
  109. if(tio.player()==0) {
  110. pretty_print(R, reconstructed_root.xshare);
  111. }
  112. }
  113. // Check the BST invariant of the tree (that all keys to the left are
  114. // less than or equal to this key, all keys to the right are strictly
  115. // greater, and this is true recursively). Returns a
  116. // tuple<bool,address_t>, where the bool says whether the BST invariant
  117. // holds, and the address_t is the height of the tree (which will be
  118. // useful later when we check AVL trees).
  119. std::tuple<bool, address_t> BST::check_bst(const std::vector<Node> &R,
  120. value_t node, value_t min_key = 0, value_t max_key = ~0)
  121. {
  122. if (node == 0) {
  123. return { true, 0 };
  124. }
  125. const Node &n = R[node];
  126. value_t key = n.key.ashare;
  127. value_t left_ptr = extractLeftPtr(n.pointers).xshare;
  128. value_t right_ptr = extractRightPtr(n.pointers).xshare;
  129. auto [leftok, leftheight ] = check_bst(R, left_ptr, min_key, key);
  130. auto [rightok, rightheight ] = check_bst(R, right_ptr, key+1, max_key);
  131. address_t height = leftheight;
  132. if (rightheight > height) {
  133. height = rightheight;
  134. }
  135. height += 1;
  136. return { leftok && rightok && key >= min_key && key <= max_key,
  137. height };
  138. }
  139. void BST::check_bst(MPCTIO &tio, yield_t &yield) {
  140. auto A = oram->flat(tio, yield);
  141. auto R = A.reconstruct();
  142. auto [ ok, height ] = check_bst(R, root.xshare);
  143. printf("BST structure %s\nBST height = %u\n",
  144. ok ? "ok" : "NOT OK", height);
  145. }
  146. void newnode(Node &a) {
  147. a.key.randomize(8);
  148. a.pointers.set(0);
  149. a.value.randomize();
  150. }
  151. void BST::initialize(int num_players, size_t size) {
  152. this->MAX_SIZE = size;
  153. oram = new Duoram<Node>(num_players, size);
  154. }
  155. std::tuple<RegXS, RegBS> BST::insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  156. const Node &new_node, Duoram<Node>::Flat &A, int TTL, RegBS isDummy) {
  157. if(TTL==0) {
  158. RegBS zero;
  159. return {ptr, zero};
  160. }
  161. RegBS isNotDummy = isDummy ^ (tio.player());
  162. Node cnode = A[ptr];
  163. // Compare key
  164. auto [lteq, gt] = compare_keys(tio, yield, cnode, new_node);
  165. // Depending on [lteq, gt] select the next ptr/index as
  166. // upper 32 bits of cnode.pointers if lteq
  167. // lower 32 bits of cnode.pointers if gt
  168. RegXS left = extractLeftPtr(cnode.pointers);
  169. RegXS right = extractRightPtr(cnode.pointers);
  170. RegXS next_ptr;
  171. mpc_select(tio, yield, next_ptr, gt, left, right, 32);
  172. CDPF dpf = tio.cdpf(yield);
  173. size_t &aes_ops = tio.aes_ops();
  174. // F_z: Check if this is last node on path
  175. RegBS F_z = dpf.is_zero(tio, yield, next_ptr, aes_ops);
  176. RegBS F_i;
  177. // F_i: If this was last node on path (F_z), and isNotDummy insert.
  178. mpc_and(tio, yield, F_i, (isNotDummy), F_z);
  179. isDummy^=F_i;
  180. auto [wptr, direction] = insert(tio, yield, next_ptr, new_node, A, TTL-1, isDummy);
  181. RegXS ret_ptr;
  182. RegBS ret_direction;
  183. // If we insert here (F_i), return the ptr to this node as wptr
  184. // and update direction to the direction taken by compare_keys
  185. mpc_select(tio, yield, ret_ptr, F_i, wptr, ptr);
  186. //ret_direction = direction + F_p(direction - gt)
  187. mpc_and(tio, yield, ret_direction, F_i, direction^gt);
  188. ret_direction^=direction;
  189. return {ret_ptr, ret_direction};
  190. }
  191. // Insert(root, ptr, key, TTL, isDummy) -> (new_ptr, wptr, wnode, f_p)
  192. void BST::insert(MPCTIO &tio, yield_t &yield, const Node &node, Duoram<Node>::Flat &A) {
  193. bool player0 = tio.player()==0;
  194. // If there are no items in tree. Make this new item the root.
  195. if(num_items==0) {
  196. Node zero;
  197. A[0] = zero;
  198. A[1] = node;
  199. (root).set(1*tio.player());
  200. num_items++;
  201. //printf("num_items == %ld!\n", num_items);
  202. return;
  203. } else {
  204. // Insert node into next free slot in the ORAM
  205. int new_id = 1 + num_items;
  206. int TTL = num_items++;
  207. A[new_id] = node;
  208. RegXS new_addr;
  209. new_addr.set(new_id * tio.player());
  210. RegBS isDummy;
  211. //Do a recursive insert
  212. auto [wptr, direction] = insert(tio, yield, root, node, A, TTL, isDummy);
  213. //Complete the insertion by reading wptr and updating its pointers
  214. RegXS pointers = A[wptr].NODE_POINTERS;
  215. RegXS left_ptr = extractLeftPtr(pointers);
  216. RegXS right_ptr = extractRightPtr(pointers);
  217. RegXS new_right_ptr, new_left_ptr;
  218. mpc_select(tio, yield, new_right_ptr, direction, right_ptr, new_addr);
  219. if(player0) {
  220. direction^=1;
  221. }
  222. mpc_select(tio, yield, new_left_ptr, direction, left_ptr, new_addr);
  223. setLeftPtr(pointers, new_left_ptr);
  224. setRightPtr(pointers, new_right_ptr);
  225. A[wptr].NODE_POINTERS = pointers;
  226. //printf("num_items == %ld!\n", num_items);
  227. }
  228. }
  229. void BST::insert(MPCTIO &tio, yield_t &yield, Node &node) {
  230. auto A = oram->flat(tio, yield);
  231. auto R = A.reconstruct();
  232. insert(tio, yield, node, A);
  233. /*
  234. // To visualize database and tree after each insert:
  235. if (tio.player() == 0) {
  236. for(size_t i=0;i<R.size();++i) {
  237. printf("\n%04lx ", i);
  238. R[i].dump();
  239. }
  240. printf("\n");
  241. }
  242. pretty_print(R, 1);
  243. */
  244. }
  245. /*
  246. // Compute in MPC a | b
  247. void mpc_or(MPCTIO &tio, yield_t &yield, RegBS &result, RegBS a, RegBS b) {
  248. int player0 = tio.player();
  249. if(player0) {
  250. a^=1;
  251. b^=1;
  252. }
  253. mpc_and(tio, yield, result, a, b);
  254. if(player0)
  255. result^=1;
  256. }
  257. */
  258. int BST::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
  259. Duoram<Node>::Flat &A, RegBS af, RegBS fs, int TTL,
  260. del_return &ret_struct) {
  261. if(TTL==0) {
  262. //Reconstruct and return af
  263. bool af = reconstruct_flag(tio, yield, af);
  264. printf("Reconstructed flag = %d\n", af);
  265. return af;
  266. } else {
  267. bool player0 = tio.player()==0;
  268. Node node = A[ptr];
  269. // Compare key
  270. CDPF cdpf = tio.cdpf(yield);
  271. auto [lt, eq, gt] = cdpf.compare(tio, yield, node.key - del_key, tio.aes_ops());
  272. // c is the direction bit for next_ptr
  273. // (c=0: go left or c=1: go right)
  274. RegBS c = gt;
  275. // lf = local found. We found the key to delete in this level.
  276. RegBS lf = eq;
  277. // Depending on [lteq, gt] select the next ptr/index as
  278. // upper 32 bits of cnode.pointers if lteq
  279. // lower 32 bits of cnode.pointers if gt
  280. RegXS left = extractLeftPtr(node.pointers);
  281. RegXS right = extractRightPtr(node.pointers);
  282. CDPF dpf = tio.cdpf(yield);
  283. size_t &aes_ops = tio.aes_ops();
  284. // Check if left and right children are 0, and compute F_0, F_1, F_2
  285. RegBS l0 = dpf.is_zero(tio, yield, left, aes_ops);
  286. RegBS r0 = dpf.is_zero(tio, yield, right, aes_ops);
  287. RegBS F_0, F_1, F_2;
  288. // F_0 = l0 & r0
  289. mpc_and(tio, yield, F_0, l0, r0);
  290. // F_1 = l0 \xor r0
  291. F_1 = l0 ^ r0;
  292. // F_2 = !(F_0 + F_1) (Only 1 of F_0, F_1, and F_2 can be true)
  293. F_2 = F_0 ^ F_1;
  294. if(player0)
  295. F_2^=1;
  296. // We set next ptr based on c, but we need to handle three
  297. // edge cases where we do not go by just the comparison result
  298. RegXS next_ptr;
  299. RegBS c_prime;
  300. // Case 1: found the node here (lf) or we are finding successor (fs)
  301. // and there is only one child. We traverse down the lone child path.
  302. RegBS F_c11, F_c12, F_c2, F_c3;
  303. // Case 1a: lf & F_1
  304. mpc_and(tio, yield, F_c11, lf, F_1);
  305. // Case 1b: fs & F_1
  306. mpc_and(tio, yield, F_c12, fs, F_1);
  307. // Set c_prime for Case 1a and 1b
  308. mpc_select(tio, yield, c_prime, F_c1, c, l0);
  309. mpc_select(tio, yield, c_prime, F_c2, c, l0);
  310. // s1: shares of 1 bit, s0: shares of 0 bit
  311. RegBS s1, s0;
  312. s1.set(tio.player()==1);
  313. // Case 2: found the node here (lf) and node has both children (F_2)
  314. // In find successor case, so find inorder successor
  315. // (Go right and then find leftmost child.)
  316. mpc_and(tio, yield, F_c2, lf, F_2);
  317. mpc_select(tio, yield, c_prime, F_c2, c, s1);
  318. // Case 3: finding successor (fs) and node has both children (F_2)
  319. // Go left.
  320. mpc_and(tio, yield, F_c3, fs, F_2);
  321. mpc_select(tio, yield, c_prime, F_c3, c, s0);
  322. // Set next_ptr
  323. mpc_select(tio, yield, next_ptr, c_prime, left, right, 32);
  324. RegBS af_prime, fs_prime;
  325. mpc_or(tio, yield, af_prime, af, lf);
  326. // If in Case 2, set fs. We are now finding successor
  327. mpc_or(tio, yield, fs_prime, fs, F_c2);
  328. int key_found = del(tio, yield, next_ptr, del_key, A, af_prime, fs_prime, TTL-1, ret_struct);
  329. // If we didn't find the key, we can end here.
  330. if(!key_found)
  331. return 0;
  332. // Update node.left and node.right with ret_struct.rptr and [c] as slct bit
  333. // Update the return structure
  334. }
  335. return 1;
  336. }
  337. int BST::del(MPCTIO &tio, yield_t &yield, RegAS del_key) {
  338. if(num_items==0)
  339. return 0;
  340. if(num_items==1) {
  341. //Delete root
  342. auto A = oram->flat(tio, yield);
  343. Node zero;
  344. A[0] = zero;
  345. num_items--;
  346. return 1;
  347. } else {
  348. int TTL = num_items;
  349. // Flags for already found (af) item to delete and find successor (fs)
  350. // if this deletion requires a successor swap
  351. RegBS af;
  352. RegBS fs;
  353. del_return ret_struct;
  354. auto A = oram->flat(tio, yield);
  355. int success = del(tio, yield, root, del_key, A, af, fs, TTL, ret_struct);
  356. printf ("Success = %d\n", success);
  357. return 1;
  358. }
  359. }
  360. // Now we use the node in various ways. This function is called by
  361. // online.cpp.
  362. void bst(MPCIO &mpcio,
  363. const PRACOptions &opts, char **args)
  364. {
  365. nbits_t depth=3;
  366. if (*args) {
  367. depth = atoi(*args);
  368. ++args;
  369. }
  370. size_t items = (size_t(1)<<depth)-1;
  371. if (*args) {
  372. items = atoi(*args);
  373. ++args;
  374. }
  375. MPCTIO tio(mpcio, 0, opts.num_threads);
  376. run_coroutines(tio, [&tio, depth, items] (yield_t &yield) {
  377. size_t size = size_t(1)<<depth;
  378. BST tree(tio.player(), size);
  379. /*
  380. Node node;
  381. for(size_t i = 1; i<=items; i++) {
  382. newnode(node);
  383. node.key.set(i * tio.player());
  384. tree.insert(tio, yield, node);
  385. }
  386. tree.pretty_print(tio, yield);
  387. */
  388. RegAS del_key;
  389. tree.del(tio, yield, del_key);
  390. /*
  391. if (depth < 10) {
  392. //oram.dump();
  393. auto R = A.reconstruct();
  394. // Reconstruct the root
  395. if (tio.player() == 1) {
  396. tio.queue_peer(&root, sizeof(root));
  397. } else {
  398. RegXS peer_root;
  399. tio.recv_peer(&peer_root, sizeof(peer_root));
  400. root += peer_root;
  401. }
  402. if (tio.player() == 0) {
  403. for(size_t i=0;i<R.size();++i) {
  404. printf("\n%04lx ", i);
  405. R[i].dump();
  406. }
  407. printf("\n");
  408. pretty_print(R, root.xshare);
  409. auto [ ok, height ] = check_bst(R, root.xshare);
  410. printf("BST structure %s\nBST height = %u\n",
  411. ok ? "ok" : "NOT OK", height);
  412. }
  413. }
  414. */
  415. });
  416. }