# Implementation of square root approximation without Cheby

**URL:** <https://openfhe.discourse.group/t/implementation-of-square-root-approximation-without-cheby/1812>\
**Category:** FHE Questions\
**Tags:** questions\
**Created:** [December 30, 2024, 3:22pm UTC](https://openfhe.discourse.group/t/implementation-of-square-root-approximation-without-cheby/1812 "2024-12-30T15:22:09Z")\
**Posts on this page:** 2\
**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 30, 2024, 3:22pm UTC](https://openfhe.discourse.group/t/implementation-of-square-root-approximation-without-cheby/1812/1 "2024-12-30T15:22:09Z")

</div>

I tried to implement the algorithm for approximating the square root from [https://eprint.iacr.org/2019/417.pdf](https://eprint.iacr.org/2019/417.pdf) Section 3.2

I know i can use chebyshev approximation but i tried to experiment with alternatives.

 ![Screenshot 2024-12-30 at 15.10.52](https://canada1.discourse-cdn.com/flex031/uploads/openfhe/original/1X/34db175f9e236e03b10dde258f24ce703dafeaf7.png)

```auto
auto ct_a = ctScaledSquares; // vector of downscaled values to meet the condition for 0 ≤ x ≤ 1
auto ct_b = cc->EvalSub(ct_a, 1);

int d = 15; // higher d consumes more levels and results in smaller values for every n
for (int n = 0; n < d; n++) {
   auto ct1 = cc->EvalMult(cc->EvalSub(1, ct_b), 0.5);
   ct_a = cc->EvalMult(ct_a, ct1);
   cout << ct_a->GetLevel() << endl;
   auto ct2 = cc->EvalMult(cc->EvalSub(ct_b, 3), 0.25);
   ct_b = cc->EvalMult(cc->EvalSquare(ct_b), ct2);
   cout << ct_b->GetLevel() << endl;
}

```

Did i forget something in that code? I am getting incorrect results and the approximation gets smaller with every iteration, so a higher `d` will result in smaller values.

---

<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 30, 2024, 3:33pm UTC](https://openfhe.discourse.group/t/implementation-of-square-root-approximation-without-cheby/1812/2 "2024-12-30T15:33:25Z")

</div>

Found the error by myself. I changed:

`auto ct1 = cc->EvalSub(1, cc->EvalMult(ct_b, 0.5));`
