Browse Source

fixing a few things with serialization of types that I hadn't been using yet

tristangurtler 3 years ago
parent
commit
d83f19388e
4 changed files with 168 additions and 3 deletions
  1. 1 1
      bgn2/inc/PublicKey.hpp
  2. 44 0
      bgn2/inc/print_helpers.hpp
  3. 87 2
      bgn2/src/Scalar.cpp
  4. 36 0
      bgn2/src/print_helpers.cpp

+ 1 - 1
bgn2/inc/PublicKey.hpp

@@ -9,6 +9,7 @@
 class BGNPublicKey
 {
 	public:
+		BGNPublicKey();
 		BGNPublicKey(const BGNPublicKey& other);
 		
 		void encrypt(CurveBipoint& G_element, const Scalar& cleartext) const;
@@ -42,7 +43,6 @@ class BGNPublicKey
         friend std::istream& operator>>(std::istream& is, BGNPublicKey& input);
 		
 	private:
-		BGNPublicKey();
 		void set(const CurveBipoint& g, const TwistBipoint& h, const CurveBipoint& g1, const TwistBipoint& h1);
 		friend class BGN;
 

+ 44 - 0
bgn2/inc/print_helpers.hpp

@@ -15,6 +15,50 @@ extern "C" {
 #include "fpe.h"
 }
 
+class BinarySizeT {
+    public:
+        BinarySizeT()
+        { /* */ }
+
+        BinarySizeT(size_t data)
+            : data(data)
+        { /* */ }
+
+        void set(size_t in)
+        { data = in; }
+
+        size_t val() const
+        { return data; }
+
+        friend std::ostream& operator<<(std::ostream& os, const BinarySizeT& output);
+        friend std::istream& operator>>(std::istream& is, BinarySizeT& input);
+
+    private:
+        size_t data;
+};
+
+class BinaryBool {
+    public:
+        BinaryBool()
+        { /* */ }
+        
+        BinaryBool(bool data)
+            : data(data)
+        { /* */ }
+
+        void set(bool in)
+        { data = in; }
+
+        bool val() const
+        { return data; }
+
+        friend std::ostream& operator<<(std::ostream& os, const BinaryBool& output);
+        friend std::istream& operator>>(std::istream& is, BinaryBool& input);
+
+    private:
+        bool data;
+};
+
 class Fp12e {
     public:
         Fp12e();

+ 87 - 2
bgn2/src/Scalar.cpp

@@ -1,6 +1,8 @@
 #include "Scalar.hpp"
 #include <iostream>
 
+#include "proof.hpp"
+
 extern const scalar_t bn_n;
 extern const scalar_t bn_p;
 mpz_class Scalar::mpz_bn_p = 0;
@@ -295,14 +297,97 @@ Scalar::SecretScalar Scalar::to_scalar_t() const
     return SecretScalar(element);
 }
 
+char byteToHexByte(char in)
+{
+    if (in < 0xA)
+        return in + '0';
+    else
+        return (in - 0xA) + 'a';
+}
+
+char hexByteToByte(char in)
+{
+    if (in >= '0' && in <= '9')
+        return in - '0';
+    else if (in >= 'A' && in <= 'F')
+        return (in - 'A') + 0xA;
+    else
+        return (in - 'a') + 0xA;
+}
+
+std::vector<char> hexToBytes(const std::string& hex)
+{
+    std::vector<char> bytes;
+
+    for (size_t i = 0; i < hex.length(); i += 2)
+    {
+        char partA, partB, currByte;
+
+        partA = hex[i];
+        partB = hex[i + 1];
+
+        partA = hexByteToByte(partA);
+        partB = hexByteToByte(partB);
+
+        currByte = (partA << 4) | partB;
+        bytes.push_back(byte);
+    }
+
+    return bytes;
+}
+
+std::string bytesToHex(const std::vector<char>& bytes)
+{
+    std::string hex;
+
+    for (size_t i = 0; i < bytes.length(); i++)
+    {
+        char partA, partB;
+
+        partA = (0xF0 & bytes[i]) >> 4;
+        partB = (0xF & bytes[i]);
+
+        partA = byteToHexByte(partA);
+        partB = byteToHexByte(partB);
+
+        hex += partA;
+        hex += partB;
+    }
+
+    return hex;
+}
+
 std::ostream& operator<<(std::ostream& os, const Scalar& output)
 {
-    os << output.element;
+    std::string outString = output.element.get_str(16);
+    std::vector<char> bytes = hexToBytes(outString);
+
+    BinarySizeT sizeOfVector(bytes.size());
+    os << sizeOfVector;
+    for (size_t i = 0; i < sizeOfVector.val(); i++)
+        os.write(&(bytes[i]), sizeof(bytes[i]));
+
     return os;
 }
 
 std::istream& operator>>(std::istream& is, Scalar& input)
 {
-    is >> input.element;
+    std::vector<char> bytes;
+
+    BinarySizeT sizeOfVector;
+    is >> sizeOfVector;
+
+    for (size_t i = 0; i < sizeOfVector.val(); i++)
+    {
+        char currByte;
+        is.read(&currByte, sizeof(currByte));
+
+        bytes.push_back(currByte);
+    }
+
+    std::string hex = bytesToHex(bytes);
+
+    input.element.set_str(hex, 16);
+
     return is;
 }

+ 36 - 0
bgn2/src/print_helpers.cpp

@@ -1,5 +1,41 @@
 #include "print_helpers.hpp"
 
+std::ostream& operator<<(std::ostream& os, const BinarySizeT& output)
+{
+    size_t outval = output.val();
+    os.write(reinterpret_cast<const char*>(&outval), sizeof(outval));
+
+    return os;
+}
+
+std::istream& operator>>(std::istream& is, BinarySizeT& input)
+{
+    size_t inval;
+    is.read(reinterpret_cast<const char*>(&inval), sizeof(inval));
+
+    input.data = inval;
+
+    return is;
+}
+
+std::ostream& operator<<(std::ostream& os, const BinaryBool& output)
+{
+    uint8_t outval = (output.val() ? 1 : 0);
+    os.write(reinterpret_cast<const char*>(&outval), sizeof(outval));
+
+    return os;
+}
+
+std::istream& operator>>(std::istream& is, BinaryBool& input)
+{
+    uint8_t inval;
+    is.read(reinterpret_cast<const char*>(&inval), sizeof(inval));
+
+    input.data = (inval > 0 ? true : false);
+
+    return is;
+}
+
 Fp12e::Fp12e(const fp12e_t& input)
 {
     fp12e_set(data, input);