Browse Source

bootstrapping python script

Rener Oliveira (Ubuntu WSL) 1 year ago
parent
commit
f94c8b062a
2 changed files with 42 additions and 8 deletions
  1. 4 4
      src/bindings.cpp
  2. 38 4
      src/pke/examples/simple-ckks-bootstrapping.py

+ 4 - 4
src/bindings.cpp

@@ -95,7 +95,7 @@ void bind_crypto_context(py::module &m)
             py::arg("slots") = 0,
             py::arg("correctionFactor") = 0            )
         .def("EvalBootstrapKeyGen", &CryptoContextImpl<DCRTPoly>::EvalBootstrapKeyGen,
-            py::arg("ciphertext"),
+            py::arg("privateKey"),
             py::arg("slots"))
         .def("EvalBootstrap", &CryptoContextImpl<DCRTPoly>::EvalBootstrap,
             py::arg("ciphertext"),
@@ -254,12 +254,12 @@ void bind_ciphertext(py::module &m)
     py::class_<CiphertextImpl<DCRTPoly>, std::shared_ptr<CiphertextImpl<DCRTPoly>>>(m, "Ciphertext")
         .def(py::init<>())
         .def("__add__", [](const Ciphertext<DCRTPoly> &a, const Ciphertext<DCRTPoly> &b)
-             {return a + b; },py::is_operator(),pybind11::keep_alive<0, 1>());
+             {return a + b; },py::is_operator(),pybind11::keep_alive<0, 1>())
        // .def(py::self + py::self);
     // .def("GetDepth", &CiphertextImpl<DCRTPoly>::GetDepth)
     // .def("SetDepth", &CiphertextImpl<DCRTPoly>::SetDepth)
-    // .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel)
-    // .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel)
+     .def("GetLevel", &CiphertextImpl<DCRTPoly>::GetLevel)
+     .def("SetLevel", &CiphertextImpl<DCRTPoly>::SetLevel);
     // .def("GetHopLevel", &CiphertextImpl<DCRTPoly>::GetHopLevel)
     // .def("SetHopLevel", &CiphertextImpl<DCRTPoly>::SetHopLevel)
     // .def("GetScalingFactor", &CiphertextImpl<DCRTPoly>::GetScalingFactor)

+ 38 - 4
src/pke/examples/simple-ckks-bootstrapping.py

@@ -1,6 +1,6 @@
 from openfhe import *
 
-def main(nativeint=128):
+def main(nativeint=64):
     SimpleBootstrapExample(nativeint)
 
 def SimpleBootstrapExample(nativeint):
@@ -31,13 +31,47 @@ def SimpleBootstrapExample(nativeint):
     levelsUsedBeforeBootstrap = 10
 
     depth = levelsUsedBeforeBootstrap + FHECKKSRNS.GetBootstrapDepth(approxBootstrappDepth, levelBudget, secretKeyDist)
-    print(depth)
+
     parameters.SetMultiplicativeDepth(depth)
 
     cryptocontext = GenCryptoContext(parameters)
     cryptocontext.Enable(PKESchemeFeature.PKE)
     cryptocontext.Enable(PKESchemeFeature.KEYSWITCH)
-    #cryptocontext.Enable()
+    cryptocontext.Enable(PKESchemeFeature.LEVELEDSHE)
+    cryptocontext.Enable(PKESchemeFeature.ADVANCEDSHE)
+    cryptocontext.Enable(PKESchemeFeature.FHE)
+
+    ringDim = cryptocontext.GetRingDimension()
+    # This is the mazimum number of slots that can be used full packing.
+
+    numSlots = int(ringDim / 2)
+    print(f"CKKS is using ring dimension {ringDim}")
+
+    cryptocontext.EvalBootstrapSetup(levelBudget)
+
+    keyPair = cryptocontext.KeyGen()
+    cryptocontext.EvalMultKeyGen(keyPair.secretKey)
+    cryptocontext.EvalBootstrapKeyGen(keyPair.secretKey, numSlots)
+
+    x = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
+    encodedLength = len(x)
+
+    ptxt = cryptocontext.MakeCKKSPackedPlaintext(x)
+    ptxt.SetLength(encodedLength)
+
+    print(f"Input: {x}")
+
+    ciph = cryptocontext.Encrypt(keyPair.publicKey, ptxt)
+
+    print(f"Initial number of levels remaining: {ciph.GetLevel()}")
+
+    ciphertextAfter = cryptocontext.EvalBootstrap(ciph)
+
+    print(f"Number of levels remaining after bootstrapping: {ciphertextAfter.GetLevel()}")
+
+    result = cryptocontext.Decrypt(keyPair.secretKey, ciphertextAfter)
+    result.SetLength(encodedLength)
+    print(f"Output after bootstrapping: {result}")
 
 if __name__ == '__main__':
-    main()
+    main(64)