CKKS scaling issue on Ciphertext

I am using CKKS to carryout a certain operation.
I keep getting the error


terminate called after throwing an instance of 'lbcrypto::OpenFHEException'
  what(): openfhe-development/src/pke/lib/encoding/ckkspackedencoding.cpp:l.353:Encode(): 
Overflow in data encoding - scaled input is too large to fit into a NativeInteger (60 bits). Try decreasing scaling factor.
Overflow at slot number 0
- Max real part contribution from input[7594]: inf
- Max imaginary part contribution from input[4294967295]: -1
Scaling factor is 56 bits 
Scaled input is inf bits 

Aborted (core dumped)

I have tried Bootstrapping the ciphertext but it doesn’t work. Any ideas on how to fix this issue will be highly appreciated

I think the issue has to do with my parameters


 int dcrtBits               = 55;
    int firstMod               = 58;

    auto secretKeyDist = SPARSE_TERNARY;
    parameters.SetSecretKeyDist(SPARSE_TERNARY);
    // parameters.SetSecurityLevel(lbcrypto::HEStd_128_classic);
    parameters.SetSecurityLevel(lbcrypto::HEStd_NotSet);
    parameters.SetNumLargeDigits(3);
    parameters.SetRingDim(1 << ringDim);
    parameters.SetBatchSize(num_slots);
    level_budget = {4, 4};
    ScalingTechnique rescaleTech = FLEXIBLEAUTO;

    parameters.SetScalingModSize(dcrtBits);
    parameters.SetFirstModSize(firstMod);
    parameters.SetScalingTechnique(rescaleTech);

But I don’t really understand how to fix it. Any ideas will be greatly appreciated

The magnitude of your data seems large. Make sure your data does not grow too much.

Try reducing the dcrtbits down to 50 to check if your application work.
You can set firstMod=60 too.

If you really need to work with large precision in CKKS, try using the 128-bit version. Here is an example of how to use that, but you need to rebuild OpenFHE with NATIVE_SIZE set to 128.

Note 128-bit CKKS is slower than its 64-bit counterpart.

thank you @Caesar how do I understand how to set those values? How do they affect the precision, security etc. I don’t seem to see a quick resource that can assist me in understanding how to set it.

The parameter firstMod defines the zero-level modulus in CKKS. In most CKKS papers, it is denoted as q_0 \approx 2^{firstMod}. You must ensure that q_0 is larger than all other q_i for correct decryption. Loosely speaking, you can think of q_0 as the plaintext modulus where your messages should not grow beyond it.

The parameter dcrtbBits defines the rest of the moduli q_i \approx 2^{dcrtBits}, and the quantity 2^{dcrtBits} is the message scaling factor parameter in CKKS, usually donated as \Delta in most CKKS papers.

The library is well documented to explain the main purpose of each parameter. Check the examples provided such as this example for how to use CKKS.

Feel free to ask here for any parameter you would like to understand.

Thank you @Caesar . I am not very the issue on the rescaling value. I have tried several values of firstMod and dcrtbBits but I am still running into this issue.

Basically, I am developing a convolution neural network network.
This happens when I start down sampling particularly when the total data in my ciphertext reduces from the vector size of 16384 to 8192. I think the data is till within the same range but unfortunately, I can’t even see the data in the new ciphertext because I can’t decrypt.

Why can’t you decrypt? Do you get an error or an exception? It is possible that the ciphertext was corrupted due to calculation errors or incorrect configuration. Try to decrypt after each operation in your code and see where things break.

It is not clear how you do the down-sampling but this could be the procedure corrupting your ciphertext. It would be better to provide further details so we can help you better.

@Caesar I can not decrypt because of the error above.
Depending on my values of firstMod and dcrtbBits, I get the above error or

pke/lib/encoding/ckkspackedencoding.cpp:l.543:Decode(): The decryption failed because the approximation error is too high. Check the parameters. Aborted (core dumped).

I think the issue is because of incorrect configurations.

I have a convolution layer with striding. In the specific case where this issue occurs, the previous convolution layer takes a ciphertext of 16384 elements and outputs 8192 elements. When I decrypt the output, the results are correct. I then pass this ciphertext as input into the convolution layer again this time without striding. The total number of elements in the output is the same as in the input. ( This same case works fine and gives the correct results in all previous layers when the ciphertext still contained 16384 elements), that is when I get the errors above depending on the parameters.

I have exhausted by thought process around this issue as I am very sure my code is functionally correct.

This is typical behavior when the ciphertext is corrupted - too much noise is accumulated in the ciphertext.
Try the following:

  1. increase the multiplicative depth a bit
  2. try the scaling technique FLEXIBLEAUTOEXT which provides higher precision.
  3. set the unneeded slots in your ciphertext to zero by multiplying by a suitable binary mask vector of zeros and ones. Specify one for slots you want to keep and zero otherwise.
  4. decrypt right before the layer that breaks and estimate the magnitude of the coefficients. Are they large or small numbers? If they are too large, scale them down by a constant factor like 10 or 100 to make them smaller.
  5. lastly, you can try the 128-bit version of CKKS in OpenFHE.

This is the best we can help you given that we do not have access to your code.

@Caesar thank you for the help.
I found the issue. You were right, I had some issues manipulating data that got it corrupted. I got it fixed