# Maximum CoefPacked/Packed Plaintext Size

**URL:** <https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420>\
**Category:** Library Questions\
**Created:** [March 20, 2023, 3:12pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420 "2023-03-20T15:12:09Z")\
**Posts on this page:** 9\
**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:** [March 20, 2023, 3:12pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/1 "2023-03-20T15:12:09Z")

</div>

Hello,

So I’m using the BFV scheme and experimenting with MakeCoefPackedPlaintext and MakePackedPlaintext. My question is, what is the maximum number of elements I can encrypt with each one?

For context, my `n = cryptoContext->GetCryptoParameters()->GetElementParams()->GetCyclotomicOrder() / 2 ` is equal to `n = 8192 `.

In MakePackedPlaintext, I get an error if I try to encrypt a vector with size 8194 (so I can encrypt a vector with 8193 elements). In MakeCoefPackedPlaintext, I can encrypt vectors with size 16385. I didn’t test this one for error since I can’t understand the limit. I thought 8192 elements would be the maximum size for both plaintexts.

---

<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:** [March 23, 2023, 4:07pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/2 "2023-03-23T16:07:33Z")

</div>

Theoretically, you should only be able to pack n values, where n is the underlying ring dimension. Note for PackedEncoding, the number of available slots depends on the plaintext modulus you choose. To get the maximum number of slots in PackedEncoding, your plaintext modulus t should be a prime integer that satisfies the following condition:  
1\equiv t \pmod{m=2n}

You can change t to get different plaintext algebras with a lower number of slots, but these are trickier to treat. For a complete treatment of this, you can refer to Appendix C in this [work](https://eprint.iacr.org/2011/566.pdf).

---

<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:** [March 25, 2023, 2:18am UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/3 "2023-03-25T02:18:12Z")

</div>

@Bernardo_Ramalho Yes, the limit should be 8192 for n = 8192. We will look into this. The exception handling for this case should be changed.

---

<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:** [March 25, 2023, 2:29am UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/4 "2023-03-25T02:29:05Z")

</div>

Created an issue for this: [MakeCoefPackedPlaintext allows encoding more integers than the number of slots · Issue #340 · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/issues/340)

---

<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:** [April 21, 2023, 7:15pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/5 "2023-04-21T19:15:19Z")

</div>

Hi @Bernardo_Ramalho, we failed to regenerate the problem reported above as OpenFHE does raise an exception for a proper info message as detailed [here](https://github.com/openfheorg/openfhe-development/issues/340). OpenFHE does not allow encoding vectors of size larger than n for both encoding methods `MakeCoefPackedPlaintext` and `MakePackedPlaintext`.  
If you can help us in regenerating this issue, that would be helpful so we can fix the issue if it exists.

---

<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:** [April 24, 2023, 3:32pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/6 "2023-04-24T15:32:53Z")

</div>

Hey, sorry for the delay but you can check the code I used to test this here [HE\_Optimization/plaintextSize.cpp at variance · BernardoRamalho/HE\_Optimization · GitHub](https://github.com/BernardoRamalho/HE_Optimization/blob/variance/tests/plaintextSize.cpp)

If you have any problem accessing it I can paste the code here

---

<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:** [April 24, 2023, 4:02pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/7 "2023-04-24T16:02:17Z")

</div>

Hi @Bernardo_Ramalho  
I have just executed your program against OpenFHE (dev branch: commit ID: `60b59f7008a2de0657ce6956badef647d2739772`) and I got the following putout:

```auto
n = 8192
log2 q = 180
Encrypting vector v of size: 8192
Encrypting vector r of size: 8194
terminate called after throwing an instance of 'lbcrypto::config_error'
  what(): /home/caesar/development/projects/openfhe_issues/openfhe-development/src/pke/include/encoding/plaintextfactory.h:89 The size [8194] of the vector with values should not be greater than ringDim [8192] if the scheme is NOT CKKS
Aborted

```

The behavior is expected and the message is clear enough to explain the issue.

---

<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:** [April 24, 2023, 4:21pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/8 "2023-04-24T16:21:48Z")

</div>

That is very weird, when I run I get the following output:

```auto
n = 8192
log2 q = 180
Encrypting vector v of size: 8192
Encrypting vector r of size: 8194
Encrypting vector p of size: 16384
Encrypting vector s of size: 16385
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

```

---

<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:** [April 24, 2023, 6:22pm UTC](https://openfhe.discourse.group/t/maximum-coefpacked-packed-plaintext-size/420/9 "2023-04-24T18:22:09Z")

</div>

I have just tried against OpenFHE the main branch (commit ID `122f470`) and I still got the same error message I noted above. Could you try running against [OpenFHE v1.0.3](https://github.com/openfheorg/openfhe-development/releases/tag/v1.0.3)?
