# Can't enconde integer that is in range

**URL:** <https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553>\
**Category:** Library Questions\
**Created:** [May 16, 2023, 2:10pm UTC](https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553 "2023-05-16T14:10:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bernardo\_Ramalho](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/bernardo_ramalho/32/92_2.png) [@Bernardo\_Ramalho](https://openfhe.discourse.group/u/Bernardo_Ramalho)\
**Post date:** [May 16, 2023, 2:10pm UTC](https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553/1 "2023-05-16T14:10:14Z")

</div>

Hello,

I’m getting the following error:

```auto
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!

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [May 16, 2023, 2:52pm UTC](https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553/2 "2023-05-16T14:52:08Z")

</div>

Please share your code

---

<div class="post-metadata">

**Author:** ![Bernardo\_Ramalho](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/bernardo_ramalho/32/92_2.png) [@Bernardo\_Ramalho](https://openfhe.discourse.group/u/Bernardo_Ramalho)\
**Post date:** [May 16, 2023, 2:56pm UTC](https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553/3 "2023-05-16T14:56:50Z")

</div>

This is the part where I create the values:

```auto
// 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:

```auto
    // Set CryptoContext
    CCParams<CryptoContextBFVRNS> parameters;
    parameters.SetPlaintextModulus(65537);
    parameters.SetMultiplicativeDepth(2);

    CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters);
    // Enable features that you wish to use
    cryptoContext->Enable(PKE);
    cryptoContext->Enable(KEYSWITCH);
    cryptoContext->Enable(LEVELEDSHE);
    cryptoContext->Enable(ADVANCEDSHE);

    // Key Generation

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

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

    // Generate the relinearization key
    cryptoContext->EvalMultKeyGen(keyPair.secretKey);

    // 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;

        rotation_plaintexts.push_back(cryptoContext->MakeCoefPackedPlaintext(rotationVector));
    }

```

and this is where I encrypt:

```auto
// 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));
    }

```

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [May 16, 2023, 6:15pm UTC](https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553/4 "2023-05-16T18:15:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bernardo\_Ramalho](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/bernardo_ramalho/32/92_2.png) [@Bernardo\_Ramalho](https://openfhe.discourse.group/u/Bernardo_Ramalho)\
**Post date:** [May 16, 2023, 6:21pm UTC](https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553/5 "2023-05-16T18:21:39Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)\
**Post date:** [May 18, 2023, 10:07pm UTC](https://openfhe.discourse.group/t/cant-enconde-integer-that-is-in-range/553/6 "2023-05-18T22:07:46Z")

</div>

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.
