Browse Source

serialization is now slightly better

tristangurtler 3 years ago
parent
commit
ba5f8bda75
2 changed files with 61 additions and 11 deletions
  1. 59 0
      bgn2/src/main.cpp
  2. 2 11
      bgn2/src/print_helpers.cpp

+ 59 - 0
bgn2/src/main.cpp

@@ -1,4 +1,5 @@
 #include <iostream>
+#include <sstream>
 #include <random>
 #include <chrono>
 
@@ -46,6 +47,57 @@ bool testDecrypt(int x)
     return retval;
 }
 
+bool testSerialize(int x)
+{
+    bool retval;
+
+    BGN system;
+
+    Scalar testVal(x);
+    Scalar one(1);
+    Scalar decrypted;
+
+    CurveBipoint curveEnc, curveReceive, curveOne;
+    TwistBipoint twistEnc, twistReceive, twistOne;
+    Quadripoint quadEncA, quadEncB, quadRecA, quadRecB;
+
+    system.encrypt(curveEnc, testVal);
+    system.encrypt(curveOne, one);
+    system.encrypt(twistEnc, testVal);
+    system.encrypt(twistOne, one);
+
+    quadEncA = system.homomorphic_multiplication(curveEnc, twistOne);
+    quadEncB = system.homomorphic_multiplication(curveOne, twistEnc);
+    
+    stringstream serializer;
+
+    serializer << curveEnc;
+    serializer >> curveReceive;
+
+    serializer << twistEnc;
+    serializer >> twistReceive;
+
+    serializer << quadEncA;
+    serializer >> quadRecA;
+
+    serializer << quadEncB;
+    serializer >> quadRecB;
+
+    decrypted = system.decrypt(curveReceive);
+    retval = (decrypted == testVal);
+
+    decrypted = system.decrypt(twistReceive);
+    retval = retval && (decrypted == testVal);
+
+    decrypted = system.decrypt(quadRecA);
+    retval = retval && (decrypted == testVal);
+
+    decrypted = system.decrypt(quadRecB);
+    retval = retval && (decrypted == testVal);
+
+    return retval;
+}
+
 double testCurveEncryptSpeed(default_random_engine& generator)
 {
     BGN system;
@@ -666,6 +718,13 @@ int main(int argc, char *argv[])
     else
         cout << "FAIL" << endl;
 
+    int serializingPoint = distribution(generator);
+    cout << "test_Serialization (" << serializingPoint << "): ";
+    if (testSerialize(serializingPoint))
+        cout << "PASS" << endl;
+    else
+        cout << "FAIL" << endl;
+
     cout << "test_CurveEncryptSpeed (" << NUM_RUNS_PER_TEST << " runs): ";
     cout << testCurveEncryptSpeed(generator) << " seconds" << endl;
 

+ 2 - 11
bgn2/src/print_helpers.cpp

@@ -114,23 +114,14 @@ std::istream& operator>>(std::istream& is, Fpe& input)
 
 std::ostream& hex_double(std::ostream& os, double d)
 {
-    uint64_t u;
-    memcpy(&u, &d, sizeof(d));
-    os << std::hex << std::setw(16) << std::setfill('0');
-    os << u;
-    os << std::setfill(' ') << std::setw(0) << std::dec;
+    os.write(reinterpret_cast<const char*>(&d), sizeof(d));
 
     return os;
 }
 
 std::istream& hex_double(std::istream& is, double& d)
 { 
-    char buffer[17];
-    is.get(buffer, 17);
-
-    uint64_t u = std::stoull(buffer, 0, 16);
-
-    memcpy(&d, &u, sizeof(u));
+    is.read(reinterpret_cast<char*>(&d), sizeof(d));
 
     return is;
 }