Browse Source

Move binary search from Flat to Shape

So other Shapes can use it as well
Ian Goldberg 1 year ago
parent
commit
d1b91ebae8
3 changed files with 30 additions and 27 deletions
  1. 8 8
      duoram.cpp
  2. 14 14
      duoram.hpp
  3. 8 5
      online.cpp

+ 8 - 8
duoram.cpp

@@ -3,21 +3,21 @@
 
 // Assuming the memory is already sorted, do an oblivious binary
 // search for the smallest index containing the value at least the
-// given one.  (The answer will be the length of the Flat if all
+// given one.  (The answer will be the length of the Shape if all
 // elements are smaller than the target.) Only available for additive
 // shared databases for now.
 
 // The basic version uses log(N) ORAM reads of size N, where N is the
-// smallest power of 2 strictly larger than the Flat size
+// smallest power of 2 strictly larger than the Shape size
 template <>
-RegAS Duoram<RegAS>::Flat::basic_binary_search(RegAS &target)
+RegAS Duoram<RegAS>::Shape::basic_binary_search(RegAS &target)
 {
     if (this->shape_size == 0) {
         RegAS zero;
         return zero;
     }
     // Create a Pad of the smallest power of 2 size strictly greater
-    // than the Flat size
+    // than the Shape size
     address_t padsize = 1;
     nbits_t depth = 0;
     while (padsize <= this->shape_size) {
@@ -74,16 +74,16 @@ RegAS Duoram<RegAS>::Flat::basic_binary_search(RegAS &target)
 
 // This version does 1 ORAM read of size 2, 1 of size 4, 1 of size
 // 8, ..., 1 of size N/2, where N is the smallest power of 2 strictly
-// larger than the Flat size
+// larger than the Shape size
 template <>
-RegXS Duoram<RegAS>::Flat::binary_search(RegAS &target)
+RegXS Duoram<RegAS>::Shape::binary_search(RegAS &target)
 {
     if (this->shape_size == 0) {
         RegXS zero;
         return zero;
     }
     // Create a Pad of the smallest power of 2 size strictly greater
-    // than the Flat size
+    // than the Shape size
     address_t padsize = 1;
     nbits_t depth = 0;
     while (padsize <= this->shape_size) {
@@ -99,7 +99,7 @@ RegXS Duoram<RegAS>::Flat::binary_search(RegAS &target)
     auto [lt, eq, gt] = cdpf.compare(this->tio, this->yield,
         val-target, tio.aes_ops());
     if (depth == 1) {
-        // There was only one item in the Flat, and mid will equal 0, so
+        // There was only one item in the Shape, and mid will equal 0, so
         // val is (a share of) that item, P[0].  If val >= target, the
         // answer is here or to the left, so it must be 0.  If val <
         // target, the answer is strictly to the right, so it must be 1.

+ 14 - 14
duoram.hpp

@@ -322,6 +322,20 @@ public:
         }
     }
 
+    // Assuming the Shape is already sorted, do an oblivious binary
+    // search for the smallest index containing the value at least the
+    // given one.  (The answer will be the length of the Shape if all
+    // elements are smaller than the target.) Only available for additive
+    // shared databases for now.
+
+    // The basic version uses log(N) ORAM reads of size N, where N is
+    // the smallest power of 2 strictly larger than the Shape size
+    RegAS basic_binary_search(RegAS &target);
+    // This version does 1 ORAM read of size 2, 1 of size 4, 1 of size
+    // 8, ..., 1 of size N/2, where N is the smallest power of 2
+    // strictly larger than the Shape size
+    RegXS binary_search(RegAS &target);
+
     // Enable or disable explicit-only mode.  Only using [] with
     // explicit (address_t) indices are allowed in this mode.  Using []
     // with RegAS or RegXS indices will automatically turn off this
@@ -484,20 +498,6 @@ public:
     // the elements must be at most 63 bits long each for the notion of
     // ">" to make consistent sense.
     void bitonic_sort(address_t start, address_t len, bool dir=0);
-
-    // Assuming the memory is already sorted, do an oblivious binary
-    // search for the smallest index containing the value at least the
-    // given one.  (The answer will be the length of the Flat if all
-    // elements are smaller than the target.) Only available for additive
-    // shared databases for now.
-
-    // The basic version uses log(N) ORAM reads of size N, where N is
-    // the smallest power of 2 strictly larger than the Flat size
-    RegAS basic_binary_search(RegAS &target);
-    // This version does 1 ORAM read of size 2, 1 of size 4, 1 of size
-    // 8, ..., 1 of size N/2, where N is the smallest power of 2
-    // strictly larger than the Flat size
-    RegXS binary_search(RegAS &target);
 };
 
 // Oblivious indices for use in related-index ORAM accesses.

+ 8 - 5
online.cpp

@@ -1494,6 +1494,11 @@ static void path(MPCIO &mpcio,
         val.set(tio.player() * 0xaaaa00000000);
         P[idx] += val;
 
+        // Binary search along that path
+        T lookup;
+        lookup.set(tio.player() * 0x3000000000000);
+        RegXS foundidx = P.binary_search(lookup);
+
         // Check the answer
         auto check = A.reconstruct();
         if (depth <= 10) {
@@ -1504,6 +1509,8 @@ static void path(MPCIO &mpcio,
                 }
             }
         }
+        value_t found = mpc_reconstruct(tio, yield, foundidx);
+        printf("foundidx = %lu\n", found);
     });
 }
 
@@ -1605,11 +1612,7 @@ void online_main(MPCIO &mpcio, const PRACOptions &opts, char **args)
         }
     } else if (!strcmp(*args, "path")) {
         ++args;
-        if (opts.use_xor_db) {
-            path<RegXS>(mpcio, opts, args);
-        } else {
-            path<RegAS>(mpcio, opts, args);
-        }
+        path<RegAS>(mpcio, opts, args);
     } else if (!strcmp(*args, "cell")) {
         ++args;
         cell(mpcio, opts, args);