Browse Source

Add functions for serializing and deserializing private keys

Ian Goldberg 1 month ago
parent
commit
b354adb2fa
5 changed files with 50 additions and 0 deletions
  1. 6 0
      src/PrivateKey.cc
  2. 3 0
      src/PrivateKey.h
  3. 31 0
      src/SerialDeserial.cc
  4. 4 0
      src/SerialDeserial.h
  5. 6 0
      src/lib.rs

+ 6 - 0
src/PrivateKey.cc

@@ -13,4 +13,10 @@ std::shared_ptr<PrivateKeyImpl> PrivateKeyDCRTPoly::GetInternal() const
     return m_privateKey;
 }
 
+// Generator functions
+std::unique_ptr<PrivateKeyDCRTPoly> GenNullPrivateKey()
+{
+    return std::make_unique<PrivateKeyDCRTPoly>();
+}
+
 } // openfhe

+ 3 - 0
src/PrivateKey.h

@@ -21,4 +21,7 @@ public:
     [[nodiscard]] std::shared_ptr<PrivateKeyImpl> GetInternal() const;
 };
 
+// Generator functions
+[[nodiscard]] std::unique_ptr<PrivateKeyDCRTPoly> GenNullPrivateKey();
+
 } // openfhe

+ 31 - 0
src/SerialDeserial.cc

@@ -5,6 +5,7 @@
 #include "Ciphertext.h"
 #include "CryptoContext.h"
 #include "PublicKey.h"
+#include "PrivateKey.h"
 
 namespace openfhe
 {
@@ -260,6 +261,36 @@ bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
     }
     return false;
 }
+bool SerializePrivateKeyToFile(const std::string& privateKeyLocation,
+    const PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::SerializeToFile(privateKeyLocation,
+            privateKey.m_privateKey, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::SerializeToFile(privateKeyLocation,
+            privateKey.m_privateKey, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
+bool DeserializePrivateKeyFromFile(const std::string& privateKeyLocation,
+    PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(privateKeyLocation,
+            privateKey.m_privateKey, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(privateKeyLocation,
+            privateKey.m_privateKey, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
 bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
     const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode)
 {

+ 4 - 0
src/SerialDeserial.h

@@ -36,6 +36,10 @@ bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
     const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
 bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
     PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
+bool SerializePrivateKeyToFile(const std::string& privateKeyLocation,
+    const PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode);
+bool DeserializePrivateKeyFromFile(const std::string& privateKeyLocation,
+    PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode);
 bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
     const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
 bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,

+ 6 - 0
src/lib.rs

@@ -503,6 +503,12 @@ pub mod ffi
         fn GenNullPublicKey() -> UniquePtr<PublicKeyDCRTPoly>;
     }
 
+    // PrivateKeyDCRTPoly
+    unsafe extern "C++"
+    {
+        fn GenNullPrivateKey() -> UniquePtr<PrivateKeyDCRTPoly>;
+    }
+
     // KeyPairDCRTPoly
     unsafe extern "C++"
     {