Browse Source

Merge pull request #7 from openfheorg/Oliveira_enc_dec

Oliveira enc dec
Rener Oliveira 2 years ago
parent
commit
6a6770d6df
5 changed files with 32 additions and 4 deletions
  1. 3 1
      CMakeLists.txt
  2. 3 1
      src/bindings.cpp
  3. 1 0
      src/bindings.h
  4. 18 0
      src/pke/decryption.cpp
  5. 7 2
      src/pke/examples/simple-integers.py

+ 3 - 1
CMakeLists.txt

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

+ 3 - 1
src/bindings.cpp

@@ -35,7 +35,8 @@ void bind_crypto_context(py::module &m){
             py::arg("privateKey"),py::arg("indexList"),py::arg("publicKey")=nullptr)
             py::arg("privateKey"),py::arg("indexList"),py::arg("publicKey")=nullptr)
             .def("MakePackedPlaintext",&CryptoContextImpl<DCRTPoly>::MakePackedPlaintext,"Make a plaintext from a vector of integers",
             .def("MakePackedPlaintext",&CryptoContextImpl<DCRTPoly>::MakePackedPlaintext,"Make a plaintext from a vector of integers",
             py::arg("value"),py::arg("depth")=1,py::arg("level")=0)
             py::arg("value"),py::arg("depth")=1,py::arg("level")=0)
-            .def("EvalRotate",&CryptoContextImpl<DCRTPoly>::EvalRotate,"Rotate a ciphertext");
+            .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");
             //.def("MakePackedPlaintext",static_cast<Plaintext (CryptoContextImpl<DCRTPoly>::*)(const std::vector<int64_t>&)>(&CryptoContextImpl<DCRTPoly>::MakePackedPlaintext), "Make a plaintext from a vector of integers")
             //.def("MakePackedPlaintext",static_cast<Plaintext (CryptoContextImpl<DCRTPoly>::*)(const std::vector<int64_t>&)>(&CryptoContextImpl<DCRTPoly>::MakePackedPlaintext), "Make a plaintext from a vector of integers")
     // Generator Functions    
     // Generator Functions    
     m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>);
     m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>);
@@ -109,5 +110,6 @@ PYBIND11_MODULE(openfhe, m) {
     bind_keys(m);
     bind_keys(m);
     bind_encodings(m);
     bind_encodings(m);
     bind_ciphertext(m);
     bind_ciphertext(m);
+    bind_decryption(m);
 
 
 }
 }

+ 1 - 0
src/bindings.h

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

+ 18 - 0
src/pke/decryption.cpp

@@ -0,0 +1,18 @@
+#include <pybind11/pybind11.h>
+#include <pybind11/stl.h>
+#include <openfhe/pke/openfhe.h>
+#include "bindings.h"
+
+using namespace lbcrypto;
+namespace py = pybind11;
+
+template<typename Element>
+Plaintext DecryptInterface(Ciphertext<Element> ciphertext,const PrivateKey<Element> privateKey){
+    Plaintext plaintextDecResult;
+    auto cc = ciphertext->GetCryptoContext();
+    cc->Decrypt(privateKey, ciphertext,&plaintextDecResult);
+    return plaintextDecResult;
+}
+void bind_decryption(py::module &m){
+    m.def("Decrypt",&DecryptInterface<DCRTPoly>,"Decrypt a ciphertext using private key");
+}

+ 7 - 2
src/pke/examples/simple-integers.py

@@ -39,5 +39,10 @@ plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
 plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
 plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
 
 
 
 
-ciphertext1 = Ciphertext()
-ciphertextRot1 = cryptoContext.EvalRotate(ciphertext1, 1)
+
+ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
+ciphertextRot1 = cryptoContext.EvalRotate(ciphertext1, 1)
+
+plaintextRot1 = Decrypt(ciphertextRot1,keypair.secretKey)
+print(plaintextRot1) # still not printing the vector
+