Skip to content

Commit

Permalink
Use a new function to print plaintext values
Browse files Browse the repository at this point in the history
  • Loading branch information
dsuponitskiy-duality committed Aug 14, 2024
1 parent c83f553 commit fc9ea27
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 35 deletions.
10 changes: 5 additions & 5 deletions examples/pke/simple-real-numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
1 change: 0 additions & 1 deletion src/include/pke/cryptocontext_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const uint64_t GetModulusCKKSWrapper(CryptoContext<DCRTPoly>& self);
const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext<DCRTPoly>& self);
const usint GetDigitSizeWrapper(CryptoContext<DCRTPoly>& self);

std::string GetPlaintextValuesWithPrecision(PlaintextImpl& ptxt, int precision);
void ClearEvalMultKeysWrapper();

#endif // OPENFHE_CRYPTOCONTEXT_BINDINGS_H
3 changes: 1 addition & 2 deletions src/lib/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
27 changes: 0 additions & 27 deletions src/lib/pke/cryptocontext_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,33 +153,6 @@ const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext<DCRTPoly> & 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<std::complex<double>>& values = ptxt.GetCKKSPackedValue();
// get rid of trailing zeroes
size_t i = values.size();
bool allZeroes = true;
while (i > 0) {
if (values[--i] != std::complex<double>(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<DCRTPoly>::ClearEvalMultKeys();
}

0 comments on commit fc9ea27

Please sign in to comment.