# Modular Exponentiation algorithm on BGV

**URL:** <https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356>\
**Category:** Library Questions\
**Created:** [February 13, 2023, 4:46pm UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356 "2023-02-13T16:46:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![julen\_bernabe](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@julen\_bernabe](https://openfhe.discourse.group/u/julen_bernabe)\
**Post date:** [February 13, 2023, 4:46pm UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356/1 "2023-02-13T16:46:38Z")

</div>

Hi there! I’m trying to implement a modular exponentiation algorithm for the BGV ciphertexts. The idea is to be able to extend this algorithm for computing Fermat’s Little Theorem.

I have successfully implemented the algorithm for Plaintext Modulus p=65537 for up to a multiplicative depth of 13 iterations. For Fermat’s Little Theorem I need to compute a^65536, but this implicitly needs a multiplicative depth of 16 or 17. However, if I try to increase the multipicative depth, the following error arises:

Please provide a primeModulus(q) and a cyclotomic number(m) satisfying the condition: (q-1)/m is an integer. The values of primeModulus = 65537 and m = 131072 do not satisfy this condition

Any ideas on how can I avoid this issue?

---

<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:** [February 13, 2023, 5:07pm UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356/2 "2023-02-13T17:07:53Z")

</div>

Set plaintext modulus p=786433 and let me know if it solves your issue.

---

<div class="post-metadata">

**Author:** ![julen\_bernabe](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@julen\_bernabe](https://openfhe.discourse.group/u/julen_bernabe)\
**Post date:** [February 14, 2023, 7:26am UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356/3 "2023-02-14T07:26:06Z")

</div>

Issue solved! The problem was of course about choosing the right prime number. Thanks for the answer!

---

<div class="post-metadata">

**Author:** ![julen\_bernabe](https://avatars.discourse-cdn.com/v4/letter/j/2acd7d/32.png) [@julen\_bernabe](https://openfhe.discourse.group/u/julen_bernabe)\
**Post date:** [February 14, 2023, 7:28am UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356/4 "2023-02-14T07:28:14Z")

</div>

Related to this, is there any ‘list of suitable prime numbers’ to use in OpenFHE? It would be great to have this list of primes with their maxDepth, for developers to know what prime to choose deoending on the operations to be done on the ciphertexts.

Looking forward to your answer!

---

<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:** [February 14, 2023, 4:41pm UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356/5 "2023-02-14T16:41:27Z")

</div>

You are using ultra-large parameters that we do not generally recommend. The default plaintext modulus used in the example should support reasonable ring dimensions.  
Look at the last paragraph in this [post](https://openfhe.discourse.group/t/is-there-a-limit-to-plaintext-size/329/4). It should give you an idea about how to choose your prime modulus.

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [February 15, 2023, 4:42pm UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356/6 "2023-02-15T16:42:08Z")

</div>

@Caesar is there any security issue raised when selecting such ultra-large parameters?

---

<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:** [February 15, 2023, 9:01pm UTC](https://openfhe.discourse.group/t/modular-exponentiation-algorithm-on-bgv/356/7 "2023-02-15T21:01:06Z")

</div>

@dcousins - As long as the ring dimension is set appropriately (which is done automatically in OpenFHE), then there should be security issues. What I meant by not recommended settings above was merely based on the performance aspect.
