OpenMined / TenSEAL

A library for doing homomorphic encryption operations on tensors
Apache License 2.0
837 stars 158 forks source link

How can I save the private key with the context when serializing it? #438

Closed wzjin2017 closed 1 year ago

wzjin2017 commented 1 year ago

Question

How can I save the private key with the context when serializing it?

Further Information

Error when decrypting using deserialized context says :

ValueError: the current context of the tensor doesn't hold a secret_key, please provide one as argument

From this, it states the old version saves the secret key with the context by default, but the new version seems to drop the secret key by default now. So how can I save the private key with the context or how can I save and load the private in general?

Reproduce

This is how I create and serialize my context:

context = ts.context(ts.SCHEME_TYPE.CKKS, 8192, coeff_mod_bit_sizes=[60, 40, 40, 60])
context.global_scale = pow(2, 52)
context.generate_galois_keys()

with open('context.pickle', 'wb') as handle:
    pickle.dump(context.serialize(), handle, protocol=pickle.HIGHEST_PROTOCOL)
wzjin2017 commented 1 year ago

Found this example, the fix is to add this argument in serialize: context.serialize(save_secret_key=True)

It would be nice to update this instruction in the tutorial examples. Thanks!