Browse Source

fixing makeckks parameters

Rener Oliveira (Ubuntu WSL) 2 years ago
parent
commit
d29e2b4fd0
1 changed files with 107 additions and 89 deletions
  1. 107 89
      src/bindings.cpp

+ 107 - 89
src/bindings.cpp

@@ -10,43 +10,43 @@
 using namespace lbcrypto;
 namespace py = pybind11;
 
-void bind_parameters(py::module &m){
+void bind_parameters(py::module &m)
+{
     py::class_<Params>(m, "Params");
     py::class_<CCParams<CryptoContextBFVRNS>, Params>(m, "CCParamsBFVRNS")
-            .def(py::init<>())
-            // setters
-            .def("SetPlaintextModulus", &CCParams<CryptoContextBFVRNS>::SetPlaintextModulus)
-            .def("SetMultiplicativeDepth",&CCParams<CryptoContextBFVRNS>::SetMultiplicativeDepth)
-            // getters
-            .def("GetPlaintextModulus", &CCParams<CryptoContextBFVRNS>::GetPlaintextModulus)
-            .def("GetMultiplicativeDepth", &CCParams<CryptoContextBFVRNS>::GetMultiplicativeDepth);
+        .def(py::init<>())
+        // setters
+        .def("SetPlaintextModulus", &CCParams<CryptoContextBFVRNS>::SetPlaintextModulus)
+        .def("SetMultiplicativeDepth", &CCParams<CryptoContextBFVRNS>::SetMultiplicativeDepth)
+        // getters
+        .def("GetPlaintextModulus", &CCParams<CryptoContextBFVRNS>::GetPlaintextModulus)
+        .def("GetMultiplicativeDepth", &CCParams<CryptoContextBFVRNS>::GetMultiplicativeDepth);
     py::class_<CCParams<CryptoContextBGVRNS>, Params>(m, "CCParamsBGVRNS")
-            .def(py::init<>())
-            // setters
-            .def("SetPlaintextModulus", &CCParams<CryptoContextBGVRNS>::SetPlaintextModulus)
-            .def("SetMultiplicativeDepth",&CCParams<CryptoContextBGVRNS>::SetMultiplicativeDepth)
-            // getters
-            .def("GetPlaintextModulus", &CCParams<CryptoContextBGVRNS>::GetPlaintextModulus)
-            .def("GetMultiplicativeDepth", &CCParams<CryptoContextBGVRNS>::GetMultiplicativeDepth);
-    //bind ckks rns params
+        .def(py::init<>())
+        // setters
+        .def("SetPlaintextModulus", &CCParams<CryptoContextBGVRNS>::SetPlaintextModulus)
+        .def("SetMultiplicativeDepth", &CCParams<CryptoContextBGVRNS>::SetMultiplicativeDepth)
+        // getters
+        .def("GetPlaintextModulus", &CCParams<CryptoContextBGVRNS>::GetPlaintextModulus)
+        .def("GetMultiplicativeDepth", &CCParams<CryptoContextBGVRNS>::GetMultiplicativeDepth);
+    // bind ckks rns params
     py::class_<CCParams<CryptoContextCKKSRNS>, Params>(m, "CCParamsCKKSRNS")
-            .def(py::init<>())
-            // setters
-            .def("SetPlaintextModulus", &CCParams<CryptoContextCKKSRNS>::SetPlaintextModulus)
-            .def("SetMultiplicativeDepth",&CCParams<CryptoContextCKKSRNS>::SetMultiplicativeDepth)
-            .def("SetScalingModSize", &CCParams<CryptoContextCKKSRNS>::SetScalingModSize)
-            .def("SetBatchSize", &CCParams<CryptoContextCKKSRNS>::SetBatchSize)
-            // getters
-            .def("GetPlaintextModulus", &CCParams<CryptoContextCKKSRNS>::GetPlaintextModulus)
-            .def("GetMultiplicativeDepth", &CCParams<CryptoContextCKKSRNS>::GetMultiplicativeDepth)
-            .def("GetScalingModSize", &CCParams<CryptoContextCKKSRNS>::GetScalingModSize)
-            .def("GetBatchSize", &CCParams<CryptoContextCKKSRNS>::GetBatchSize);
-           
+        .def(py::init<>())
+        // setters
+        .def("SetPlaintextModulus", &CCParams<CryptoContextCKKSRNS>::SetPlaintextModulus)
+        .def("SetMultiplicativeDepth", &CCParams<CryptoContextCKKSRNS>::SetMultiplicativeDepth)
+        .def("SetScalingModSize", &CCParams<CryptoContextCKKSRNS>::SetScalingModSize)
+        .def("SetBatchSize", &CCParams<CryptoContextCKKSRNS>::SetBatchSize)
+        // getters
+        .def("GetPlaintextModulus", &CCParams<CryptoContextCKKSRNS>::GetPlaintextModulus)
+        .def("GetMultiplicativeDepth", &CCParams<CryptoContextCKKSRNS>::GetMultiplicativeDepth)
+        .def("GetScalingModSize", &CCParams<CryptoContextCKKSRNS>::GetScalingModSize)
+        .def("GetBatchSize", &CCParams<CryptoContextCKKSRNS>::GetBatchSize);
 }
 
 void bind_crypto_context(py::module &m)
 {
-    //using ParmType = typename DCRTPoly::Params;
+    using ParmType = typename DCRTPoly::Params;
     py::class_<CryptoContextImpl<DCRTPoly>, std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m, "CryptoContext")
         .def(py::init<>())
         .def("GetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::GetKeyGenLevel)
@@ -57,18 +57,29 @@ void bind_crypto_context(py::module &m)
         .def("EvalMultKeyGen", &CryptoContextImpl<DCRTPoly>::EvalMultKeyGen, "Generate the evaluation key for multiplication")
         .def("EvalRotateKeyGen", &CryptoContextImpl<DCRTPoly>::EvalRotateKeyGen, "Generate the evaluation key for rotation",
              py::arg("privateKey"), py::arg("indexList"), py::arg("publicKey") = nullptr)
-       
-            //py::arg("value"), py::arg("depth") = 1, py::arg("level") = 0, py::arg("params") = nullptr, py::arg("slots") = 0)
-        .def("MakeCKKSPackedPlaintext", [](std::shared_ptr<CryptoContextImpl<DCRTPoly>> &self, const std::vector<float>& value){
+        .def("MakePackedPlaintext", &CryptoContextImpl<DCRTPoly>::MakePackedPlaintext, "Make a plaintext from a vector of integers",
+             py::arg("value"), py::arg("depth") = 1, py::arg("level") = 0)
+        .def(
+            "MakeCKKSPackedPlaintext", [](std::shared_ptr<CryptoContextImpl<DCRTPoly>> &self, 
+            const std::vector<float> &value, 
+            size_t depth, uint32_t level, 
+            const std::shared_ptr<ParmType> params,
+            usint slots) 
+            {
                 if (!value.size())
                     OPENFHE_THROW(config_error, "Cannot encode an empty value vector");
 
                 std::vector<std::complex<double>> complexValue(value.size());
                 std::transform(value.begin(), value.end(), complexValue.begin(),
                        [](float da) { return std::complex<double>(da); });
-                return self->MakeCKKSPackedPlaintext(complexValue, 1, 0, nullptr, 0);
-                },"Make a CKKS plaintext from a vector of doubles",
-                py::arg("value"))
+                return self->MakeCKKSPackedPlaintext(complexValue, depth, level, params, slots); },
+            "Make a CKKS plaintext from a vector of floats",
+            py::arg("value"),
+            py::arg("depth") = static_cast<size_t>(1),
+            py::arg("level") = static_cast<uint32_t>(0),
+            py::arg("params") = py::none(),
+            py::arg("slots") = 0)
+
         .def("EvalRotate", &CryptoContextImpl<DCRTPoly>::EvalRotate, "Rotate a ciphertext")
         .def("Encrypt", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(const PublicKey<DCRTPoly>, Plaintext) const>(&CryptoContextImpl<DCRTPoly>::Encrypt),
              "Encrypt a plaintext using public key")
@@ -126,85 +137,92 @@ void bind_crypto_context(py::module &m)
     m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>);
     m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBGVRNS>);
     m.def("GenCryptoContext", &GenCryptoContext<CryptoContextCKKSRNS>);
-    m.def("ReleaseAllContexts",&CryptoContextFactory<DCRTPoly>::ReleaseAllContexts);
+    m.def("ReleaseAllContexts", &CryptoContextFactory<DCRTPoly>::ReleaseAllContexts);
 }
 
-void bind_enums_and_constants(py::module &m){
+void bind_enums_and_constants(py::module &m)
+{
     // Scheme Types
     py::enum_<SCHEME>(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);
+        .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_<PKESchemeFeature>(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);
+        .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_<SerType::SERJSON >(m, "SERJSON");
+    py::class_<SerType::SERJSON>(m, "SERJSON");
     py::class_<SerType::SERBINARY>(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_<ParmType, std::shared_ptr<ParmType>>(m, "ParmType");
 }
 
-void bind_keys(py::module &m){
-    py::class_<PublicKeyImpl<DCRTPoly>,std::shared_ptr<PublicKeyImpl<DCRTPoly>>>(m,"PublicKey")
-    .def(py::init<>());
-    py::class_<PrivateKeyImpl<DCRTPoly>,std::shared_ptr<PrivateKeyImpl<DCRTPoly>>>(m,"PrivateKey");
-    py::class_<KeyPair<DCRTPoly>>(m,"KeyPair")
-            .def_readwrite("publicKey", &KeyPair<DCRTPoly>::publicKey)
-            .def_readwrite("secretKey", &KeyPair<DCRTPoly>::secretKey);
+void bind_keys(py::module &m)
+{
+    py::class_<PublicKeyImpl<DCRTPoly>, std::shared_ptr<PublicKeyImpl<DCRTPoly>>>(m, "PublicKey")
+        .def(py::init<>());
+    py::class_<PrivateKeyImpl<DCRTPoly>, std::shared_ptr<PrivateKeyImpl<DCRTPoly>>>(m, "PrivateKey");
+    py::class_<KeyPair<DCRTPoly>>(m, "KeyPair")
+        .def_readwrite("publicKey", &KeyPair<DCRTPoly>::publicKey)
+        .def_readwrite("secretKey", &KeyPair<DCRTPoly>::secretKey);
 }
 
-void bind_encodings(py::module &m){
-    py::class_<PlaintextImpl,std::shared_ptr<PlaintextImpl>>(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) {
+void bind_encodings(py::module &m)
+{
+    py::class_<PlaintextImpl, std::shared_ptr<PlaintextImpl>>(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 << "<Plaintext Object: ";
         p.PrintValue(ss);
         ss << ">";
-        return ss.str();
-    })
-    .def("__str__", [] (const PlaintextImpl& p) {
+        return ss.str(); })
+        .def("__str__", [](const PlaintextImpl &p)
+             {
         std::stringstream ss;
         p.PrintValue(ss);
-        return ss.str();
-    });
-
+        return ss.str(); });
 }
 
-void bind_ciphertext(py::module &m){
-    py::class_<CiphertextImpl<DCRTPoly>,std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m,"Ciphertext")
+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);
+    // .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) {
+PYBIND11_MODULE(openfhe, m)
+{
     m.doc() = "Open-Source Fully Homomorphic Encryption Library";
     bind_parameters(m);
     bind_crypto_context(m);