Selaa lähdekoodia

Add functions for serializing and deserializing private keys

Ian Goldberg 5 kuukautta sitten
vanhempi
commit
be80ff9a39
5 muutettua tiedostoa jossa 51 lisäystä ja 0 poistoa
  1. 6 0
      src/PrivateKey.cc
  2. 4 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

+ 4 - 0
src/PrivateKey.h

@@ -12,6 +12,7 @@ class PrivateKeyDCRTPoly final
 {
     std::shared_ptr<PrivateKeyImpl> m_privateKey;
 public:
+    PrivateKeyDCRTPoly() = default;
     explicit PrivateKeyDCRTPoly(const std::shared_ptr<PrivateKeyImpl>& privateKey);
     PrivateKeyDCRTPoly(const PrivateKeyDCRTPoly&) = delete;
     PrivateKeyDCRTPoly(PrivateKeyDCRTPoly&&) = delete;
@@ -21,4 +22,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++"
     {