Browse Source

File separation

Hovsep Papoyan 11 months ago
parent
commit
92a43f3a08
17 changed files with 794 additions and 600 deletions
  1. 21 3
      build.rs
  2. 23 0
      src/Ciphertext.cc
  3. 33 0
      src/Ciphertext.h
  4. 11 410
      src/CryptoContext.cc
  5. 33 161
      src/CryptoContext.h
  6. 21 0
      src/KeyPair.cc
  7. 37 0
      src/KeyPair.h
  8. 40 0
      src/Params.cc
  9. 42 0
      src/Params.h
  10. 52 0
      src/Plaintext.cc
  11. 33 0
      src/Plaintext.h
  12. 20 0
      src/PublicKey.cc
  13. 32 0
      src/PublicKey.h
  14. 294 0
      src/SerialDeserial.cc
  15. 44 0
      src/SerialDeserial.h
  16. 12 0
      src/SerialMode.h
  17. 46 26
      src/lib.rs

+ 21 - 3
build.rs

@@ -1,7 +1,13 @@
 fn main()
 {
     cxx_build::bridge("src/lib.rs")
-        .file("src/bindings.cc")
+        .file("src/Ciphertext.cc")
+        .file("src/CryptoContext.cc")
+        .file("src/KeyPair.cc")
+        .file("src/Params.cc")
+        .file("src/Plaintext.cc")
+        .file("src/PublicKey.cc")
+        .file("src/SerialDeserial.cc")
         .include("/usr/local/include/openfhe")
         .include("/usr/local/include/openfhe/third-party/include")
         .include("/usr/local/include/openfhe/core")
@@ -19,8 +25,20 @@ fn main()
         .compile("openfhe");
 
     println!("cargo::rerun-if-changed=src/lib.rs");
-    println!("cargo::rerun-if-changed=src/bindings.hpp");
-    println!("cargo::rerun-if-changed=src/bindings.cc");
+    println!("cargo::rerun-if-changed=src/Ciphertext.h");
+    println!("cargo::rerun-if-changed=src/Ciphertext.cc");
+    println!("cargo::rerun-if-changed=src/CryptoContext.h");
+    println!("cargo::rerun-if-changed=src/CryptoContext.cc");
+    println!("cargo::rerun-if-changed=src/KeyPair.h");
+    println!("cargo::rerun-if-changed=src/KeyPair.cc");
+    println!("cargo::rerun-if-changed=src/Params.h");
+    println!("cargo::rerun-if-changed=src/Params.cc");
+    println!("cargo::rerun-if-changed=src/Plaintext.h");
+    println!("cargo::rerun-if-changed=src/Plaintext.cc");
+    println!("cargo::rerun-if-changed=src/PublicKey.h");
+    println!("cargo::rerun-if-changed=src/PublicKey.cc");
+    println!("cargo::rerun-if-changed=src/SerialDeserial.h");
+    println!("cargo::rerun-if-changed=src/SerialDeserial.cc");
 
     // linking openFHE
     println!("cargo::rustc-link-arg=-L/usr/local/lib");

+ 23 - 0
src/Ciphertext.cc

@@ -0,0 +1,23 @@
+#include "Ciphertext.h"
+
+#include "openfhe/pke/ciphertext.h"
+
+namespace openfhe
+{
+
+CiphertextDCRTPoly::CiphertextDCRTPoly()
+    : m_ciphertext(std::make_shared<CiphertextImpl>())
+{ }
+CiphertextDCRTPoly::CiphertextDCRTPoly(const std::shared_ptr<CiphertextImpl>& ciphertext)
+    : m_ciphertext(ciphertext)
+{ }
+std::shared_ptr<CiphertextImpl> CiphertextDCRTPoly::GetInternal() const
+{
+    return m_ciphertext;
+}
+std::unique_ptr<CiphertextDCRTPoly> GenDefaultConstructedCiphertext()
+{
+    return std::make_unique<CiphertextDCRTPoly>();
+}
+
+} // openfhe

+ 33 - 0
src/Ciphertext.h

@@ -0,0 +1,33 @@
+#pragma once
+
+#include "openfhe/core/lattice/hal/lat-backend.h"
+#include "openfhe/pke/ciphertext-fwd.h"
+
+#include "SerialMode.h" // SerialMode
+
+namespace openfhe
+{
+
+using CiphertextImpl = lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly>;
+
+class CiphertextDCRTPoly final
+{
+    std::shared_ptr<CiphertextImpl> m_ciphertext;
+public:
+    friend bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
+        const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
+    friend bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,
+        CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
+
+    explicit CiphertextDCRTPoly();
+    explicit CiphertextDCRTPoly(const std::shared_ptr<CiphertextImpl>& ciphertext);
+    CiphertextDCRTPoly(const CiphertextDCRTPoly&) = delete;
+    CiphertextDCRTPoly(CiphertextDCRTPoly&&) = delete;
+    CiphertextDCRTPoly& operator=(const CiphertextDCRTPoly&) = delete;
+    CiphertextDCRTPoly& operator=(CiphertextDCRTPoly&&) = delete;
+
+    [[nodiscard]] std::shared_ptr<CiphertextImpl> GetInternal() const;
+};
+[[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> GenDefaultConstructedCiphertext();
+
+} // openfhe

+ 11 - 410
src/bindings.cc → src/CryptoContext.cc

@@ -1,91 +1,21 @@
-#include "openfhe/src/lib.rs.h"
+#include "CryptoContext.h"
 
 #include "openfhe/pke/gen-cryptocontext.h"
+#include "openfhe/pke/scheme/bfvrns/gen-cryptocontext-bfvrns.h"
+#include "openfhe/pke/scheme/bgvrns/gen-cryptocontext-bgvrns.h"
+#include "openfhe/pke/scheme/ckksrns/gen-cryptocontext-ckksrns.h"
 
-// This inclusion is required for SerializeCryptoContextToFile
-// and DeserializeCryptoContextFromFile functions for calling
-// specialized versions of the corresponding functions.
-#include "openfhe/pke/cryptocontext-ser.h"
+#include "openfhe/src/lib.rs.h" // ComplexPair
 
-namespace openfhe
-{
-PublicKeyDCRTPoly::PublicKeyDCRTPoly()
-    : m_publicKey(std::make_shared<PublicKeyImpl>())
-{ }
-std::shared_ptr<PublicKeyImpl> PublicKeyDCRTPoly::GetInternal() const
-{
-    return m_publicKey;
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-KeyPairDCRTPoly::KeyPairDCRTPoly(lbcrypto::KeyPair<lbcrypto::DCRTPoly> keyPair)
-    : m_publicKey(keyPair.publicKey)
-    , m_privateKey(keyPair.secretKey)
-{ }
-std::shared_ptr<PublicKeyImpl> KeyPairDCRTPoly::GetPublicKey() const
-{
-    return m_publicKey;
-}
-std::shared_ptr<PrivateKeyImpl> KeyPairDCRTPoly::GetPrivateKey() const
-{
-    return m_privateKey;
-}
+#include "Ciphertext.h"
+#include "KeyPair.h"
+#include "Plaintext.h"
+#include "PublicKey.h"
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-Plaintext::Plaintext(std::shared_ptr<PlaintextImpl> plaintext)
-    : m_plaintext(plaintext)
-{ }
-Plaintext& Plaintext::operator=(std::shared_ptr<PlaintextImpl> plaintext)
-{
-    m_plaintext = plaintext;
-    return *this;
-}
-std::shared_ptr<PlaintextImpl> Plaintext::GetInternal() const
-{
-    return m_plaintext;
-}
-void Plaintext::SetLength(const size_t newSize) const
-{
-    m_plaintext->SetLength(newSize);
-}
-double Plaintext::GetLogPrecision() const
-{
-    return m_plaintext->GetLogPrecision();
-}
-rust::String Plaintext::GetString() const
-{
-    std::stringstream stream;
-    stream << *m_plaintext;
-    return rust::String(stream.str());
-}
-std::unique_ptr<std::vector<ComplexPair>> Plaintext::GetCopyOfCKKSPackedValue() const
-{
-    const std::vector<std::complex<double>>& v = m_plaintext->GetCKKSPackedValue();
-    std::vector<ComplexPair> result;
-    result.reserve(v.size());
-    for (const std::complex<double>& elem : v)
-    {
-        result.push_back(ComplexPair{elem.real(), elem.imag()});
-    }
-    return std::make_unique<std::vector<ComplexPair>>(std::move(result));
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-CiphertextDCRTPoly::CiphertextDCRTPoly()
-    : m_ciphertext(std::make_shared<CiphertextImpl>())
-{ }
-CiphertextDCRTPoly::CiphertextDCRTPoly(std::shared_ptr<CiphertextImpl> ciphertext)
-    : m_ciphertext(ciphertext)
-{ }
-std::shared_ptr<CiphertextImpl> CiphertextDCRTPoly::GetInternal() const
+namespace openfhe
 {
-    return m_ciphertext;
-}
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
+using PlaintextImpl = lbcrypto::PlaintextImpl;
 
 CryptoContextDCRTPoly::CryptoContextDCRTPoly(const ParamsBFVRNS& params)
     : m_cryptoContextImplSharedPtr(lbcrypto::GenCryptoContext(params))
@@ -561,44 +491,6 @@ std::unique_ptr<std::vector<uint32_t>> GetUniqueValues(const std::vector<uint32_
         CryptoContextImpl::GetUniqueValues(oldValues, newValues));
 }
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-std::unique_ptr<Params> GetParamsByScheme(const SCHEME scheme)
-{
-    return std::make_unique<Params>(scheme);
-}
-std::unique_ptr<Params> GetParamsByVectorOfString(const std::vector<std::string>& vals)
-{
-    return std::make_unique<Params>(vals);
-}
-std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNS()
-{
-    return std::make_unique<ParamsBFVRNS>();
-}
-std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNSbyVectorOfString(const std::vector<std::string>& vals)
-{
-    return std::make_unique<ParamsBFVRNS>(vals);
-}
-std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNS()
-{
-    return std::make_unique<ParamsBGVRNS>();
-}
-std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNSbyVectorOfString(const std::vector<std::string>& vals)
-{
-    return std::make_unique<ParamsBGVRNS>(vals);
-}
-std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNS()
-{
-    return std::make_unique<ParamsCKKSRNS>();
-}
-std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNSbyVectorOfString(const std::vector<std::string>& vals)
-{
-    return std::make_unique<ParamsCKKSRNS>(vals);
-}
-std::unique_ptr<Plaintext> GenEmptyPlainText()
-{
-    return std::make_unique<Plaintext>();
-}
 std::unique_ptr<CryptoContextDCRTPoly> GenEmptyCryptoContext()
 {
     return std::make_unique<CryptoContextDCRTPoly>();
@@ -615,296 +507,5 @@ std::unique_ptr<CryptoContextDCRTPoly> GenCryptoContextByParamsCKKSRNS(const Par
 {
     return std::make_unique<CryptoContextDCRTPoly>(params);
 }
-std::unique_ptr<PublicKeyDCRTPoly> GenDefaultConstructedPublicKey()
-{
-    return std::make_unique<PublicKeyDCRTPoly>();
-}
-std::unique_ptr<CiphertextDCRTPoly> GenDefaultConstructedCiphertext()
-{
-    return std::make_unique<CiphertextDCRTPoly>();
-}
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
 
-bool SerializeCryptoContextToFile(const std::string& ccLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
-{
-    if (serialMode == SerialMode::BINARY)
-    {
-        return lbcrypto::Serial::SerializeToFile(ccLocation,
-            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::BINARY);
-    }
-    if (serialMode == SerialMode::JSON)
-    {
-        return lbcrypto::Serial::SerializeToFile(ccLocation,
-            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::JSON);
-    }
-    return false;
-}
-bool DeserializeCryptoContextFromFile(const std::string& ccLocation,
-    CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
-{
-    if (serialMode == SerialMode::BINARY)
-    {
-        return lbcrypto::Serial::DeserializeFromFile(ccLocation,
-            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::BINARY);
-    }
-    if (serialMode == SerialMode::JSON)
-    {
-        return lbcrypto::Serial::DeserializeFromFile(ccLocation,
-            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::JSON);
-    }
-    return false;
-}
-bool SerializeEvalMultKeyToFile(const std::string& multKeyLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
-{
-    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
-    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
-        new std::ofstream(multKeyLocation, std::ios::out | std::ios::binary), close);
-
-    if (ofs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::SerializeEvalMultKey(*ofs,
-                lbcrypto::SerType::BINARY, cryptoContext.m_cryptoContextImplSharedPtr);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::SerializeEvalMultKey(*ofs,
-                lbcrypto::SerType::JSON, cryptoContext.m_cryptoContextImplSharedPtr);
-        }
-    }
-    return false;
-}
-bool SerializeEvalMultKeyByIdToFile(const std::string& multKeyLocation,
-    const SerialMode serialMode, const std::string& id)
-{
-    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
-    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
-        new std::ofstream(multKeyLocation, std::ios::out | std::ios::binary), close);
-
-    if (ofs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::SerializeEvalMultKey(*ofs, lbcrypto::SerType::BINARY, id);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::SerializeEvalMultKey(*ofs, lbcrypto::SerType::JSON, id);
-        }
-    }
-    return false;
-}
-bool DeserializeEvalMultKeyFromFile(const std::string& multKeyLocation,
-    const SerialMode serialMode)
-{
-    const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
-    const std::unique_ptr<std::ifstream, decltype(close)> ifs(
-        new std::ifstream(multKeyLocation, std::ios::in | std::ios::binary), close);
-
-    if (ifs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::DeserializeEvalMultKey(*ifs, lbcrypto::SerType::BINARY);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::DeserializeEvalMultKey(*ifs, lbcrypto::SerType::JSON);
-        }
-    }
-    return false;
-}
-bool SerializeEvalSumKeyToFile(const std::string& sumKeyLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
-{
-    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
-    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
-        new std::ofstream(sumKeyLocation, std::ios::out | std::ios::binary), close);
-
-    if (ofs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
-                cryptoContext.m_cryptoContextImplSharedPtr);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
-                cryptoContext.m_cryptoContextImplSharedPtr);
-        }
-    }
-    return false;
-}
-bool SerializeEvalSumKeyByIdToFile(const std::string& sumKeyLocation,
-    const SerialMode serialMode, const std::string& id)
-{
-    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
-    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
-        new std::ofstream(sumKeyLocation, std::ios::out | std::ios::binary), close);
-
-    if (ofs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::SerializeEvalSumKey(*ofs, lbcrypto::SerType::BINARY, id);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::SerializeEvalSumKey(*ofs, lbcrypto::SerType::JSON, id);
-        }
-    }
-    return false;
-}
-bool DeserializeEvalSumKeyFromFile(const std::string& sumKeyLocation, const SerialMode serialMode)
-{
-    const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
-    const std::unique_ptr<std::ifstream, decltype(close)> ifs(
-        new std::ifstream(sumKeyLocation, std::ios::in | std::ios::binary), close);
-
-    if (ifs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
-                lbcrypto::SerType::BINARY);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
-                lbcrypto::SerType::JSON);
-        }
-    }
-    return false;
-}
-bool SerializeEvalAutomorphismKeyToFile(const std::string& automorphismKeyLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
-{
-    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
-    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
-        new std::ofstream(automorphismKeyLocation, std::ios::out | std::ios::binary), close);
-
-    if (ofs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
-                cryptoContext.m_cryptoContextImplSharedPtr);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
-                cryptoContext.m_cryptoContextImplSharedPtr);
-        }
-    }
-    return false;
-}
-bool SerializeEvalAutomorphismKeyByIdToFile(const std::string& automorphismKeyLocation,
-    const SerialMode serialMode, const std::string& id)
-{
-    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
-    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
-        new std::ofstream(automorphismKeyLocation, std::ios::out | std::ios::binary), close);
-
-    if (ofs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
-                id);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
-                id);
-        }
-    }
-    return false;
-}
-bool DeserializeEvalAutomorphismKeyFromFile(const std::string& automorphismKeyLocation,
-    const SerialMode serialMode)
-{
-    const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
-    const std::unique_ptr<std::ifstream, decltype(close)> ifs(
-        new std::ifstream(automorphismKeyLocation, std::ios::in | std::ios::binary), close);
-
-    if (ifs->is_open())
-    {
-        if (serialMode == SerialMode::BINARY)
-        {
-            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
-                lbcrypto::SerType::BINARY);
-        }
-        if (serialMode == SerialMode::JSON)
-        {
-            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
-                lbcrypto::SerType::JSON);
-        }
-    }
-    return false;
-}
-bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
-    const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode)
-{
-    if (serialMode == SerialMode::BINARY)
-    {
-        return lbcrypto::Serial::SerializeToFile(ciphertextLocation,
-            ciphertext.m_ciphertext, lbcrypto::SerType::BINARY);
-    }
-    if (serialMode == SerialMode::JSON)
-    {
-        return lbcrypto::Serial::SerializeToFile(ciphertextLocation,
-            ciphertext.m_ciphertext, lbcrypto::SerType::JSON);
-    }
-    return false;
-}
-bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,
-    CiphertextDCRTPoly& ciphertext, const SerialMode serialMode)
-{
-    if (serialMode == SerialMode::BINARY)
-    {
-        return lbcrypto::Serial::DeserializeFromFile(ciphertextLocation,
-            ciphertext.m_ciphertext, lbcrypto::SerType::BINARY);
-    }
-    if (serialMode == SerialMode::JSON)
-    {
-        return lbcrypto::Serial::DeserializeFromFile(ciphertextLocation,
-            ciphertext.m_ciphertext, lbcrypto::SerType::JSON);
-    }
-    return false;
-}
-bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
-    const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode)
-{
-    if (serialMode == SerialMode::BINARY)
-    {
-        return lbcrypto::Serial::SerializeToFile(publicKeyLocation,
-            publicKey.m_publicKey, lbcrypto::SerType::BINARY);
-    }
-    if (serialMode == SerialMode::JSON)
-    {
-        return lbcrypto::Serial::SerializeToFile(publicKeyLocation,
-            publicKey.m_publicKey, lbcrypto::SerType::JSON);
-    }
-    return false;
-}
-bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
-    PublicKeyDCRTPoly& publicKey, const SerialMode serialMode)
-{
-    if (serialMode == SerialMode::BINARY)
-    {
-        return lbcrypto::Serial::DeserializeFromFile(publicKeyLocation,
-            publicKey.m_publicKey, lbcrypto::SerType::BINARY);
-    }
-    if (serialMode == SerialMode::JSON)
-    {
-        return lbcrypto::Serial::DeserializeFromFile(publicKeyLocation,
-            publicKey.m_publicKey, lbcrypto::SerType::JSON);
-    }
-    return false;
-}
 } // openfhe

+ 33 - 161
src/bindings.hpp → src/CryptoContext.h

@@ -1,132 +1,52 @@
 #pragma once
 
-#include "openfhe/pke/scheme/ckksrns/gen-cryptocontext-ckksrns.h"
-#include "openfhe/pke/scheme/bfvrns/gen-cryptocontext-bfvrns.h"
-#include "openfhe/pke/scheme/bgvrns/gen-cryptocontext-bgvrns.h"
+#include "openfhe/pke/constants-fwd.h" // PKESchemeFeature
+#include "openfhe/pke/cryptocontext-fwd.h" // CryptoContextImpl
+#include "openfhe/pke/key/privatekey-fwd.h" // PrivateKeyImpl
+#include "openfhe/pke/key/publickey-fwd.h" // PublicKeyImpl
+#include "openfhe/pke/scheme/scheme-id.h" // SCHEME
+#include "openfhe/pke/schemebase/decrypt-result.h" // DecryptResult
 
-#include "rust/cxx.h" // rust::String
+#include "rust/cxx.h" // rust::Fn
 
-enum SerialMode
-{
-    BINARY = 0,
-    JSON = 1,
-};
-
-namespace openfhe
-{
-using ParamsBFVRNS = lbcrypto::CCParams<lbcrypto::CryptoContextBFVRNS>;
-using ParamsBGVRNS = lbcrypto::CCParams<lbcrypto::CryptoContextBGVRNS>;
-using ParamsCKKSRNS = lbcrypto::CCParams<lbcrypto::CryptoContextCKKSRNS>;
-using Params = lbcrypto::Params;
-using SCHEME = lbcrypto::SCHEME;
-using SecretKeyDist = lbcrypto::SecretKeyDist;
-using ProxyReEncryptionMode = lbcrypto::ProxyReEncryptionMode;
-using MultipartyMode = lbcrypto::MultipartyMode;
-using ExecutionMode = lbcrypto::ExecutionMode;
-using DecryptionNoiseMode = lbcrypto::DecryptionNoiseMode;
-using KeySwitchTechnique = lbcrypto::KeySwitchTechnique;
-using ScalingTechnique = lbcrypto::ScalingTechnique;
-using SecurityLevel = lbcrypto::SecurityLevel;
-using EncryptionTechnique = lbcrypto::EncryptionTechnique;
-using MultiplicationTechnique = lbcrypto::MultiplicationTechnique;
-using COMPRESSION_LEVEL = lbcrypto::COMPRESSION_LEVEL;
-using PKESchemeFeature = lbcrypto::PKESchemeFeature;
-using PublicKeyImpl = lbcrypto::PublicKeyImpl<lbcrypto::DCRTPoly>;
-using PrivateKeyImpl = lbcrypto::PrivateKeyImpl<lbcrypto::DCRTPoly>;
-using DecryptResult = lbcrypto::DecryptResult;
-using DCRTPolyParams = lbcrypto::DCRTPoly::Params;
-using ::SerialMode;
-struct ComplexPair;
-
-// not used in the Rust side
-using PlaintextImpl = lbcrypto::PlaintextImpl;
-// not used in the Rust side
-using CiphertextImpl = lbcrypto::CiphertextImpl<lbcrypto::DCRTPoly>;
-// not used in the Rust side
-using CryptoContextImpl = lbcrypto::CryptoContextImpl<lbcrypto::DCRTPoly>;
-// not used in the Rust side
-using KeyPair = lbcrypto::KeyPair<lbcrypto::DCRTPoly>;
+#include "SerialMode.h" // SerialMode
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-class PublicKeyDCRTPoly final
+namespace lbcrypto
 {
-    std::shared_ptr<PublicKeyImpl> m_publicKey;
-public:
-    friend bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
-        const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
-    friend bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
-        PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
-
-    explicit PublicKeyDCRTPoly();
-    PublicKeyDCRTPoly(const PublicKeyDCRTPoly&) = delete;
-    PublicKeyDCRTPoly(PublicKeyDCRTPoly&&) = delete;
-    PublicKeyDCRTPoly& operator=(const PublicKeyDCRTPoly&) = delete;
-    PublicKeyDCRTPoly& operator=(PublicKeyDCRTPoly&&) = delete;
-
-    [[nodiscard]] std::shared_ptr<PublicKeyImpl> GetInternal() const;
-};
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
 
-class KeyPairDCRTPoly final
-{
-    std::shared_ptr<PublicKeyImpl> m_publicKey;
-    std::shared_ptr<PrivateKeyImpl> m_privateKey;
-public:
-    explicit KeyPairDCRTPoly(KeyPair keyPair);
-    KeyPairDCRTPoly(const KeyPairDCRTPoly&) = delete;
-    KeyPairDCRTPoly(KeyPairDCRTPoly&&) = delete;
-    KeyPairDCRTPoly& operator=(const KeyPairDCRTPoly&) = delete;
-    KeyPairDCRTPoly& operator=(KeyPairDCRTPoly&&) = delete;
+class Params;
+class CryptoContextBFVRNS;
+class CryptoContextBGVRNS;
+class CryptoContextCKKSRNS;
 
-    [[nodiscard]] std::shared_ptr<PublicKeyImpl> GetPublicKey() const;
-    [[nodiscard]] std::shared_ptr<PrivateKeyImpl> GetPrivateKey() const;
-};
+template <typename T>
+class CCParams;
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
+} // lbcrypto
 
-class Plaintext final
+namespace openfhe
 {
-    std::shared_ptr<PlaintextImpl> m_plaintext;
-public:
-    explicit Plaintext() = default;
-    explicit Plaintext(std::shared_ptr<PlaintextImpl> plaintext);
-    Plaintext(const Plaintext&) = delete;
-    Plaintext(Plaintext&&) = delete;
-    Plaintext& operator=(const Plaintext&) = delete;
-    Plaintext& operator=(Plaintext&&) = delete;
-    Plaintext& operator=(std::shared_ptr<PlaintextImpl> plaintext);
-
-    [[nodiscard]] std::shared_ptr<PlaintextImpl> GetInternal() const;
-    void SetLength(const size_t newSize) const;
-    [[nodiscard]] double GetLogPrecision() const;
-    [[nodiscard]] rust::String GetString() const;
-    [[nodiscard]] std::unique_ptr<std::vector<ComplexPair>> GetCopyOfCKKSPackedValue() const;
-};
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
+struct ComplexPair;
 
-class CiphertextDCRTPoly final
-{
-    std::shared_ptr<CiphertextImpl> m_ciphertext;
-public:
-    friend bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
-        const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
-    friend bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,
-        CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
+class KeyPairDCRTPoly;
+class PublicKeyDCRTPoly;
+class Plaintext;
+class CiphertextDCRTPoly;
 
-    explicit CiphertextDCRTPoly();
-    explicit CiphertextDCRTPoly(std::shared_ptr<CiphertextImpl> ciphertext);
-    CiphertextDCRTPoly(const CiphertextDCRTPoly&) = delete;
-    CiphertextDCRTPoly(CiphertextDCRTPoly&&) = delete;
-    CiphertextDCRTPoly& operator=(const CiphertextDCRTPoly&) = delete;
-    CiphertextDCRTPoly& operator=(CiphertextDCRTPoly&&) = delete;
+using SCHEME = lbcrypto::SCHEME;
+using PKESchemeFeature = lbcrypto::PKESchemeFeature;
 
-    [[nodiscard]] std::shared_ptr<CiphertextImpl> GetInternal() const;
-};
+using Params = lbcrypto::Params;
+using ParamsBFVRNS = lbcrypto::CCParams<lbcrypto::CryptoContextBFVRNS>;
+using ParamsBGVRNS = lbcrypto::CCParams<lbcrypto::CryptoContextBGVRNS>;
+using ParamsCKKSRNS = lbcrypto::CCParams<lbcrypto::CryptoContextCKKSRNS>;
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
+using CryptoContextImpl = lbcrypto::CryptoContextImpl<lbcrypto::DCRTPoly>;
+using PublicKeyImpl = lbcrypto::PublicKeyImpl<lbcrypto::DCRTPoly>;
+using PrivateKeyImpl = lbcrypto::PrivateKeyImpl<lbcrypto::DCRTPoly>;
+using DCRTPolyParams = lbcrypto::DCRTPoly::Params;
+using DecryptResult = lbcrypto::DecryptResult;
 
 class CryptoContextDCRTPoly final
 {
@@ -312,21 +232,6 @@ void ClearEvalAutomorphismKeysByCryptoContext(const CryptoContextDCRTPoly& crypt
 [[nodiscard]] std::unique_ptr<std::vector<uint32_t>> GetUniqueValues(
     const std::vector<uint32_t>& oldValues, const std::vector<uint32_t>& newValues);
 
-///////////////////////////////////////////////////////////////////////////////////////////////////
-
-[[nodiscard]] std::unique_ptr<Params> GetParamsByScheme(const SCHEME scheme);
-[[nodiscard]] std::unique_ptr<Params> GetParamsByVectorOfString(
-    const std::vector<std::string>& vals);
-[[nodiscard]] std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNS();
-[[nodiscard]] std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNSbyVectorOfString(
-    const std::vector<std::string>& vals);
-[[nodiscard]] std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNS();
-[[nodiscard]] std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNSbyVectorOfString(
-    const std::vector<std::string>& vals);
-[[nodiscard]] std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNS();
-[[nodiscard]] std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNSbyVectorOfString(
-    const std::vector<std::string>& vals);
-[[nodiscard]] std::unique_ptr<Plaintext> GenEmptyPlainText();
 [[nodiscard]] std::unique_ptr<CryptoContextDCRTPoly> GenEmptyCryptoContext();
 [[nodiscard]] std::unique_ptr<CryptoContextDCRTPoly> GenCryptoContextByParamsBFVRNS(
     const ParamsBFVRNS& params);
@@ -334,38 +239,5 @@ void ClearEvalAutomorphismKeysByCryptoContext(const CryptoContextDCRTPoly& crypt
     const ParamsBGVRNS& params);
 [[nodiscard]] std::unique_ptr<CryptoContextDCRTPoly> GenCryptoContextByParamsCKKSRNS(
     const ParamsCKKSRNS& params);
-[[nodiscard]] std::unique_ptr<PublicKeyDCRTPoly> GenDefaultConstructedPublicKey();
-[[nodiscard]] std::unique_ptr<CiphertextDCRTPoly> GenDefaultConstructedCiphertext();
-
-///////////////////////////////////////////////////////////////////////////////////////////////////
 
-bool SerializeCryptoContextToFile(const std::string& ccLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
-bool DeserializeCryptoContextFromFile(const std::string& ccLocation,
-    CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
-bool SerializeEvalMultKeyToFile(const std::string& multKeyLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
-bool SerializeEvalMultKeyByIdToFile(const std::string& multKeyLocation,
-    const SerialMode serialMode, const std::string& id);
-bool DeserializeEvalMultKeyFromFile(const std::string& multKeyLocation,
-    const SerialMode serialMode);
-bool SerializeEvalSumKeyToFile(const std::string& sumKeyLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
-bool SerializeEvalSumKeyByIdToFile(const std::string& sumKeyLocation,
-    const SerialMode serialMode, const std::string& id);
-bool DeserializeEvalSumKeyFromFile(const std::string& sumKeyLocation, const SerialMode serialMode);
-bool SerializeEvalAutomorphismKeyToFile(const std::string& automorphismKeyLocation,
-    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
-bool SerializeEvalAutomorphismKeyByIdToFile(const std::string& automorphismKeyLocation,
-    const SerialMode serialMode, const std::string& id);
-bool DeserializeEvalAutomorphismKeyFromFile(const std::string& automorphismKeyLocation,
-    const SerialMode serialMode);
-bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
-    const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
-bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,
-    CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
-bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
-    const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
-bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
-    PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
 } // openfhe

+ 21 - 0
src/KeyPair.cc

@@ -0,0 +1,21 @@
+#include "KeyPair.h"
+
+#include "openfhe/pke/key/keypair.h"
+
+namespace openfhe
+{
+
+KeyPairDCRTPoly::KeyPairDCRTPoly(const KeyPair& keyPair)
+    : m_publicKey(keyPair.publicKey)
+    , m_privateKey(keyPair.secretKey)
+{ }
+std::shared_ptr<PublicKeyImpl> KeyPairDCRTPoly::GetPublicKey() const
+{
+    return m_publicKey;
+}
+std::shared_ptr<PrivateKeyImpl> KeyPairDCRTPoly::GetPrivateKey() const
+{
+    return m_privateKey;
+}
+
+} // openfhe

+ 37 - 0
src/KeyPair.h

@@ -0,0 +1,37 @@
+#pragma once
+
+#include "openfhe/core/lattice/hal/lat-backend.h"
+#include "openfhe/pke/key/privatekey-fwd.h"
+#include "openfhe/pke/key/publickey-fwd.h"
+
+namespace lbcrypto
+{
+
+template <class Element>
+class KeyPair;
+
+} // lbcrypto
+
+namespace openfhe
+{
+
+using PublicKeyImpl = lbcrypto::PublicKeyImpl<lbcrypto::DCRTPoly>;
+using PrivateKeyImpl = lbcrypto::PrivateKeyImpl<lbcrypto::DCRTPoly>;
+using KeyPair = lbcrypto::KeyPair<lbcrypto::DCRTPoly>;
+
+class KeyPairDCRTPoly final
+{
+    std::shared_ptr<PublicKeyImpl> m_publicKey;
+    std::shared_ptr<PrivateKeyImpl> m_privateKey;
+public:
+    explicit KeyPairDCRTPoly(const KeyPair& keyPair);
+    KeyPairDCRTPoly(const KeyPairDCRTPoly&) = delete;
+    KeyPairDCRTPoly(KeyPairDCRTPoly&&) = delete;
+    KeyPairDCRTPoly& operator=(const KeyPairDCRTPoly&) = delete;
+    KeyPairDCRTPoly& operator=(KeyPairDCRTPoly&&) = delete;
+
+    [[nodiscard]] std::shared_ptr<PublicKeyImpl> GetPublicKey() const;
+    [[nodiscard]] std::shared_ptr<PrivateKeyImpl> GetPrivateKey() const;
+};
+
+} // openfhe

+ 40 - 0
src/Params.cc

@@ -0,0 +1,40 @@
+#include "Params.h"
+
+namespace openfhe
+{
+
+std::unique_ptr<Params> GetParamsByScheme(const SCHEME scheme)
+{
+    return std::make_unique<Params>(scheme);
+}
+std::unique_ptr<Params> GetParamsByVectorOfString(const std::vector<std::string>& vals)
+{
+    return std::make_unique<Params>(vals);
+}
+std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNS()
+{
+    return std::make_unique<ParamsBFVRNS>();
+}
+std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNSbyVectorOfString(const std::vector<std::string>& vals)
+{
+    return std::make_unique<ParamsBFVRNS>(vals);
+}
+std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNS()
+{
+    return std::make_unique<ParamsBGVRNS>();
+}
+std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNSbyVectorOfString(const std::vector<std::string>& vals)
+{
+    return std::make_unique<ParamsBGVRNS>(vals);
+}
+std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNS()
+{
+    return std::make_unique<ParamsCKKSRNS>();
+}
+std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNSbyVectorOfString(
+    const std::vector<std::string>& vals)
+{
+    return std::make_unique<ParamsCKKSRNS>(vals);
+}
+
+} // openfhe

+ 42 - 0
src/Params.h

@@ -0,0 +1,42 @@
+#pragma once
+
+#include "openfhe/pke/scheme/gen-cryptocontext-params.h"
+#include "openfhe/pke/scheme/bfvrns/gen-cryptocontext-bfvrns-params.h"
+#include "openfhe/pke/scheme/bgvrns/gen-cryptocontext-bgvrns-params.h"
+#include "openfhe/pke/scheme/ckksrns/gen-cryptocontext-ckksrns-params.h"
+
+namespace openfhe
+{
+
+using SCHEME = lbcrypto::SCHEME;
+using SecretKeyDist = lbcrypto::SecretKeyDist;
+using ProxyReEncryptionMode = lbcrypto::ProxyReEncryptionMode;
+using MultipartyMode = lbcrypto::MultipartyMode;
+using ExecutionMode = lbcrypto::ExecutionMode;
+using DecryptionNoiseMode = lbcrypto::DecryptionNoiseMode;
+using KeySwitchTechnique = lbcrypto::KeySwitchTechnique;
+using ScalingTechnique = lbcrypto::ScalingTechnique;
+using SecurityLevel = lbcrypto::SecurityLevel;
+using EncryptionTechnique = lbcrypto::EncryptionTechnique;
+using MultiplicationTechnique = lbcrypto::MultiplicationTechnique;
+using COMPRESSION_LEVEL = lbcrypto::COMPRESSION_LEVEL;
+
+using Params = lbcrypto::Params;
+using ParamsBFVRNS = lbcrypto::CCParams<lbcrypto::CryptoContextBFVRNS>;
+using ParamsBGVRNS = lbcrypto::CCParams<lbcrypto::CryptoContextBGVRNS>;
+using ParamsCKKSRNS = lbcrypto::CCParams<lbcrypto::CryptoContextCKKSRNS>;
+
+[[nodiscard]] std::unique_ptr<Params> GetParamsByScheme(const SCHEME scheme);
+[[nodiscard]] std::unique_ptr<Params> GetParamsByVectorOfString(
+    const std::vector<std::string>& vals);
+[[nodiscard]] std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNS();
+[[nodiscard]] std::unique_ptr<ParamsBFVRNS> GetParamsBFVRNSbyVectorOfString(
+    const std::vector<std::string>& vals);
+[[nodiscard]] std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNS();
+[[nodiscard]] std::unique_ptr<ParamsBGVRNS> GetParamsBGVRNSbyVectorOfString(
+    const std::vector<std::string>& vals);
+[[nodiscard]] std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNS();
+[[nodiscard]] std::unique_ptr<ParamsCKKSRNS> GetParamsCKKSRNSbyVectorOfString(
+    const std::vector<std::string>& vals);
+
+} // openfhe

+ 52 - 0
src/Plaintext.cc

@@ -0,0 +1,52 @@
+#include "Plaintext.h"
+
+#include "openfhe/pke/encoding/plaintext.h"
+
+#include "openfhe/src/lib.rs.h" // ComplexPair
+
+namespace openfhe
+{
+
+Plaintext::Plaintext(const std::shared_ptr<PlaintextImpl>& plaintext)
+    : m_plaintext(plaintext)
+{ }
+Plaintext& Plaintext::operator=(const std::shared_ptr<PlaintextImpl>& plaintext)
+{
+    m_plaintext = plaintext;
+    return *this;
+}
+std::shared_ptr<PlaintextImpl> Plaintext::GetInternal() const
+{
+    return m_plaintext;
+}
+void Plaintext::SetLength(const size_t newSize) const
+{
+    m_plaintext->SetLength(newSize);
+}
+double Plaintext::GetLogPrecision() const
+{
+    return m_plaintext->GetLogPrecision();
+}
+rust::String Plaintext::GetString() const
+{
+    std::stringstream stream;
+    stream << *m_plaintext;
+    return rust::String(stream.str());
+}
+std::unique_ptr<std::vector<ComplexPair>> Plaintext::GetCopyOfCKKSPackedValue() const
+{
+    const std::vector<std::complex<double>>& v = m_plaintext->GetCKKSPackedValue();
+    std::vector<ComplexPair> result;
+    result.reserve(v.size());
+    for (const std::complex<double>& elem : v)
+    {
+        result.push_back(ComplexPair{elem.real(), elem.imag()});
+    }
+    return std::make_unique<std::vector<ComplexPair>>(std::move(result));
+}
+std::unique_ptr<Plaintext> GenEmptyPlainText()
+{
+    return std::make_unique<Plaintext>();
+}
+
+} // openfhe

+ 33 - 0
src/Plaintext.h

@@ -0,0 +1,33 @@
+#pragma once
+
+#include "openfhe/pke/encoding/plaintext-fwd.h"
+
+#include "rust/cxx.h" // rust::String
+
+namespace openfhe
+{
+
+using PlaintextImpl = lbcrypto::PlaintextImpl;
+struct ComplexPair;
+
+class Plaintext final
+{
+    std::shared_ptr<PlaintextImpl> m_plaintext;
+public:
+    explicit Plaintext() = default;
+    explicit Plaintext(const std::shared_ptr<PlaintextImpl>& plaintext);
+    Plaintext(const Plaintext&) = delete;
+    Plaintext(Plaintext&&) = delete;
+    Plaintext& operator=(const Plaintext&) = delete;
+    Plaintext& operator=(Plaintext&&) = delete;
+    Plaintext& operator=(const std::shared_ptr<PlaintextImpl>& plaintext);
+
+    [[nodiscard]] std::shared_ptr<PlaintextImpl> GetInternal() const;
+    void SetLength(const size_t newSize) const;
+    [[nodiscard]] double GetLogPrecision() const;
+    [[nodiscard]] rust::String GetString() const;
+    [[nodiscard]] std::unique_ptr<std::vector<ComplexPair>> GetCopyOfCKKSPackedValue() const;
+};
+[[nodiscard]] std::unique_ptr<Plaintext> GenEmptyPlainText();
+
+} // openfhe

+ 20 - 0
src/PublicKey.cc

@@ -0,0 +1,20 @@
+#include "PublicKey.h"
+
+#include "openfhe/pke/key/publickey.h"
+
+namespace openfhe
+{
+
+PublicKeyDCRTPoly::PublicKeyDCRTPoly()
+    : m_publicKey(std::make_shared<PublicKeyImpl>())
+{ }
+std::shared_ptr<PublicKeyImpl> PublicKeyDCRTPoly::GetInternal() const
+{
+    return m_publicKey;
+}
+std::unique_ptr<PublicKeyDCRTPoly> GenDefaultConstructedPublicKey()
+{
+    return std::make_unique<PublicKeyDCRTPoly>();
+}
+
+} // openfhe

+ 32 - 0
src/PublicKey.h

@@ -0,0 +1,32 @@
+#pragma once
+
+#include "openfhe/core/lattice/hal/lat-backend.h"
+#include "openfhe/pke/key/publickey-fwd.h"
+
+#include "SerialMode.h" // SerialMode
+
+namespace openfhe
+{
+
+using PublicKeyImpl = lbcrypto::PublicKeyImpl<lbcrypto::DCRTPoly>;
+
+class PublicKeyDCRTPoly final
+{
+    std::shared_ptr<PublicKeyImpl> m_publicKey;
+public:
+    friend bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
+        const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
+    friend bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
+        PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
+
+    explicit PublicKeyDCRTPoly();
+    PublicKeyDCRTPoly(const PublicKeyDCRTPoly&) = delete;
+    PublicKeyDCRTPoly(PublicKeyDCRTPoly&&) = delete;
+    PublicKeyDCRTPoly& operator=(const PublicKeyDCRTPoly&) = delete;
+    PublicKeyDCRTPoly& operator=(PublicKeyDCRTPoly&&) = delete;
+
+    [[nodiscard]] std::shared_ptr<PublicKeyImpl> GetInternal() const;
+};
+[[nodiscard]] std::unique_ptr<PublicKeyDCRTPoly> GenDefaultConstructedPublicKey();
+
+} // openfhe

+ 294 - 0
src/SerialDeserial.cc

@@ -0,0 +1,294 @@
+#include "SerialDeserial.h"
+
+#include "openfhe/pke/cryptocontext-ser.h"
+
+#include "Ciphertext.h"
+#include "CryptoContext.h"
+#include "PublicKey.h"
+
+namespace openfhe
+{
+
+bool SerializeCryptoContextToFile(const std::string& ccLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::SerializeToFile(ccLocation,
+            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::SerializeToFile(ccLocation,
+            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
+bool DeserializeCryptoContextFromFile(const std::string& ccLocation,
+    CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(ccLocation,
+            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(ccLocation,
+            cryptoContext.m_cryptoContextImplSharedPtr, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
+bool SerializeEvalMultKeyToFile(const std::string& multKeyLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
+{
+    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
+    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
+        new std::ofstream(multKeyLocation, std::ios::out | std::ios::binary), close);
+
+    if (ofs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::SerializeEvalMultKey(*ofs,
+                lbcrypto::SerType::BINARY, cryptoContext.m_cryptoContextImplSharedPtr);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::SerializeEvalMultKey(*ofs,
+                lbcrypto::SerType::JSON, cryptoContext.m_cryptoContextImplSharedPtr);
+        }
+    }
+    return false;
+}
+bool SerializeEvalMultKeyByIdToFile(const std::string& multKeyLocation,
+    const SerialMode serialMode, const std::string& id)
+{
+    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
+    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
+        new std::ofstream(multKeyLocation, std::ios::out | std::ios::binary), close);
+
+    if (ofs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::SerializeEvalMultKey(*ofs, lbcrypto::SerType::BINARY, id);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::SerializeEvalMultKey(*ofs, lbcrypto::SerType::JSON, id);
+        }
+    }
+    return false;
+}
+bool DeserializeEvalMultKeyFromFile(const std::string& multKeyLocation,
+    const SerialMode serialMode)
+{
+    const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
+    const std::unique_ptr<std::ifstream, decltype(close)> ifs(
+        new std::ifstream(multKeyLocation, std::ios::in | std::ios::binary), close);
+
+    if (ifs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::DeserializeEvalMultKey(*ifs, lbcrypto::SerType::BINARY);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::DeserializeEvalMultKey(*ifs, lbcrypto::SerType::JSON);
+        }
+    }
+    return false;
+}
+bool SerializeEvalSumKeyToFile(const std::string& sumKeyLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
+{
+    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
+    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
+        new std::ofstream(sumKeyLocation, std::ios::out | std::ios::binary), close);
+
+    if (ofs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
+                cryptoContext.m_cryptoContextImplSharedPtr);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
+                cryptoContext.m_cryptoContextImplSharedPtr);
+        }
+    }
+    return false;
+}
+bool SerializeEvalSumKeyByIdToFile(const std::string& sumKeyLocation,
+    const SerialMode serialMode, const std::string& id)
+{
+    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
+    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
+        new std::ofstream(sumKeyLocation, std::ios::out | std::ios::binary), close);
+
+    if (ofs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::SerializeEvalSumKey(*ofs, lbcrypto::SerType::BINARY, id);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::SerializeEvalSumKey(*ofs, lbcrypto::SerType::JSON, id);
+        }
+    }
+    return false;
+}
+bool DeserializeEvalSumKeyFromFile(const std::string& sumKeyLocation, const SerialMode serialMode)
+{
+    const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
+    const std::unique_ptr<std::ifstream, decltype(close)> ifs(
+        new std::ifstream(sumKeyLocation, std::ios::in | std::ios::binary), close);
+
+    if (ifs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
+                lbcrypto::SerType::BINARY);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
+                lbcrypto::SerType::JSON);
+        }
+    }
+    return false;
+}
+bool SerializeEvalAutomorphismKeyToFile(const std::string& automorphismKeyLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode)
+{
+    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
+    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
+        new std::ofstream(automorphismKeyLocation, std::ios::out | std::ios::binary), close);
+
+    if (ofs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
+                cryptoContext.m_cryptoContextImplSharedPtr);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
+                cryptoContext.m_cryptoContextImplSharedPtr);
+        }
+    }
+    return false;
+}
+bool SerializeEvalAutomorphismKeyByIdToFile(const std::string& automorphismKeyLocation,
+    const SerialMode serialMode, const std::string& id)
+{
+    const auto close = [](std::ofstream* const ofs){ if (ofs->is_open()) { ofs->close(); } };
+    const std::unique_ptr<std::ofstream, decltype(close)> ofs(
+        new std::ofstream(automorphismKeyLocation, std::ios::out | std::ios::binary), close);
+
+    if (ofs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::BINARY,
+                id);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::SerializeEvalAutomorphismKey(*ofs, lbcrypto::SerType::JSON,
+                id);
+        }
+    }
+    return false;
+}
+bool DeserializeEvalAutomorphismKeyFromFile(const std::string& automorphismKeyLocation,
+    const SerialMode serialMode)
+{
+    const auto close = [](std::ifstream* const ifs){ if (ifs->is_open()) { ifs->close(); } };
+    const std::unique_ptr<std::ifstream, decltype(close)> ifs(
+        new std::ifstream(automorphismKeyLocation, std::ios::in | std::ios::binary), close);
+
+    if (ifs->is_open())
+    {
+        if (serialMode == SerialMode::BINARY)
+        {
+            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
+                lbcrypto::SerType::BINARY);
+        }
+        if (serialMode == SerialMode::JSON)
+        {
+            return CryptoContextImpl::DeserializeEvalAutomorphismKey(*ifs,
+                lbcrypto::SerType::JSON);
+        }
+    }
+    return false;
+}
+bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
+    const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::SerializeToFile(publicKeyLocation,
+            publicKey.m_publicKey, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::SerializeToFile(publicKeyLocation,
+            publicKey.m_publicKey, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
+bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
+    PublicKeyDCRTPoly& publicKey, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(publicKeyLocation,
+            publicKey.m_publicKey, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(publicKeyLocation,
+            publicKey.m_publicKey, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
+bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
+    const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::SerializeToFile(ciphertextLocation,
+            ciphertext.m_ciphertext, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::SerializeToFile(ciphertextLocation,
+            ciphertext.m_ciphertext, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
+bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,
+    CiphertextDCRTPoly& ciphertext, const SerialMode serialMode)
+{
+    if (serialMode == SerialMode::BINARY)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(ciphertextLocation,
+            ciphertext.m_ciphertext, lbcrypto::SerType::BINARY);
+    }
+    if (serialMode == SerialMode::JSON)
+    {
+        return lbcrypto::Serial::DeserializeFromFile(ciphertextLocation,
+            ciphertext.m_ciphertext, lbcrypto::SerType::JSON);
+    }
+    return false;
+}
+
+} // openfhe

+ 44 - 0
src/SerialDeserial.h

@@ -0,0 +1,44 @@
+#pragma once
+
+#include "bits/stringfwd.h"
+
+#include "SerialMode.h" // SerialMode
+
+namespace openfhe
+{
+
+class CiphertextDCRTPoly;
+class CryptoContextDCRTPoly;
+class PublicKeyDCRTPoly;
+
+bool SerializeCryptoContextToFile(const std::string& ccLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
+bool DeserializeCryptoContextFromFile(const std::string& ccLocation,
+    CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
+bool SerializeEvalMultKeyToFile(const std::string& multKeyLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
+bool SerializeEvalMultKeyByIdToFile(const std::string& multKeyLocation,
+    const SerialMode serialMode, const std::string& id);
+bool DeserializeEvalMultKeyFromFile(const std::string& multKeyLocation,
+    const SerialMode serialMode);
+bool SerializeEvalSumKeyToFile(const std::string& sumKeyLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
+bool SerializeEvalSumKeyByIdToFile(const std::string& sumKeyLocation,
+    const SerialMode serialMode, const std::string& id);
+bool DeserializeEvalSumKeyFromFile(const std::string& sumKeyLocation, const SerialMode serialMode);
+bool SerializeEvalAutomorphismKeyToFile(const std::string& automorphismKeyLocation,
+    const CryptoContextDCRTPoly& cryptoContext, const SerialMode serialMode);
+bool SerializeEvalAutomorphismKeyByIdToFile(const std::string& automorphismKeyLocation,
+    const SerialMode serialMode, const std::string& id);
+bool DeserializeEvalAutomorphismKeyFromFile(const std::string& automorphismKeyLocation,
+    const SerialMode serialMode);
+bool SerializePublicKeyToFile(const std::string& publicKeyLocation,
+    const PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
+bool DeserializePublicKeyFromFile(const std::string& publicKeyLocation,
+    PublicKeyDCRTPoly& publicKey, const SerialMode serialMode);
+bool SerializeCiphertextToFile(const std::string& ciphertextLocation,
+    const CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
+bool DeserializeCiphertextFromFile(const std::string& ciphertextLocation,
+    CiphertextDCRTPoly& ciphertext, const SerialMode serialMode);
+
+} // openfhe

+ 12 - 0
src/SerialMode.h

@@ -0,0 +1,12 @@
+#pragma once
+
+namespace openfhe
+{
+
+enum SerialMode
+{
+    BINARY = 0,
+    JSON = 1,
+};
+
+} // openfhe

+ 46 - 26
src/lib.rs

@@ -16,6 +16,7 @@ pub mod ffi
         BFVRNS_SCHEME,
         BGVRNS_SCHEME,
     }
+
     #[repr(i32)]
     enum SecretKeyDist
     {
@@ -23,6 +24,7 @@ pub mod ffi
         UNIFORM_TERNARY = 1,
         SPARSE_TERNARY  = 2,
     }
+
     #[repr(i32)]
     enum ProxyReEncryptionMode
     {
@@ -32,6 +34,7 @@ pub mod ffi
         NOISE_FLOODING_HRA,
         DIVIDE_AND_ROUND_HRA,
     }
+
     #[repr(i32)]
     enum MultipartyMode
     {
@@ -39,18 +42,21 @@ pub mod ffi
         FIXED_NOISE_MULTIPARTY,
         NOISE_FLOODING_MULTIPARTY,
     }
+
     #[repr(i32)]
     enum ExecutionMode
     {
         EXEC_EVALUATION = 0,
         EXEC_NOISE_ESTIMATION,
     }
+
     #[repr(i32)]
     enum DecryptionNoiseMode
     {
         FIXED_NOISE_DECRYPT = 0,
         NOISE_FLOODING_DECRYPT,
     }
+
     #[repr(i32)]    
     enum KeySwitchTechnique
     {
@@ -58,6 +64,7 @@ pub mod ffi
         BV,
         HYBRID,
     }
+
     #[repr(i32)]
     enum ScalingTechnique
     {
@@ -68,6 +75,7 @@ pub mod ffi
         NORESCALE,
         INVALID_RS_TECHNIQUE,
     }
+
     #[repr(i32)]
     enum SecurityLevel
     {
@@ -79,12 +87,14 @@ pub mod ffi
         HEStd_256_quantum,
         HEStd_NotSet,
     }
+
     #[repr(i32)]
     enum EncryptionTechnique
     {
         STANDARD = 0,
         EXTENDED,
     }
+
     #[repr(i32)]
     enum MultiplicationTechnique
     {
@@ -93,12 +103,14 @@ pub mod ffi
         HPSPOVERQ,
         HPSPOVERQLEVELED,
     }
+
     #[repr(i32)]
     enum COMPRESSION_LEVEL
     {
         COMPACT = 2,
         SLACK   = 3,
     }
+
     #[repr(i32)]
     enum PKESchemeFeature
     {
@@ -111,6 +123,7 @@ pub mod ffi
         FHE          = 0x40,
         SCHEMESWITCH = 0x80,
     }
+
     #[repr(i32)]
     enum SerialMode
     {
@@ -118,36 +131,50 @@ pub mod ffi
         JSON = 1,
     }
 
+    struct ComplexPair
+    {
+        re: f64,
+        im: f64,
+    }
+
     unsafe extern "C++"
     {
-        include!("openfhe/src/bindings.hpp");
-        type SCHEME;
-        type Params;
-        type SecretKeyDist;
-        type ProxyReEncryptionMode;
-        type MultipartyMode;
-        type ExecutionMode;
+        include!("openfhe/src/Ciphertext.h");
+        include!("openfhe/src/CryptoContext.h");
+        include!("openfhe/src/KeyPair.h");
+        include!("openfhe/src/Params.h");
+        include!("openfhe/src/Plaintext.h");
+        include!("openfhe/src/PublicKey.h");
+        include!("openfhe/src/SerialDeserial.h");
+
+        type COMPRESSION_LEVEL;
         type DecryptionNoiseMode;
+        type EncryptionTechnique;
+        type ExecutionMode;
         type KeySwitchTechnique;
+        type MultipartyMode;
+        type MultiplicationTechnique;
+        type PKESchemeFeature;
+        type ProxyReEncryptionMode;
         type ScalingTechnique;
+        type SCHEME;
+        type SecretKeyDist;
         type SecurityLevel;
-        type EncryptionTechnique;
-        type MultiplicationTechnique;
-        type COMPRESSION_LEVEL;
+        type SerialMode;
+
+        type CiphertextDCRTPoly;
+        type CryptoContextDCRTPoly;
+        type DCRTPolyParams;
+        type DecryptResult;
+        type KeyPairDCRTPoly;
+        type Params;
         type ParamsBFVRNS;
         type ParamsBGVRNS;
         type ParamsCKKSRNS;
-        type CryptoContextDCRTPoly;
-        type PKESchemeFeature;
-        type KeyPairDCRTPoly;
-        type PrivateKeyImpl;
-        type PublicKeyImpl;
         type Plaintext;
-        type CiphertextDCRTPoly;
-        type DecryptResult;
-        type DCRTPolyParams;
-        type SerialMode;
+        type PrivateKeyImpl;
         type PublicKeyDCRTPoly;
+        type PublicKeyImpl;
     }
 
     // Params
@@ -469,13 +496,6 @@ pub mod ffi
         fn GenDefaultConstructedCiphertext() -> UniquePtr<CiphertextDCRTPoly>;
     }
 
-    // ComplexPair
-    struct ComplexPair
-    {
-        re: f64,
-        im: f64,
-    }
-
     // CryptoContextDCRTPoly
     unsafe extern "C++"
     {