Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to save encrypted vectors? #454

Closed
bluetail14 opened this issue Jul 31, 2023 · 4 comments
Closed

Is there a way to save encrypted vectors? #454

bluetail14 opened this issue Jul 31, 2023 · 4 comments
Labels
Type: Question ❔ Question about implementation or some technical aspect

Comments

@bluetail14
Copy link

bluetail14 commented Jul 31, 2023

Question

I have tried np.savez , torch.save and pickle.dump to save my encrypted vectors and am getting an error that 'TypeError: cannot pickle '_tenseal_cpp.CKKSVector' object'.
Is there a way to save them into a file?

Further Information

Describe your question in greater length here.
#e.g. of an encrypted vector

enc_x_test = [ts.ckks_vector(context, x.tolist()) for x in x_test]

    with open(f'enc_test_{i}.pkl','wb') as fIn:
        pickle.dump(enc_x_test, fIn)

or,
enc_x_arr = np.array(enc_x_test)
np.savez('enc_x_test_saved.npz', enc_x_arr)

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 np.savez('enc_x_test_saved.npz', enc_x_test_arr)

File /usr/local/lib/python3.10/site-packages/numpy/lib/npyio.py:639, in savez(file, *args, **kwds)
    555 @array_function_dispatch(_savez_dispatcher)
    556 def savez(file, *args, **kwds):
    557     """Save several arrays into a single file in uncompressed ``.npz`` format.
    558 
    559     Provide arrays as keyword arguments to store them under the
   (...)
    637 
    638     """
--> 639     _savez(file, args, kwds, False)

File /usr/local/lib/python3.10/site-packages/numpy/lib/npyio.py:743, in _savez(file, args, kwds, compress, allow_pickle, pickle_kwargs)
    741     # always force zip64, gh-10776
    742     with zipf.open(fname, 'w', force_zip64=True) as fid:
--> 743         format.write_array(fid, val,
    744                            allow_pickle=allow_pickle,
    745                            pickle_kwargs=pickle_kwargs)
    747 zipf.close()

File /usr/local/lib/python3.10/site-packages/numpy/lib/format.py:719, in write_array(fp, array, version, allow_pickle, pickle_kwargs)
    717     if pickle_kwargs is None:
    718         pickle_kwargs = {}
--> 719     pickle.dump(array, fp, protocol=3, **pickle_kwargs)
    720 elif array.flags.f_contiguous and not array.flags.c_contiguous:
    721     if isfileobj(fp):

TypeError: cannot pickle '_tenseal_cpp.CKKSVector' object

Screenshots

error_saving_file
If applicable, add screenshots to help explain your question.

System Information

Python 3.10.
numpy '1.25.1'

Additional Context

Add any other information

@bluetail14 bluetail14 added the Type: Question ❔ Question about implementation or some technical aspect label Jul 31, 2023
@youben11
Copy link
Member

youben11 commented Jul 31, 2023

There should be a serialize and load functions for that

@bluetail14
Copy link
Author

bluetail14 commented Jul 31, 2023

how do I use them? e.g. enc_x_test.serialize?

@tanjuntao
Copy link

You can refer to this example

@youben11 youben11 closed this as completed Aug 1, 2023
@harper-yuan
Copy link

I encrypted a 512-dimensional vector, turning it into a CKKS ciphertext encrypted_tensor, and then used the serialization function encrypted_tensor.serialize(). I found that its size became 40MB, while the original variable encrypted_tensor was only 0.046875 KB. It expanded by 1000 times, is this normal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question ❔ Question about implementation or some technical aspect
Projects
None yet
Development

No branches or pull requests

4 participants