Evaluating BinGate on LWECiphertext after CKKS to FHEW scheme switching

The functionality you want can be achieved, but the current capability available in OpenFHE was not designed for this so the solution will be a bit clunky. We are planning to add a CKKS-only version of functional bootstrapping in v1.6.

Please read the paper describing the functional bootstrapping capability (Algorithm 1) to understand the inner working of EvalFBT. The reason why decryption fails in your case is that you are decrypting and decoding a ciphertext that is in coefficients, not in slots. So if you want to correctly decrypt the CKKS ciphertext resulting from the functional bootstrapping, you should skip the homomorphic decoding step. This is exposed in OpenFHE as EvalFBTNoDecoding (or EvalMVBNoDecoding). You need to also set levelsAvailableAfterBootstrap to at least 1, and to multiply by scaleTHI, which currently happens in the homomorphic decoding step (now skipped).

    ctxtAfterFBT = cc->EvalFBTNoDecoding(ctxt, coeff, PInput.GetMSB() - 1, ep->GetModulus(), order);
    ctxtAfterFBT = cc->EvalMult(ctxtAfterFBT, scaleTHI);

However, because of the internal workings of homomorphic FFT, if you set the level budget to be larger than 1, the message will have the order bit reversed; setting the level budget to 1 resolves this, but it is very slow at large ring dimensions. If your computation does not involve rotations, you can ignore the bit reversed order, and just deal with it at the final result. Otherwise, we have a flag to encode the input already bit reversed in RLWE, such that after EvalFBTNoDecoding, it will be in the natural order. This is explained in the MultiValueBootstrapping example, which you based your code on.