Skip to content

Commit 4536dd2

Browse files
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 <[email protected]>
1 parent 6e45fa6 commit 4536dd2

File tree

4 files changed

+35
-31
lines changed

4 files changed

+35
-31
lines changed

examples/pke/simple-real-numbers.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,38 @@ def main():
5151
# Step 5: Decryption and output
5252
# Decrypt the result of additions
5353
ptAdd = cc.Decrypt(c_add, keys.secretKey)
54+
print("\nResults of homomorphic additions: ")
55+
print(ptAdd)
5456

5557
# We set the precision to 8 decimal digits for a nicer output.
5658
# If you want to see the error/noise introduced by CKKS, bump it up
5759
# to 15 and it should become visible.
5860

5961
precision = 8
60-
print("Results of homomorphic computations:")
62+
print("\nResults of homomorphic computations:")
6163
result = cc.Decrypt(c1, keys.secretKey)
6264
result.SetLength(batch_size)
63-
print("x1 = " + str(result))
64-
print("Estimated precision in bits: " + str(result.GetLogPrecision()))
65+
print("x1 = " + result.GetFormattedValues(precision))
6566

6667
# Decrypt the result of scalar multiplication
6768
result = cc.Decrypt(c_scalar, keys.secretKey)
6869
result.SetLength(batch_size)
69-
print("4 * x1 = " + str(result))
70+
print("4 * x1 = " + result.GetFormattedValues(precision))
7071

7172
# Decrypt the result of multiplication
7273
result = cc.Decrypt(c_mult, keys.secretKey)
7374
result.SetLength(batch_size)
74-
print("x1 * x2 = " + str(result))
75+
print("x1 * x2 = " + result.GetFormattedValues(precision))
7576

7677
# Decrypt the result of rotations
7778
result = cc.Decrypt(c_rot1, keys.secretKey)
7879
result.SetLength(batch_size)
79-
print("In rotations, very small outputs (~10^-10 here) correspond to 0's:")
80-
print("x1 rotated by 1 = " + str(result))
80+
print("\nIn rotations, very small outputs (~10^-10 here) correspond to 0's:")
81+
print("x1 rotated by 1 = " + result.GetFormattedValues(precision))
8182

8283
result = cc.Decrypt(c_rot2, keys.secretKey)
8384
result.SetLength(batch_size)
84-
print("x1 rotated by -2 = " + str(result))
85+
print("x1 rotated by -2 = " + result.GetFormattedValues(precision))
8586

8687

8788
if __name__ == "__main__":

src/include/pke/cryptocontext_wrapper.h

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,34 @@
2828
#ifndef OPENFHE_CRYPTOCONTEXT_BINDINGS_H
2929
#define OPENFHE_CRYPTOCONTEXT_BINDINGS_H
3030

31+
#include "bindings.h"
32+
#include "openfhe.h"
33+
3134
#include <pybind11/pybind11.h>
3235
#include <pybind11/stl.h>
3336
#include <vector>
3437
#include <algorithm>
3538
#include <complex>
36-
#include "openfhe.h"
37-
#include "bindings.h"
39+
3840

3941
namespace py = pybind11;
4042
using namespace lbcrypto;
4143
using ParmType = typename DCRTPoly::Params;
4244

43-
Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly>& self,
44-
ConstCiphertext<DCRTPoly> ciphertext);
45+
Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly> &self,
46+
ConstCiphertext<DCRTPoly> ciphertext);
4547

46-
Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly>& self,
47-
ConstCiphertext<DCRTPoly> ciphertext,
48-
const usint index,
49-
const usint m,
50-
ConstCiphertext<DCRTPoly> digits);
51-
Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);
48+
Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly> &self,
49+
ConstCiphertext<DCRTPoly> ciphertext,
50+
const usint index,
51+
const usint m,
52+
ConstCiphertext<DCRTPoly> digits);
53+
Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly> &self, ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);
5254

53-
Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,
54-
ConstCiphertext<DCRTPoly> ciphertext,const PrivateKey<DCRTPoly> privateKey);
55-
Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,
56-
const PrivateKey<DCRTPoly> privateKey,ConstCiphertext<DCRTPoly> ciphertext);
55+
Plaintext DecryptWrapper(CryptoContext<DCRTPoly> &self,
56+
ConstCiphertext<DCRTPoly> ciphertext, const PrivateKey<DCRTPoly> privateKey);
57+
Plaintext DecryptWrapper(CryptoContext<DCRTPoly> &self,
58+
const PrivateKey<DCRTPoly> privateKey, ConstCiphertext<DCRTPoly> ciphertext);
5759
Plaintext MultipartyDecryptFusionWrapper(CryptoContext<DCRTPoly>& self,const std::vector<Ciphertext<DCRTPoly>>& partialCiphertextVec);
5860

5961
const std::map<usint, EvalKey<DCRTPoly>> EvalAutomorphismKeyGenWrapper(CryptoContext<DCRTPoly>& self,const PrivateKey<DCRTPoly> privateKey,const std::vector<usint> &indexList);

src/lib/bindings.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,17 +1080,16 @@ void bind_encodings(py::module &m)
10801080
.def("GetStringValue", &PlaintextImpl::GetStringValue)
10811081
.def("SetStringValue", &PlaintextImpl::SetStringValue)
10821082
.def("SetIntVectorValue", &PlaintextImpl::SetIntVectorValue)
1083+
.def("GetFormattedValues", &PlaintextImpl::GetFormattedValues)
10831084
.def("__repr__", [](const PlaintextImpl &p)
10841085
{
10851086
std::stringstream ss;
1086-
ss << "<Plaintext Object: ";
1087-
p.PrintValue(ss);
1088-
ss << ">";
1087+
ss << "<Plaintext Object: " << p << ">";
10891088
return ss.str(); })
10901089
.def("__str__", [](const PlaintextImpl &p)
10911090
{
10921091
std::stringstream ss;
1093-
p.PrintValue(ss);
1092+
ss << p;
10941093
return ss.str(); });
10951094
}
10961095

src/lib/pke/cryptocontext_wrapper.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28+
#include "cryptocontext_wrapper.h"
29+
#include <openfhe.h>
30+
2831
#include <pybind11/pybind11.h>
2932
#include <pybind11/stl.h>
30-
#include <openfhe.h>
3133
#include <vector>
3234
#include <algorithm>
33-
#include <complex>
34-
#include "cryptocontext_wrapper.h"
35+
#include <complex>
36+
#include <iomanip>
3537

3638
using namespace lbcrypto;
3739
namespace py = pybind11;
@@ -117,7 +119,7 @@ const double GetScalingFactorRealWrapper(CryptoContext<DCRTPoly>& self, uint32_t
117119
return scFactor;
118120
}
119121
else{
120-
OPENFHE_THROW(not_available_error, "GetScalingFactorRealWrapper: Invalid scheme");
122+
OPENFHE_THROW("Invalid scheme");
121123
return 0;
122124
}
123125
}
@@ -146,7 +148,7 @@ const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext<DCRTPoly> & self
146148
return cryptoParams->GetScalingTechnique();
147149
}
148150
else{
149-
OPENFHE_THROW(not_available_error, "GetScalingTechniqueWrapper: Invalid scheme");
151+
OPENFHE_THROW("Invalid scheme");
150152
}
151153

152154
}

0 commit comments

Comments
 (0)