# Relation between ring dimension, batch size, num slots and length

**URL:** <https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036>\
**Category:** Library Questions\
**Created:** [January 24, 2024, 11:28am UTC](https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036 "2024-01-24T11:28:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arseniy](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@Arseniy](https://openfhe.discourse.group/u/Arseniy)\
**Post date:** [January 24, 2024, 11:28am UTC](https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036/1 "2024-01-24T11:28:32Z")

</div>

Hi!  
I use OpenFHE to perform some simple numerical simulations. And I have questions to the naming and meaning of several parameters used in OpenFHE. It would be great if someone could clarify it me.

Is a batch size used to configure a CryptoContext in `parameters.SetBatchSize(batchSize);` the same as num slots used to create Plaintext in `cryptoContext->MakeCKKSPackedPlaintext(x, 1, depth - 1, nullptr, numSlots);`? Are their interchangeable or have to be the same?

Which relation do batch size and num slots have to ring dimension and length of data vector. ( length of data vector could be not a power of two, unlike three other parameters)

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![Cesare](https://avatars.discourse-cdn.com/v4/letter/c/dfb087/32.png) [@Cesare](https://openfhe.discourse.group/u/Cesare)\
**Post date:** [January 25, 2024, 8:34am UTC](https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036/2 "2024-01-25T08:34:30Z")

</div>

In the CKKS scheme, slots \* 2 = dimension. The maximum value of batch size is slots. (Only in CKKS scheme, BGV/BFV is different from CKKS)  
I think the remainder is filled with 0 when the length of vector is less than batch size.

---

<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:** [January 26, 2024, 5:35am UTC](https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036/3 "2024-01-26T05:35:51Z")

</div>

When the number of slots is specified during `MakeCKKSPackedPlaintext`, that number of slots is set for a specific plaintext/ciphertext. The batch size is the global (default) setting for all ciphertexts in the cryptocontext.

---

<div class="post-metadata">

**Author:** ![Arseniy](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@Arseniy](https://openfhe.discourse.group/u/Arseniy)\
**Post date:** [January 26, 2024, 10:23am UTC](https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036/4 "2024-01-26T10:23:21Z")

</div>

Thank you very much!  
Could you also take look at following questions? Just to ensure, that I’m configure `num_slots` and `batch_size` in effective way.

- Can `num_slots` be greater than the originally specified `batch_size`?
- What is the motivation is to create `Plaintext`s with different `num_slots` from a single `CryptoContext` (CC)? Are there performance benefits?
- Is it possible to do arithmetic with plaintext/ciphertests of different `num_slots`?
- If the previous question was answered with “yes”, and the one before that with “better performance”, is there any reason to _not_ always set `num_slots` to the smallest power-of-two such that the data fits?

Example for the last question: If `batchSize == 128` , but my data is either `length == 30` or `length == 50` , should I just always set `num_slots` to `32` and `64` respectively?

---

<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:** [February 1, 2024, 4:18am UTC](https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036/5 "2024-02-01T04:18:21Z")

</div>

> - Can `num_slots` be greater than the originally specified `batch_size`?

Yes

> - What is the motivation is to create `Plaintext`s with different `num_slots` from a single `CryptoContext` (CC)? Are there performance benefits?

Primarily for CKKS bootstrapping (it gets slightly cheaper for a smaller number of slots). Also the approximation error is smaller in some operations when working with more sparse ciphertexts.

> - Is it possible to do arithmetic with plaintext/ciphertests of different `num_slots`?

No, it does not make sense (in the same binary operation)

---

<div class="post-metadata">

**Author:** ![sloede](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/sloede/32/198_2.png) [@sloede](https://openfhe.discourse.group/u/sloede)\
**Post date:** [February 1, 2024, 1:50pm UTC](https://openfhe.discourse.group/t/relation-between-ring-dimension-batch-size-num-slots-and-length/1036/6 "2024-02-01T13:50:52Z")

</div>

OK, so just to confirm I understood it correctly:

- The _technical_ maximum number of slots for CKKS-based encoding is `GetRingDimension()/2` (inherent in CKKS)
- When specifying `batchSize` for `CCParams`, …
  - it is used as one of the factors to determine the required ring dimension
  - it is used to set the _default_ number of slots for newly created `Plaintext`s/`Ciphertext`s

- Using `GetSlots` on a plaintext (or its derived ciphertext) created without specifying `slots` in `MakeCKKSPackedPlaintext`, results in the number of slots equal to the batch size
- Using `SetSlots` on a plaintext or _before_ creating a ciphertext allows one to dynamically reduce the number of slots.
- Using `SetSlots` _after_ a ciphertext was created to change the number of slots generally leads to a _broken_ ciphertext unless manually.

At least this is what I have found from experimentation…
