Working with Coefficent Packed Plaintexts

Hello,

I made a simple program to calculate the sum of all the values in all the plaintexts using Packed Encoding. Now I wanted to compare the performance with Coefficient Packed Encoding. I’m using BFVrns, and the code is the following:

    /...../
    for(int i = 0; i < number_vectors; i++){
        begin = i * number_vectors + i;
        end = size_vectors * (i + 1);

        Plaintext plaintext = cryptoContext->MakeCoefPackedPlaintext(std::vector<int64_t>(all_numbers.begin() + begin, all_numbers.begin() + end));
        ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext));
    }

    TOC(t);
    processingTime = TOC(t);

    std::cout << "Time spend during encryption " << processingTime << "ms" << std::endl;

    TIC(t);

    auto ciphertextAdd = cryptoContext->EvalAddMany(ciphertexts);
    auto ciphertextRot = ciphertextAdd;

    for(int i = 0; i < size_vectors; i++){
        ciphertextRot = cryptoContext->EvalRotate(ciphertextRot, 1);

        ciphertextAdd = cryptoContext->EvalAdd(ciphertextAdd, ciphertextRot);
    }
    /...../

If I use Plaintext plaintext = cryptoContext->MakePackedPlaintext(.....); everything works but with the code above I get the following error:

Time spend during encryption 71ms
terminate called after throwing an instance of 'lbcrypto::type_error'
  what():  /usr/local/include/openfhe/pke/encoding/plaintext.h:361 not a packed coefficient vector
Aborted (core dumped)

I assume that I cannot use cryptoContext->EvalAddMany() , but I can’t find anything in the documentation that mentions it. Do I need to set up anything during the context?

Thanks for the help in advance :smiley:

The Coefficient packed encoding represents a vector of integers using the coefficients of polynomials. This means that it supports addition and scalar multiplication (both are done coefficient-wise). For multiplication, it does polynomial multiplication (not regular component-wise multiplication). Rotation for this case is also much trickier than for regular (CRT) packed encoding and is not implemented in OpenFHE. This is why you are getting the exception. Without running the code, I assume that the exception is thrown by EvalRotate rather than EvalAddMany.

Generally speaking, EvalRotate and related operations only work for the regular packed encoding.

Sorry for the late response. I thought I had already responded.

What you said is totally correct and it was indeed the rotation that was causing the problem.