Function Evaluation and CMUX

Hi all,

using FHEW/TFHE I wanted to calculate a simple score

a = b* (x) + (b-1)* (x+1) ; for x = {1,..,5} and b = {0,1}

I figured I could use EvalFunc() for computing x+1 and use the CMUX gate to choose either x or x+1 depending on b. But this does not seem to be the case. The result I get is neither x nor x+1. What can I do to fix this?

It would be hard for us to help you without examining your code. Please share your code or provide more details so we can help you better.

Most of the code is originally from the eval_func example. Here is the code:

 auto cc = BinFHEContext();
    cc.GenerateBinFHEContext(STD128, true, 12);
    auto sk = cc.KeyGen();
    cc.BTKeyGen(sk);
    int p = cc.GetMaxPlaintextSpace().ConvertToInt();  // Obtain the maximum plaintext space
    std::cout << "Maximum plaintext space " << p << "." << std::endl;
    auto xEnc = cc.Encrypt(sk, xIn % p, SMALL_DIM, p);    
    // Initialize Function f(x) = x + 1 % p
    auto fp = [](NativeInteger m, NativeInteger p1) -> NativeInteger {
        if (m < p1)
            return (m + 1) % p1;
        else
            return ((m +1 - p1 / 2)) % p1;
    };

    // Generate LUT from function f(x)    
    LWEPlaintext result;    
    auto lut = cc.GenerateLUTviaFunction(fp, p);
    auto x_plus_one = cc.EvalFunc(xEnc, lut);
   
    for (int i = 0; i < 2; i++){
        std::vector<LWECiphertext> cmux_test;                
        cmux_test.push_back(xEnc);
        cmux_test.push_back(x_plus_one);
        cmux_test.push_back(cc.Encrypt(sk, i, SMALL_DIM, p));
        auto test_score = cc.EvalBinGate(CMUX, cmux_test);
        cc.Decrypt(sk, test_score, &result, p);
        std::cout << "Input x: " << xIn << "b: " << i << ". score: " << result << std::endl;
    }

Hi @mgall,

The CMUX gate is only supported for the mode where all inputs are Boolean, which is not the case for x.

A simpler way would be to to use BGV or BFV for this (w/o any bootstrapping). You could simply use the homomorphic multiplication for CMUX. If b comes from an encrypted comparison, then you could do comparison using EvalFunc and run CMUX (multiplication) in CKKS. In other words, you could look into scheme switching examples for this.