# ChebyshevFunction evaluation error

**URL:** https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403
**Category:** Library Questions
**Tags:** questions
**Created:** [July 5, 2024, 7:38am UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403 "2024-07-05T07:38:08Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sumw](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sumw](https://openfhe.discourse.group/u/sumw)
#### Post date: [July 5, 2024, 7:38am UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/1 "2024-07-05T07:38:08Z")

</div>

Hi, I am using ChebyshevFunction to evaluate exp(x). But I encountered some error.

My ciphertext - a can be decrypted into:  
[0.923663 -8.27976e-06 5.00274e-06 -9.2856e-06 -2.67874e-06 0.950437 0.999738 0.999993 -1.93079e-05 7.29427e-06 -1.34528e-05 -1.0477e-05 2.14772e-06 4.70038e-06 6.8613e-06 -1.6628e-05 1.68422e-05 -8.30343e-06 -1.22689e-05 9.37811e-06]  
My ChebyshevFunction usage is:

```auto
    double lowerBound = -1.0;
    double upperBound = 1.0;
    uint32_t polyDegree = 27;
    a = cc->EvalBootstrap(a);
    a = cc->EvalChebyshevFunction([](double x) -> double { return std::exp(x); }, a, lowerBound,
                                              upperBound, polyDegree);

```

My firstModSize is 42, the multiple depth for the whole program is 14, scale factor is 35.  
But I received the following error when trying to decrypt:  
The decryption failed because the approximation error is too high. Check the parameters.

---

<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: [July 5, 2024, 1:22pm UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/2 "2024-07-05T13:22:17Z")

</div>

firstModSize and scaling factor are way too small for the bootstrapping and a precise polynomial evaluation, try to use larger values (as the ones in the examples)

---

<div class="post-metadata">

### Author: ![sumw](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sumw](https://openfhe.discourse.group/u/sumw)
#### Post date: [July 5, 2024, 1:57pm UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/3 "2024-07-05T13:57:22Z")

</div>

Thanks for your fast reply. I am using small values because I need to use some rotation keys for this program (other parts of the program), and they have large memory sizes. To use larger parameters, I need to find out how to reduce key sizes. (I created a new topic to follow up on that) Is there a walkaround on the Chebyshev Function side? Are there ways to estimate the required first mode size and scale factor for the Chebyshev Function?

---

<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: [July 5, 2024, 2:08pm UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/4 "2024-07-05T14:08:54Z")

</div>

I think that the ciphertext is already “done” after the bootstrapping, try to print it before the chebyshev evaluation.

---

<div class="post-metadata">

### Author: ![sumw](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sumw](https://openfhe.discourse.group/u/sumw)
#### Post date: [July 5, 2024, 5:15pm UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/5 "2024-07-05T17:15:11Z")

</div>

Hi, my ciphertext is printed before bootstrapping, it comes from other parts of my program. Essentially, the ciphertext `a` in `a = cc->EvalBootstrap(a);` contains the value [0.923663 -8.27976e-06 5.00274e-06 -9.2856e-06 -2.67874e-06 0.950437 0.999738 0.999993 -1.93079e-05 7.29427e-06 -1.34528e-05 -1.0477e-05 2.14772e-06 4.70038e-06 6.8613e-06 -1.6628e-05 1.68422e-05 -8.30343e-06 -1.22689e-05 9.37811e-06].

---

<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: [July 5, 2024, 5:42pm UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/6 "2024-07-05T17:42:15Z")

</div>

Yeah, but is it (almost) the same before and after the bootstrap?

---

<div class="post-metadata">

### Author: ![sumw](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@sumw](https://openfhe.discourse.group/u/sumw)
#### Post date: [July 5, 2024, 7:22pm UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/7 "2024-07-05T19:22:46Z")

</div>

Thanks for asking that, it raises a good point, when I try to decrypt after Bootstrap as following:

```auto
cout << "ChebyshevFunction begin" << endl;
    double lowerBound = -2.0;
    double upperBound = 2.0;
    uint32_t polyDegree = 27;
    a = cc->EvalBootstrap(a);

    Plaintext outcome;
    cc->Decrypt(keys.secretKey, a, &outcome);
    outcome->SetLength(20);
    auto dec_outcome = outcome->GetRealPackedValue();
    cout << "After bootstrapping" << ": ";
    cout << dec_outcome << endl;  
    cout << endl;

```

I will also get the error: The decryption failed because the approximation error is too high. Check the parameters.

The larger piece of my program follows like this:

```auto
        auto a = cc->EvalMult(x1, x2);

        Plaintext outcome;
        cc->Decrypt(keys.secretKey, a, &outcome);
        outcome->SetLength(20);
        auto dec_outcome = outcome->GetRealPackedValue();
        cout << "[Debug]" << ": ";
        cout << dec_outcome << endl;  
        cout << endl;

        double lowerBound = -1.0;
        double upperBound = 1.0;
        uint32_t polyDegree = 27;
        a = cc->EvalBootstrap(a);
        Plaintext outcome2;
        cc->Decrypt(keys.secretKey, a, &outcome2);
        outcome->SetLength(20);
        auto dec_outcome2 = outcome2->GetRealPackedValue();
        cout << "[Debug]" << ": ";
        cout << dec_outcome2 << endl;  
        cout << endl;

        a = cc->EvalChebyshevFunction([](double x) -> double { return std::exp(x); }, a, lowerBound, upperBound, polyDegree);
        

```

---

<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: [July 5, 2024, 7:43pm UTC](https://openfhe.discourse.group/t/chebyshevfunction-evaluation-error/1403/8 "2024-07-05T19:43:02Z")

</div>

You should increase firstModSize and dcrtBits ([CKKS Bootstrapping gives incorrect results on scale != 60? - #4 by ypolyakov](https://openfhe.discourse.group/t/ckks-bootstrapping-gives-incorrect-results-on-scale-60/844/4))
