Wrong use of BinFHE

Hello,

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

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:

Decryption: 1
Decryption: 1
Decryption: 3
Decryption: 0
Decryption: 0
Decryption: 2
Decryption: 1
Decryption: 2
Decryption: 1
Decryption: 3

What is wrong? Thanks

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 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;
}

Thank you! Now it works as intended :slight_smile:

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.

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

1 Like