# Fast rotations example

**URL:** <https://openfhe.discourse.group/t/fast-rotations-example/522>\
**Category:** Library Questions\
**Created:** [May 3, 2023, 2:21pm UTC](https://openfhe.discourse.group/t/fast-rotations-example/522 "2023-05-03T14:21:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marc-alonso](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@marc-alonso](https://openfhe.discourse.group/u/marc-alonso)\
**Post date:** [May 3, 2023, 2:21pm UTC](https://openfhe.discourse.group/t/fast-rotations-example/522/1 "2023-05-03T14:21:50Z")

</div>

Hello there, I am writing a program in which I perform many rotations on a ciphertext, and I noticed the function EvalFastRotation. I was wondering if you can provide an example of how it works, and which are the benefits in terms of time

Thank you

---

<div class="post-metadata">

**Author:** ![marc-alonso](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@marc-alonso](https://openfhe.discourse.group/u/marc-alonso)\
**Post date:** [May 5, 2023, 9:10am UTC](https://openfhe.discourse.group/t/fast-rotations-example/522/2 "2023-05-05T09:10:01Z")

</div>

I tried a couple of experiments, but the program has some kind of error. I leave the [code here](https://pastecode.io/s/whaimi2f).

The output is the following:

```auto
CryptoContext and Keys generation started...
Generating crypto context...
Evaluating MultKeyGen...
Encoding and encrypting a vector...
Iteration 0: 
Decryptable
Iteration 1: 
Decryptable
Iteration 2: 
Decryptable
Iteration 3: 
Decryptable
Iteration 4: 
Decryptable
Iteration 5: 

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

```

---

<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:** [May 5, 2023, 3:58pm UTC](https://openfhe.discourse.group/t/fast-rotations-example/522/3 "2023-05-05T15:58:31Z")

</div>

Hi @marc-alonso

`EvalFastRotation` is function that can speed up rotations when multiple rotations of the same ciphertext need to be performed. The first idea is that digit decomposition for the ciphertext can be precomputed and then reused for multiple rotations. A good example of this is [openfhe-development/advanced-real-numbers.cpp at v1.0.3 · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/blob/v1.0.3/src/pke/examples/advanced-real-numbers.cpp#L748)

For hybrid key switching only, there is another optimization for fast rotations. It is slightly more involved. The example is provided at [openfhe-development/UnitTestBootstrap.cpp at v1.0.3 · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/blob/v1.0.3/src/pke/unittest/utckksrns/UnitTestBootstrap.cpp#L411) In addition to the digit decomposition, one can perform actual fast rotations using the extended modulus (used internally in hybrid key switching), and then call expensive `KeySwitchDown` only once. This works well for the scenario when the results of fast rotations are added together (as in linear transforms). Note that in this case, you should call `EvalFastRotationExt` rather than `EvalFastRotation` to take advantage of the second optimization.

---

<div class="post-metadata">

**Author:** ![marc-alonso](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@marc-alonso](https://openfhe.discourse.group/u/marc-alonso)\
**Post date:** [May 6, 2023, 11:49am UTC](https://openfhe.discourse.group/t/fast-rotations-example/522/4 "2023-05-06T11:49:59Z")

</div>

Hello @ypolyakov , thank you very much for the references. Any suggestion for my previous example? I think I am using the fast rotation correctly …

---

<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:** [May 7, 2023, 5:30pm UTC](https://openfhe.discourse.group/t/fast-rotations-example/522/5 "2023-05-07T17:30:37Z")

</div>

In your example, `EvalFastRotation` is not used properly. The idea is that you apply it when multiple rotations of the same ciphertext need to be computed. In your example, the ciphertext keeps changing. So if you run your example, you will see the answer is garbage from iteration 1 on.

On top of this, you try to generate two rotations keys for the same index: 128\*2\*2 and 128\*4.

Moreover, the ciphertext uses only 4 slots, which also does not make sense for the desired rotations.

Another issue is that no key for iteration 5 is generated with EvalRotateKeyGen (missing key).

So there are four different problems with the provided example.
