# CKKS: Controling PRNG in encryption

**URL:** <https://openfhe.discourse.group/t/ckks-controling-prng-in-encryption/999>\
**Category:** Library Questions\
**Created:** [December 27, 2023, 7:06pm UTC](https://openfhe.discourse.group/t/ckks-controling-prng-in-encryption/999 "2023-12-27T19:06:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmazz](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@mmazz](https://openfhe.discourse.group/u/mmazz)\
**Post date:** [December 27, 2023, 7:06pm UTC](https://openfhe.discourse.group/t/ckks-controling-prng-in-encryption/999/1 "2023-12-27T19:06:57Z")

</div>

Hello!

For study reasons, I need to control de PRNG. Basically I need to get the same encryption if i encrypt the same plaintext.

I compile without OpenMP and uncomment the **#FIXED\_SEED** in _ **“src/core/include/math/distributiongenerator.h”** _

If I do something like this for some input:

```auto
  Plaintext ptxt1 = cc->MakeCKKSPackedPlaintext(input);
  auto c1 = cc->Encrypt(keys.publicKey, ptxt1);
  auto c2 = cc->Encrypt(keys.publicKey, ptxt1);
  auto encryptElems = c1->GetElements();
  auto encryptElems_bis = c2->GetElements();

```

_encryptElems_ and _encryptElems\_bis_, have coefficients totally different.

How can I make that the random part of the encryption be fix so c1 and c2 are the same?

I saw that they fix de seed in 1. But in can figure out where this is changing.

---

<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:** [December 27, 2023, 7:58pm UTC](https://openfhe.discourse.group/t/ckks-controling-prng-in-encryption/999/2 "2023-12-27T19:58:24Z")

</div>

Lattice crypto schemes do not work that way. Encrypting the same plaintext should never generate the same ciphertext otherwise it would violate IND-CPA.

Fixed seed simply fixes the RNG seed to a known value, so each run of a given piece of code should deliver the same results (primarily for software testing purposes, you should NEVER use that flag for anything secure).

---

<div class="post-metadata">

**Author:** ![mmazz](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@mmazz](https://openfhe.discourse.group/u/mmazz)\
**Post date:** [December 28, 2023, 12:02am UTC](https://openfhe.discourse.group/t/ckks-controling-prng-in-encryption/999/3 "2023-12-28T00:02:52Z")

</div>

Thanks for your reply!

I understand what you say. And also about the seed.  
For study purpose I need to have the same encryption. So I try to see in the code where it chance the seed after an encryption to try to hardcode it. I saw that with fixed\_seed the seed is fix to one but i don’t see the change after. So after my fail I post here.

There is an “easy” way to get this? Or some tip? Or in witch file the actual Encryption of CKKS is implemented? I try to get deep and can’t find it, is like a rabbit hole! jeje

---

<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:** [December 28, 2023, 4:21pm UTC](https://openfhe.discourse.group/t/ckks-controling-prng-in-encryption/999/4 "2023-12-28T16:21:59Z")

</div>

CKKS public-key encryption of message (m) calculates : (c\_0,c\_1) = ((p\_0v + e\_0+m),(p\_1v + e\_1)) \pmod{Q} , where p\_0, p\_1 are the public key components, v is a random ring element in R\_Q following a uniform distribution, and e\_0, e\_1 are random ring elements from a truncated Gaussian distribution.

To fix the encryption, you’ll need to manually modify the random elements mentioned above. Be aware that this is an invasive procedure and carries risks. OpenFHE doesn’t provide any built-in methods for this modification.

CKKS encryption logic can be found [here](https://github.com/openfheorg/openfhe-development/blob/v1.1.2/src/pke/lib/schemerns/rns-pke.cpp#L55).

---

<div class="post-metadata">

**Author:** ![mmazz](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@mmazz](https://openfhe.discourse.group/u/mmazz)\
**Post date:** [January 2, 2024, 1:12pm UTC](https://openfhe.discourse.group/t/ckks-controling-prng-in-encryption/999/5 "2024-01-02T13:12:39Z")

</div>

Thanks! With that I manage to fix the random generation of the samples of errors and v.

I add a question that I can’t find. When I did this, I find new data types that you don’t even know what the name comes from.  
For example the data type Dgg, Dug, Bug and Tug. Some insight that you can give me?

Edit: I get it no. DGG is for Discrete Gaussian Generator, TUG for Ternary Uniform Generator BUG for Binary Uniform Generator.

Edit2: If someone is interested, the simple way that I found to get this job done is by adding a method to the blake2engine class to reset its internal variables, and call it in the discrete Gaussian generator implementation, in the different parts that calls GetPRNG.
