# Modulus missmatch after EvalCompareSchemeSwitch()

**URL:** <https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779>\
**Category:** Library Questions\
**Tags:** questions, openfhe-help\
**Created:** [December 16, 2024, 10:57am UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779 "2024-12-16T10:57:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![DenisRed](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/denisred/32/303_2.png) [@DenisRed](https://openfhe.discourse.group/u/DenisRed)\
**Post date:** [December 16, 2024, 10:57am UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/1 "2024-12-16T10:57:04Z")

</div>

Hi guys,

i tried to compare two CKKS ciphertexts with EvalCompareSchemeSwitch() and it throws a Modulus missmatch error. Can you tell me why this is thrown and how to avoid it?

```auto
SchSwchParams params;
    params.SetSecurityLevelCKKS(sl);
    params.SetSecurityLevelFHEW(slBin);
    params.SetCtxtModSizeFHEWLargePrec(logQ_LWE);
    params.SetNumSlotsCKKS(4096);
    params.SetNumValues(4096);
    params.SetComputeArgmin(true);
    params.SetOneHotEncoding(oneHot);

auto ctCompareDistances = serverCC->EvalCompareSchemeSwitching(ct_1, ct_2);

```

---

<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:** [December 16, 2024, 4:48pm UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/2 "2024-12-16T16:48:47Z")

</div>

Please provide a minimum working example that can be compiled. Have you followed the example from [openfhe-development/src/pke/examples/scheme-switching.cpp at main · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/scheme-switching.cpp#L638)?

---

<div class="post-metadata">

**Author:** ![DenisRed](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/denisred/32/303_2.png) [@DenisRed](https://openfhe.discourse.group/u/DenisRed)\
**Post date:** [December 17, 2024, 12:53pm UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/3 "2024-12-17T12:53:50Z")

</div>

I cannot provide you a working example, because one of the ciphertext vectors i want to compare is the result of some computations for euclidian distance. The other vector is created with the value i want to compare. Heres some pseudo code:

```auto
    vector radius(zeros.size(), 0.5);
    auto ct_radius = serverCC->Encrypt(serverPublicKey, serverCC->MakeCKKSPackedPlaintext(radius, 1, 0, nullptr, slots));
    auto ct_result = // result of euclidian distance calculations

    auto ctCompareDistances = serverCC->EvalCompareSchemeSwitching(ct_radius, ct_result, slots, slots);

// This runs into a modulus missmatch error

// If I replace ct_result with for example
    vector some_other_vector(zeros.size(), 1.568);
    auto ct_some_other_vector = serverCC->Encrypt(serverPublicKey, serverCC->MakeCKKSPackedPlaintext(some_other_vector, 1, 0, nullptr, slots));

    auto ctCompareDistances = serverCC->EvalCompareSchemeSwitching(ct_radius, ct_some_other_vector, slots, slots);

// This somehow works

```

What is the condition for throwing a modulus missmatch?

---

<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:** [December 17, 2024, 3:10pm UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/4 "2024-12-17T15:10:36Z")

</div>

The condition for throwing a modulus missmatch error is a modulus missmatch – without a compilable code, I can’t say where it happens, although I suspect that since ct\_result is a result of leveled computations, the issue lies there.

- Can you subtract ct\_radius and ct\_result and decrypt without errors?
- What scaling technique are you using? You might need to manually align the scaling factors (it is a good practice to check ct\_result-\>GetLevel() and ct\_result-\>GetNoiseScaleDeg()).
- Go to ckksrns-schemeswitching.cpp in SWITCHCKKSRNS::EvalCompareSchemeSwitching and check at which point the error occurs.

---

<div class="post-metadata">

**Author:** ![DenisRed](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/denisred/32/303_2.png) [@DenisRed](https://openfhe.discourse.group/u/DenisRed)\
**Post date:** [January 20, 2025, 2:08pm UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/5 "2025-01-20T14:08:41Z")

</div>

Hi @andreea.alexandru,

what do you mean with manually aligning the scaling factors? I checked `ct_result->GetLevel()` and `ct_result->GetNoiseScaleDeg()` and it gave me 3 and 2. What do i have to do with this information? =) I tried so much but i can’t bring this to run …

---

<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:** [January 20, 2025, 5:04pm UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/6 "2025-01-20T17:04:28Z")

</div>

You only gave one piece of response to the above questions, which is not sufficient to identify the problem. Are you able to subtract ct\_radius and ct\_result and decrypt without errors? What scaling technique are you using? If you are using FIXEDMANUAL, you need to manually align the scaling factors of ct\_result and ct\_radius, by running ModReduceInPlace on ct\_result. At which point in EvalCompareSchemeSwitching is the error thrown? Again, without a minimum working example, it is difficult to guess the problem in vacuum.

---

<div class="post-metadata">

**Author:** ![DenisRed](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/denisred/32/303_2.png) [@DenisRed](https://openfhe.discourse.group/u/DenisRed)\
**Post date:** [January 21, 2025, 8:30am UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/7 "2025-01-21T08:30:21Z")

</div>

> [@andreea.alexandru](#):
>
> Are you able to subtract ct\_radius and ct\_result and decrypt without errors?

Yes, that works fine.

> [@andreea.alexandru](#):
>
> What scaling technique are you using?

I’m using FLEXIBLEAUTO.

> [@andreea.alexandru](#):
>
> At which point in EvalCompareSchemeSwitching is the error thrown?

I tried to debug and dive into it and it is thrown in

```auto
 return GetScheme()->EvalCompareSchemeSwitching(ciphertext1, ciphertext2, numCtxts, numSlots, pLWE, scaleSign,
                                                       unit);

```

in the `cryptocontext.h` file

---

<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:** [January 29, 2025, 6:58pm UTC](https://openfhe.discourse.group/t/modulus-missmatch-after-evalcompareschemeswitch/1779/8 "2025-01-29T18:58:00Z")

</div>

Please try to come up with a small minimum working example that can be run and recreates the issue. I could not recreate the issue on my end even when one of the ciphertexts to be compared is obtained as a result of encrypted computations involving multiplications and rotations.
