Browse Source

wrapper for makeckksplaintext + changing includes

Rener Oliveira (Ubuntu WSL) 1 year ago
parent
commit
59878b8452

+ 1 - 1
CMakeLists.txt

@@ -54,7 +54,7 @@ endif()
 ### add_executable( test demo-simple-example.cpp )
 
 ### Pybind Modules
-pybind11_add_module(openfhe src/bindings.cpp src/pke/decryption.cpp src/pke/serialization.cpp)
+pybind11_add_module(openfhe src/bindings.cpp src/pke/decryption.cpp src/pke/serialization.cpp src/pke/cryptocontext_wrapper.cpp)
 ### Python installation 
 find_package(Python REQUIRED COMPONENTS Interpreter Development)
 

+ 22 - 0
include/pke/cryptocontext_wrapper.h

@@ -0,0 +1,22 @@
+#ifndef OPENFHE_CRYPTOCONTEXT_BINDINGS_H
+#define OPENFHE_CRYPTOCONTEXT_BINDINGS_H
+
+#include <pybind11/pybind11.h>
+#include <pybind11/stl.h>
+#include <vector>
+#include <algorithm>
+#include <complex>
+#include "openfhe.h"
+#include "bindings.h"
+
+namespace py = pybind11;
+using namespace lbcrypto;
+using ParmType = typename DCRTPoly::Params;
+
+Plaintext MakeCKKSPackedPlaintextWrapper(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);
+
+#endif // OPENFHE_CRYPTOCONTEXT_BINDINGS_H

+ 4 - 20
src/bindings.cpp

@@ -2,10 +2,10 @@
 #include <pybind11/stl.h>
 #include <pybind11/iostream.h>
 #include <iostream>
-#include <openfhe/pke/openfhe.h>
-#include <openfhe/pke/key/key-ser.h>
+#include "openfhe.h"
+#include "key/key-ser.h"
 #include "bindings.h"
-#include "cryptocontext.h"
+#include "cryptocontext_wrapper.h"
 
 using namespace lbcrypto;
 namespace py = pybind11;
@@ -46,7 +46,6 @@ void bind_parameters(py::module &m)
 
 void bind_crypto_context(py::module &m)
 {
-    using ParmType = typename DCRTPoly::Params;
     py::class_<CryptoContextImpl<DCRTPoly>, std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m, "CryptoContext")
         .def(py::init<>())
         .def("GetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::GetKeyGenLevel)
@@ -59,27 +58,12 @@ void bind_crypto_context(py::module &m)
              py::arg("privateKey"), py::arg("indexList"), py::arg("publicKey") = nullptr)
         .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, depth, level, params, slots); },
-            "Make a CKKS plaintext from a vector of floats",
+        .def("MakeCKKSPackedPlaintext",&MakeCKKSPackedPlaintextWrapper, "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")

+ 24 - 0
src/pke/cryptocontext_wrapper.cpp

@@ -0,0 +1,24 @@
+#include <pybind11/pybind11.h>
+#include <pybind11/stl.h>
+#include <openfhe/pke/openfhe.h>
+#include <vector>
+#include <algorithm>
+#include <complex> 
+#include "cryptocontext_wrapper.h"
+
+using namespace lbcrypto;
+namespace py = pybind11;
+
+Plaintext MakeCKKSPackedPlaintextWrapper(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, depth, level, params, slots); }

+ 1 - 1
src/pke/decryption.cpp

@@ -1,6 +1,6 @@
 #include <pybind11/pybind11.h>
 #include <pybind11/stl.h>
-#include <openfhe/pke/openfhe.h>
+#include "openfhe.h"
 #include "bindings.h"
 
 using namespace lbcrypto;

+ 4 - 4
src/pke/serialization.cpp

@@ -1,9 +1,9 @@
 #include <pybind11/pybind11.h>
 #include <pybind11/stl.h>
-#include <openfhe/pke/openfhe.h>
-#include <openfhe/pke/scheme/bfvrns/bfvrns-ser.h>
-#include <openfhe/pke/scheme/bgvrns/bgvrns-ser.h>
-#include <openfhe/pke/cryptocontext-ser.h>
+#include "openfhe.h"
+#include "scheme/bfvrns/bfvrns-ser.h"
+#include "scheme/bgvrns/bgvrns-ser.h"
+#include "cryptocontext-ser.h"
 #include "bindings.h"
 #include "serialization.h"