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
479 stars 78 forks source link

Why does the pyfhel library run dozens of times faster than the seal library? #252

Open Lionhehao opened 1 day ago

Lionhehao commented 1 day ago

I first wrote a code for dense state matrix multiplication based on the seal library. After transplanting it to the pyfhel library, I found that the speed was much faster. So I tested the ciphertext multiplication of ckks locally.

Without performing re-linearization, the time consumption of 1000 multiplications was: seal 48s c++ pyfhel 1s python

This makes me feel unbelievable. My cpu is i3-10100f. Does pyfhel use gpu acceleration?

ShokofehVS commented 1 day ago

Based on previous questions and answers regarding the GPU support in Pyfhel, you can refer to these issues: #205, #171. All in all, Pyfhel depends on the underlying backend FHE libs, which have not yet been ported to GPU.

ibarrond commented 1 day ago

@Lionhehao I suspect two things migth be in play:

Lionhehao commented 22 hours ago

@Lionhehao I suspect two things migth be in play:

  • The multiplication in Pyfhel might be using parallelisation across several CPUs.
  • The multiplication happens in-place in Pyfhel, if you are running many multiplications in SEAL creating new objects every time you might be paying some extra computational cost.

@ibarrond Thank you very much for your answer, but what confuses me is whether homomorphic multiplication can really run so fast on CPU? It seems that the time it takes to perform a homomorphic multiplication is only 1ms. Did your homomorphic multiplication test also take such a short time? Below is my test code, I don't know if there is anything wrong.

image

image

I found that openFHE seems to use parallelization under the hood, which may be one of the reasons. I guess openfhe may have made some other optimizations in other underlying operations. If the test is correct, I will no longer use the seal library hahaha. Thank you again.