Bladeren bron

Merge pull request #19 from openfheorg/Oliveira_boostrapping

Merging branch with renamed name
Rener Oliveira 1 jaar geleden
bovenliggende
commit
52f6292ac5
2 gewijzigde bestanden met toevoegingen van 46 en 17 verwijderingen
  1. 4 4
      src/bindings.cpp
  2. 42 13
      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)

+ 42 - 13
src/pke/examples/simple-ckks-bootstrapping.py

@@ -1,9 +1,9 @@
 from openfhe import *
 
-def main(nativeint=128):
-    SimpleBootstrapExample(nativeint)
+def main():
+    SimpleBootstrapExample()
 
-def SimpleBootstrapExample(nativeint):
+def SimpleBootstrapExample():
     parameters = CCParamsCKKSRNS()
 
     secretKeyDist = SecretKeyDist.UNIFORM_TERNARY
@@ -12,14 +12,9 @@ def SimpleBootstrapExample(nativeint):
     parameters.SetSecurityLevel(SecurityLevel.HEStd_NotSet)
     parameters.SetRingDim(1<<12)
 
-    if nativeint==128:
-        rescaleTech = ScalingTechnique.FIXEDAUTO
-        dcrtBits = 78
-        firstMod = 89
-    else:
-        rescaleTech = ScalingTechnique.FLEXIBLEAUTO
-        dcrtBits = 59
-        firstMod = 60
+    rescaleTech = ScalingTechnique.FLEXIBLEAUTO
+    dcrtBits = 59
+    firstMod = 60
     
     parameters.SetScalingModSize(dcrtBits)
     parameters.SetScalingTechnique(rescaleTech)
@@ -31,13 +26,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 = Decrypt(ciphertextAfter,keyPair.secretKey)
+    result.SetLength(encodedLength)
+    print(f"Output after bootstrapping: {result}")
 
 if __name__ == '__main__':
     main()