# Unexpected Behavior with SetScalingModSize and FLEXIBLEAUTOEXT in BGV Scheme

**URL:** https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111
**Category:** Library Questions
**Tags:** questions, bugs
**Created:** [March 8, 2024, 2:01pm UTC](https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111 "2024-03-08T14:01:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Maokami](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/maokami/32/180_2.png) [@Maokami](https://openfhe.discourse.group/u/Maokami)
#### Post date: [March 8, 2024, 2:01pm UTC](https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111/1 "2024-03-08T14:01:24Z")

</div>

Hello OpenFHE Team,

I’ve encountered a unexpected behavior in the library and wanted to report it.  
Here is a minimal snippet of the code that reproduces the issue:

```auto
#include "openfhe.h"

using namespace lbcrypto;
using namespace std;

int main(void)
{
  CCParams<CryptoContextBGVRNS> parameters;
  parameters.SetRingDim(32768);
  parameters.SetMultiplicativeDepth(2);
  parameters.SetPlaintextModulus(65537);
  parameters.SetFirstModSize(52);
  parameters.SetScalingModSize(50);
  parameters.SetSecurityLevel(HEStd_256_classic);
  parameters.SetScalingTechnique(FLEXIBLEAUTOEXT);

  CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
  cc->Enable(PKE);
  cc->Enable(KEYSWITCH);
  cc->Enable(LEVELEDSHE);
  return 0;
}

```

When executed on OpenFHE version 1.0.4, this code results in a crash with the following output:

```auto
floating point exception (core dumped)

```

And when I change it to the latest version (OpenFHE v1.1.3), it throws the following exception:

```auto
terminate called after throwing an instance of 'lbcrypto::OpenFHEException'
  what(): NativeIntegerT ComputeMu: Divide by zero

```

The program terminates normally if the ScalingTechnique is changed to something else, the scheme is switched to BFV/CKKS, or if `SetScalingModSize` is not called.

Could you please clarify if using `SetScalingTechnique` with the BGV scheme is incorrect or if there might be another issue at play?

Thank you for your attention to this matter.

---

<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 8, 2024, 7:47pm UTC](https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111/2 "2024-03-08T19:47:50Z")

</div>

Thank you. FLEXIBLEAUTOEXT automatically selects all parameters. So these parameters should not be allowed to be configured for this mode. We will create an issue and fix this in the next release (v1.1.5).

---

<div class="post-metadata">

### Author: ![Maokami](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/maokami/32/180_2.png) [@Maokami](https://openfhe.discourse.group/u/Maokami)
#### Post date: [March 14, 2024, 8:47am UTC](https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111/3 "2024-03-14T08:47:15Z")

</div>

> [@ypolyakov](#):
>
> Thank you. FLEXIBLEAUTOEXT automatically selects all parameters. So these parameters should not be allowed to be configured for this mode. We will create an issue and fix this in the next release (v1.1.5).

Thank you for your response. I have one more related question. Are these parameters also prohibited from being configured in FIXEDAUTO and FLEXIBLEAUTO modes? I found that the two codes below also cause a `floating point exception (core dumped)` crash **in the latest version (v1.1.4)**, and I’m curious if it’s for the same reason.

```auto
int main(void)
{
    CCParams<CryptoContextCKKSRNS> parameters;
    parameters.SetRingDim(32768);
    parameters.SetMultiplicativeDepth(2);
    parameters.SetPlaintextModulus(65537);
    parameters.SetFirstModSize(20);
    parameters.SetScalingModSize(19);
    parameters.SetSecurityLevel(HEStd_256_classic);
    parameters.SetScalingTechnique(FIXEDAUTO);

    CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
    cc->Enable(PKE);
    cc->Enable(KEYSWITCH);
    cc->Enable(LEVELEDSHE);
    return 0;
}

```

```auto
int main(void)
{
    CCParams<CryptoContextCKKSRNS> parameters;
    parameters.SetRingDim(32768);
    parameters.SetMultiplicativeDepth(2);
    parameters.SetPlaintextModulus(65537);
    parameters.SetFirstModSize(17);
    parameters.SetScalingModSize(17);
    parameters.SetSecurityLevel(HEStd_256_classic);
    parameters.SetScalingTechnique(FLEXIBLEAUTO);

    CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
    cc->Enable(PKE);
    cc->Enable(KEYSWITCH);
    cc->Enable(LEVELEDSHE);
    return 0;
}

```

---

<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 14, 2024, 4:55pm UTC](https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111/4 "2024-03-14T16:55:08Z")

</div>

All BGV modes compute all moduli using the MultiplicativeDepth, EvalAddCount, and EvalKeyswitchCount supplied by the user. So we should also throw an exception for the examples you added. Will will do this as part of the same issue.

---

<div class="post-metadata">

### Author: ![Maokami](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/maokami/32/180_2.png) [@Maokami](https://openfhe.discourse.group/u/Maokami)
#### Post date: [March 14, 2024, 5:47pm UTC](https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111/5 "2024-03-14T17:47:35Z")

</div>

Thank you for your response.

I just have a quick question

> [@ypolyakov](#):
>
> All BGV modes compute all moduli using the MultiplicativeDepth, EvalAddCount, and EvalKeyswitchCount supplied by the user.

Does this hold in CKKS? I’m asking because the crashes I mentioned for the two pieces of code are occurring within CKKS. (Please ignore the line with `parameters.SetPlaintextModulus(65537);`)

---

<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 14, 2024, 5:59pm UTC](https://openfhe.discourse.group/t/unexpected-behavior-with-setscalingmodsize-and-flexibleautoext-in-bgv-scheme/1111/6 "2024-03-14T17:59:43Z")

</div>

It is a similar issue: the plaintext modulus is not used by CKKS. The high-level idea is that we will add scheme-specific validation of input parameters to handle all these situations.
