Browse Source

BST Lookup updated to return a RegBS without explicit reconstruction of lookup success/fail. Fixed incorrect usage of mpc_reconstruct for RegBS in both BST and AVL.

sshsshy 10 months ago
parent
commit
122288d672
3 changed files with 61 additions and 46 deletions
  1. 18 18
      avl.cpp
  2. 41 26
      bst.cpp
  3. 2 2
      bst.hpp

+ 18 - 18
avl.cpp

@@ -490,8 +490,8 @@ std::tuple<RegBS, RegBS, RegXS, RegBS> AVL::insert(MPCTIO &tio, yield_t &yield,
     auto [bal_upd, F_gp, prev_node, prev_dir] = insert(tio, yield,
           next_ptr, ins_addr, insert_key, A, TTL-1, isDummy, ret);
     /*
-    rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd, 1);
-    rec_F_gp = mpc_reconstruct(tio, yield, F_gp, 1);
+    rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd);
+    rec_F_gp = mpc_reconstruct(tio, yield, F_gp);
     printf("Insert returns: rec_bal_upd = %d, rec_F_gp = %d\n",
           rec_bal_upd, rec_F_gp);
     size_t rec_ptr = mpc_reconstruct(tio, yield, pt);
@@ -558,8 +558,8 @@ std::tuple<RegBS, RegBS, RegXS, RegBS> AVL::insert(MPCTIO &tio, yield_t &yield,
 
     /*
     bool rec_F_ir, rec_F_il;
-    rec_F_ir = mpc_reconstruct(tio, yield, F_ir, 1);
-    rec_F_il = mpc_reconstruct(tio, yield, F_il, 1);
+    rec_F_ir = mpc_reconstruct(tio, yield, F_ir);
+    rec_F_il = mpc_reconstruct(tio, yield, F_il);
     rec_left = mpc_reconstruct(tio, yield, left, AVL_PTR_SIZE);
     rec_right = mpc_reconstruct(tio, yield, right, AVL_PTR_SIZE);
     printf("(After recursing) F_il = %d, left = %ld, F_ir = %d, right = %ld\n",
@@ -626,10 +626,10 @@ void AVL::insert(MPCTIO &tio, yield_t &yield, const Node &node) {
         /*
         // Debug code
         bool rec_bal_upd, rec_F_gp, ret_dir_pc, ret_dir_cn;
-        rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd, 1);
-        rec_F_gp = mpc_reconstruct(tio, yield, F_gp, 1);
-        ret_dir_pc = mpc_reconstruct(tio, yield, ret.dir_pc, 1);
-        ret_dir_cn = mpc_reconstruct(tio, yield, ret.dir_cn, 1);
+        rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd);
+        rec_F_gp = mpc_reconstruct(tio, yield, F_gp);
+        ret_dir_pc = mpc_reconstruct(tio, yield, ret.dir_pc);
+        ret_dir_cn = mpc_reconstruct(tio, yield, ret.dir_cn);
         printf("(Top level) Insert returns: rec_bal_upd = %d, rec_F_gp = %d\n",
               rec_bal_upd, rec_F_gp);
         printf("(Top level) Insert returns: ret.dir_pc = %d, rt.dir_cn = %d\n",
@@ -666,9 +666,9 @@ void AVL::insert(MPCTIO &tio, yield_t &yield, const Node &node) {
             child_pointers = A[oidx_c].NODE_POINTERS;
             */
             /*
-            size_t rec_gp_key = mpc_reconstruct(tio, yield, A[oidx_gp].NODE_KEY, 1);
-            size_t rec_p_key = mpc_reconstruct(tio, yield, A[oidx_p].NODE_KEY, 1);
-            size_t rec_c_key = mpc_reconstruct(tio, yield, A[oidx_c].NODE_KEY, 1);
+            size_t rec_gp_key = mpc_reconstruct(tio, yield, A[oidx_gp].NODE_KEY);
+            size_t rec_p_key = mpc_reconstruct(tio, yield, A[oidx_p].NODE_KEY);
+            size_t rec_c_key = mpc_reconstruct(tio, yield, A[oidx_c].NODE_KEY);
             size_t rec_gp_lptr = mpc_reconstruct(tio, iyield, getAVLLeftPtr(A[oidx_gp].NODE_POINTERS), AVL_PTR_SIZE);
             size_t rec_gp_rptr = mpc_reconstruct(tio, yield, getAVLRightPtr(A[oidx_gp].NODE_POINTERS), AVL_PTR_SIZE);
             size_t rec_p_lptr = mpc_reconstruct(tio, yield, getAVLLeftPtr(A[oidx_p].NODE_POINTERS), AVL_PTR_SIZE);
@@ -1463,7 +1463,7 @@ std::tuple<bool, RegBS> AVL::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS d
 
         #ifdef DEBUG
           size_t rec_key = mpc_reconstruct(tio, yield, node.key, 64);
-          bool rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd, 1);
+          bool rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd);
           printf("current_key = %ld, bal_upd (before updateBalanceDel) = %d\n", rec_key, rec_bal_upd);
         #endif
 
@@ -1472,8 +1472,8 @@ std::tuple<bool, RegBS> AVL::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS d
         bal_upd = new_bal_upd;
 
         #ifdef DEBUG
-          bool rec_imb = mpc_reconstruct(tio, yield, imb, 1);
-          bool rec_new_bal_upd = mpc_reconstruct(tio, yield, new_bal_upd, 1);
+          bool rec_imb = mpc_reconstruct(tio, yield, imb);
+          bool rec_new_bal_upd = mpc_reconstruct(tio, yield, new_bal_upd);
           printf("new_bal_upd (after updateBalanceDel) = %d, imb = %d\n", rec_new_bal_upd, rec_imb);
         #endif
         
@@ -1483,14 +1483,14 @@ std::tuple<bool, RegBS> AVL::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS d
               c_prime, cs_ptr, imb, F_ri, ret_struct);
 
         #ifdef DEBUG
-          rec_imb = mpc_reconstruct(tio, yield, imb, 1);
-          rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd, 1); 
+          rec_imb = mpc_reconstruct(tio, yield, imb);
+          rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd); 
           printf("imb (after fixImbalance) = %d, bal_upd = %d\n", rec_imb, rec_bal_upd);
         #endif
         updateRetStruct(tio, yield, ptr, F_rs, F_dh, F_ri, bal_upd, ret_struct); 
 
         #ifdef DEBUG
-          rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd, 1);
+          rec_bal_upd = mpc_reconstruct(tio, yield, bal_upd);
           printf("bal_upd (after updateRetStruct) = %d\n", rec_bal_upd);
         #endif
 
@@ -1570,7 +1570,7 @@ bool AVL::del(MPCTIO &tio, yield_t &yield, RegAS del_key) {
             mpc_select(tio, yield, root, ret_struct.F_r, root, ret_struct.ret_ptr);
 
             /*
-            bool rec_F_ss = mpc_reconstruct(tio, yield, ret_struct.F_ss, 1);
+            bool rec_F_ss = mpc_reconstruct(tio, yield, ret_struct.F_ss);
             size_t rec_del_key = mpc_reconstruct(tio, yield, del_node.key, 64);
             size_t rec_suc_key = mpc_reconstruct(tio, yield, suc_node.key, 64);
             printf("rec_F_ss = %d, del_node.key = %lu, suc_nod.key = %lu\n",

+ 41 - 26
bst.cpp

@@ -330,16 +330,15 @@ void BST::insert(MPCTIO &tio, yield_t &yield, Node &node) {
     */
 }
 
-bool BST::lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key, Duoram<Node>::Flat &A,
+RegBS BST::lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key, Duoram<Node>::Flat &A,
     int TTL, RegBS isDummy, Node *ret_node) {
     if(TTL==0) {
-        // Reconstruct and return isDummy
         // If we found the key, then isDummy will be true
-        bool found = mpc_reconstruct(tio, yield, isDummy, 1);
-        return found;
+        return isDummy;
     }
 
-    RegBS isNotDummy = isDummy ^ (tio.player());
+
+    RegBS isNotDummy = isDummy ^ (!tio.player());
     Node cnode = A[ptr];
     // Compare key
     CDPF cdpf = tio.cdpf(yield);
@@ -360,19 +359,31 @@ bool BST::lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key, Duoram<Node>
     mpc_and(tio, yield, F_found, isNotDummy, eq);
     mpc_select(tio, yield, ret_node->key, eq, ret_node->key, cnode.key); 
     mpc_select(tio, yield, ret_node->value, eq, ret_node->value, cnode.value); 
-  
+
+    #ifdef BST_DEBUG 
+        size_t ckey = mpc_reconstruct(tio, yield, cnode.key, 64);
+        size_t lkey = mpc_reconstruct(tio, yield, key, 64);
+        bool rec_lt = mpc_reconstruct(tio, yield, lt);
+        bool rec_eq = mpc_reconstruct(tio, yield, eq);
+        bool rec_gt = mpc_reconstruct(tio, yield, gt);
+        bool rec_found = mpc_reconstruct(tio, yield, isDummy);
+        bool rec_f_found = mpc_reconstruct(tio, yield, F_found);
+        printf("rec_lt = %d, rec_eq = %d, rec_gt = %d\n", rec_lt, rec_eq, rec_gt);
+        printf("rec_isDummy/found = %d ,rec_f_found = %d, cnode.key = %ld, lookup key = %ld\n", rec_found, rec_f_found, ckey, lkey);
+    #endif
+
     isDummy^=F_found;
-    bool found = lookup(tio, yield, next_ptr, key, A, TTL-1, isDummy, ret_node);
+    RegBS found = lookup(tio, yield, next_ptr, key, A, TTL-1, isDummy, ret_node);
 
     return found;    
 }
 
-bool BST::lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node) {
+RegBS BST::lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node) {
     auto A = oram.flat(tio, yield);
 
     RegBS isDummy;
 
-    bool found = lookup(tio, yield, root, key, A, num_items, isDummy, ret_node);
+    RegBS found = lookup(tio, yield, root, key, A, num_items, isDummy, ret_node);
     /*
     // To visualize database and tree after each lookup:
     auto R = A.reconstruct();
@@ -410,9 +421,9 @@ bool BST::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
         /*
         // Reconstruct and Debug Block 0
         bool lt_rec, eq_rec, gt_rec;
-        lt_rec = mpc_reconstruct(tio, yield, lt, 1);
-        eq_rec = mpc_reconstruct(tio, yield, eq, 1);
-        gt_rec = mpc_reconstruct(tio, yield, gt, 1);
+        lt_rec = mpc_reconstruct(tio, yield, lt);
+        eq_rec = mpc_reconstruct(tio, yield, eq);
+        gt_rec = mpc_reconstruct(tio, yield, gt);
         size_t del_key_rec, node_key_rec;
         del_key_rec = mpc_reconstruct(tio, yield, del_key, 64);
         node_key_rec = mpc_reconstruct(tio, yield, node.key, 64);
@@ -461,10 +472,10 @@ bool BST::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
         /*
         // Reconstruct and Debug Block 1
         bool F_0_rec, F_1_rec, F_2_rec, c_prime_rec;
-        F_0_rec = mpc_reconstruct(tio, yield, F_0, 1);
-        F_1_rec = mpc_reconstruct(tio, yield, F_1, 1);
-        F_2_rec = mpc_reconstruct(tio, yield, F_2, 1);
-        c_prime_rec = mpc_reconstruct(tio, yield, c_prime, 1);
+        F_0_rec = mpc_reconstruct(tio, yield, F_0);
+        F_1_rec = mpc_reconstruct(tio, yield, F_1);
+        F_2_rec = mpc_reconstruct(tio, yield, F_2);
+        c_prime_rec = mpc_reconstruct(tio, yield, c_prime);
         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);
         */
 
@@ -480,9 +491,9 @@ bool BST::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
         /*
         // Reconstruct and Debug Block 2
         bool F_c2_rec, s1_rec;
-        F_c2_rec = mpc_reconstruct(tio, yield, F_c2, 1);
-        s1_rec = mpc_reconstruct(tio, yield, s1, 1); 
-        c_prime_rec = mpc_reconstruct(tio, yield, c_prime, 1); 
+        F_c2_rec = mpc_reconstruct(tio, yield, F_c2);
+        s1_rec = mpc_reconstruct(tio, yield, s1); 
+        c_prime_rec = mpc_reconstruct(tio, yield, c_prime); 
         printf("c_prime = %d, F_c2 = %d, s1 = %d\n", c_prime_rec, F_c2_rec, s1_rec);
         */
 
@@ -531,8 +542,8 @@ bool BST::del(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS del_key,
         // Reconstruct and Debug Block 3
         bool F_rs_rec, F_ls_rec;
         size_t ret_ptr_rec;
-        F_rs_rec = mpc_reconstruct(tio, yield, F_rs, 1);
-        F_ls_rec = mpc_reconstruct(tio, yield, F_rs, 1);
+        F_rs_rec = mpc_reconstruct(tio, yield, F_rs);
+        F_ls_rec = mpc_reconstruct(tio, yield, F_rs);
         ret_ptr_rec = mpc_reconstruct(tio, yield, ret_struct.ret_ptr, 64);
         printf("F_rs_rec = %d, F_ls_rec = %d, ret_ptr_rec = %ld\n", F_rs_rec, F_ls_rec, ret_ptr_rec);
         */
@@ -713,11 +724,13 @@ void bst(MPCIO &mpcio,
         printf("\n\nLookup %x\n", 8);
         randomize_node(node);
         RegAS lookup_key;
-        bool found;
+        RegBS found;
+        bool rec_found;
         lookup_key.set(8 * tio.player());
         found = tree.lookup(tio, yield, lookup_key, &node);
+        rec_found = mpc_reconstruct(tio, yield, found);
         tree.pretty_print(tio, yield);
-        if(found) {
+        if(rec_found) {
           printf("Lookup Success\n");
           size_t value = mpc_reconstruct(tio, yield, node.value, 64);
           printf("value = %lx\n", value);
@@ -725,12 +738,14 @@ void bst(MPCIO &mpcio,
           printf("Lookup Failed\n");
         }
 
-        printf("\n\nLookup %x\n", 99);
+        printf("\n\nLookup %x\n", 63);
         randomize_node(node);
-        lookup_key.set(99 * tio.player());
+        lookup_key.set(63 * tio.player());
         found = tree.lookup(tio, yield, lookup_key, &node);
+        rec_found = mpc_reconstruct(tio, yield, found);
+        //rec_found = reconstruct_RegBS(tio, yield, found);
         tree.pretty_print(tio, yield);
-        if(found) {
+        if(rec_found) {
           printf("Lookup Success\n");
           size_t value = mpc_reconstruct(tio, yield, node.value, 64);
           printf("value = %lx\n", value);    

+ 2 - 2
bst.hpp

@@ -178,7 +178,7 @@ class BST {
         Duoram<Node>::Flat &A, RegBS F_af, RegBS F_fs, int TTL, 
         del_return &ret_struct);
 
-    bool lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key, 
+    RegBS lookup(MPCTIO &tio, yield_t &yield, RegXS ptr, RegAS key, 
         Duoram<Node>::Flat &A, int TTL, RegBS isDummy, Node *ret_node);
 
     void pretty_print(const std::vector<Node> &R, value_t node,
@@ -200,7 +200,7 @@ class BST {
     bool del(MPCTIO &tio, yield_t &yield, RegAS del_key); 
 
     // Returns the first node that matches key in the BST
-    bool lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node);
+    RegBS lookup(MPCTIO &tio, yield_t &yield, RegAS key, Node *ret_node);
 
     // Display and correctness check functions