Why does decryption of a fresh ciphertext, or a single EvalMul using a plaintext and a fresh ciphertext, result in NaN?

I have a question regarding the Python wrapper for OpenFHE (version 0.8.4).
I encountered an issue.

The problem is that the result of decrypting a fresh ciphertext, or of decrypting a ciphertext that has been through a single EvalMul using a plaintext and a fresh ciphertext, sometimes becomes “NaN” after performing certain operations over the ciphertext space (these operations resulted in a RuntimeError: “The decryption failed because the approximation error is too high. Check the parameters.”).

Specifically, when decrypting `a_pt` inside a loop, I noticed that it sometimes produces the correct values, but at unpredictable iterations, the decryption output suddenly becomes NaN:

a = [1] * batch_size
a_pt = cc.MakeCKKSPackedPlaintext(a)
a_ct = cc.Encrypt(keys.publicKey, a_pt)

dim = 2 ** 8

for i in tqdm(range(dim)):
temp_pt = cc.Decrypt(keys.secretKey, a_ct)
temp = temp_pt.GetCKKSPackedValue()
print(temp[:4])

Interestingly, if I restart the computer and run the same loop, the output is always correct without producing NaN.

I would like to ask why this kind of error occurs.

The exact code is as follows::

#######

import Receiver
from openfhe import *
from tqdm.notebook import tqdm

mult_depth = 41
scale_mod_size = 59
batch_size = (131072 >> 1)
ring_dim = 131072

parameters = CCParamsCKKSRNS()
parameters.SetMultiplicativeDepth(mult_depth)
parameters.SetScalingModSize(scale_mod_size)
parameters.SetBatchSize(batch_size)
parameters.SetScalingTechnique(FLEXIBLEAUTOEXT)
parameters.SetRingDim(ring_dim)
parameters.SetSecurityLevel(HEStd_128_classic)
parameters.SetExecutionMode(EXEC_EVALUATION)

cc = GenCryptoContext(parameters)

cc.Enable(PKESchemeFeature.PKE)
cc.Enable(PKESchemeFeature.KEYSWITCH)
cc.Enable(PKESchemeFeature.LEVELEDSHE)
cc.Enable(PKESchemeFeature.ADVANCEDSHE)

keys = cc.KeyGen()
cc.EvalMultKeyGen(keys.secretKey)

a = [1] * batch_size
b = [2] * batch_size

a_pt = cc.MakeCKKSPackedPlaintext(a)

b_pt = cc.MakeCKKSPackedPlaintext(b)

a_ct = cc.Encrypt(keys.publicKey, a_pt)
b_ct = cc.Encrypt(keys.publicKey, b_pt)

dim = 2 ** 8

for i in tqdm(range(dim)):
temp_pt = cc.Decrypt(keys.secretKey,a_ct)
temp = temp_pt.GetCKKSPackedValue()
print(temp[:4])

for i in tqdm(range(dim)):
temp_ct = cc.EvalMult(a_ct, b_ct)
temp_pt = cc.Decrypt(keys.secretKey,temp_ct)
temp = temp_pt.GetCKKSPackedValue()
print(temp[:4])

print(“End”)

I ran your code on my machine with OpenFHE-Python v1.4.0 and I did not face any issues.
You might be running out of memory on your machine. The cryptographic parameters are unnecessarily set very large. That might be causing some memory issues on your system.

You do not need that high multiplicative depth for the computation in your code. Accordingly, you do not need a large ring dimension.