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

Encrypting and operating with 2D numpy arrays (images) #212

Open elyasgoli opened 1 year ago

elyasgoli commented 1 year ago

Description When I try Demo_3_Float_CKKS.py with a 2-dimensional numpyarray, I get an error.

Code To Reproduce Error

arr_x [[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]
Traceback (most recent call last):
  File "/home/elyas/Pyfhel/examples/Try_Float_CKKS.py", line 60, in <module>
    ptxt_x = HE.encodeFrac(arr_x)   # Creates a PyPtxt plaintext with the encoded arr_x
  File "Pyfhel/Pyfhel.pyx", line 742, in Pyfhel.Pyfhel.Pyfhel.encodeFrac
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)

Setup:

ShokofehVS commented 1 year ago

Hi @elyasgoli, I just want to add that there is a nice example of how encrypting and performing simple arithmetic operations over 2-D matrices in this issue.

elyasgoli commented 1 year ago

Hi Shokofeh,

Thank you! That's helpful! My goal is actually to encrypt images with CKKS or BFV. For that purpose, I need to encrypt a Numpy 2D array as a whole, not by breaking it down row by row. Is it possible to do that with PyFHEL?

Many thanks, Elyas

On Fri, Sep 1, 2023 at 4:55 AM Shokofeh VahidianSadegh < @.***> wrote:

Hi @elyasgoli https://github.com/elyasgoli, I just want to add that there is a nice example of how encrypting and performing simple arithmetic operations over 2-D matrices in this issue https://github.com/ibarrond/Pyfhel/issues/183#issuecomment-1507055784.

— Reply to this email directly, view it on GitHub https://github.com/ibarrond/Pyfhel/issues/212#issuecomment-1702630558, or unsubscribe https://github.com/notifications/unsubscribe-auth/A7HSUU7XPBOUCHB5NIO235DXYHEL7ANCNFSM6AAAAAA4HAH56U . You are receiving this because you were mentioned.Message ID: @.***>

-- Elyas Goli, Ph.D. Principal Machine Learning Engineer Peak Artificial Intelligence Cell Phone: 217-904-4873 Email: @.***

AlexanderViand-Intel commented 1 year ago

You virtually always want to flatten images before encryption, as FHE ciphertexts are effectively vectors and FHE offers SIMD operations over vectors. For advanced use cases, more complex patterns might be beneficial, but expressing your FHE computation as a vectorized computation is a good start for most applications.