From fc9ea271cf61fb1f2cc13f9f2df9c2d152c4c840 Mon Sep 17 00:00:00 2001 From: Dmitriy Suponitskiy Date: Tue, 13 Aug 2024 21:20:21 -0400 Subject: [PATCH] Use a new function to print plaintext values --- examples/pke/simple-real-numbers.py | 10 ++++----- src/include/pke/cryptocontext_wrapper.h | 1 - src/lib/bindings.cpp | 3 +-- src/lib/pke/cryptocontext_wrapper.cpp | 27 ------------------------- 4 files changed, 6 insertions(+), 35 deletions(-) diff --git a/examples/pke/simple-real-numbers.py b/examples/pke/simple-real-numbers.py index 0847686..300673d 100644 --- a/examples/pke/simple-real-numbers.py +++ b/examples/pke/simple-real-numbers.py @@ -62,27 +62,27 @@ def main(): print("\nResults of homomorphic computations:") result = cc.Decrypt(c1, keys.secretKey) result.SetLength(batch_size) - print("x1 = " + GetPlaintextValuesWithPrecision(result, precision)) + 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 = " + GetPlaintextValuesWithPrecision(result, precision)) + 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 = " + GetPlaintextValuesWithPrecision(result, precision)) + print("x1 * x2 = " + result.GetFormattedValues(precision)) # Decrypt the result of rotations result = cc.Decrypt(c_rot1, keys.secretKey) result.SetLength(batch_size) print("\nIn rotations, very small outputs (~10^-10 here) correspond to 0's:") - print("x1 rotated by 1 = " + GetPlaintextValuesWithPrecision(result, precision)) + print("x1 rotated by 1 = " + result.GetFormattedValues(precision)) result = cc.Decrypt(c_rot2, keys.secretKey) result.SetLength(batch_size) - print("x1 rotated by -2 = " + GetPlaintextValuesWithPrecision(result, precision)) + print("x1 rotated by -2 = " + result.GetFormattedValues(precision)) if __name__ == "__main__": diff --git a/src/include/pke/cryptocontext_wrapper.h b/src/include/pke/cryptocontext_wrapper.h index 6e005af..8a28595 100644 --- a/src/include/pke/cryptocontext_wrapper.h +++ b/src/include/pke/cryptocontext_wrapper.h @@ -68,7 +68,6 @@ const uint64_t GetModulusCKKSWrapper(CryptoContext& self); const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext& self); const usint GetDigitSizeWrapper(CryptoContext& self); -std::string GetPlaintextValuesWithPrecision(PlaintextImpl& ptxt, int precision); void ClearEvalMultKeysWrapper(); #endif // OPENFHE_CRYPTOCONTEXT_BINDINGS_H diff --git a/src/lib/bindings.cpp b/src/lib/bindings.cpp index f1ccf85..c68a30b 100644 --- a/src/lib/bindings.cpp +++ b/src/lib/bindings.cpp @@ -1080,6 +1080,7 @@ 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; @@ -1090,8 +1091,6 @@ void bind_encodings(py::module &m) std::stringstream ss; ss << p; return ss.str(); }); - - m.def("GetPlaintextValuesWithPrecision", &GetPlaintextValuesWithPrecision); } void bind_ciphertext(py::module &m) diff --git a/src/lib/pke/cryptocontext_wrapper.cpp b/src/lib/pke/cryptocontext_wrapper.cpp index b033f3f..d901c79 100644 --- a/src/lib/pke/cryptocontext_wrapper.cpp +++ b/src/lib/pke/cryptocontext_wrapper.cpp @@ -153,33 +153,6 @@ const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext & self } -std::string GetPlaintextValuesWithPrecision(PlaintextImpl& ptxt, int precision) { - if(!isCKKS(ptxt.GetSchemeID())) - OPENFHE_THROW("Implemented for CKKSPackedEncoding only"); - - std::stringstream ss; - ss << "("; - - const std::vector>& values = ptxt.GetCKKSPackedValue(); - // get rid of trailing zeroes - size_t i = values.size(); - bool allZeroes = true; - while (i > 0) { - if (values[--i] != std::complex(0, 0)) { - allZeroes = false; - break; - } - } - if(allZeroes == false) { - for (size_t j = 0; j <= i; ++j) - ss << std::setprecision(precision) << values[j].real() << ", "; - } - ss << " ... ); "; - ss << "Estimated precision: " << ptxt.GetLogPrecision() << " bits"; - - return ss.str(); -} - void ClearEvalMultKeysWrapper() { CryptoContextImpl::ClearEvalMultKeys(); } \ No newline at end of file