Przeglądaj źródła

Harmonize I/O for MultTriple, HalfTriple, and the DPFs

Ian Goldberg 1 rok temu
rodzic
commit
0e1cc51527
4 zmienionych plików z 40 dodań i 7 usunięć
  1. 1 1
      Makefile
  2. 2 2
      mpcio.cpp
  3. 2 2
      preproc.cpp
  4. 35 2
      types.hpp

+ 1 - 1
Makefile

@@ -17,7 +17,7 @@ $(BIN): $(OBJS)
 
 # Remove the files created by the preprocessing phase
 reset:
-	-rm -f *.p[01].t*
+	-rm -f *.p[012].t*
 
 clean: reset
 	-rm -f $(BIN) $(OBJS) $(ASMS)

+ 2 - 2
mpcio.cpp

@@ -19,8 +19,8 @@ PreCompStorage<T>::PreCompStorage(unsigned player, bool preprocessing,
 
 template<typename T>
 void PreCompStorage<T>::get(T& nextval) {
-    storage.read((char *)&nextval, sizeof(T));
-    if (storage.gcount() != sizeof(T)) {
+    storage >> nextval;
+    if (!storage.good()) {
         std::cerr << "Failed to read precomputed value from storage\n";
         exit(1);
     }

+ 2 - 2
preproc.cpp

@@ -96,7 +96,7 @@ void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args)
                     MultTriple T;
                     for (unsigned int i=0; i<num; ++i) {
                         T = tio.triple();
-                        tripfile.os().write((const char *)&T, sizeof(T));
+                        tripfile.os() << T;
                     }
                 } else if (type == 0x81) {
                     // Multiplication half triples
@@ -106,7 +106,7 @@ void preprocessing_comp(MPCIO &mpcio, int num_threads, char **args)
                     HalfTriple H;
                     for (unsigned int i=0; i<num; ++i) {
                         H = tio.halftriple();
-                        halffile.os().write((const char *)&H, sizeof(H));
+                        halffile.os() << H;
                     }
                 } else if (type >= 0x01 && type <= 0x30) {
                     // RAM DPFs

+ 35 - 2
types.hpp

@@ -104,8 +104,9 @@ struct RegBS {
 
     // Set each side's share to a random bit
     inline void randomize() {
-        arc4random_buf(&bshare, sizeof(bshare));
-        bshare &= 1;
+        unsigned char randb;
+        arc4random_buf(&randb, sizeof(randb));
+        bshare = randb & 1;
     }
 };
 
@@ -179,12 +180,44 @@ using address_t = uint64_t;
 
 using MultTriple = std::tuple<value_t, value_t, value_t>;
 
+// I/O for a MultTriple
+
+template <typename T>
+T& operator>>(T& is, MultTriple &m)
+{
+    is.read((char *)&m, sizeof(m));
+    return is;
+}
+
+template <typename T>
+T& operator<<(T& os, const MultTriple &m)
+{
+    os.write((const char *)&m, sizeof(m));
+    return os;
+}
+
 // A half-triple is (X0,Z0) held by P0 (and correspondingly (Y1,Z1) held
 // by P1), with all values random, but subject to the relation that
 // X0*Y1 = Z0+Z1
 
 using HalfTriple = std::tuple<value_t, value_t>;
 
+// I/O for a HalfTriple
+
+template <typename T>
+T& operator>>(T& is, HalfTriple &h)
+{
+    is.read((char *)&h, sizeof(h));
+    return is;
+}
+
+template <typename T>
+T& operator<<(T& os, const HalfTriple &h)
+{
+    os.write((const char *)&h, sizeof(h));
+    return os;
+}
+
 // The type of nodes in a DPF.  This must be at least as many bits as
 // the security parameter, and at least twice as many bits as value_t.