Bladeren bron

unlocking simple ckks example

Rener Oliveira (Ubuntu WSL) 1 jaar geleden
bovenliggende
commit
28b7d78c92
2 gewijzigde bestanden met toevoegingen van 62 en 4 verwijderingen
  1. 23 1
      src/bindings.cpp
  2. 39 3
      src/pke/examples/simple-real-numbers.py

+ 23 - 1
src/bindings.cpp

@@ -79,10 +79,32 @@ void bind_crypto_context(py::module &m)
                 return self->MakeCKKSPackedPlaintext(complexValue, depth, level, params, slots);},
                 "Make a CKKS plaintext from a vector of doubles",
                 py::arg("value"), py::arg("depth") = 1, py::arg("level") = 0, py::arg("params") = nullptr, py::arg("slots") = 0)
+        .def("MakeCKKSPackedPlaintext4",[](std::shared_ptr<CryptoContextImpl<DCRTPoly>> &self,const int &value, size_t depth, uint32_t level, const std::shared_ptr<ParmType> params, usint slots) -> auto {
+                std::vector<std::complex<double>> complexValue;
+                complexValue.push_back(std::complex<double>(value, 0));
+                return self->MakeCKKSPackedPlaintext(complexValue, depth, level, params, slots);
+            }, "Make a CKKS plaintext from a list of floats",
+            py::arg("value"), py::arg("depth") = 1, py::arg("level") = 0, py::arg("params") = nullptr, py::arg("slots") = 0)
+        .def("MakeCKKSPackedPlaintext5", [] (std::shared_ptr<CryptoContextImpl<DCRTPoly>> &self, const std::vector<float>& value) -> auto {
+                if (!value.size())
+                    OPENFHE_THROW(config_error, "Cannot encode an empty value vector");
+
+                std::vector<std::complex<double>> complexValue(value.size());
+                std::transform(value.begin(), value.end(), complexValue.begin(),
+                       [](float da) { return std::complex<double>(da); });
+                return self->MakeCKKSPackedPlaintext(complexValue, 1, 0, nullptr, 0);},
+                "Make a CKKS plaintext from a vector of doubles",
+                py::arg("value"))
+
+        .def("Test_Member_Function",[](std::shared_ptr<CryptoContextImpl<DCRTPoly>> &self, const std::string &str) { 
+                std::cout << "Ring Dimension: " << self->GetRingDimension() << std::endl;
+                std::cout << "String: " << str << std::endl;
+            }, "A test member function that receives a string and print it along with ring dimension")
         .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("EvalSub", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalSub), "Subtract two ciphertexts")
         .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalMult), "Multiply two ciphertexts")
         .def_static(
             "ClearEvalMultKeys", []()
@@ -177,6 +199,7 @@ void bind_encodings(py::module &m){
     .def("GetSchemeID", &PlaintextImpl::GetSchemeID)
     .def("SetLength", &PlaintextImpl::SetLength)
     .def("IsEncoded", &PlaintextImpl::IsEncoded)
+    .def("GetLogPrecision", &PlaintextImpl::GetLogPrecision)
     //.def("GetEncondingParams", &PlaintextImpl::GetEncondingParams)
     .def("Encode", &PlaintextImpl::Encode)
     .def("Decode", &PlaintextImpl::Decode)
@@ -221,5 +244,4 @@ PYBIND11_MODULE(openfhe, m) {
     bind_ciphertext(m);
     bind_decryption(m);
     bind_serialization(m);
-    bind_a(m);
 }

+ 39 - 3
src/pke/examples/simple-real-numbers.py

@@ -18,11 +18,47 @@ print("The CKKS scheme is using ring dimension: " + str(cc.GetRingDimension()))
 
 keys = cc.KeyGen()
 cc.EvalMultKeyGen(keys.secretKey)
-cc.EvalRotateKeyGen(keys.secretKey, [-1, 2])
+cc.EvalRotateKeyGen(keys.secretKey, [1, -2])
 
 x1 = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
 x2 = [5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25]
 
+cc.Test_Member_Function('test')
+ptx1 = cc.MakeCKKSPackedPlaintext5(x1)
+ptx2 = cc.MakeCKKSPackedPlaintext5(x2)
+
+print("Input x1: " + str(ptx1))
+print("Input x2: " + str(ptx2))
+
+# Encrypt the encoded vectors
+c1 = cc.Encrypt(keys.publicKey, ptx1)
+c2 = cc.Encrypt(keys.publicKey, ptx2)
+
+# Step 4: Evaluation
+# Homomorphic additions
+cAdd = cc.EvalAdd(c1, c2)
+# Homomorphic Subtraction
+cSub = cc.EvalSub(c1, c2)
+# Homomorphic Multiplication
+cMult = cc.EvalMult(c1, c2)
+# Homomorphic Rotations
+cRot1 = cc.EvalRotate(c1, 1)
+cRot2 = cc.EvalRotate(c1, -2)
+
+# Step 5: Decryption and output
+# Decrypt the result of additions
+ptAdd = Decrypt(cAdd,keys.secretKey)
+
+# We set the precision to 8 decimal digits for a nicer output.
+# If you want to see the error/noise introduced by CKKS, bump it up
+# to 15 and it should become visible.
+
+precision = 8
+print("Results of homomorphic computations:")
+result = Decrypt(c1, keys.secretKey)
+result.SetLength(batchSize)
+print("x1 = " + str(result))
+print("Estimated precision in bits: " + str(result.GetLogPrecision()))
+
+
 
-ptx1 = cc.MakeCKKSPackedPlaintext(x1)
-# ptx2 = cc.MakeCKKSPackedPlaintext(x2)