Browse Source

evaladd, evalmult, print method for plaintexts

Rener Oliveira (Ubuntu WSL) 2 years ago
parent
commit
0583a2ffef
2 changed files with 63 additions and 5 deletions
  1. 19 2
      src/bindings.cpp
  2. 44 3
      src/pke/examples/simple-integers.py

+ 19 - 2
src/bindings.cpp

@@ -36,7 +36,9 @@ void bind_crypto_context(py::module &m){
             .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("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");
             //.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>);
@@ -78,11 +80,26 @@ void bind_encodings(py::module &m){
     py::class_<PlaintextImpl,std::shared_ptr<PlaintextImpl>>(m,"Plaintext")
     py::class_<PlaintextImpl,std::shared_ptr<PlaintextImpl>>(m,"Plaintext")
     .def("GetScalingFactor", &PlaintextImpl::GetScalingFactor)
     .def("GetScalingFactor", &PlaintextImpl::GetScalingFactor)
     .def("SetScalingFactor", &PlaintextImpl::SetScalingFactor)
     .def("SetScalingFactor", &PlaintextImpl::SetScalingFactor)
+    .def("GetLength", &PlaintextImpl::GetLength)
     .def("GetSchemeID", &PlaintextImpl::GetSchemeID)
     .def("GetSchemeID", &PlaintextImpl::GetSchemeID)
+    .def("SetLength", &PlaintextImpl::SetLength)
     .def("IsEncoded", &PlaintextImpl::IsEncoded)
     .def("IsEncoded", &PlaintextImpl::IsEncoded)
     //.def("GetEncondingParams", &PlaintextImpl::GetEncondingParams)
     //.def("GetEncondingParams", &PlaintextImpl::GetEncondingParams)
     .def("Encode", &PlaintextImpl::Encode)
     .def("Encode", &PlaintextImpl::Encode)
-    .def("Decode", &PlaintextImpl::Decode);
+    .def("Decode", &PlaintextImpl::Decode)
+    // The PlaintextImpl has a PrintValue void function that prints the plaintext, The following function uses it to defining __repr__ method in order to print it in python
+    .def("__repr__", [] (const PlaintextImpl& p) {
+        std::stringstream ss;
+        ss << "<Plaintext Object: ";
+        p.PrintValue(ss);
+        ss << ">";
+        return ss.str();
+    })
+    .def("__str__", [] (const PlaintextImpl& p) {
+        std::stringstream ss;
+        p.PrintValue(ss);
+        return ss.str();
+    });
 
 
 }
 }
 
 

+ 44 - 3
src/pke/examples/simple-integers.py

@@ -18,8 +18,8 @@ print("New BFV Multiplicative Depth = " + str(parameters.GetMultiplicativeDepth(
 
 
 cryptoContext = GenCryptoContext(parameters)
 cryptoContext = GenCryptoContext(parameters)
 
 
-cryptoContext.SetKeyGenLevel(2)
-print(cryptoContext.GetKeyGenLevel())
+# cryptoContext.SetKeyGenLevel(2)
+# print(cryptoContext.GetKeyGenLevel())
 print(PKESchemeFeature.__members__)
 print(PKESchemeFeature.__members__)
 cryptoContext.Enable(PKESchemeFeature.PKE)
 cryptoContext.Enable(PKESchemeFeature.PKE)
 cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
 cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
@@ -41,8 +41,49 @@ plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
 
 
 
 
 ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
 ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
+ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
+ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
+
+# Homomorphic additions
+ciphertextAdd12 = cryptoContext.EvalAdd(ciphertext1, ciphertext2)
+ciphertextAddResult = cryptoContext.EvalAdd(ciphertextAdd12, ciphertext3)
+
+# Homomorphic Multiplication
+ciphertextMult12 = cryptoContext.EvalMult(ciphertext1, ciphertext2)
+ciphertextMultResult = cryptoContext.EvalMult(ciphertextMult12, ciphertext3)
+
+# Homomorphic Rotations
 ciphertextRot1 = cryptoContext.EvalRotate(ciphertext1, 1)
 ciphertextRot1 = cryptoContext.EvalRotate(ciphertext1, 1)
+ciphertextRot2 = cryptoContext.EvalRotate(ciphertext1, 2)
+ciphertextRot3 = cryptoContext.EvalRotate(ciphertext1, -1)
+ciphertextRot4 = cryptoContext.EvalRotate(ciphertext1, -2)
+
+# Decrypting
 
 
+plaintextAddResult = Decrypt(ciphertextAddResult,keypair.secretKey)
+plaintextMultResult = Decrypt(ciphertextMultResult,keypair.secretKey)
 plaintextRot1 = Decrypt(ciphertextRot1,keypair.secretKey)
 plaintextRot1 = Decrypt(ciphertextRot1,keypair.secretKey)
-print(plaintextRot1) # still not printing the vector
+plaintextRot2 = Decrypt(ciphertextRot2,keypair.secretKey)
+plaintextRot3 = Decrypt(ciphertextRot3,keypair.secretKey)
+plaintextRot4 = Decrypt(ciphertextRot4,keypair.secretKey)
+
+plaintextRot1 = Decrypt(ciphertextRot1,keypair.secretKey)
+#print(plaintextRot1) # still not printing the vector
+
+plaintextRot1.SetLength(len(vectorOfInts1))
+plaintextRot2.SetLength(len(vectorOfInts1))
+plaintextRot3.SetLength(len(vectorOfInts1))
+plaintextRot4.SetLength(len(vectorOfInts1))
+
+print("Plaintext #1: " + str(plaintext1))
+print("Plaintext #2: " + str(plaintext2))
+print("Plaintext #3: " + str(plaintext3))
 
 
+# Output Results
+print("\nResults of homomorphic computations")
+print("#1 + #2 + #3 = " + str(plaintextAddResult))
+print("#1 * #2 * #3 = " + str(plaintextMultResult))
+print("Left rotation of #1 by 1 = " + str(plaintextRot1))
+print("Left rotation of #1 by 2 = " + str(plaintextRot2))
+print("Right rotation of #1 by 1 = " + str(plaintextRot3))
+print("Right rotation of #1 by 2 = " + str(plaintextRot4))