소스 검색

Add facility for reconstructing the database to check your answers or debugging

Ian Goldberg 2 년 전
부모
커밋
643aed9597
3개의 변경된 파일39개의 추가작업 그리고 4개의 파일을 삭제
  1. 4 0
      duoram.hpp
  2. 25 2
      duoram.tcc
  3. 10 2
      online.cpp

+ 4 - 0
duoram.hpp

@@ -134,6 +134,10 @@ public:
     MemRefAS operator[](const RegAS &idx) { return MemRefAS(*this, idx); }
     MemRefXS operator[](const RegXS &idx) { return MemRefXS(*this, idx); }
     MemRefExpl operator[](address_t idx) { return MemRefExpl(*this, idx); }
+
+    // For debugging or checking your answers (using this in general is
+    // of course insecure)
+    std::vector<T> reconstruct() const;
 };
 
 // The most basic shape is Flat.  It is almost always the topmost shape,

+ 25 - 2
duoram.tcc

@@ -14,8 +14,31 @@ Duoram<T>::Duoram(int player, size_t size) : player(player),
     }
 }
 
-// Constructor.  len=0 means the maximum size (the parent's size
-// minus start).
+// For debugging or checking your answers (using this in general is
+// of course insecure)
+template <typename T>
+std::vector<T> Duoram<T>::Shape::reconstruct() const
+{
+    int player = tio.player();
+    std::vector<T> res;
+    res.resize(duoram.size());
+    // Player 1 sends their share of the database to player 0
+    if (player == 1) {
+        tio.queue_peer(duoram.database.data(), duoram.size()*sizeof(T));
+    } else if (player == 0) {
+        tio.recv_peer(res.data(), duoram.size()*sizeof(T));
+        for(size_t i=0;i<duoram.size();++i) {
+            res[i] += duoram.database[i];
+        }
+    }
+    // The server (player 2) does nothing
+
+    // Players 1 and 2 will get an empty vector here
+    return res;
+}
+
+// Constructor for the Flat shape.  len=0 means the maximum size (the
+// parent's size minus start).
 template <typename T>
 Duoram<T>::Flat::Flat(Duoram &duoram, MPCTIO &tio, yield_t &yield,
     size_t start, size_t len) : Shape(*this, duoram, tio, yield)

+ 10 - 2
online.cpp

@@ -428,9 +428,10 @@ static void duoram_test(MPCIO &mpcio, yield_t &yield,
     boost::asio::thread_pool pool(num_threads);
     for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
         boost::asio::post(pool, [&mpcio, &yield, thread_num, depth] {
+            size_t size = size_t(1)<<depth;
             MPCTIO tio(mpcio, thread_num);
             // size_t &op_counter = tio.aes_ops();
-            Duoram<RegAS> oram(mpcio.player, size_t(1)<<depth);
+            Duoram<RegAS> oram(mpcio.player, size);
             printf("%ld\n", oram.size());
             auto A = oram.flat(tio, yield);
             RegAS aidx;
@@ -438,11 +439,18 @@ static void duoram_test(MPCIO &mpcio, yield_t &yield,
             RegXS xidx;
             xidx.randomize(depth);
             size_t eidx = arc4random();
-            eidx &= (size_t(1)<<depth)-1;
+            eidx &= (size-1);
             RegAS Aa = A[aidx];
             auto Ax = A[xidx];
             auto Ae = A[eidx];
             printf("%ld %ld\n", A.size(), Aa.ashare);
+
+            auto check = A.reconstruct();
+            if (tio.player() == 0) {
+                for (address_t i=0;i<size;++i) {
+                    printf("%04x %016lx\n", i, check[i].ashare);
+                }
+            }
             tio.send();
         });
     }