Explorar el Código

serialization interface + reorganizing src/includes

Rener Oliveira (Ubuntu WSL) hace 3 años
padre
commit
343e1091e6
Se han modificado 5 ficheros con 74 adiciones y 3 borrados
  1. 5 2
      CMakeLists.txt
  2. 1 0
      include/bindings.h
  3. 13 0
      include/pke/serialization.h
  4. 2 1
      src/bindings.cpp
  5. 53 0
      src/pke/serialization.cpp

+ 5 - 2
CMakeLists.txt

@@ -25,13 +25,16 @@ find_package(pybind11 REQUIRED)
 
 set( CMAKE_CXX_FLAGS ${OpenFHE_CXX_FLAGS} )
 set( OpenFHE_Py_SOURCES src)
+set( OpenFHE_Py_INCLUDES include)
 
 include_directories( ${OPENMP_INCLUDES} )
 include_directories( ${OpenFHE_INCLUDE} )
 include_directories( ${OpenFHE_INCLUDE}/third-party/include )
 include_directories( ${OpenFHE_INCLUDE}/core )
 include_directories( ${OpenFHE_INCLUDE}/pke )
-include_directories( ${OpenFHE_Py_SOURCES} )
+# include_directories( ${OpenFHE_Py_SOURCES} )
+include_directories( ${OpenFHE_Py_INCLUDES}/pke )
+include_directories( ${OpenFHE_Py_INCLUDES} )
 ### add directories for other OpenFHE modules as needed for your project
 
 link_directories( ${OpenFHE_LIBDIR} )
@@ -51,7 +54,7 @@ endif()
 ### add_executable( test demo-simple-example.cpp )
 
 ### Pybind Modules
-pybind11_add_module(openfhe src/bindings.cpp src/pke/decryption.cpp)
+pybind11_add_module(openfhe src/bindings.cpp src/pke/decryption.cpp src/pke/serialization.cpp)
 ### Python installation 
 find_package(Python REQUIRED COMPONENTS Interpreter Development)
 

+ 1 - 0
src/bindings.h → include/bindings.h

@@ -10,4 +10,5 @@ void bind_keys(pybind11::module &m);
 void bind_encodings(pybind11::module &m);
 void bind_ciphertext(pybind11::module &m);
 void bind_decryption(pybind11::module &m);
+void bind_serialization(pybind11::module &m);
 #endif // OPENFHE_BINDINGS_H

+ 13 - 0
include/pke/serialization.h

@@ -0,0 +1,13 @@
+#ifndef OPENFHE_SERIALIZATION_BINDINGS_H
+#define OPENFHE_SERIALIZATION_BINDINGS_H
+
+#include <pybind11/pybind11.h>
+using namespace lbcrypto;
+
+template <typename T>
+bool SerializeToFileImpl(const std::string& filename, const T& obj, const std::string& sertype_str);
+bool SerializeToFileInterface(const std::string& filename, const CryptoContext<DCRTPoly>& obj, const std::string& sertype_str);
+bool SerializeToFileInterface(const std::string& filename, const PublicKey<DCRTPoly>& obj, const std::string& sertype_str);
+bool SerializeToFileInterface(const std::string& filename, const PrivateKey<DCRTPoly>& obj, const std::string& sertype_str);
+
+#endif // OPENFHE_SERIALIZATION_BINDINGS_H

+ 2 - 1
src/bindings.cpp

@@ -122,5 +122,6 @@ PYBIND11_MODULE(openfhe, m) {
     bind_encodings(m);
     bind_ciphertext(m);
     bind_decryption(m);
+    bind_serialization(m);
 
-}
+}

+ 53 - 0
src/pke/serialization.cpp

@@ -0,0 +1,53 @@
+#include <pybind11/pybind11.h>
+#include <pybind11/stl.h>
+#include <openfhe/pke/openfhe.h>
+#include "bindings.h"
+#include "serialization.h"
+
+using namespace lbcrypto;
+namespace py = pybind11;
+
+template <typename T>
+bool SerializeToFileImpl(const std::string& filename, const T& obj, const std::string& sertype_str) {
+    // call the appropriate serialization function based on the string
+    if (sertype_str == "binary") {
+        return Serial::SerializeToFile(filename, obj, SerType::BINARY);
+    } else if (sertype_str == "json") {
+        return Serial::SerializeToFile(filename, obj, SerType::JSON);
+    }else {
+        OPENFHE_THROW(serialize_error,"Serialization type not supported, use 'json' or 'binary'");
+    }
+    
+    // switch (sertype_str)
+    // {
+    // case "json":
+    //     return Serial::SerializeToFile(filename, obj, SerType::JSON);
+    //     break;
+    
+    // case "binary":
+    //     return Serial::SerializeToFile(filename, obj, SerType::BINARY);
+    //     break;
+    
+    // default:
+        
+    // }
+}
+
+bool SerializeToFileInterface(const std::string& filename, const CryptoContext<DCRTPoly>& obj, const std::string& sertype_str) {
+    return SerializeToFileImpl(filename, obj, sertype_str);
+}
+
+bool SerializeToFileInterface(const std::string& filename, const PublicKey<DCRTPoly>& obj, const std::string& sertype_str) {
+    return SerializeToFileImpl(filename, obj, sertype_str);
+}
+
+bool SerializeToFileInterface(const std::string& filename, const PrivateKey<DCRTPoly>& obj, const std::string& sertype_str) {
+    return SerializeToFileImpl(filename, obj, sertype_str);
+}
+
+void bind_serialization(pybind11::module &m) {
+    m.def("SerializeToFile", static_cast<bool (*)(const std::string&, const CryptoContext<DCRTPoly>&, const std::string&)>(&SerializeToFileInterface), py::arg("filename"), py::arg("obj"), py::arg("sertype_str")="binary");
+    
+}
+
+