# OpenFHE SetSlots method: what is the usage scenario?

**URL:** https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620
**Category:** Library Questions
**Tags:** openfhe-help
**Created:** [June 9, 2023, 6:09am UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620 "2023-06-09T06:09:58Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Knightley](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Knightley](https://openfhe.discourse.group/u/Knightley)
#### Post date: [June 9, 2023, 6:09am UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/1 "2023-06-09T06:09:58Z")

</div>

I know slots is something like the batch size. What is the usage scenario of SetSlots method?  
In a machine learning application, the size of cipher text will become smaller due to downsampling, can OpenFHE change batch size or slots dynamically to enable sparse packing and then make bootstrap faster?

---

<div class="post-metadata">

### Author: ![narger](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/narger/32/420_2.png) [@narger](https://openfhe.discourse.group/u/narger)
#### Post date: [June 9, 2023, 3:10pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/2 "2023-06-09T15:10:09Z")

</div>

Yep, I guess you can

> [@Difference between sparse/full packing](https://openfhe.discourse.group/t/difference-between-sparse-full-packing/493):
>
> Hello, I have a question about the types of packing in CKKS. There are two examples: sparse and full. I got that full packing uses all the slots of the ciphertext, while sparse does not. My question is: what is the threshold that divides sparse and full packing? I mean, if I have 8 slots and use 7 of them, it is considered a sparse packing? Will it benefit of faster bootstrapping and so on?

---

<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: [June 9, 2023, 11:35pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/3 "2023-06-09T23:35:06Z")

</div>

`SetSlots` can be used to treat the current CKKS ciphertext as a sparse ciphertext (subring element). However, internally this means that a smaller vector has to be cloned as many times as needed to fill the maximum number of slots (half the ring dimension in CKKS). If this ciphertext is not set up this way, the result will be incorrect. GetSlots() is used during CKKS bootstrapping, and during encoding/decoding. All of these run faster for sparse ciphertexts. Note that the number of slots should be set accordingly in `EvalBootstrapSetup` and `EvalBootstrapKeyGen` to make bootstrapping faster for sparse ciphertexts.

---

<div class="post-metadata">

### Author: ![Knightley](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Knightley](https://openfhe.discourse.group/u/Knightley)
#### Post date: [June 30, 2023, 2:20am UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/5 "2023-06-30T02:20:20Z")

</div>

Thanks for the explanation. Because the size of valid ciphertext is changed in my application. When it becomes smaller, before doing bootstrap, I use setSlots method to set to the small value(the size of currently valid cipher text), but after bootstrap, the value is incorrect.  
For example:  
The ring dimension is 65536. At first, the size of valid cipher text is 16384. After some computations, the size of valid cipher text becomes 32768. Then 8192, then 4096. I want to change the slots size to these small value to make bootstrapping faster. But Using setSlots method make the results become incorrect.  
Could you tell me why? And how can I improve it? Thanks

---

<div class="post-metadata">

### Author: ![narger](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/narger/32/420_2.png) [@narger](https://openfhe.discourse.group/u/narger)
#### Post date: [June 30, 2023, 10:24am UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/6 "2023-06-30T10:24:49Z")

</div>

On which numSlot did you run the bootstrap setup?

---

<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: [June 30, 2023, 5:47pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/7 "2023-06-30T17:47:22Z")

</div>

When `SetSlots` is set to a smaller value, you want to make sure that the sparsely packed ciphertext is a proper subring element for the desired number of slots. For example, let N be the ring dimension and n the desired number of slots, a subring element with n slots is formed by cloning the n-sized vector \frac{N}{2n} times. CKKS bootstrapping will work correctly only if the sparsely packed ciphertext is cloned this way.

A naive way to achieve this is by clearing all slots above n (multiply by a binary mask) and then cloning the vector using rotations and additions (similar to `EvalSum`). This does consume one level. If you can achieve this cloned structure in the previous step, then the extra cost of one level can be avoided.

---

<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: [June 30, 2023, 5:53pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/8 "2023-06-30T17:53:45Z")

</div>

In addition to proper cloning of the ciphertext, `EvalBootstrapSetup` and `EvalBootstrapKeyGen` should be called for that value of `numSlots`, as pointed out by @narger.

---

<div class="post-metadata">

### Author: ![Knightley](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Knightley](https://openfhe.discourse.group/u/Knightley)
#### Post date: [July 3, 2023, 12:10pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/9 "2023-07-03T12:10:22Z")

</div>

after clear/rotate/addition, it works, thanks.

---

<div class="post-metadata">

### Author: ![damionfan](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@damionfan](https://openfhe.discourse.group/u/damionfan)
#### Post date: [June 14, 2024, 7:03am UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/10 "2024-06-14T07:03:24Z")

</div>

what is the cloned format? a0b0c0 or abcabc?

---

<div class="post-metadata">

### Author: ![andreea.alexandru](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/andreea.alexandru/32/378_2.png) [@andreea.alexandru](https://openfhe.discourse.group/u/andreea.alexandru)
#### Post date: [June 14, 2024, 1:43pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/11 "2024-06-14T13:43:10Z")

</div>

Cloning means repetition, so your input will be repeated abcabc, in the sense that you will have batch\_size | batch\_size | … | batch\_size for the ring\_dimension / batch\_size times.

---

<div class="post-metadata">

### Author: ![Knightley](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Knightley](https://openfhe.discourse.group/u/Knightley)
#### Post date: [June 17, 2025, 12:25pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/12 "2025-06-17T12:25:31Z")

</div>

Another question. If the valid data in the ciphertext is abc, and N=32. The number of valid data cannot be divided by N. How to replicate it in this case?

---

<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: [June 17, 2025, 12:48pm UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/13 "2025-06-17T12:48:01Z")

</div>

@Knightley please ask in a new topic as this is a new question

---

<div class="post-metadata">

### Author: ![Knightley](https://avatars.discourse-cdn.com/v4/letter/k/ba9def/32.png) [@Knightley](https://openfhe.discourse.group/u/Knightley)
#### Post date: [June 18, 2025, 2:34am UTC](https://openfhe.discourse.group/t/openfhe-setslots-method-what-is-the-usage-scenario/620/14 "2025-06-18T02:34:57Z")

</div>

> [@Knightley](#):
>
> If the valid data in the ciphertext is abc, and N=32. The number of valid data cannot be divided by N. How to replicate it in this case?

ok. [When the valid data length is an odd number, how to replicate before SetSlots?](https://openfhe.discourse.group/t/when-the-valid-data-length-is-an-odd-number-how-to-replicate-before-setslots/2069)
