bst.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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(Node n1, Node n2, MPCTIO tio, yield_t &yield) {
  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. std::tuple<RegXS, RegBS> BST::insert(MPCTIO &tio, yield_t &yield, RegXS ptr,
  36. const Node &new_node, Duoram<Node>::Flat &A, int TTL, RegBS isDummy) {
  37. if(TTL==0) {
  38. RegBS zero;
  39. return {ptr, zero};
  40. }
  41. RegBS isNotDummy = isDummy ^ (tio.player());
  42. Node cnode = A[ptr];
  43. // Compare key
  44. auto [lteq, gt] = compare_keys(cnode, new_node, tio, yield);
  45. // Depending on [lteq, gt] select the next ptr/index as
  46. // upper 32 bits of cnode.pointers if lteq
  47. // lower 32 bits of cnode.pointers if gt
  48. RegXS left = extractLeftPtr(cnode.pointers);
  49. RegXS right = extractRightPtr(cnode.pointers);
  50. RegXS next_ptr;
  51. mpc_select(tio, yield, next_ptr, gt, left, right, 32);
  52. CDPF dpf = tio.cdpf(yield);
  53. size_t &aes_ops = tio.aes_ops();
  54. // F_z: Check if this is last node on path
  55. RegBS F_z = dpf.is_zero(tio, yield, next_ptr, aes_ops);
  56. RegBS F_i;
  57. // F_i: If this was last node on path (F_z), and isNotDummy insert.
  58. mpc_and(tio, yield, F_i, (isNotDummy), F_z);
  59. isDummy^=F_i;
  60. auto [wptr, direction] = insert(tio, yield, next_ptr, new_node, A, TTL-1, isDummy);
  61. RegXS ret_ptr;
  62. RegBS ret_direction;
  63. // If we insert here (F_i), return the ptr to this node as wptr
  64. // and update direction to the direction taken by compare_keys
  65. mpc_select(tio, yield, ret_ptr, F_i, wptr, ptr);
  66. //ret_direction = direction + F_p(direction - gt)
  67. mpc_and(tio, yield, ret_direction, F_i, direction^gt);
  68. ret_direction^=direction;
  69. return {ret_ptr, ret_direction};
  70. }
  71. // Pretty-print a reconstructed BST, rooted at node. is_left_child and
  72. // is_right_child indicate whether node is a left or right child of its
  73. // parent. They cannot both be true, but the root of the tree has both
  74. // of them false.
  75. void BST::pretty_print(const std::vector<Node> &R, value_t node,
  76. const std::string &prefix = "", bool is_left_child = false,
  77. bool is_right_child = false)
  78. {
  79. if (node == 0) {
  80. // NULL pointer
  81. if (is_left_child) {
  82. printf("%s\xE2\x95\xA7\n", prefix.c_str()); // ╧
  83. } else if (is_right_child) {
  84. printf("%s\xE2\x95\xA4\n", prefix.c_str()); // ╤
  85. } else {
  86. printf("%s\xE2\x95\xA2\n", prefix.c_str()); // ╢
  87. }
  88. return;
  89. }
  90. const Node &n = R[node];
  91. value_t left_ptr = extractLeftPtr(n.pointers).xshare;
  92. value_t right_ptr = extractRightPtr(n.pointers).xshare;
  93. std::string rightprefix(prefix), leftprefix(prefix),
  94. nodeprefix(prefix);
  95. if (is_left_child) {
  96. rightprefix.append("\xE2\x94\x82"); // │
  97. leftprefix.append(" ");
  98. nodeprefix.append("\xE2\x94\x94"); // └
  99. } else if (is_right_child) {
  100. rightprefix.append(" ");
  101. leftprefix.append("\xE2\x94\x82"); // │
  102. nodeprefix.append("\xE2\x94\x8C"); // ┌
  103. } else {
  104. rightprefix.append(" ");
  105. leftprefix.append(" ");
  106. nodeprefix.append("\xE2\x94\x80"); // ─
  107. }
  108. pretty_print(R, right_ptr, rightprefix, false, true);
  109. printf("%s\xE2\x94\xA4", nodeprefix.c_str()); // ┤
  110. n.dump();
  111. printf("\n");
  112. pretty_print(R, left_ptr, leftprefix, true, false);
  113. }
  114. void BST::pretty_print(MPCTIO &tio, yield_t &yield) {
  115. RegXS peer_root;
  116. RegXS reconstructed_root = root;
  117. if (tio.player() == 1) {
  118. tio.queue_peer(&root, sizeof(root));
  119. } else {
  120. RegXS peer_root;
  121. tio.recv_peer(&peer_root, sizeof(peer_root));
  122. reconstructed_root += peer_root;
  123. }
  124. auto A = oram->flat(tio, yield);
  125. auto R = A.reconstruct();
  126. if(tio.player()==0) {
  127. pretty_print(R, reconstructed_root.xshare);
  128. }
  129. }
  130. // Check the BST invariant of the tree (that all keys to the left are
  131. // less than or equal to this key, all keys to the right are strictly
  132. // greater, and this is true recursively). Returns a
  133. // tuple<bool,address_t>, where the bool says whether the BST invariant
  134. // holds, and the address_t is the height of the tree (which will be
  135. // useful later when we check AVL trees).
  136. std::tuple<bool, address_t> BST::check_bst(const std::vector<Node> &R,
  137. value_t node, value_t min_key = 0, value_t max_key = ~0)
  138. {
  139. if (node == 0) {
  140. return { true, 0 };
  141. }
  142. const Node &n = R[node];
  143. value_t key = n.key.ashare;
  144. value_t left_ptr = extractLeftPtr(n.pointers).xshare;
  145. value_t right_ptr = extractRightPtr(n.pointers).xshare;
  146. auto [leftok, leftheight ] = check_bst(R, left_ptr, min_key, key);
  147. auto [rightok, rightheight ] = check_bst(R, right_ptr, key+1, max_key);
  148. address_t height = leftheight;
  149. if (rightheight > height) {
  150. height = rightheight;
  151. }
  152. height += 1;
  153. return { leftok && rightok && key >= min_key && key <= max_key,
  154. height };
  155. }
  156. void BST::check_bst(MPCTIO &tio, yield_t &yield) {
  157. auto A = oram->flat(tio, yield);
  158. auto R = A.reconstruct();
  159. auto [ ok, height ] = check_bst(R, root.xshare);
  160. printf("BST structure %s\nBST height = %u\n",
  161. ok ? "ok" : "NOT OK", height);
  162. }
  163. void newnode(Node &a) {
  164. a.key.randomize(8);
  165. a.pointers.set(0);
  166. a.value.randomize();
  167. }
  168. void BST::initialize(int num_players, size_t size) {
  169. this->MAX_SIZE = size;
  170. oram = new Duoram<Node>(num_players, size);
  171. }
  172. // Insert(root, ptr, key, TTL, isDummy) -> (new_ptr, wptr, wnode, f_p)
  173. void BST::insert(MPCTIO &tio, yield_t &yield, const Node &node, Duoram<Node>::Flat &A) {
  174. bool player0 = tio.player()==0;
  175. // If there are no items in tree. Make this new item the root.
  176. if(num_items==0) {
  177. Node zero;
  178. A[0] = zero;
  179. A[1] = node;
  180. (root).set(1*tio.player());
  181. num_items++;
  182. //printf("num_items == %ld!\n", num_items);
  183. return;
  184. } else {
  185. // Insert node into next free slot in the ORAM
  186. int new_id = 1 + num_items;
  187. int TTL = num_items++;
  188. A[new_id] = node;
  189. RegXS new_addr;
  190. new_addr.set(new_id * tio.player());
  191. RegBS isDummy;
  192. //Do a recursive insert
  193. auto [wptr, direction] = insert(tio, yield, root, node, A, TTL, isDummy);
  194. //Complete the insertion by reading wptr and updating its pointers
  195. RegXS pointers = A[wptr].NODE_POINTERS;
  196. RegXS left_ptr = extractLeftPtr(pointers);
  197. RegXS right_ptr = extractRightPtr(pointers);
  198. RegXS new_right_ptr, new_left_ptr;
  199. mpc_select(tio, yield, new_right_ptr, direction, right_ptr, new_addr);
  200. if(player0) {
  201. direction^=1;
  202. }
  203. mpc_select(tio, yield, new_left_ptr, direction, left_ptr, new_addr);
  204. setLeftPtr(pointers, new_left_ptr);
  205. setRightPtr(pointers, new_right_ptr);
  206. A[wptr].NODE_POINTERS = pointers;
  207. //printf("num_items == %ld!\n", num_items);
  208. }
  209. }
  210. void BST::insert(MPCTIO &tio, yield_t &yield, Node &node) {
  211. auto A = oram->flat(tio, yield);
  212. auto R = A.reconstruct();
  213. insert(tio, yield, node, A);
  214. /*
  215. // To visualize database and tree after each insert:
  216. if (tio.player() == 0) {
  217. for(size_t i=0;i<R.size();++i) {
  218. printf("\n%04lx ", i);
  219. R[i].dump();
  220. }
  221. printf("\n");
  222. }
  223. pretty_print(R, 1);
  224. */
  225. }
  226. // Now we use the node in various ways. This function is called by
  227. // online.cpp.
  228. void bst(MPCIO &mpcio,
  229. const PRACOptions &opts, char **args)
  230. {
  231. nbits_t depth=3;
  232. if (*args) {
  233. depth = atoi(*args);
  234. ++args;
  235. }
  236. size_t items = (size_t(1)<<depth)-1;
  237. if (*args) {
  238. items = atoi(*args);
  239. ++args;
  240. }
  241. MPCTIO tio(mpcio, 0, opts.num_threads);
  242. run_coroutines(tio, [&tio, depth, items] (yield_t &yield) {
  243. size_t size = size_t(1)<<depth;
  244. BST tree(tio.player(), size);
  245. Node node;
  246. for(size_t i = 1; i<=items; i++) {
  247. newnode(node);
  248. node.key.set(i * tio.player());
  249. tree.insert(tio, yield, node);
  250. }
  251. tree.pretty_print(tio, yield);
  252. /*
  253. if (depth < 10) {
  254. //oram.dump();
  255. auto R = A.reconstruct();
  256. // Reconstruct the root
  257. if (tio.player() == 1) {
  258. tio.queue_peer(&root, sizeof(root));
  259. } else {
  260. RegXS peer_root;
  261. tio.recv_peer(&peer_root, sizeof(peer_root));
  262. root += peer_root;
  263. }
  264. if (tio.player() == 0) {
  265. for(size_t i=0;i<R.size();++i) {
  266. printf("\n%04lx ", i);
  267. R[i].dump();
  268. }
  269. printf("\n");
  270. pretty_print(R, root.xshare);
  271. auto [ ok, height ] = check_bst(R, root.xshare);
  272. printf("BST structure %s\nBST height = %u\n",
  273. ok ? "ok" : "NOT OK", height);
  274. }
  275. }
  276. */
  277. });
  278. }