From 733a4dda7cf3cf8851c8f4de9b2062e83b2d0571 Mon Sep 17 00:00:00 2001 From: Dmitriy Suponitskiy Date: Fri, 4 Apr 2025 14:08:47 -0400 Subject: [PATCH 1/7] Made Serialize/Deserialize functions available for EvalKeyMap --- src/lib/pke/serialization.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/pke/serialization.cpp b/src/lib/pke/serialization.cpp index 58d1384..676dbe2 100644 --- a/src/lib/pke/serialization.cpp +++ b/src/lib/pke/serialization.cpp @@ -310,6 +310,10 @@ void bind_serialization(pybind11::module &m) { py::arg("str"), py::arg("sertype")); m.def("Serialize", &SerializeToBytesWrapper, SerType::SERBINARY>, py::arg("obj"), py::arg("sertype")); + m.def("Serialize", &SerializeToBytesWrapper>, SerType::SERBINARY>, + py::arg("obj"), py::arg("sertype")); + m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>, SerType::SERBINARY>, + py::arg("str"), py::arg("sertype")); m.def("DeserializeEvalKeyString", &DeserializeFromBytesWrapper, SerType::SERBINARY>, py::arg("str"), py::arg("sertype")); m.def("SerializeEvalMultKeyString", &SerializeEvalMultKeyToBytesWrapper, From e1bf413217ddcdc8f2d7e5d2bfdb4845a3611a32 Mon Sep 17 00:00:00 2001 From: Dmitriy Suponitskiy Date: Sun, 6 Apr 2025 18:02:22 -0400 Subject: [PATCH 2/7] Additional pair of overloaded Serialize/Deserialize functions --- src/lib/pke/serialization.cpp | 56 +++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/lib/pke/serialization.cpp b/src/lib/pke/serialization.cpp index 676dbe2..25feeb5 100644 --- a/src/lib/pke/serialization.cpp +++ b/src/lib/pke/serialization.cpp @@ -108,6 +108,12 @@ T DeserializeFromStringWrapper(const std::string& str, const ST& sertype) { return obj; } +template +py::bytes SerializeEvalKeyMapSharedPtr( + const std::shared_ptr>>> &obj, const ST& sertype) { + return SerializeToBytesWrapper(*obj, sertype); +} + template CryptoContext DeserializeCCFromStringWrapper(const std::string& str, const ST& sertype) { CryptoContext obj; @@ -117,12 +123,34 @@ CryptoContext DeserializeCCFromStringWrapper(const std::string& str, c } template -T DeserializeFromBytesWrapper(const py::bytes& bytes, const ST& sertype) { - T obj; - std::string str(bytes); - std::istringstream iss(str, std::ios::binary); - Serial::Deserialize(obj, iss, sertype); - return obj; +T DeserializeFromBytesWrapper(const py::bytes &bytes, const ST &sertype) { + static_assert(!std::is_same>>::value, + "Avoid nested shared_ptr!"); + T obj; + std::string str(bytes); + std::istringstream iss(str, std::ios::binary); + Serial::Deserialize(obj, iss, sertype); + return obj; +} + +// template +// std::shared_ptr>>> DeserializeEvalKeyMapSharedPtr(const py::bytes& bytes, const ST& sertype) { +// return std::make_shared>>>( +// DeserializeFromBytesWrapper>>>(bytes, sertype)); +// } + +template +std::shared_ptr>>> +DeserializeEvalKeyMapSharedPtr(const py::bytes& bytes, const ST& sertype) { + auto map = DeserializeFromBytesWrapper>>>(bytes, sertype); + + // ✅ Check for nested shared_ptr values + for (const auto& [k, v] : map) { + static_assert(!std::is_same>>>::value, + "Detected nested shared_ptr in map values!"); + } + + return std::make_shared>>>(std::move(map)); } template @@ -314,6 +342,22 @@ void bind_serialization(pybind11::module &m) { py::arg("obj"), py::arg("sertype")); m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>, SerType::SERBINARY>, py::arg("str"), py::arg("sertype")); + m.def("Serialize", + [](const std::shared_ptr>>>& obj, const SerType::SERBINARY& sertype) { + return SerializeEvalKeyMapSharedPtr(obj, sertype); + }, + py::arg("obj"), py::arg("sertype")); +// m.def("DeserializeEvalKeyMapString", +// [](const py::bytes& s, const SerType::SERBINARY& sertype) { +// auto map = DeserializeFromBytesWrapper>>>(s, sertype); +// return std::make_shared>>>(std::move(map)); +// }, +// py::arg("str"), py::arg("sertype")); + m.def("DeserializeEvalKeyMapString", + [](const py::bytes &s) { + return DeserializeEvalKeyMapSharedPtr(s, SerType::SERBINARY); + }, + py::arg("str")); m.def("DeserializeEvalKeyString", &DeserializeFromBytesWrapper, SerType::SERBINARY>, py::arg("str"), py::arg("sertype")); m.def("SerializeEvalMultKeyString", &SerializeEvalMultKeyToBytesWrapper, From 260028a1c10c5b3e52273e909868348e949b2c9c Mon Sep 17 00:00:00 2001 From: Dmitriy Suponitskiy Date: Mon, 7 Apr 2025 12:56:34 -0400 Subject: [PATCH 3/7] Revert "Additional pair of overloaded Serialize/Deserialize functions" This reverts commit e1bf413217ddcdc8f2d7e5d2bfdb4845a3611a32. --- src/lib/bindings.cpp | 5 ++- src/lib/pke/serialization.cpp | 60 +++++------------------------------ 2 files changed, 12 insertions(+), 53 deletions(-) diff --git a/src/lib/bindings.cpp b/src/lib/bindings.cpp index b12f5bf..b3a9a20 100644 --- a/src/lib/bindings.cpp +++ b/src/lib/bindings.cpp @@ -762,7 +762,10 @@ void bind_crypto_context(py::module &m) cc_GetEvalAutomorphismKeyMap_docs, py::arg("keyId") = "") .def("GetEvalSumKeyMap", &GetEvalSumKeyMapWrapper, - cc_GetEvalSumKeyMap_docs) + cc_GetEvalSumKeyMap_docs, + py::arg("keyId") = "") + // .def_static("GetEvalSumKeyMap", &CryptoContextImpl::GetEvalSumKeyMap, + // cc_GetEvalSumKeyMap_docs) .def("GetBinCCForSchemeSwitch", &CryptoContextImpl::GetBinCCForSchemeSwitch) .def_static( "SerializeEvalMultKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "") diff --git a/src/lib/pke/serialization.cpp b/src/lib/pke/serialization.cpp index 25feeb5..35d2cff 100644 --- a/src/lib/pke/serialization.cpp +++ b/src/lib/pke/serialization.cpp @@ -108,12 +108,6 @@ T DeserializeFromStringWrapper(const std::string& str, const ST& sertype) { return obj; } -template -py::bytes SerializeEvalKeyMapSharedPtr( - const std::shared_ptr>>> &obj, const ST& sertype) { - return SerializeToBytesWrapper(*obj, sertype); -} - template CryptoContext DeserializeCCFromStringWrapper(const std::string& str, const ST& sertype) { CryptoContext obj; @@ -123,34 +117,12 @@ CryptoContext DeserializeCCFromStringWrapper(const std::string& str, c } template -T DeserializeFromBytesWrapper(const py::bytes &bytes, const ST &sertype) { - static_assert(!std::is_same>>::value, - "Avoid nested shared_ptr!"); - T obj; - std::string str(bytes); - std::istringstream iss(str, std::ios::binary); - Serial::Deserialize(obj, iss, sertype); - return obj; -} - -// template -// std::shared_ptr>>> DeserializeEvalKeyMapSharedPtr(const py::bytes& bytes, const ST& sertype) { -// return std::make_shared>>>( -// DeserializeFromBytesWrapper>>>(bytes, sertype)); -// } - -template -std::shared_ptr>>> -DeserializeEvalKeyMapSharedPtr(const py::bytes& bytes, const ST& sertype) { - auto map = DeserializeFromBytesWrapper>>>(bytes, sertype); - - // ✅ Check for nested shared_ptr values - for (const auto& [k, v] : map) { - static_assert(!std::is_same>>>::value, - "Detected nested shared_ptr in map values!"); - } - - return std::make_shared>>>(std::move(map)); +T DeserializeFromBytesWrapper(const py::bytes& bytes, const ST& sertype) { + T obj; + std::string str(bytes); + std::istringstream iss(str, std::ios::binary); + Serial::Deserialize(obj, iss, sertype); + return obj; } template @@ -338,26 +310,10 @@ void bind_serialization(pybind11::module &m) { py::arg("str"), py::arg("sertype")); m.def("Serialize", &SerializeToBytesWrapper, SerType::SERBINARY>, py::arg("obj"), py::arg("sertype")); - m.def("Serialize", &SerializeToBytesWrapper>, SerType::SERBINARY>, + m.def("Serialize", &SerializeToBytesWrapper>>, SerType::SERBINARY>, py::arg("obj"), py::arg("sertype")); - m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>, SerType::SERBINARY>, + m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>>, SerType::SERBINARY>, py::arg("str"), py::arg("sertype")); - m.def("Serialize", - [](const std::shared_ptr>>>& obj, const SerType::SERBINARY& sertype) { - return SerializeEvalKeyMapSharedPtr(obj, sertype); - }, - py::arg("obj"), py::arg("sertype")); -// m.def("DeserializeEvalKeyMapString", -// [](const py::bytes& s, const SerType::SERBINARY& sertype) { -// auto map = DeserializeFromBytesWrapper>>>(s, sertype); -// return std::make_shared>>>(std::move(map)); -// }, -// py::arg("str"), py::arg("sertype")); - m.def("DeserializeEvalKeyMapString", - [](const py::bytes &s) { - return DeserializeEvalKeyMapSharedPtr(s, SerType::SERBINARY); - }, - py::arg("str")); m.def("DeserializeEvalKeyString", &DeserializeFromBytesWrapper, SerType::SERBINARY>, py::arg("str"), py::arg("sertype")); m.def("SerializeEvalMultKeyString", &SerializeEvalMultKeyToBytesWrapper, From e5279ffdd9df36c0eabf1bb9d341c9467987329a Mon Sep 17 00:00:00 2001 From: Yuriy Polyakov Date: Mon, 7 Apr 2025 19:48:08 -0400 Subject: [PATCH 4/7] compilation works --- examples/pke/test-threshold-2p.py | 63 +++++++++++++++++++++++++++++++ src/lib/pke/serialization.cpp | 14 +++++-- 2 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 examples/pke/test-threshold-2p.py diff --git a/examples/pke/test-threshold-2p.py b/examples/pke/test-threshold-2p.py new file mode 100644 index 0000000..f07ef8a --- /dev/null +++ b/examples/pke/test-threshold-2p.py @@ -0,0 +1,63 @@ +from openfhe import * + + +def main(): + mult_depth = 1 + scale_mod_size = 50 + batch_size = 16 + sertype = BINARY + + parameters = CCParamsCKKSRNS() + parameters.SetMultiplicativeDepth(mult_depth) + parameters.SetScalingModSize(scale_mod_size) + parameters.SetBatchSize(batch_size) + + cc = GenCryptoContext(parameters) + cc.Enable(PKE) + cc.Enable(KEYSWITCH) + cc.Enable(LEVELEDSHE) + cc.Enable(ADVANCEDSHE) + cc.Enable(MULTIPARTY) + + + ''' + Say we have 2 parties: p1 (lead) and p2. + IMPORTANT: Any communication between the 2 has to be serialized. + ''' + + '''We set the same CryptoContext''' + # Party 1 + cc_p1 = cc + cc_to_share = Serialize(cc_p1, sertype) # Send cc_to_share to Party 2 + + # Party 2 + cc_p2 = DeserializeCryptoContextString(cc_to_share, sertype) # Received cc_to_share from Party 1 + + + + + '''Generating keys for each party''' + # Party 1 + keys_p1 = cc_p1.KeyGen() + pk_to_share = Serialize(keys_p1.publicKey, sertype) # Send pk_to_share to Party 2 + + # Party 2 + pk_received = DeserializePublicKeyString(pk_to_share, sertype) # Received pk_to_share from Party 1 + # keys_p2 = cc_p2.MultipartyKeyGen(pk_received, False, True) + + # mp_public_key = cc_p1.MultiAddPubKeys(keys_p1.publicKey, keys_p2.publicKey, keys_p2.publicKey.GetKeyTag()) + + + + '''EvalSumKeys''' + # Party 1 + cc_p1.EvalSumKeyGen(keys_p1.secretKey) + evalSumKeys_p1 = cc_p1.GetEvalSumKeyMap(keys_p1.secretKey.GetKeyTag()) + evalSumKeys_to_share = Serialize(evalSumKeys_p1, sertype) # Send evalSumKeys_to_share to Party 2 + + # # Party 2 + evalSumKeysB = cc_p2.MultiEvalSumKeyGen(keys_p2.secretKey, DeserializeEvalKeyString(evalSumKeys_to_share, sertype), keys_p2.publicKey.GetKeyTag()) + + +if __name__ == "__main__": + main() diff --git a/src/lib/pke/serialization.cpp b/src/lib/pke/serialization.cpp index 35d2cff..904a685 100644 --- a/src/lib/pke/serialization.cpp +++ b/src/lib/pke/serialization.cpp @@ -216,6 +216,8 @@ void DeserializeEvalAutomorphismKeyFromBytesWrapper(const std::string& data, con } } +PYBIND11_MAKE_OPAQUE(std::map>); + void bind_serialization(pybind11::module &m) { // Json Serialization m.def("SerializeToFile", static_cast &, const SerType::SERJSON &)>(&Serial::SerializeToFile), @@ -260,6 +262,10 @@ void bind_serialization(pybind11::module &m) { py::arg("obj"), py::arg("sertype")); m.def("DeserializeEvalKeyString", &DeserializeFromStringWrapper, SerType::SERJSON>, py::arg("str"), py::arg("sertype")); + m.def("Serialize", &SerializeToBytesWrapper>>, SerType::SERJSON>, + py::arg("obj"), py::arg("sertype")); + m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>>, SerType::SERJSON>, + py::arg("str"), py::arg("sertype")); m.def("SerializeEvalMultKeyString", &SerializeEvalMultKeyToStringWrapper, py::arg("sertype"), py::arg("id") = ""); m.def("DeserializeEvalMultKeyString", &DeserializeEvalMultKeyFromStringWrapper, @@ -310,12 +316,12 @@ void bind_serialization(pybind11::module &m) { py::arg("str"), py::arg("sertype")); m.def("Serialize", &SerializeToBytesWrapper, SerType::SERBINARY>, py::arg("obj"), py::arg("sertype")); - m.def("Serialize", &SerializeToBytesWrapper>>, SerType::SERBINARY>, - py::arg("obj"), py::arg("sertype")); - m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>>, SerType::SERBINARY>, - py::arg("str"), py::arg("sertype")); m.def("DeserializeEvalKeyString", &DeserializeFromBytesWrapper, SerType::SERBINARY>, py::arg("str"), py::arg("sertype")); + m.def("Serialize", &SerializeToBytesWrapper>>, SerType::SERBINARY>, + py::arg("obj"), py::arg("sertype")); + m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>>, SerType::SERBINARY>, + py::arg("str"), py::arg("sertype")); m.def("SerializeEvalMultKeyString", &SerializeEvalMultKeyToBytesWrapper, py::arg("sertype"), py::arg("id") = ""); m.def("DeserializeEvalMultKeyString", &DeserializeEvalMultKeyFromBytesWrapper, From 9896f101623f0e113730b56221fa72916ea33096 Mon Sep 17 00:00:00 2001 From: Yuriy Polyakov Date: Mon, 7 Apr 2025 20:17:14 -0400 Subject: [PATCH 5/7] updated the example --- examples/pke/test-threshold-2p.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/pke/test-threshold-2p.py b/examples/pke/test-threshold-2p.py index f07ef8a..5cc4319 100644 --- a/examples/pke/test-threshold-2p.py +++ b/examples/pke/test-threshold-2p.py @@ -43,9 +43,9 @@ def main(): # Party 2 pk_received = DeserializePublicKeyString(pk_to_share, sertype) # Received pk_to_share from Party 1 - # keys_p2 = cc_p2.MultipartyKeyGen(pk_received, False, True) + keys_p2 = cc_p2.MultipartyKeyGen(pk_received, False, True) - # mp_public_key = cc_p1.MultiAddPubKeys(keys_p1.publicKey, keys_p2.publicKey, keys_p2.publicKey.GetKeyTag()) + mp_public_key = cc_p1.MultiAddPubKeys(keys_p1.publicKey, keys_p2.publicKey, keys_p2.publicKey.GetKeyTag()) @@ -54,9 +54,10 @@ def main(): cc_p1.EvalSumKeyGen(keys_p1.secretKey) evalSumKeys_p1 = cc_p1.GetEvalSumKeyMap(keys_p1.secretKey.GetKeyTag()) evalSumKeys_to_share = Serialize(evalSumKeys_p1, sertype) # Send evalSumKeys_to_share to Party 2 + testkeys = DeserializeEvalKeyMapString(evalSumKeys_to_share, sertype) # # Party 2 - evalSumKeysB = cc_p2.MultiEvalSumKeyGen(keys_p2.secretKey, DeserializeEvalKeyString(evalSumKeys_to_share, sertype), keys_p2.publicKey.GetKeyTag()) + evalSumKeysB = cc_p2.MultiEvalSumKeyGen(keys_p2.secretKey, DeserializeEvalKeyMapString(evalSumKeys_to_share, sertype), keys_p2.publicKey.GetKeyTag()) if __name__ == "__main__": From dc3b513df79c89468d75e5957926f90927d69fb2 Mon Sep 17 00:00:00 2001 From: Dmitriy Suponitskiy Date: Mon, 7 Apr 2025 20:30:17 -0400 Subject: [PATCH 6/7] Removed commented code --- src/lib/bindings.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib/bindings.cpp b/src/lib/bindings.cpp index b3a9a20..b12f5bf 100644 --- a/src/lib/bindings.cpp +++ b/src/lib/bindings.cpp @@ -762,10 +762,7 @@ void bind_crypto_context(py::module &m) cc_GetEvalAutomorphismKeyMap_docs, py::arg("keyId") = "") .def("GetEvalSumKeyMap", &GetEvalSumKeyMapWrapper, - cc_GetEvalSumKeyMap_docs, - py::arg("keyId") = "") - // .def_static("GetEvalSumKeyMap", &CryptoContextImpl::GetEvalSumKeyMap, - // cc_GetEvalSumKeyMap_docs) + cc_GetEvalSumKeyMap_docs) .def("GetBinCCForSchemeSwitch", &CryptoContextImpl::GetBinCCForSchemeSwitch) .def_static( "SerializeEvalMultKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "") From d6f6bcd2bc9ce505948d9a86219b4d093d5c7644 Mon Sep 17 00:00:00 2001 From: Dmitriy Suponitskiy Date: Tue, 8 Apr 2025 15:35:41 -0400 Subject: [PATCH 7/7] Added SerializeToFile() and organized header files --- examples/pke/test-threshold-2p.py | 64 ------------------------- src/include/bindings.h | 26 +++++----- src/include/pke/cryptocontext_wrapper.h | 36 ++++++-------- src/include/pke/serialization.h | 37 +++++++------- src/lib/bindings.cpp | 57 +++++++++++++++++----- src/lib/binfhe_bindings.cpp | 52 ++++++++++---------- src/lib/pke/cryptocontext_wrapper.cpp | 29 +++++------ src/lib/pke/serialization.cpp | 45 +++++++++++------ 8 files changed, 160 insertions(+), 186 deletions(-) delete mode 100644 examples/pke/test-threshold-2p.py diff --git a/examples/pke/test-threshold-2p.py b/examples/pke/test-threshold-2p.py deleted file mode 100644 index 5cc4319..0000000 --- a/examples/pke/test-threshold-2p.py +++ /dev/null @@ -1,64 +0,0 @@ -from openfhe import * - - -def main(): - mult_depth = 1 - scale_mod_size = 50 - batch_size = 16 - sertype = BINARY - - parameters = CCParamsCKKSRNS() - parameters.SetMultiplicativeDepth(mult_depth) - parameters.SetScalingModSize(scale_mod_size) - parameters.SetBatchSize(batch_size) - - cc = GenCryptoContext(parameters) - cc.Enable(PKE) - cc.Enable(KEYSWITCH) - cc.Enable(LEVELEDSHE) - cc.Enable(ADVANCEDSHE) - cc.Enable(MULTIPARTY) - - - ''' - Say we have 2 parties: p1 (lead) and p2. - IMPORTANT: Any communication between the 2 has to be serialized. - ''' - - '''We set the same CryptoContext''' - # Party 1 - cc_p1 = cc - cc_to_share = Serialize(cc_p1, sertype) # Send cc_to_share to Party 2 - - # Party 2 - cc_p2 = DeserializeCryptoContextString(cc_to_share, sertype) # Received cc_to_share from Party 1 - - - - - '''Generating keys for each party''' - # Party 1 - keys_p1 = cc_p1.KeyGen() - pk_to_share = Serialize(keys_p1.publicKey, sertype) # Send pk_to_share to Party 2 - - # Party 2 - pk_received = DeserializePublicKeyString(pk_to_share, sertype) # Received pk_to_share from Party 1 - keys_p2 = cc_p2.MultipartyKeyGen(pk_received, False, True) - - mp_public_key = cc_p1.MultiAddPubKeys(keys_p1.publicKey, keys_p2.publicKey, keys_p2.publicKey.GetKeyTag()) - - - - '''EvalSumKeys''' - # Party 1 - cc_p1.EvalSumKeyGen(keys_p1.secretKey) - evalSumKeys_p1 = cc_p1.GetEvalSumKeyMap(keys_p1.secretKey.GetKeyTag()) - evalSumKeys_to_share = Serialize(evalSumKeys_p1, sertype) # Send evalSumKeys_to_share to Party 2 - testkeys = DeserializeEvalKeyMapString(evalSumKeys_to_share, sertype) - - # # Party 2 - evalSumKeysB = cc_p2.MultiEvalSumKeyGen(keys_p2.secretKey, DeserializeEvalKeyMapString(evalSumKeys_to_share, sertype), keys_p2.publicKey.GetKeyTag()) - - -if __name__ == "__main__": - main() diff --git a/src/include/bindings.h b/src/include/bindings.h index 01370b9..b3e19a0 100644 --- a/src/include/bindings.h +++ b/src/include/bindings.h @@ -1,19 +1,22 @@ +//================================================================================== // BSD 2-Clause License - -// Copyright (c) 2023, OpenFHE - +// +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors +// // All rights reserved. - +// +// Author TPOC: contact@openfhe.org +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: - +// // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. - +// // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. - +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -24,9 +27,9 @@ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#ifndef OPENFHE_BINDINGS_H -#define OPENFHE_BINDINGS_H +//================================================================================== +#ifndef __BINDINGS_H__ +#define __BINDINGS_H__ #include @@ -39,4 +42,5 @@ void bind_ciphertext(pybind11::module &m); void bind_serialization(pybind11::module &m); void bind_schemes(pybind11::module &m); void bind_sch_swch_params(pybind11::module &m); -#endif // OPENFHE_BINDINGS_H + +#endif // __BINDINGS_H__ diff --git a/src/include/pke/cryptocontext_wrapper.h b/src/include/pke/cryptocontext_wrapper.h index 413110c..7318d20 100644 --- a/src/include/pke/cryptocontext_wrapper.h +++ b/src/include/pke/cryptocontext_wrapper.h @@ -1,19 +1,22 @@ +//================================================================================== // BSD 2-Clause License - -// Copyright (c) 2023, OpenFHE - +// +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors +// // All rights reserved. - +// +// Author TPOC: contact@openfhe.org +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: - +// // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. - +// // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. - +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -24,23 +27,14 @@ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//================================================================================== +#ifndef __CRYPTOCONTEXT_WRAPPER_H__ +#define __CRYPTOCONTEXT_WRAPPER_H__ -#ifndef OPENFHE_CRYPTOCONTEXT_BINDINGS_H -#define OPENFHE_CRYPTOCONTEXT_BINDINGS_H - -#include "bindings.h" #include "openfhe.h" -#include -#include -#include -#include -#include - - -namespace py = pybind11; using namespace lbcrypto; -using ParmType = typename DCRTPoly::Params; + Ciphertext EvalFastRotationPrecomputeWrapper(CryptoContext &self, ConstCiphertext ciphertext); @@ -69,4 +63,4 @@ const usint GetDigitSizeWrapper(CryptoContext& self); void ClearEvalMultKeysWrapper(); -#endif // OPENFHE_CRYPTOCONTEXT_BINDINGS_H +#endif // __CRYPTOCONTEXT_WRAPPER_H__ diff --git a/src/include/pke/serialization.h b/src/include/pke/serialization.h index 648edba..9515881 100644 --- a/src/include/pke/serialization.h +++ b/src/include/pke/serialization.h @@ -1,19 +1,22 @@ +//================================================================================== // BSD 2-Clause License - -// Copyright (c) 2023, OpenFHE - +// +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors +// // All rights reserved. - +// +// Author TPOC: contact@openfhe.org +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: - +// // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. - +// // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. - +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -24,16 +27,12 @@ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -#ifndef OPENFHE_SERIALIZATION_BINDINGS_H -#define OPENFHE_SERIALIZATION_BINDINGS_H +//================================================================================== +#ifndef __SERIALIZATION_H__ +#define __SERIALIZATION_H__ #include -#include "openfhe.h" -#include "bindings.h" -using namespace lbcrypto; -namespace py = pybind11; template bool SerializeEvalMultKeyWrapper(const std::string& filename, const ST& sertype, std::string id); @@ -48,25 +47,25 @@ template std::string SerializeToStringWrapper(const T& obj, const ST& sertype); template -py::bytes SerializeToBytesWrapper(const T& obj, const ST& sertype); +pybind11::bytes SerializeToBytesWrapper(const T& obj, const ST& sertype); template T DeserializeFromStringWrapper(const std::string& str, const ST& sertype); template -T DeserializeFromBytesWrapper(const py::bytes& bytes, const ST& sertype); +T DeserializeFromBytesWrapper(const pybind11::bytes& bytes, const ST& sertype); template std::string SerializeEvalMultKeyToStringWrapper(const ST& sertype, const std::string& id); template -py::bytes SerializeEvalMultKeyToBytesWrapper(const ST& sertype, const std::string& id); +pybind11::bytes SerializeEvalMultKeyToBytesWrapper(const ST& sertype, const std::string& id); template std::string SerializeEvalAutomorphismKeyToStringWrapper(const ST& sertype, const std::string& id); template -py::bytes SerializeEvalAutomorphismKeyToBytesWrapper(const ST& sertype, const std::string& id); +pybind11::bytes SerializeEvalAutomorphismKeyToBytesWrapper(const ST& sertype, const std::string& id); template void DeserializeEvalMultKeyFromStringWrapper(const std::string& data, const ST& sertype); @@ -80,4 +79,4 @@ void DeserializeEvalAutomorphismKeyFromStringWrapper(const std::string& data, co template void DeserializeEvalAutomorphismKeyFromBytesWrapper(const std::string& data, const ST& sertype); -#endif // OPENFHE_SERIALIZATION_BINDINGS_H \ No newline at end of file +#endif // __SERIALIZATION_H__ \ No newline at end of file diff --git a/src/lib/bindings.cpp b/src/lib/bindings.cpp index b12f5bf..6725a84 100644 --- a/src/lib/bindings.cpp +++ b/src/lib/bindings.cpp @@ -1,25 +1,57 @@ -#include +//================================================================================== +// BSD 2-Clause License +// +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors +// +// All rights reserved. +// +// Author TPOC: contact@openfhe.org +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this +// list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//================================================================================== +#include "bindings.h" + #include #include #include #include #include #include -#include -#include + #include "openfhe.h" + #include "key/key-ser.h" -#include "bindings.h" -#include "cryptocontext_wrapper.h" #include "binfhe_bindings.h" + +#include "cryptocontext_wrapper.h" #include "cryptocontext_docs.h" #include "cryptoparameters_docs.h" #include "plaintext_docs.h" #include "ciphertext_docs.h" -#include "serialization.h" using namespace lbcrypto; namespace py = pybind11; + +// disable the PYBIND11 template-based conversion for this type PYBIND11_MAKE_OPAQUE(std::map>); template @@ -100,6 +132,12 @@ void bind_parameters(py::module &m,const std::string name) void bind_crypto_context(py::module &m) { + //Parameters Type + /*TODO (Oliveira): If we expose Poly's and ParmType, this block will go somewhere else */ + using ParmType = typename DCRTPoly::Params; + using ParmTypePtr = std::shared_ptr; + py::class_(m, "ParmType"); + py::class_, std::shared_ptr>>(m, "CryptoContext") .def(py::init<>()) .def("GetKeyGenLevel", &CryptoContextImpl::GetKeyGenLevel, cc_GetKeyGenLevel_docs) @@ -1028,15 +1066,8 @@ void bind_enums_and_constants(py::module &m) m.attr("HEStd_256_classic") = py::cast(SecurityLevel::HEStd_256_classic); m.attr("HEStd_NotSet") = py::cast(SecurityLevel::HEStd_NotSet); - //Parameters Type - /*TODO (Oliveira): If we expose Poly's and ParmType, this block will go somewhere else */ - using ParmType = typename DCRTPoly::Params; - py::class_>(m, "ParmType"); - //NATIVEINT function m.def("get_native_int", &get_native_int); - - } void bind_keys(py::module &m) diff --git a/src/lib/binfhe_bindings.cpp b/src/lib/binfhe_bindings.cpp index 9f32304..e957799 100644 --- a/src/lib/binfhe_bindings.cpp +++ b/src/lib/binfhe_bindings.cpp @@ -1,43 +1,45 @@ +//================================================================================== // BSD 2-Clause License - -// Copyright (c) 2023, OpenFHE - +// +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors +// // All rights reserved. - +// +// Author TPOC: contact@openfhe.org +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: - -// 1. Redistributions of source code must retain the above copyright notice, -// this +// +// 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. - +// // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. - +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -// POSSIBILITY OF SUCH DAMAGE. - +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//================================================================================== #include "binfhe_bindings.h" + +#include + +#include "openfhe.h" #include "binfhecontext.h" #include "binfhecontext_docs.h" #include "binfhecontext_wrapper.h" -#include "openfhe.h" + #include "cereal/archives/binary.hpp" -#include "cereal/archives/portable_binary.hpp" -#include "core/utils/serial.h" -#include -#include -#include +// #include "cereal/archives/portable_binary.hpp" +// #include "core/utils/serial.h" using namespace lbcrypto; namespace py = pybind11; diff --git a/src/lib/pke/cryptocontext_wrapper.cpp b/src/lib/pke/cryptocontext_wrapper.cpp index 3d35b37..f8ed8f2 100644 --- a/src/lib/pke/cryptocontext_wrapper.cpp +++ b/src/lib/pke/cryptocontext_wrapper.cpp @@ -1,19 +1,22 @@ +//================================================================================== // BSD 2-Clause License - -// Copyright (c) 2023, OpenFHE - +// +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors +// // All rights reserved. - +// +// Author TPOC: contact@openfhe.org +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: - +// // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. - +// // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. - +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -24,19 +27,9 @@ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - +//================================================================================== #include "cryptocontext_wrapper.h" -#include - -#include -#include -#include -#include -#include -#include -using namespace lbcrypto; -namespace py = pybind11; Ciphertext EvalFastRotationPrecomputeWrapper(CryptoContext &self,ConstCiphertext ciphertext) { std::shared_ptr> precomp = self->EvalFastRotationPrecompute(ciphertext); diff --git a/src/lib/pke/serialization.cpp b/src/lib/pke/serialization.cpp index 904a685..36d4a1f 100644 --- a/src/lib/pke/serialization.cpp +++ b/src/lib/pke/serialization.cpp @@ -1,19 +1,22 @@ +//================================================================================== // BSD 2-Clause License - -// Copyright (c) 2023, OpenFHE - +// +// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors +// // All rights reserved. - +// +// Author TPOC: contact@openfhe.org +// // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: - +// // 1. Redistributions of source code must retain the above copyright notice, this // list of conditions and the following disclaimer. - +// // 2. Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. - +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE @@ -24,14 +27,14 @@ // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +//================================================================================== +#include "serialization.h" -#include #include +// #include - not needed for now + #include "openfhe.h" -#include "bindings.h" -#include "utils/exception.h" // header files needed for serialization -#include "serialization.h" #include "metadata-ser.h" #include "ciphertext-ser.h" #include "cryptocontext-ser.h" @@ -43,6 +46,10 @@ using namespace lbcrypto; namespace py = pybind11; +// disable the PYBIND11 template-based conversion for this type +PYBIND11_MAKE_OPAQUE(std::map>); + + template bool SerializeEvalMultKeyWrapper(const std::string &filename, const ST &sertype, std::string id) { @@ -216,8 +223,6 @@ void DeserializeEvalAutomorphismKeyFromBytesWrapper(const std::string& data, con } } -PYBIND11_MAKE_OPAQUE(std::map>); - void bind_serialization(pybind11::module &m) { // Json Serialization m.def("SerializeToFile", static_cast &, const SerType::SERJSON &)>(&Serial::SerializeToFile), @@ -240,6 +245,10 @@ void bind_serialization(pybind11::module &m) { py::arg("filename"), py::arg("obj"), py::arg("sertype")); m.def("DeserializeEvalKey", static_cast,bool> (*)(const std::string&, const SerType::SERJSON&)>(&DeserializeFromFileWrapper, SerType::SERJSON>), py::arg("filename"), py::arg("sertype")); + m.def("SerializeToFile", static_cast>>&, const SerType::SERJSON&)>(&Serial::SerializeToFile>>>), + py::arg("filename"), py::arg("obj"), py::arg("sertype")); + m.def("DeserializeEvalKeyMap", static_cast>>, bool> (*)(const std::string&, const SerType::SERJSON&)>(&DeserializeFromFileWrapper>>, SerType::SERJSON>), + py::arg("filename"), py::arg("sertype")); // JSON Serialization to string m.def("Serialize", &SerializeToStringWrapper, SerType::SERJSON>, @@ -265,7 +274,8 @@ void bind_serialization(pybind11::module &m) { m.def("Serialize", &SerializeToBytesWrapper>>, SerType::SERJSON>, py::arg("obj"), py::arg("sertype")); m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>>, SerType::SERJSON>, - py::arg("str"), py::arg("sertype")); + py::arg("str"), py::arg("sertype")); + m.def("SerializeEvalMultKeyString", &SerializeEvalMultKeyToStringWrapper, py::arg("sertype"), py::arg("id") = ""); m.def("DeserializeEvalMultKeyString", &DeserializeEvalMultKeyFromStringWrapper, @@ -296,6 +306,10 @@ void bind_serialization(pybind11::module &m) { py::arg("filename"), py::arg("obj"), py::arg("sertype")); m.def("DeserializeEvalKey", static_cast,bool> (*)(const std::string&, const SerType::SERBINARY&)>(&DeserializeFromFileWrapper, SerType::SERBINARY>), py::arg("filename"), py::arg("sertype")); + m.def("SerializeToFile", static_cast>>&, const SerType::SERBINARY&)>(&Serial::SerializeToFile>>>), + py::arg("filename"), py::arg("obj"), py::arg("sertype")); + m.def("DeserializeEvalKeyMap", static_cast>>, bool> (*)(const std::string&, const SerType::SERBINARY&)>(&DeserializeFromFileWrapper>>, SerType::SERBINARY>), + py::arg("filename"), py::arg("sertype")); // Binary Serialization to bytes m.def("Serialize", &SerializeToBytesWrapper, SerType::SERBINARY>, @@ -321,7 +335,8 @@ void bind_serialization(pybind11::module &m) { m.def("Serialize", &SerializeToBytesWrapper>>, SerType::SERBINARY>, py::arg("obj"), py::arg("sertype")); m.def("DeserializeEvalKeyMapString", &DeserializeFromBytesWrapper>>, SerType::SERBINARY>, - py::arg("str"), py::arg("sertype")); + py::arg("str"), py::arg("sertype")); + m.def("SerializeEvalMultKeyString", &SerializeEvalMultKeyToBytesWrapper, py::arg("sertype"), py::arg("id") = ""); m.def("DeserializeEvalMultKeyString", &DeserializeEvalMultKeyFromBytesWrapper,