ibarrond / Pyfhel

PYthon For Homomorphic Encryption Libraries, perform encrypted computations such as sum, mult, scalar product or matrix multiplication in Python, with NumPy compatibility. Uses SEAL/PALISADE as backends, implemented using Cython.
https://pyfhel.readthedocs.io/
Apache License 2.0
475 stars 78 forks source link

scale mismatch #235

Closed scutyuqiu closed 3 months ago

scutyuqiu commented 5 months ago

File "Pyfhel/PyCtxt.pyx", line 360, in Pyfhel.PyCtxt.PyCtxt.add File "Pyfhel/Pyfhel.pyx", line 1050, in Pyfhel.Pyfhel.Pyfhel.add_plain File "Pyfhel/Pyfhel.pyx", line 1070, in Pyfhel.Pyfhel.Pyfhel.add_plain ValueError: scale mismatch

How can I resolve this error?

I use homomorphic encryption in neural networks. At the full connection layer, the encrypted data has been weighted, but this error occurs when adding with bias.

code.docx

ShokofehVS commented 5 months ago

Hi @scutyuqiu, I reviewed issues on Pyfhel and demos, I found one that is helpful for you to read (exploring CKKS pitfalls).

I can recall that I faced similar issue and fixed that with rescaling (HE.rescale) as also pointed out in this issue on SEAL because when you are doing multiplication, you'll have ciphertext in different scale (compared to addition/subtraction) then addition wont work properly as you desired.

scutyuqiu commented 5 months ago

I solved the problem, but I was curious. It doesn't work when I set qi to 21, but it works when I set it to 22.

ShokofehVS commented 5 months ago

Well-done!

qi_size is about the number of bits of each prime in the chain to ensure precision in encoding and decoding. However, I also dont have any connection now in mind between your scale mismatch and qi_size.