Browse Source

Add functions for serializing and deserializing private keys

Ian Goldberg 5 months ago
parent
commit
5fff55a4b3
5 changed files with 64 additions and 0 deletions
  1. 6 0
      src/PrivateKey.cc
  2. 11 0
      src/PrivateKey.h
  3. 31 0
      src/SerialDeserial.cc
  4. 5 0
      src/SerialDeserial.h
  5. 11 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

+ 11 - 0
src/PrivateKey.h

@@ -3,6 +3,8 @@
 #include "openfhe/core/lattice/hal/lat-backend.h"
 #include "openfhe/pke/key/privatekey-fwd.h"
 
+#include "SerialMode.h" // SerialMode
+
 namespace openfhe
 {
 
@@ -12,6 +14,12 @@ class PrivateKeyDCRTPoly final
 {
     std::shared_ptr<PrivateKeyImpl> m_privateKey;
 public:
+    friend bool SerializePrivateKeyToFile(const std::string& privateKeyLocation,
+        const PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode);
+    friend bool DeserializePrivateKeyFromFile(const std::string& privateKeyLocation,
+        PrivateKeyDCRTPoly& privateKey, const SerialMode serialMode);
+
+    PrivateKeyDCRTPoly() = default;
     explicit PrivateKeyDCRTPoly(const std::shared_ptr<PrivateKeyImpl>& privateKey);
     PrivateKeyDCRTPoly(const PrivateKeyDCRTPoly&) = delete;
     PrivateKeyDCRTPoly(PrivateKeyDCRTPoly&&) = delete;
@@ -21,4 +29,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)
 {

+ 5 - 0
src/SerialDeserial.h

@@ -10,6 +10,7 @@ namespace openfhe
 class CiphertextDCRTPoly;
 class CryptoContextDCRTPoly;
 class PublicKeyDCRTPoly;
+class PrivateKeyDCRTPoly;
 
 bool SerializeCryptoContextToFile(const std::string& ccLocation,
     const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
@@ -36,6 +37,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,

+ 11 - 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++"
     {
@@ -1034,6 +1040,11 @@ pub mod ffi
         fn DeserializePublicKeyFromFile(publicKeyLocation: &CxxString,
                                         publicKey: Pin<&mut PublicKeyDCRTPoly>,
                                         serialMode: SerialMode) -> bool;
+        fn SerializePrivateKeyToFile(privateKeyLocation: &CxxString, privateKey: &PrivateKeyDCRTPoly,
+                                    serialMode: SerialMode) -> bool;
+        fn DeserializePrivateKeyFromFile(privateKeyLocation: &CxxString,
+                                        privateKey: Pin<&mut PrivateKeyDCRTPoly>,
+                                        serialMode: SerialMode) -> bool;
     }
 }