Browse Source

encryption working

Rener Oliveira (Ubuntu WSL) 2 years ago
parent
commit
db9f4551b7
4 changed files with 24 additions and 2 deletions
  1. 3 1
      src/bindings.cpp
  2. 1 0
      src/bindings.h
  3. 18 0
      src/pke/decryption.cpp
  4. 2 1
      src/pke/examples/simple-integers.py

+ 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");
+}

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

@@ -39,5 +39,6 @@ plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
 plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
 plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
 
 
 
 
-ciphertext1 = Ciphertext()
+
+ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
 ciphertextRot1 = cryptoContext.EvalRotate(ciphertext1, 1)
 ciphertextRot1 = cryptoContext.EvalRotate(ciphertext1, 1)