| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- #include "SerialDeserial.h"
- #include "openfhe/pke/cryptocontext-ser.h"
- #include "Ciphertext.h"
- #include "CryptoContext.h"
- #include "PublicKey.h"
- #include "PrivateKey.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 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)
- {
- 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
|