# Control ciphertext level after conversion from FHEW to CKKS

**URL:** <https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375>\
**Category:** Library Questions\
**Created:** [June 22, 2024, 12:20pm UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375 "2024-06-22T12:20:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![stone](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stone](https://openfhe.discourse.group/u/stone)\
**Post date:** [June 22, 2024, 12:20pm UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/1 "2024-06-22T12:20:43Z")

</div>

Hello, I have checked the code of `openfhe-development/src/pke/examples/scheme-switching.cpp` and found through testing that after FHEW is converted to CKKS, the ciphertext level of CKKS is 11. Can the ciphertext of CKKS be specified after ciphertext conversion? If so, how should it be set?

---

<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 26, 2024, 9:20am UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/2 "2024-06-26T09:20:28Z")

</div>

The conversion from FHEW to CKKS consumes a number of levels, depending on some parameters (e.g., whether you know the encrypted message is binary, on the scaling technique etc). You can control the total number of levels, i.e., how many levels you can still consume after the conversion to CKKS, by specifying the multiplicative depth of the CKKS cryptocontext.

---

<div class="post-metadata">

**Author:** ![stone](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stone](https://openfhe.discourse.group/u/stone)\
**Post date:** [June 27, 2024, 1:07am UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/3 "2024-06-27T01:07:45Z")

</div>

Yes, I know about CKKS comparison, but all FHEW don’t know much about it, I see OpenFHE provides `EvalCompareSchemeSwitching` this function in the library, I want to specify in the function run results of the level of the ciphertext

---

<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 27, 2024, 5:26pm UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/4 "2024-06-27T17:26:33Z")

</div>

You cannot specify how many levels this function consumes, because it is determined by some parameters. What you can do, is to specify the number of levels to support in the CKKS crypto context, such that you will have sufficient additional levels after you obtain the resulting ciphertext from e.g., `EvalCompareSchemeSwitching`. The comparison through scheme switching consumes 12 levels (you observed 11 levels, but if you print the noise scale degree, that shows 2), so you should specify a multiplicative depth of 12 + x in the cryptocontext generation, where x is the number of remaining levels you want.

You can look at the code of `EvalCompareSchemeSwitching` if you are interested to see why these many levels are consumed: there is a polynomial approximation for the light bootstrapping consuming 8 levels, there are 3 iterations of the double angle formula also consuming 3 levels, and finally there is a scaling consuming another level.

---

<div class="post-metadata">

**Author:** ![stone](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stone](https://openfhe.discourse.group/u/stone)\
**Post date:** [July 2, 2024, 12:19am UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/5 "2024-07-02T00:19:10Z")

</div>

Oh, I understand `EvalCompareSchemeSwitching` function. So if I want to implement my own level, I’ll use `EvalCKKStoFHEW` and `EvalFHEWtoCKKS` if I need to so that I can control the CKKS level of ciphertext, right?

---

<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:** [July 2, 2024, 7:43am UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/6 "2024-07-02T07:43:05Z")

</div>

If you want to use scheme switching for a functionality that it not yet implemented, then yes, you should use `EvalCKKStoFHEW` and `EvalFHEWtoCKKS` and whatever else you need in between. But again, the number of levels consumed by these functions is fixed, and you control how many levels remain from the CKKS cryptocontext.

---

<div class="post-metadata">

**Author:** ![stone](https://avatars.discourse-cdn.com/v4/letter/s/c89c15/32.png) [@stone](https://openfhe.discourse.group/u/stone)\
**Post date:** [October 8, 2024, 2:26am UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/7 "2024-10-08T02:26:13Z")

</div>

Can you tell me the principle behind implementing the `EvalCompareSchemeSwitching` function？

---

<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:** [October 8, 2024, 2:14pm UTC](https://openfhe.discourse.group/t/control-ciphertext-level-after-conversion-from-fhew-to-ckks/1375/8 "2024-10-08T14:14:33Z")

</div>

EvalCompareSchemeSwitching has 4 steps:

1. Subtract the two CKKS input ciphertexts to obtain the difference.
2. Convert the difference to FHEW ciphertexts.
3. Evaluate the sign of each of these ciphertexts (encrypts 0 if positive and 1 if negative)
4. Convert the encrypted signs back to CKKS.

Please create a new thread if you have additional questions, since this one is on a different topic and has been marked as solved.
