Przeglądaj źródła

Merge pull request #55 from openfheorg/rsoliveira-ckks-boot-bug

Update ckks-boot example (Issue #54)
Rener Oliveira 1 rok temu
rodzic
commit
d84144aa11

+ 12 - 7
examples/pke/iterative-ckks-bootstrapping.py

@@ -21,9 +21,14 @@ def iterative_bootstrap_example():
     parameters.SetSecurityLevel(SecurityLevel.HEStd_NotSet)
     parameters.SetRingDim(1 << 12)
 
-    rescale_tech = ScalingTechnique.FLEXIBLEAUTO
-    dcrt_bits = 59
-    first_mod = 60
+    if get_native_int()==128:
+        rescale_tech = ScalingTechnique.FIXEDAUTO
+        dcrt_bits = 78
+        first_mod = 89
+    else:
+        rescale_tech = ScalingTechnique.FLEXIBLEAUTO
+        dcrt_bits = 59
+        first_mod = 60
 
     parameters.SetScalingModSize(dcrt_bits)
     parameters.SetScalingTechnique(rescale_tech)
@@ -35,12 +40,10 @@ def iterative_bootstrap_example():
     num_iterations = 2
 
     level_budget = [3, 3]
-    # Each extra iteration on top of 1 requires an extra level to be consumed.
-    approx_bootstrapp_depth = 8 + (num_iterations - 1)
     bsgs_dim = [0,0]
 
-    levels_used_before_bootstrap = 10
-    depth = levels_used_before_bootstrap + FHECKKSRNS.GetBootstrapDepth(approx_bootstrapp_depth, level_budget, secret_key_dist)
+    levels_available_after_bootstrap = 10
+    depth = levels_available_after_bootstrap = 10 + FHECKKSRNS.GetBootstrapDepth(level_budget, secret_key_dist) + (num_iterations - 1)
     parameters.SetMultiplicativeDepth(depth)
 
     # Generate crypto context
@@ -80,6 +83,8 @@ def iterative_bootstrap_example():
         Here, we assume all ciphertexts in the cryptoContext will have num_slots slots.
         We start with a depleted ciphertext that has used up all of its levels."""
     ptxt = cryptocontext.MakeCKKSPackedPlaintext(x, 1, depth -1,None,num_slots)
+    ptxt.SetLength(num_slots)
+    print(f"Input: {ptxt}")
 
     # Encrypt the encoded vectors
     ciph = cryptocontext.Encrypt(key_pair.publicKey, ptxt)

+ 14 - 10
examples/pke/simple-ckks-bootstrapping.py

@@ -12,20 +12,24 @@ def simple_bootstrap_example():
     parameters.SetSecurityLevel(SecurityLevel.HEStd_NotSet)
     parameters.SetRingDim(1<<12)
 
-    rescale_tech = ScalingTechnique.FLEXIBLEAUTO
-    dcrt_bits = 59
-    first_mod = 60
+    if get_native_int()==128:
+        rescale_tech = ScalingTechnique.FIXEDAUTO
+        dcrt_bits = 78
+        first_mod = 89
+    else:
+        rescale_tech = ScalingTechnique.FLEXIBLEAUTO
+        dcrt_bits = 59
+        first_mod = 60
     
     parameters.SetScalingModSize(dcrt_bits)
     parameters.SetScalingTechnique(rescale_tech)
     parameters.SetFirstModSize(first_mod)
 
     level_budget = [4, 4]
-    approx_bootstrapp_depth = 8
 
-    levels_used_before_bootstrap = 10
+    levels_available_after_bootstrap = 10
 
-    depth = levels_used_before_bootstrap + FHECKKSRNS.GetBootstrapDepth(approx_bootstrapp_depth, level_budget, secret_key_dist)
+    depth = levels_available_after_bootstrap + FHECKKSRNS.GetBootstrapDepth(level_budget, secret_key_dist)
 
     parameters.SetMultiplicativeDepth(depth)
 
@@ -51,18 +55,18 @@ def simple_bootstrap_example():
     x = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
     encoded_length = len(x)
 
-    ptxt = cryptocontext.MakeCKKSPackedPlaintext(x)
+    ptxt = cryptocontext.MakeCKKSPackedPlaintext(x,1,depth-1)
     ptxt.SetLength(encoded_length)
 
-    print(f"Input: {x}")
+    print(f"Input: {ptxt}")
 
     ciph = cryptocontext.Encrypt(key_pair.publicKey, ptxt)
 
-    print(f"Initial number of levels remaining: {ciph.GetLevel()}")
+    print(f"Initial number of levels remaining: {depth - ciph.GetLevel()}")
 
     ciphertext_after = cryptocontext.EvalBootstrap(ciph)
 
-    print(f"Number of levels remaining after bootstrapping: {ciphertext_after.GetLevel()}")
+    print(f"Number of levels remaining after bootstrapping: {depth - ciphertext_after.GetLevel()}")
 
     result = cryptocontext.Decrypt(ciphertext_after,key_pair.secretKey)
     result.SetLength(encoded_length)

+ 4 - 4
src/include/docstrings/cryptocontext_docs.h

@@ -143,8 +143,8 @@ const char* cc_MakeCKKSPackedPlaintextComplex_docs = R"pbdoc(
 
     :param value: input vector (of complex numbers)
     :type value: list
-    :param depth: depth used to encode the vector
-    :type depth: int
+    :param scaleDeg: degree of scaling factor used to encode the vector
+    :type scaleDeg: int
     :param level: level at each the vector will get encrypted
     :type level: int
     :param params: parameters to be used for the ciphertext (Only accepting params = None in this version)
@@ -160,8 +160,8 @@ const char* cc_MakeCKKSPlaintextReal_docs = R"pbdoc(
 
     :param value: input vector (of floats)
     :type value: list
-    :param depth: depth used to encode the vector
-    :type depth: int
+    :param scaleDeg: degree of scaling factor used to encode the vector
+    :type scaleDeg: int
     :param level: level at each the vector will get encrypted
     :type level: int
     :param params: parameters to be used for the ciphertext (Only accepting params = None in this version)

+ 4 - 3
src/lib/bindings.cpp

@@ -134,13 +134,13 @@ void bind_crypto_context(py::module &m)
         // TODO (Oliveira): allow user to specify different params values
         .def("MakeCKKSPackedPlaintext", static_cast<Plaintext (CryptoContextImpl<DCRTPoly>::*)(const std::vector<std::complex<double>> &, size_t, uint32_t, const std::shared_ptr<ParmType>, usint) const>(&CryptoContextImpl<DCRTPoly>::MakeCKKSPackedPlaintext), cc_MakeCKKSPackedPlaintextComplex_docs,
              py::arg("value"),
-             py::arg("depth") = static_cast<size_t>(1),
+             py::arg("scaleDeg") = static_cast<size_t>(1),
              py::arg("level") = static_cast<uint32_t>(0),
              py::arg("params") = py::none(),
              py::arg("slots") = 0)
         .def("MakeCKKSPackedPlaintext", static_cast<Plaintext (CryptoContextImpl<DCRTPoly>::*)(const std::vector<double> &, size_t, uint32_t, const std::shared_ptr<ParmType>, usint) const>(&CryptoContextImpl<DCRTPoly>::MakeCKKSPackedPlaintext), cc_MakeCKKSPlaintextReal_docs,
              py::arg("value"),
-             py::arg("depth") = static_cast<size_t>(1),
+             py::arg("scaleDeg") = static_cast<size_t>(1),
              py::arg("level") = static_cast<uint32_t>(0),
              py::arg("params") = py::none(),
              py::arg("slots") = 0)
@@ -794,7 +794,8 @@ void bind_schemes(py::module &m){
     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));                               
+        .def_static("GetBootstrapDepth", static_cast<uint32_t (*)(uint32_t, const std::vector<uint32_t>&, SecretKeyDist)>(&FHECKKSRNS::GetBootstrapDepth))
+        .def_static("GetBootstrapDepth", static_cast<uint32_t (*)(const std::vector<uint32_t>&, SecretKeyDist)>(&FHECKKSRNS::GetBootstrapDepth));                               
     
 }