瀏覽代碼

finished SerializeEvalMultKey lambda interface

Rener Oliveira (Ubuntu WSL) 2 年之前
父節點
當前提交
cf54d43d16
共有 3 個文件被更改,包括 78 次插入22 次删除
  1. 33 21
      src/bindings.cpp
  2. 37 1
      src/pke/examples/simple-integers-serial.py
  3. 8 0
      src/pke/serialization.cpp

+ 33 - 21
src/bindings.cpp

@@ -1,6 +1,9 @@
 #include <pybind11/pybind11.h>
 #include <pybind11/stl.h>
+#include <pybind11/iostream.h>
+#include <iostream>
 #include <openfhe/pke/openfhe.h>
+#include <openfhe/pke/key/key-ser.h>
 #include "bindings.h"
 
 using namespace lbcrypto;
@@ -19,32 +22,41 @@ void bind_parameters(py::module &m){
            
 }
 
-void bind_crypto_context(py::module &m){
-    py::class_<CryptoContextImpl<DCRTPoly>,std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m,"CryptoContextDCRTPoly")
-            .def("GetKeyGenLevel",&CryptoContextImpl<DCRTPoly>::GetKeyGenLevel)
-            .def("SetKeyGenLevel",&CryptoContextImpl<DCRTPoly>::SetKeyGenLevel)
-            .def("Enable",static_cast<void (CryptoContextImpl<DCRTPoly>::*)(PKESchemeFeature)>(&CryptoContextImpl<DCRTPoly>::Enable), "Enable a feature for the CryptoContext")
-            .def("KeyGen",&CryptoContextImpl<DCRTPoly>::KeyGen,"Generate a key pair with public and private keys") 
-            .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)
-            .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("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("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalAdd),"Add two ciphertexts")
-            .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalMult),"Multiply two ciphertexts");
+void bind_crypto_context(py::module &m)
+{
+    py::class_<CryptoContextImpl<DCRTPoly>, std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m, "CryptoContextDCRTPoly")
+        .def("GetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::GetKeyGenLevel)
+        .def("SetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::SetKeyGenLevel)
+        .def("Enable", static_cast<void (CryptoContextImpl<DCRTPoly>::*)(PKESchemeFeature)>(&CryptoContextImpl<DCRTPoly>::Enable), "Enable a feature for the CryptoContext")
+        .def("KeyGen", &CryptoContextImpl<DCRTPoly>::KeyGen, "Generate a key pair with public and private keys")
+        .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)
+        .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("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("EvalAdd", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalAdd), "Add two ciphertexts")
+        .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalMult), "Multiply two ciphertexts")
+        // lambda function with bool output
 
-    // Generator Functions    
+           .def_static(
+            "SerializeEvalMultKey", [](const std::string& filename, const SerType::SERBINARY& sertype, std::string id = "")
+            {
+                std::ofstream outfile(filename,std::ios::out | std::ios::binary);
+                bool res;
+                res = CryptoContextImpl<DCRTPoly>::SerializeEvalMultKey<SerType::SERBINARY>(outfile, sertype, id);
+                outfile.close();
+                return res; },
+                py::arg("filename"), py::arg("sertype"), py::arg("id") = "",
+            "Serialize an evaluation key for multiplication");
+
+    // Generator Functions
     m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBFVRNS>);
     m.def("GenCryptoContext", &GenCryptoContext<CryptoContextBGVRNS>);
-    
-
 }
 
-
-
-
 void bind_enums_and_constants(py::module &m){
     // Scheme Types
     py::enum_<SCHEME>(m, "SCHEME")

+ 37 - 1
src/pke/examples/simple-integers-serial.py

@@ -17,7 +17,43 @@ cryptoContext.Enable(PKESchemeFeature.PKE)
 cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
 cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
 
-if not SerializeToFile(datafolder + "/cryptocontext.txt", cryptoContext, JSON):
+# Serialize cryptocontext
+if not SerializeToFile(datafolder + "/cryptocontext.txt", cryptoContext, BINARY):
    raise Exception("Error writing serialization of the crypto context to cryptocontext.txt")
 
+print("The cryptocontext has been serialized.")
+
+# Sample Program: Step 2: Key Generation
+
+# Generate a public/private key pair
+keypair = cryptoContext.KeyGen()
+
+print("The keypair has been generated.")
+
+# Serialize the public key
+if not SerializeToFile(datafolder + "/key-public.txt", keypair.publicKey, BINARY):
+   raise Exception("Error writing serialization of the public key to key-public.txt")
+
+print("The public key has been serialized.")
+
+# Serialize the secret key
+if not SerializeToFile(datafolder + "/key-secret.txt", keypair.secretKey, BINARY):
+   raise Exception("Error writing serialization of the secret key to key-secret.txt")
+
+print("The secret key has been serialized.")
+
+# Generate the relinearization key
+cryptoContext.EvalMultKeyGen(keypair.secretKey)
+
+print("The relinearization key has been generated.")
+
+# Serialize the relinearization key
+
+if not cryptoContext.SerializeEvalMultKey(datafolder + "/key-relin.txt",BINARY):
+   raise Exception("Error writing serialization of the eval mult keys to \"key-eval-mult.txt\"")
+
+print("The relinearization key has been serialized.")
+
+
+
 

+ 8 - 0
src/pke/serialization.cpp

@@ -9,8 +9,16 @@
 using namespace lbcrypto;
 namespace py = pybind11;
 
+
 void bind_serialization(pybind11::module &m) {
+    // Json Serialization
     m.def("SerializeToFile", static_cast<bool (*)(const std::string&, const CryptoContext<DCRTPoly>&, const SerType::SERJSON&)>(&Serial::SerializeToFile<DCRTPoly>), py::arg("filename"), py::arg("obj"), py::arg("sertype"));
+    m.def("SerializeToFile", static_cast<bool (*)(const std::string&, const PublicKey<DCRTPoly>&, const SerType::SERJSON&)>(&Serial::SerializeToFile<PublicKey<DCRTPoly>>), py::arg("filename"), py::arg("obj"), py::arg("sertype"));
+    m.def("SerializeToFile", static_cast<bool (*)(const std::string&, const PrivateKey<DCRTPoly>&, const SerType::SERJSON&)>(&Serial::SerializeToFile<PrivateKey<DCRTPoly>>), py::arg("filename"), py::arg("obj"), py::arg("sertype"));
+    // Binary Serialization
+    m.def("SerializeToFile", static_cast<bool (*)(const std::string&, const CryptoContext<DCRTPoly>&, const SerType::SERBINARY&)>(&Serial::SerializeToFile<DCRTPoly>), py::arg("filename"), py::arg("obj"), py::arg("sertype"));
+    m.def("SerializeToFile", static_cast<bool (*)(const std::string&, const PublicKey<DCRTPoly>&, const SerType::SERBINARY&)>(&Serial::SerializeToFile<PublicKey<DCRTPoly>>), py::arg("filename"), py::arg("obj"), py::arg("sertype"));
+    m.def("SerializeToFile", static_cast<bool (*)(const std::string&, const PrivateKey<DCRTPoly>&, const SerType::SERBINARY&)>(&Serial::SerializeToFile<PrivateKey<DCRTPoly>>), py::arg("filename"), py::arg("obj"), py::arg("sertype"));
     
 }