Browse Source

EvalBootstrap + FHECKKS + typos fix

Rener Oliveira (Ubuntu WSL) 1 year ago
parent
commit
0358386020
3 changed files with 67 additions and 1 deletions
  1. 1 0
      include/bindings.h
  2. 23 1
      src/bindings.cpp
  3. 43 0
      src/pke/examples/simple-ckks-bootstrapping.py

+ 1 - 0
include/bindings.h

@@ -11,4 +11,5 @@ void bind_encodings(pybind11::module &m);
 void bind_ciphertext(pybind11::module &m);
 void bind_decryption(pybind11::module &m);
 void bind_serialization(pybind11::module &m);
+void bind_schemes(pybind11::module &m);
 #endif // OPENFHE_BINDINGS_H

+ 23 - 1
src/bindings.cpp

@@ -89,6 +89,18 @@ void bind_crypto_context(py::module &m)
         .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, ConstCiphertext<DCRTPoly>) const>(&CryptoContextImpl<DCRTPoly>::EvalMult), "Multiply two ciphertexts")
         .def("EvalMult", static_cast<Ciphertext<DCRTPoly> (CryptoContextImpl<DCRTPoly>::*)(ConstCiphertext<DCRTPoly>, double) const>(&CryptoContextImpl<DCRTPoly>::EvalMult), "Multiply a ciphertext with a scalar")
         .def("Rescale", &CryptoContextImpl<DCRTPoly>::Rescale, "Rescale a ciphertext")
+        .def("EvalBootstrapSetup", &CryptoContextImpl<DCRTPoly>::EvalBootstrapSetup,
+            py::arg("levelBudget") = std::vector<uint32_t>({5,4}),
+            py::arg("dim1") = std::vector<uint32_t>({0,0}),
+            py::arg("slots") = 0,
+            py::arg("correctionFactor") = 0            )
+        .def("EvalBootstrapKeyGen", &CryptoContextImpl<DCRTPoly>::EvalBootstrapKeyGen,
+            py::arg("ciphertext"),
+            py::arg("slots"))
+        .def("EvalBootstrap", &CryptoContextImpl<DCRTPoly>::EvalBootstrap,
+            py::arg("ciphertext"),
+            py::arg("numIterations") = 1,
+            py::arg("precision") = 0)
         .def_static(
             "ClearEvalMultKeys", []()
             { CryptoContextImpl<DCRTPoly>::ClearEvalMultKeys(); },
@@ -183,7 +195,7 @@ void bind_enums_and_constants(py::module &m)
     py::enum_<SecretKeyDist>(m, "SecretKeyDist")
         .value("GAUSSIAN", SecretKeyDist::GAUSSIAN)
         .value("UNIFORM_TERNARY", SecretKeyDist::UNIFORM_TERNARY)
-        .value("SPARCE_TERNARY", SecretKeyDist::SPARCE_TERNARY);
+        .value("SPARsE_TERNARY", SecretKeyDist::SPARSE_TERNARY);
 
     /* ---- CORE enums ---- */ 
     // Security Level
@@ -256,6 +268,15 @@ void bind_ciphertext(py::module &m)
     // .def("SetSlots", &CiphertextImpl<DCRTPoly>::SetSlots);
 }
 
+void bind_schemes(py::module &m){
+    /*Bind schemes specific functionalities like bootstrapping functions and multiparty*/
+    py::class_<FHECKKSRNS>(m, "FHECKKSRNS")
+        .def(py::init<>())
+        //.def_static("GetBootstrapDepth", &FHECKKSRNS::GetBootstrapDepth)
+        .def_static("GetBootstrapDepth", static_cast<uint32_t (*)(uint32_t, const std::vector<uint32_t>&, SecretKeyDist)>(&FHECKKSRNS::GetBootstrapDepth));                               
+    
+}
+
 PYBIND11_MODULE(openfhe, m)
 {
     m.doc() = "Open-Source Fully Homomorphic Encryption Library";
@@ -267,4 +288,5 @@ PYBIND11_MODULE(openfhe, m)
     bind_ciphertext(m);
     bind_decryption(m);
     bind_serialization(m);
+    bind_schemes(m);
 }

+ 43 - 0
src/pke/examples/simple-ckks-bootstrapping.py

@@ -0,0 +1,43 @@
+from openfhe import *
+
+def main(nativeint=128):
+    SimpleBootstrapExample(nativeint)
+
+def SimpleBootstrapExample(nativeint):
+    parameters = CCParamsCKKSRNS()
+
+    secretKeyDist = SecretKeyDist.UNIFORM_TERNARY
+    parameters.SetSecretKeyDist(secretKeyDist)
+
+    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
+    
+    parameters.SetScalingModSize(dcrtBits)
+    parameters.SetScalingTechnique(rescaleTech)
+    parameters.SetFirstModSize(firstMod)
+
+    levelBudget = [4, 4]
+    approxBootstrappDepth = 8
+
+    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()
+
+if __name__ == '__main__':
+    main()