#include #include #include #include #include "openfhe.h" #include "key/key-ser.h" #include "bindings.h" #include "cryptocontext_wrapper.h" using namespace lbcrypto; namespace py = pybind11; void bind_parameters(py::module &m) { py::class_(m, "Params"); py::class_, Params>(m, "CCParamsBFVRNS") .def(py::init<>()) // setters .def("SetPlaintextModulus", &CCParams::SetPlaintextModulus) .def("SetMultiplicativeDepth", &CCParams::SetMultiplicativeDepth) // getters .def("GetPlaintextModulus", &CCParams::GetPlaintextModulus) .def("GetMultiplicativeDepth", &CCParams::GetMultiplicativeDepth); py::class_, Params>(m, "CCParamsBGVRNS") .def(py::init<>()) // setters .def("SetPlaintextModulus", &CCParams::SetPlaintextModulus) .def("SetMultiplicativeDepth", &CCParams::SetMultiplicativeDepth) // getters .def("GetPlaintextModulus", &CCParams::GetPlaintextModulus) .def("GetMultiplicativeDepth", &CCParams::GetMultiplicativeDepth); // bind ckks rns params py::class_, Params>(m, "CCParamsCKKSRNS") .def(py::init<>()) // setters .def("SetPlaintextModulus", &CCParams::SetPlaintextModulus) .def("SetMultiplicativeDepth", &CCParams::SetMultiplicativeDepth) .def("SetScalingModSize", &CCParams::SetScalingModSize) .def("SetBatchSize", &CCParams::SetBatchSize) // getters .def("GetPlaintextModulus", &CCParams::GetPlaintextModulus) .def("GetMultiplicativeDepth", &CCParams::GetMultiplicativeDepth) .def("GetScalingModSize", &CCParams::GetScalingModSize) .def("GetBatchSize", &CCParams::GetBatchSize); } void bind_crypto_context(py::module &m) { py::class_, std::shared_ptr>>(m, "CryptoContext") .def(py::init<>()) .def("GetKeyGenLevel", &CryptoContextImpl::GetKeyGenLevel) .def("SetKeyGenLevel", &CryptoContextImpl::SetKeyGenLevel) .def("GetRingDimension", &CryptoContextImpl::GetRingDimension) .def("Enable", static_cast::*)(PKESchemeFeature)>(&CryptoContextImpl::Enable), "Enable a feature for the CryptoContext") .def("KeyGen", &CryptoContextImpl::KeyGen, "Generate a key pair with public and private keys") .def("EvalMultKeyGen", &CryptoContextImpl::EvalMultKeyGen, "Generate the evaluation key for multiplication") .def("EvalRotateKeyGen", &CryptoContextImpl::EvalRotateKeyGen, "Generate the evaluation key for rotation", py::arg("privateKey"), py::arg("indexList"), py::arg("publicKey") = nullptr) .def("MakePackedPlaintext", &CryptoContextImpl::MakePackedPlaintext, "Make a plaintext from a vector of integers", py::arg("value"), py::arg("depth") = 1, py::arg("level") = 0) .def("MakeCKKSPackedPlaintext",&MakeCKKSPackedPlaintextWrapper, "Make a CKKS plaintext from a vector of floats", py::arg("value"), py::arg("depth") = static_cast(1), py::arg("level") = static_cast(0), py::arg("params") = py::none(), py::arg("slots") = 0) .def("EvalRotate", &CryptoContextImpl::EvalRotate, "Rotate a ciphertext") .def("Encrypt", static_cast (CryptoContextImpl::*)(const PublicKey, Plaintext) const>(&CryptoContextImpl::Encrypt), "Encrypt a plaintext using public key") .def("EvalAdd", static_cast (CryptoContextImpl::*)(ConstCiphertext, ConstCiphertext) const>(&CryptoContextImpl::EvalAdd), "Add two ciphertexts") .def("EvalSub", static_cast (CryptoContextImpl::*)(ConstCiphertext, ConstCiphertext) const>(&CryptoContextImpl::EvalSub), "Subtract two ciphertexts") .def("EvalMult", static_cast (CryptoContextImpl::*)(ConstCiphertext, ConstCiphertext) const>(&CryptoContextImpl::EvalMult), "Multiply two ciphertexts") .def("EvalMult", static_cast (CryptoContextImpl::*)(ConstCiphertext, double) const>(&CryptoContextImpl::EvalMult), "Multiply a ciphertext with a scalar") .def_static( "ClearEvalMultKeys", []() { CryptoContextImpl::ClearEvalMultKeys(); }, "Clear the evaluation keys for multiplication") .def_static( "ClearEvalAutomorphismKeys", []() { CryptoContextImpl::ClearEvalAutomorphismKeys(); }, "Clear the evaluation keys for rotation") .def_static( "SerializeEvalMultKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "") { std::ofstream outfile(filename,std::ios::out | std::ios::binary); bool res; res = CryptoContextImpl::SerializeEvalMultKey(outfile, sertype, id); outfile.close(); return res; }, py::arg("filename"), py::arg("sertype"), py::arg("id") = "", "Serialize an evaluation key for multiplication") .def_static( "SerializeEvalAutomorphismKey", [](const std::string &filename, const SerType::SERBINARY &sertype, std::string id = "") { std::ofstream outfile(filename,std::ios::out | std::ios::binary); bool res; res = CryptoContextImpl::SerializeEvalAutomorphismKey(outfile, sertype, id); outfile.close(); return res; }, py::arg("filename"), py::arg("sertype"), py::arg("id") = "", "Serialize evaluation keys for rotation") .def_static("DeserializeEvalMultKey", [](std::shared_ptr> &self,const std::string &filename, const SerType::SERBINARY &sertype) { std::ifstream emkeys(filename, std::ios::in | std::ios::binary); if (!emkeys.is_open()) { std::cerr << "I cannot read serialization from " << filename << std::endl; } bool res; res = self->DeserializeEvalMultKey(emkeys, sertype); return res; }) .def_static("DeserializeEvalAutomorphismKey", [](std::shared_ptr> &self,const std::string &filename, const SerType::SERBINARY &sertype) { std::ifstream erkeys(filename, std::ios::in | std::ios::binary); if (!erkeys.is_open()) { std::cerr << "I cannot read serialization from " << filename << std::endl; } bool res; res = self->DeserializeEvalAutomorphismKey(erkeys, sertype); return res; }); // Generator Functions m.def("GenCryptoContext", &GenCryptoContext); m.def("GenCryptoContext", &GenCryptoContext); m.def("GenCryptoContext", &GenCryptoContext); m.def("ReleaseAllContexts", &CryptoContextFactory::ReleaseAllContexts); } void bind_enums_and_constants(py::module &m) { // Scheme Types py::enum_(m, "SCHEME") .value("INVALID_SCHEME", SCHEME::INVALID_SCHEME) .value("CKKSRNS_SCHEME", SCHEME::CKKSRNS_SCHEME) .value("BFVRNS_SCHEME", SCHEME::BFVRNS_SCHEME) .value("BGVRNS_SCHEME", SCHEME::BGVRNS_SCHEME); // PKE Features py::enum_(m, "PKESchemeFeature") .value("PKE", PKESchemeFeature::PKE) .value("KEYSWITCH", PKESchemeFeature::KEYSWITCH) .value("PRE", PKESchemeFeature::PRE) .value("LEVELEDSHE", PKESchemeFeature::LEVELEDSHE) .value("ADVANCEDSHE", PKESchemeFeature::ADVANCEDSHE) .value("MULTIPARTY", PKESchemeFeature::MULTIPARTY) .value("FHE", PKESchemeFeature::FHE); // Serialization Types py::class_(m, "SERJSON"); py::class_(m, "SERBINARY"); m.attr("JSON") = py::cast(SerType::JSON); m.attr("BINARY") = py::cast(SerType::BINARY); //Parameters Type using ParmType = typename DCRTPoly::Params; py::class_>(m, "ParmType"); } void bind_keys(py::module &m) { py::class_, std::shared_ptr>>(m, "PublicKey") .def(py::init<>()); py::class_, std::shared_ptr>>(m, "PrivateKey"); py::class_>(m, "KeyPair") .def_readwrite("publicKey", &KeyPair::publicKey) .def_readwrite("secretKey", &KeyPair::secretKey); } void bind_encodings(py::module &m) { py::class_>(m, "Plaintext") .def("GetScalingFactor", &PlaintextImpl::GetScalingFactor) .def("SetScalingFactor", &PlaintextImpl::SetScalingFactor) .def("GetLength", &PlaintextImpl::GetLength) .def("GetSchemeID", &PlaintextImpl::GetSchemeID) .def("SetLength", &PlaintextImpl::SetLength) .def("IsEncoded", &PlaintextImpl::IsEncoded) .def("GetLogPrecision", &PlaintextImpl::GetLogPrecision) //.def("GetEncondingParams", &PlaintextImpl::GetEncondingParams) .def("Encode", &PlaintextImpl::Encode) .def("Decode", &PlaintextImpl::Decode) .def("__repr__", [](const PlaintextImpl &p) { std::stringstream ss; ss << ""; return ss.str(); }) .def("__str__", [](const PlaintextImpl &p) { std::stringstream ss; p.PrintValue(ss); return ss.str(); }); } void bind_ciphertext(py::module &m) { py::class_<CiphertextImpl<DCRTPoly>, std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m, "Ciphertext") .def(py::init<>()); // .def("GetDepth", &CiphertextImpl<DCRTPoly>::GetDepth) // .def("SetDepth", &CiphertextImpl<DCRTPoly>::SetDepth) // .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel) // .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel) // .def("GetHopLevel", &CiphertextImpl<DCRTPoly>::GetHopLevel) // .def("SetHopLevel", &CiphertextImpl<DCRTPoly>::SetHopLevel) // .def("GetScalingFactor", &CiphertextImpl<DCRTPoly>::GetScalingFactor) // .def("SetScalingFactor", &CiphertextImpl<DCRTPoly>::SetScalingFactor) // .def("GetSlots", &CiphertextImpl<DCRTPoly>::GetSlots) // .def("SetSlots", &CiphertextImpl<DCRTPoly>::SetSlots); } PYBIND11_MODULE(openfhe, m) { m.doc() = "Open-Source Fully Homomorphic Encryption Library"; bind_parameters(m); bind_crypto_context(m); bind_enums_and_constants(m); bind_keys(m); bind_encodings(m); bind_ciphertext(m); bind_decryption(m); bind_serialization(m); }