# Wrong use of BinFHE

**URL:** <https://openfhe.discourse.group/t/wrong-use-of-binfhe/803>\
**Category:** Library Questions\
**Created:** [September 11, 2023, 8:04pm UTC](https://openfhe.discourse.group/t/wrong-use-of-binfhe/803 "2023-09-11T20:04:26Z")\
**Posts on this page:** 4\
**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:** [September 11, 2023, 8:04pm UTC](https://openfhe.discourse.group/t/wrong-use-of-binfhe/803/1 "2023-09-11T20:04:26Z")

</div>

Hello,

I am having troubles using BinFHE, this is my code:

```auto
auto cc = BinFHEContext();
cc.GenerateBinFHEContext(TOY);

std::cout << "FHEW scheme is using \nn: " << cc.GetParams()->GetLWEParams()->Getn() <<
          "\nN: " << cc.GetParams()->GetLWEParams()->GetN() <<
          "\nq: " << cc.GetParams()->GetLWEParams()->Getq() <<
          "\nQ: " << cc.GetParams()->GetLWEParams()->GetQ() <<
          endl;

auto kp = cc.KeyGenPair();

LWEPlaintext result;

auto sk = cc.KeyGen();

cc.BTKeyGen(sk, PUB_ENCRYPT);

for (int i = 0; i < 10; i++) {
    LWECiphertext a = cc.Encrypt(kp->publicKey, 3, FRESH);
    cc.Decrypt(sk, a, &result);
    cout << "Decryption: " << result << endl;
}

```

In particular, every decryption is failing as the output is like:

```auto
Decryption: 1
Decryption: 1
Decryption: 3
Decryption: 0
Decryption: 0
Decryption: 2
Decryption: 1
Decryption: 2
Decryption: 1
Decryption: 3

```

What is wrong? Thanks

---

<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:** [September 14, 2023, 8:47pm UTC](https://openfhe.discourse.group/t/wrong-use-of-binfhe/803/2 "2023-09-14T20:47:16Z")

</div>

Hi @marc-alonso ,

Your code generates two different secret keys, first with a public-secret key pair `kp` and then a different secret key `sk`. You use one key for encryption and a different (unrelated) key for decryption. To decrypt, you need to use the secret key associated with `kp`.

In other words, the correct code would look like this

```auto
auto cc = BinFHEContext();
cc.GenerateBinFHEContext(TOY);

std::cout << "FHEW scheme is using \nn: " << cc.GetParams()->GetLWEParams()->Getn() <<
          "\nN: " << cc.GetParams()->GetLWEParams()->GetN() <<
          "\nq: " << cc.GetParams()->GetLWEParams()->Getq() <<
          "\nQ: " << cc.GetParams()->GetLWEParams()->GetQ() <<
          endl;

auto kp = cc.KeyGenPair();

LWEPlaintext result;

// I commented it out because it should not be used
// auto sk = cc.KeyGen();

// you do not have any bootstrapping here; so this is also not needed
// cc.BTKeyGen(sk, PUB_ENCRYPT);

for (int i = 0; i < 10; i++) {
    LWECiphertext a = cc.Encrypt(kp->publicKey, 3, FRESH);
    // cc.Decrypt(sk, a, &result);
    cc.Decrypt(kp->secretKey, a, &result);
    cout << "Decryption: " << result << endl;
}

```

---

<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:** [September 15, 2023, 10:31am UTC](https://openfhe.discourse.group/t/wrong-use-of-binfhe/803/3 "2023-09-15T10:31:03Z")

</div>

Thank you! Now it works as intended 🙂

Another quick question: do we have ways to operate on values with modulo greater than 4? I mean, when using larger values, do we have a way to, for instance, sum or multiply values?  
AFAIK LWE ciphertexts are easy to be added (at least, in TFHE).

Thank you.

---

<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:** [September 15, 2023, 6:44pm UTC](https://openfhe.discourse.group/t/wrong-use-of-binfhe/803/4 "2023-09-15T18:44:50Z")

</div>

Hi @marc-alonso,

Could you create a new topic for this question as it does not seem to be directly related to the prior question?

Thanks,  
Yuriy
