Can't enconde integer that is in range


I’m getting the following error:

terminate called after throwing an instance of 'lbcrypto::config_error'
  what():  /home/bernardoramalho99/Tese/openfhe-development/src/pke/lib/encoding/coefpackedencoding.cpp:52 Cannot encode integer 38780 at position 5 because it is out of range of plaintext modulus 65537

I don’t understand how 38780 which smaller than 65537 is out of range of the modulus. Is it a bug or am I not understanding something?

Thanks in advance!

Please share your code

This is the part where I create the values:

// Header of file contains information about nr of vector and the size of each of them
    int64_t number_vectors, size_vectors, number;
    std::vector<int64_t> all_numbers;

    numbers_file >> number_vectors;
    numbers_file >> size_vectors;

    int64_t total_elements = size_vectors * number_vectors;
    int64_t alpha = 81;
           // , alpha1 = 3505;
    int alphaValue = 1;
    // Body of the file contains all the numbers
    while (numbers_file >> number) {
        all_numbers.push_back(number * alphaValue % 65537);
        alphaValue = alphaValue * alpha % 65537;

Where I create the context:

    // Set CryptoContext
    CCParams<CryptoContextBFVRNS> parameters;

    CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters);
    // Enable features that you wish to use

    // Key Generation

    // Initialize Public Key Containers
    KeyPair<DCRTPoly> keyPair;

    // Generate a public/private key pair
    keyPair = cryptoContext->KeyGen();

    // Generate the relinearization key

    // Generate the rotation plaintexts
    std::vector<Plaintext> rotation_plaintexts;
    Plaintext plaintextRot;

    for(int i = 0; i < number_rotations; i++){
            // Create vector of size 8192 filled with 0
        std::vector<int64_t> rotationVector(8191, 0);

        // Rotating by 2^i --> element @ index 2^i = 1
            rotationVector[(int)pow(2, i)] = 1;


and this is where I encrypt:

// Create Plaintexts
    std::vector<Ciphertext<DCRTPoly>> ciphertexts;
   Ciphertext<DCRTPoly> processedCipher;
    int begin, end;

    for(int i = 0; i < number_vectors; i++){
        // Calculate beginning and end of plaintext values
        begin = i * size_vectors;
        end = size_vectors * (i + 1);

        // Encode Plaintext  with coefficient packing and encrypt it into a ciphertext vector
        Plaintext plaintext = cryptoContext->MakeCoefPackedPlaintext(std::vector<int64_t>(all_numbers.begin() + begin, all_numbers.begin() + end));
        ciphertexts.push_back(cryptoContext->Encrypt(keyPair.publicKey, plaintext));

For CoefficientPackedEncoding, you can encode integers in the range [-(p-1)/2,(p-1)/2], where p is the plaintext coefficient modulus. The integer 38780 is not in the allowed range of [-32768,32768], so an exception was thrown.

Thanks so much. I didn’t know that. Could you tell me why that happens (or link me to a resource I can read to understand it)?

This is the standard way of representing signed integers mod p in a non-redundant way. 38780 would be allowed only if using unsigned representation mod p.