# RotationKey error

**URL:** <https://openfhe.discourse.group/t/rotationkey-error/1677>\
**Category:** Library Questions\
**Tags:** questions\
**Created:** [October 30, 2024, 12:58pm UTC](https://openfhe.discourse.group/t/rotationkey-error/1677 "2024-10-30T12:58:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wowblk8](https://avatars.discourse-cdn.com/v4/letter/w/e79b87/32.png) [@wowblk8](https://openfhe.discourse.group/u/wowblk8)\
**Post date:** [October 30, 2024, 12:58pm UTC](https://openfhe.discourse.group/t/rotationkey-error/1677/1 "2024-10-30T12:58:38Z")

</div>

Hi OpenFHE Team!

While running the below code with the UBSAN tool, a potential OpenFHE-related error was detected.

```c++
cc->EvalAtIndexKeyGen(keys.secretKey, {1, -2});
cc->EvalRotateKeyGen(keys.secretKey, {1, -2});

```

In the two lines of code responsible for generating keys(Each of these lines individually triggers a UBSAN message, and I believe both lines lead to a call to this function, which is why I included them together), setting the values to -2, -3, or further negative numbers causes UBSAN to issue an overflow error. which is `/data/homomorphic/openfhe-1.2.2/openfhe-development/src/core/lib/math/nbtheory2.cpp:269:20: runtime error: signed integer overflow: 52429 * 52429 cannot be represented in type 'int'`. This led us to further examine line 269 of the file `nbtheory2.cpp` for a deeper understanding.

```c++
uint32_t FindAutomorphismIndex2nComplex(int32_t i, uint32_t m) {
    if (i == 0) {
        return 1;
    }

    // conjugation automorphism
    if (i == int32_t(m - 1)) {
        return uint32_t(i);
    }
    else {
        // generator
        int32_t g0;

        if (i < 0) {
            g0 = NativeInteger(5).ModInverse(m).ConvertToInt();
        }
        else {
            g0 = 5;
        }
        uint32_t i_unsigned = (uint32_t)std::abs(i);

        int32_t g = g0;
        for (size_t j = 1; j < i_unsigned; j++) {
            g = (g * g0) % m;
        }
        return uint32_t(g);
    }
}

```

We are Examining the source code of the error message reveals g and g0 variable are int32\_t , but eventually return uint32\_t. It will cause the potential signed integer overflow.  
Additionally, while searching through other code files in the library, we found that this variable is used with the `uint32_t` type in other parts of the code, which prevents overflow issues.  
Here is the code——`NativeInteger(5).ModInverse(m).ConvertToInt<uint32_t>();`

Is this a bug?

Thank you for taking the time to look into this issue. Please let us know if any further details or clarification would be helpful!

Best regards,  
wowblk

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [October 31, 2024, 3:34pm UTC](https://openfhe.discourse.group/t/rotationkey-error/1677/2 "2024-10-31T15:34:03Z")

</div>

From an initial analysis, there seems to be a semantic bug that does not affect the functionality (the final modular result is correct), but only because the cyclotomic order is a power of 2 and the underlying number representation system in the processor is binary.  
We will investigate this further. To address this issue, a [Github issue](https://github.com/openfheorg/openfhe-development/issues/901) has been created.

Thank you for reporting this.
