瀏覽代碼

All parties can now load RDPFs of different depths and keep track of stats

Ian Goldberg 1 年之前
父節點
當前提交
b120fc84eb
共有 7 個文件被更改,包括 161 次插入11 次删除
  1. 5 5
      Makefile
  2. 90 2
      mpcio.cpp
  3. 22 0
      mpcio.hpp
  4. 37 0
      online.cpp
  5. 0 2
      rdpf.cpp
  6. 2 2
      rdpf.hpp
  7. 5 0
      types.hpp

+ 5 - 5
Makefile

@@ -28,9 +28,9 @@ depend:
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
 prac.o: mpcio.hpp types.hpp preproc.hpp online.hpp
-mpcio.o: mpcio.hpp types.hpp
+mpcio.o: mpcio.hpp types.hpp rdpf.hpp coroutine.hpp bitutils.hpp
 preproc.o: types.hpp coroutine.hpp mpcio.hpp preproc.hpp rdpf.hpp
-online.o: online.hpp mpcio.hpp types.hpp mpcops.hpp coroutine.hpp
-mpcops.o: mpcops.hpp types.hpp mpcio.hpp coroutine.hpp
-rdpf.o: rdpf.hpp mpcio.hpp types.hpp coroutine.hpp bitutils.hpp aes.hpp
-rdpf.o: prg.hpp
+online.o: online.hpp mpcio.hpp types.hpp mpcops.hpp coroutine.hpp rdpf.hpp
+mpcops.o: mpcops.hpp types.hpp mpcio.hpp coroutine.hpp bitutils.hpp
+rdpf.o: rdpf.hpp mpcio.hpp types.hpp coroutine.hpp bitutils.hpp mpcops.hpp
+rdpf.o: aes.hpp prg.hpp

+ 90 - 2
mpcio.cpp

@@ -1,16 +1,28 @@
 #include "mpcio.hpp"
+#include "rdpf.hpp"
 #include "bitutils.hpp"
 
 template<typename T>
 PreCompStorage<T>::PreCompStorage(unsigned player, bool preprocessing,
         const char *filenameprefix, unsigned thread_num) {
+    init(player, preprocessing, filenameprefix, thread_num);
+}
+
+template<typename T>
+void PreCompStorage<T>::init(unsigned player, bool preprocessing,
+        const char *filenameprefix, unsigned thread_num, nbits_t depth) {
     if (preprocessing) return;
     std::string filename(filenameprefix);
     char suffix[20];
-    sprintf(suffix, ".p%d.t%u", player%10, thread_num);
+    if (depth) {
+        sprintf(suffix, "%02d.p%d.t%u", depth, player%10, thread_num);
+    } else {
+        sprintf(suffix, ".p%d.t%u", player%10, thread_num);
+    }
     filename.append(suffix);
     storage.open(filename);
-    if (storage.fail()) {
+    // It's OK if files for not every depth exist
+    if (!depth && storage.fail()) {
         std::cerr << "Failed to open " << filename << "\n";
         exit(1);
     }
@@ -231,6 +243,13 @@ MPCPeerIO::MPCPeerIO(unsigned player, bool preprocessing,
     for (unsigned i=0; i<num_threads; ++i) {
         halftriples.emplace_back(player, preprocessing, "halves", i);
     }
+    rdpftriples.resize(num_threads);
+    for (unsigned i=0; i<num_threads; ++i) {
+        for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+            rdpftriples[i][depth-1].init(player, preprocessing,
+                "rdpf", i, depth);
+        }
+    }
     for (auto &&sock : peersocks) {
         peerios.emplace_back(std::move(sock));
     }
@@ -247,6 +266,12 @@ void MPCPeerIO::dump_precomp_stats(std::ostream &os)
         }
         os << "T" << i << " t:" << triples[i].get_stats() <<
             " h:" << halftriples[i].get_stats();
+        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+            size_t cnt = rdpftriples[i][depth-1].get_stats();
+            if (cnt > 0) {
+                os << " r" << int(depth) << ":" << cnt;
+            }
+        }
     }
     os << "\n";
 }
@@ -256,6 +281,9 @@ void MPCPeerIO::reset_precomp_stats()
     for (size_t i=0; i<triples.size(); ++i) {
         triples[i].reset_stats();
         halftriples[i].reset_stats();
+        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+            rdpftriples[i][depth-1].reset_stats();
+        }
     }
 }
 
@@ -271,6 +299,13 @@ MPCServerIO::MPCServerIO(bool preprocessing,
         std::deque<tcp::socket> &p1socks) :
     MPCIO(2, preprocessing, p0socks.size())
 {
+    rdpfpairs.resize(num_threads);
+    for (unsigned i=0; i<num_threads; ++i) {
+        for (unsigned depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+            rdpfpairs[i][depth-1].init(player, preprocessing,
+                "rdpf", i, depth);
+        }
+    }
     for (auto &&sock : p0socks) {
         p0ios.emplace_back(std::move(sock));
     }
@@ -279,6 +314,39 @@ MPCServerIO::MPCServerIO(bool preprocessing,
     }
 }
 
+void MPCServerIO::dump_precomp_stats(std::ostream &os)
+{
+    for (size_t i=0; i<rdpfpairs.size(); ++i) {
+        if (i > 0) {
+            os << " ";
+        }
+        os << "T" << i;
+        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+            size_t cnt = rdpfpairs[i][depth-1].get_stats();
+            if (cnt > 0) {
+                os << " r" << int(depth) << ":" << cnt;
+            }
+        }
+    }
+    os << "\n";
+}
+
+void MPCServerIO::reset_precomp_stats()
+{
+    for (size_t i=0; i<rdpfpairs.size(); ++i) {
+        for (nbits_t depth=1; depth<=ADDRESS_MAX_BITS; ++depth) {
+            rdpfpairs[i][depth-1].reset_stats();
+        }
+    }
+}
+
+void MPCServerIO::dump_stats(std::ostream &os)
+{
+    MPCIO::dump_stats(os);
+    os << "Precomputed values used: ";
+    dump_precomp_stats(os);
+}
+
 MPCTIO::MPCTIO(MPCIO &mpcio, int thread_num) :
         thread_num(thread_num), thread_lamport(mpcio.lamport),
         mpcio(mpcio)
@@ -522,6 +590,26 @@ SelectTriple MPCTIO::selecttriple()
     return val;
 }
 
+RDPFTriple MPCTIO::rdpftriple(nbits_t depth)
+{
+    RDPFTriple val;
+    if (!mpcio.preprocessing && mpcio.player <= 2) {
+        MPCPeerIO &mpcpio = static_cast<MPCPeerIO&>(mpcio);
+        mpcpio.rdpftriples[thread_num][depth-1].get(val);
+    }
+    return val;
+}
+
+RDPFPair MPCTIO::rdpfpair(nbits_t depth)
+{
+    RDPFPair val;
+    if (!mpcio.preprocessing && mpcio.player == 2) {
+        MPCServerIO &mpcsrvio = static_cast<MPCServerIO&>(mpcio);
+        mpcsrvio.rdpfpairs[thread_num][depth-1].get(val);
+    }
+    return val;
+}
+
 // The port number for the P1 -> P0 connection
 static const unsigned short port_p1_p0 = 2115;
 

+ 22 - 0
mpcio.hpp

@@ -4,6 +4,7 @@
 #include <iostream>
 #include <fstream>
 #include <vector>
+#include <array>
 #include <deque>
 #include <queue>
 #include <string>
@@ -24,8 +25,11 @@ using boost::asio::ip::tcp;
 template<typename T>
 class PreCompStorage {
 public:
+    PreCompStorage() : count(0) {}
     PreCompStorage(unsigned player, bool preprocessing,
         const char *filenameprefix, unsigned thread_num);
+    void init(unsigned player, bool preprocessing,
+        const char *filenameprefix, unsigned thread_num, nbits_t depth = 0);
     void get(T& nextval);
 
     inline size_t get_stats() { return count; }
@@ -192,6 +196,9 @@ struct MPCPeerIO : public MPCIO {
     std::deque<MPCSingleIO> serverios;
     std::vector<PreCompStorage<MultTriple>> triples;
     std::vector<PreCompStorage<HalfTriple>> halftriples;
+    // The outer vector is (like above) one item per thread
+    // The inner array is indexed by DPF depth (depth d is at entry d-1)
+    std::vector<std::array<PreCompStorage<RDPFTriple>,ADDRESS_MAX_BITS>> rdpftriples;
 
     MPCPeerIO(unsigned player, bool preprocessing,
             std::deque<tcp::socket> &peersocks,
@@ -210,10 +217,19 @@ struct MPCPeerIO : public MPCIO {
 struct MPCServerIO : public MPCIO {
     std::deque<MPCSingleIO> p0ios;
     std::deque<MPCSingleIO> p1ios;
+    // The outer vector is (like above) one item per thread
+    // The inner array is indexed by DPF depth (depth d is at entry d-1)
+    std::vector<std::array<PreCompStorage<RDPFPair>,ADDRESS_MAX_BITS>> rdpfpairs;
 
     MPCServerIO(bool preprocessing,
             std::deque<tcp::socket> &p0socks,
             std::deque<tcp::socket> &p1socks);
+
+    void dump_precomp_stats(std::ostream &os);
+
+    void reset_precomp_stats();
+
+    void dump_stats(std::ostream &os);
 };
 
 class MPCSingleIOStream {
@@ -310,6 +326,12 @@ public:
     HalfTriple halftriple();
     SelectTriple selecttriple();
 
+    // These ones only work during the online phase
+    // Computational peers call:
+    RDPFTriple rdpftriple(nbits_t depth);
+    // The server calls:
+    RDPFPair rdpfpair(nbits_t depth);
+
     // Accessors
 
     inline int player() { return mpcio.player; }

+ 37 - 0
online.cpp

@@ -2,6 +2,7 @@
 
 #include "online.hpp"
 #include "mpcops.hpp"
+#include "rdpf.hpp"
 
 
 static void online_test(MPCIO &mpcio, int num_threads, char **args)
@@ -149,6 +150,39 @@ static void lamport_test(MPCIO &mpcio, int num_threads, char **args)
     pool.join();
 }
 
+static void rdpf_test(MPCIO &mpcio, int num_threads, char **args)
+{
+    nbits_t depth=6;
+
+    if (*args) {
+        depth = atoi(*args);
+        ++args;
+    }
+
+    boost::asio::thread_pool pool(num_threads);
+    for (int thread_num = 0; thread_num < num_threads; ++thread_num) {
+        boost::asio::post(pool, [&mpcio, thread_num, depth] {
+            MPCTIO tio(mpcio, thread_num);
+            if (mpcio.player == 2) {
+                RDPFPair dp = tio.rdpfpair(depth);
+                printf("usi0 = %016lx\n", dp.dpf[0].unit_sum_inverse);
+                printf("ss0  = %016lx\n", dp.dpf[0].scaled_sum.ashare);
+                printf("usi1 = %016lx\n", dp.dpf[1].unit_sum_inverse);
+                printf("ss1  = %016lx\n", dp.dpf[1].scaled_sum.ashare);
+            } else {
+                RDPFTriple dt = tio.rdpftriple(depth);
+                printf("usi0 = %016lx\n", dt.dpf[0].unit_sum_inverse);
+                printf("ss0  = %016lx\n", dt.dpf[0].scaled_sum.ashare);
+                printf("usi1 = %016lx\n", dt.dpf[1].unit_sum_inverse);
+                printf("ss1  = %016lx\n", dt.dpf[1].scaled_sum.ashare);
+                printf("usi2 = %016lx\n", dt.dpf[2].unit_sum_inverse);
+                printf("ss2  = %016lx\n", dt.dpf[2].scaled_sum.ashare);
+            }
+        });
+    }
+    pool.join();
+}
+
 void online_main(MPCIO &mpcio, int num_threads, char **args)
 {
     if (!*args) {
@@ -160,6 +194,9 @@ void online_main(MPCIO &mpcio, int num_threads, char **args)
     } else if (!strcmp(*args, "lamporttest")) {
         ++args;
         lamport_test(mpcio, num_threads, args);
+    } else if (!strcmp(*args, "rdpftest")) {
+        ++args;
+        rdpf_test(mpcio, num_threads, args);
     } else {
         std::cerr << "Unknown mode " << *args << "\n";
     }

+ 0 - 2
rdpf.cpp

@@ -284,9 +284,7 @@ RDPFTriple::RDPFTriple(MPCTIO &tio, yield_t &yield,
     for (int i=0;i<3;++i) {
         coroutines.emplace_back(
             [&, i](yield_t &yield) {
-                printf("Starting DPF %d\n", i);
                 dpf[i] = RDPF(tio, yield, xs_target, depth);
-                printf("Ending DPF %d\n", i);
             });
     }
     coroutines.emplace_back(

+ 2 - 2
rdpf.hpp

@@ -62,7 +62,7 @@ T& operator>>(T &is, RDPF &rdpf)
     is.read((char *)&rdpf.seed, sizeof(rdpf.seed));
     uint8_t depth;
     is.read((char *)&depth, sizeof(depth));
-    assert(depth <= VALUE_BITS);
+    assert(depth <= ADDRESS_MAX_BITS);
     rdpf.cw.clear();
     for (uint8_t i=0; i<depth; ++i) {
         DPFnode cw;
@@ -84,7 +84,7 @@ T& operator<<(T &os, const RDPF &rdpf)
 {
     os.write((const char *)&rdpf.seed, sizeof(rdpf.seed));
     uint8_t depth = rdpf.cw.size();
-    assert(depth <= VALUE_BITS);
+    assert(depth <= ADDRESS_MAX_BITS);
     os.write((const char *)&depth, sizeof(depth));
     for (uint8_t i=0; i<depth; ++i) {
         os.write((const char *)&rdpf.cw[i], sizeof(rdpf.cw[i]));

+ 5 - 0
types.hpp

@@ -235,4 +235,9 @@ struct SelectTriple {
     DPFnode Y, Z;
 };
 
+// These are defined in rdpf.hpp, but declared here to avoid cyclic
+// header dependencies.
+struct RDPFPair;
+struct RDPFTriple;
+
 #endif