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 58d1384..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) { @@ -238,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>, @@ -260,6 +271,11 @@ 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, @@ -290,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>, @@ -312,6 +332,11 @@ void bind_serialization(pybind11::module &m) { py::arg("obj"), 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,