# Proxy Re-Encryption (PRE) for BinFHE

**URL:** <https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436>\
**Category:** Library Questions\
**Created:** [March 27, 2023, 11:34pm UTC](https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436 "2023-03-27T23:34:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalalala](https://avatars.discourse-cdn.com/v4/letter/l/9f8e36/32.png) [@lalalala](https://openfhe.discourse.group/u/lalalala)\
**Post date:** [March 27, 2023, 11:34pm UTC](https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436/1 "2023-03-27T23:34:12Z")

</div>

I need to proxy re-encrypt the output of BinaryFHE in the following code. Can you help me understand how to do so?

```
auto cc = BinFHEContext();

cc.GenerateBinFHEContext(STD128);
auto sk = cc.KeyGen();
cc.BTKeyGen(sk);
auto output_to_be_proxy_reencrypted = cc.Encrypt(sk, c);

```

Is it even possible to proxy-reencrypt ciphertext form the BinFHE library?

---

<div class="post-metadata">

**Author:** ![lalalala](https://avatars.discourse-cdn.com/v4/letter/l/9f8e36/32.png) [@lalalala](https://openfhe.discourse.group/u/lalalala)\
**Post date:** [March 28, 2023, 2:56am UTC](https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436/2 "2023-03-28T02:56:36Z")

</div>

Another way to ask this: is Key Switching enabled in the BinFHE library? If not, how can we implement this ourselves?

---

<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:** [April 6, 2023, 3:32pm UTC](https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436/3 "2023-04-06T15:32:21Z")

</div>

@lalalala Currently, we support PRE as a feature only for BGV, BFV, and CKKS. There, we repurpose the key switching functionality these schemes use for multiplication and rotations. It is correct that FHEW/TFHE bootstrapping can be used for PRE (as suggested in Gentry’s PhD dissertation on FHE) for the BinFHE schemes, but the API is not exposed to the user. It is a good idea. I will add an issue to add this functionality to OpenFHE v1.1.

---

<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:** [April 6, 2023, 3:34pm UTC](https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436/4 "2023-04-06T15:34:29Z")

</div>

Added the following issue: [Add PRE for FHEW/TFHE · Issue #354 · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/issues/354)

---

<div class="post-metadata">

**Author:** ![lalalala](https://avatars.discourse-cdn.com/v4/letter/l/9f8e36/32.png) [@lalalala](https://openfhe.discourse.group/u/lalalala)\
**Post date:** [April 8, 2023, 10:21pm UTC](https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436/5 "2023-04-08T22:21:41Z")

</div>

I can see that the object LWEEncryptionScheme (in lwe-pke.cpp) implements key switching. I’ve exposed this library in my code.

However, when I attempt to perform key switching on one bit, I am failing to decrypt correctly. Is there an error in this library, or (more likely), can you see the error in my code?

BinFHEContext cc1 = BinFHEContext();  
cc1.GenerateBinFHEContext(STD128);  
LWEPrivateKey sk\_alice = cc1.KeyGen();  
cc1.BTKeyGen(sk\_alice);

```
BinFHEContext cc2 = BinFHEContext();
cc2.GenerateBinFHEContext(STD128);
LWEPrivateKey sk_bob = cc2.KeyGen();

LWECiphertext original_ciphertext = cc1.Encrypt(sk_alice, 1);

LWEPlaintext attemptlalala;
cc1.Decrypt(sk_alice,original_ciphertext, &attemptlalala);
std::cout << "first lalala = " << attemptlalala;

LWESwitchingKey switching_key = cc1.KeySwitchGen(sk_bob, sk_alice);

const std::shared_ptr<BinFHECryptoParams> binFHECryptoParams = cc1.GetParams();
const std::shared_ptr<LWECryptoParams> lwe_crypto_params = binFHECryptoParams->GetLWEParams();
const std::shared_ptr<LWEEncryptionScheme> lwe_encryption_scheme = cc1.GetLWEScheme();

LWECiphertext switched_ct = lwe_encryption_scheme->KeySwitch(lwe_crypto_params, switching_key, original_ciphertext);

```

---

<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:** [April 14, 2023, 8:36pm UTC](https://openfhe.discourse.group/t/proxy-re-encryption-pre-for-binfhe/436/6 "2023-04-14T20:36:22Z")

</div>

My first question. Why are you creating two different binfhe cryptocontexts? This is unnecessary.

Currently `KeySwitchGen` and `KeySwitch` are tailored for the setup where we need to switch from N (RLWE ring dimension) to n (LWE lattice dimension). This behavior will need to be generalized to support key switching from any lattice dimension to any other lattice dimension (including the same one).

Note that PRE using key switching is not secure unless noise flooding is used (to erase prior error/secret key information). So extra noise would need to be added for this setup. I was referring to the bootstrapping-based approach that Gentry discussed in his PhD thesis.

I’ve added an issue to add PRE to FHEW/TFHE. I will let you know as soon as this change has been pushed to the `dev` branch.
