Explorar el Código

Added additional information to the trace from simple-real-numbers.py (#164)

* Added additional information to the trace from simple-real-numbers.py

* Use a new function to print plaintext values

---------

Co-authored-by: Dmitriy Suponitskiy <dsuponitskiy@dualitytech.com>
dsuponitskiy hace 1 año
padre
commit
4536dd27ad

+ 9 - 8
examples/pke/simple-real-numbers.py

@@ -51,37 +51,38 @@ def main():
     # Step 5: Decryption and output
     # Decrypt the result of additions
     ptAdd = cc.Decrypt(c_add, keys.secretKey)
+    print("\nResults of homomorphic additions: ")
+    print(ptAdd)
 
     # 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:")
+    print("\nResults of homomorphic computations:")
     result = cc.Decrypt(c1, keys.secretKey)
     result.SetLength(batch_size)
-    print("x1 = " + str(result))
-    print("Estimated precision in bits: " + str(result.GetLogPrecision()))
+    print("x1 = " + result.GetFormattedValues(precision))
 
     # Decrypt the result of scalar multiplication
     result = cc.Decrypt(c_scalar, keys.secretKey)
     result.SetLength(batch_size)
-    print("4 * x1 = " + str(result))
+    print("4 * x1 = " + result.GetFormattedValues(precision))
 
     # Decrypt the result of multiplication
     result = cc.Decrypt(c_mult, keys.secretKey)
     result.SetLength(batch_size)
-    print("x1 * x2 = " + str(result))
+    print("x1 * x2 = " + result.GetFormattedValues(precision))
 
     # Decrypt the result of rotations
     result = cc.Decrypt(c_rot1, keys.secretKey)
     result.SetLength(batch_size)
-    print("In rotations, very small outputs (~10^-10 here) correspond to 0's:")
-    print("x1 rotated by 1 = " + str(result))
+    print("\nIn rotations, very small outputs (~10^-10 here) correspond to 0's:")
+    print("x1 rotated by 1 = " + result.GetFormattedValues(precision))
 
     result = cc.Decrypt(c_rot2, keys.secretKey)
     result.SetLength(batch_size)
-    print("x1 rotated by -2 = " + str(result))
+    print("x1 rotated by -2 = " + result.GetFormattedValues(precision))
 
 
 if __name__ == "__main__":

+ 16 - 14
src/include/pke/cryptocontext_wrapper.h

@@ -28,32 +28,34 @@
 #ifndef OPENFHE_CRYPTOCONTEXT_BINDINGS_H
 #define OPENFHE_CRYPTOCONTEXT_BINDINGS_H
 
+#include "bindings.h"
+#include "openfhe.h"
+
 #include <pybind11/pybind11.h>
 #include <pybind11/stl.h>
 #include <vector>
 #include <algorithm>
 #include <complex>
-#include "openfhe.h"
-#include "bindings.h"
+
 
 namespace py = pybind11;
 using namespace lbcrypto;
 using ParmType = typename DCRTPoly::Params;
 
-Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly>& self,
-                                                        ConstCiphertext<DCRTPoly> ciphertext);
+Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly> &self,
+                                                       ConstCiphertext<DCRTPoly> ciphertext);
 
-Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly>& self,
-                                            ConstCiphertext<DCRTPoly> ciphertext,
-                                              const usint index,
-                                              const usint m,
-                                              ConstCiphertext<DCRTPoly> digits);
-Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);
+Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly> &self,
+                                             ConstCiphertext<DCRTPoly> ciphertext,
+                                             const usint index,
+                                             const usint m,
+                                             ConstCiphertext<DCRTPoly> digits);
+Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly> &self, ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);
 
-Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,
-ConstCiphertext<DCRTPoly> ciphertext,const PrivateKey<DCRTPoly> privateKey);
-Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,
-const PrivateKey<DCRTPoly> privateKey,ConstCiphertext<DCRTPoly> ciphertext);
+Plaintext DecryptWrapper(CryptoContext<DCRTPoly> &self,
+                         ConstCiphertext<DCRTPoly> ciphertext, const PrivateKey<DCRTPoly> privateKey);
+Plaintext DecryptWrapper(CryptoContext<DCRTPoly> &self,
+                         const PrivateKey<DCRTPoly> privateKey, ConstCiphertext<DCRTPoly> ciphertext);
 Plaintext MultipartyDecryptFusionWrapper(CryptoContext<DCRTPoly>& self,const std::vector<Ciphertext<DCRTPoly>>& partialCiphertextVec);
 
 const std::map<usint, EvalKey<DCRTPoly>> EvalAutomorphismKeyGenWrapper(CryptoContext<DCRTPoly>& self,const PrivateKey<DCRTPoly> privateKey,const std::vector<usint> &indexList);

+ 3 - 4
src/lib/bindings.cpp

@@ -1080,17 +1080,16 @@ void bind_encodings(py::module &m)
         .def("GetStringValue", &PlaintextImpl::GetStringValue)
         .def("SetStringValue", &PlaintextImpl::SetStringValue)
         .def("SetIntVectorValue", &PlaintextImpl::SetIntVectorValue)
+        .def("GetFormattedValues", &PlaintextImpl::GetFormattedValues)
         .def("__repr__", [](const PlaintextImpl &p)
              {
         std::stringstream ss;
-        ss << "<Plaintext Object: ";
-        p.PrintValue(ss);
-        ss << ">";
+        ss << "<Plaintext Object: " << p << ">";
         return ss.str(); })
         .def("__str__", [](const PlaintextImpl &p)
              {
         std::stringstream ss;
-        p.PrintValue(ss);
+        ss << p;
         return ss.str(); });
 }
 

+ 7 - 5
src/lib/pke/cryptocontext_wrapper.cpp

@@ -25,13 +25,15 @@
 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+#include "cryptocontext_wrapper.h"
+#include <openfhe.h>
+
 #include <pybind11/pybind11.h>
 #include <pybind11/stl.h>
-#include <openfhe.h>
 #include <vector>
 #include <algorithm>
-#include <complex> 
-#include "cryptocontext_wrapper.h"
+#include <complex>
+#include <iomanip>
 
 using namespace lbcrypto;
 namespace py = pybind11;
@@ -117,7 +119,7 @@ const double GetScalingFactorRealWrapper(CryptoContext<DCRTPoly>& self, uint32_t
         return scFactor;
     }
     else{
-        OPENFHE_THROW(not_available_error, "GetScalingFactorRealWrapper: Invalid scheme");
+        OPENFHE_THROW("Invalid scheme");
         return 0;
     }
 }
@@ -146,7 +148,7 @@ const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext<DCRTPoly> & self
         return cryptoParams->GetScalingTechnique();
     }
     else{
-        OPENFHE_THROW(not_available_error, "GetScalingTechniqueWrapper: Invalid scheme");
+        OPENFHE_THROW("Invalid scheme");
     }
 
 }