Desilo / liberate-fhe

A Fully Homomorphic Encryption (FHE) library for bridging the gap between theory and practice with a focus on performance and accuracy.
https://docs.desilo.ai/
BSD 3-Clause Clear License
110 stars 13 forks source link

ciphertext-plaintext multiplication and addition issue #21

Closed ranran0523 closed 3 months ago

ranran0523 commented 3 months ago

Hi, I am trying to use liberate.FHE to perform some HE operations between ciphertext and plaintext. However, the API (cc_add & cc_mult) does not work with ciphertext and encoded messages. Are these functionalities not supported yet?

juwhan-k commented 3 months ago

@ranran0523 You can simply use add and mult. Those functions detect the type of operands and call proper routines. cc_add and cc_mult are for cipher-cipher operations. For cipher-plain there are other functions. The functions you want to use (if you must manually choose the function to use), would be mc_add, cm_add, mc_mult and cm_mult. Those functions are for operating upon 'messages before encoding' and cipher textx. mc/cm describes the order of distinguishing the input parameters (message or cipher). Also, when using add and mult, you don't have to worry about levels of the cipher texts. The levels are automatically matched as well.

In case you want to add or mult an encoded plain text to cipher text, take a look at https://github.com/Desilo/liberate-fhe/blob/main/src/liberate/fhe/ckks_engine.py. Find mc_add or mc_mult. You can copy the function and just ommit the pt = self.encode(m, 0). That is, you can skip the encoding part. Note however, doing so will lessen the accuracy of the result, since due to the artifact of rescaling, cipher texts are riddled with scale errors (which I call in the code deviation), that are corrected later at the decoding stage. When doing add or mult, both operands must share the same scale error, so that at decoding the result would be correct. mc_XXX and cm_XXX does that for you. But, if you do encoding manually, that correction might have been neglected and the result would slightly deteriorate in terms of accuracy.