Pyfhel icon indicating copy to clipboard operation
Pyfhel copied to clipboard

Encrypting and operating with 2D numpy arrays (images)

Open elyasgoli opened this issue 2 years ago • 3 comments

Description When I try Demo_3_Float_CKKS.py with a 2-dimensional numpy array, 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:

  • OS: Ubuntu
  • Python: 3.9.2
  • C compiler version: GCC 10.2.1 20210110
  • Pyfhel Version: most-recent

elyasgoli avatar Sep 01 '23 04:09 elyasgoli

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.

ShokofehVS avatar Sep 01 '23 11:09 ShokofehVS

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: @.***

elyasgoli avatar Sep 01 '23 15:09 elyasgoli

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.

AlexanderViand-Intel avatar Sep 01 '23 16:09 AlexanderViand-Intel