Huelse / SEAL-Python

Microsoft SEAL 4.X For Python
MIT License
310 stars 66 forks source link

segmentation fault in set_parms #58

Closed lidh15 closed 2 years ago

lidh15 commented 3 years ago

[//]: # Before submitting this issue, make sure you have already searched and still have problems. Describe the bug A clear and concise description of what the bug is.

Device\Environment System: Ubuntu18.04 Python: 3.8.5

To Reproduce Steps to reproduce the behavior:

  1. Using seal with pickle
  2. scheme type bfv
  3. poly modulus degree 8192
  4. plain modulus generated with PlainModulus.Batching(8192, 20)
  5. coeff modulus generated with CoeffModulus.Create(8192, [60, 40, 40, 60])
  6. generate secret key by key generator, for example with variable name sk

Expected behavior A clear and concise description of what you expected to happen.

  1. if only sk.set_parms(parms) in the code, it can run but report segmentation fault after return
  2. if sk.set_parms(parms) and then pickle.dumps(sk), the code break and report segmentation fault at once
Huelse commented 3 years ago

Sorry for the reply late, could you demonstrate the code? I tried your code in windows10 and ubuntu20, and there is no problem.

from seal import *
from seal_helper import *
import pickle

parms = EncryptionParameters(scheme_type.bfv)
poly_modulus_degree = 8192
parms.set_poly_modulus_degree(poly_modulus_degree)
parms.set_coeff_modulus(CoeffModulus.Create(8192, [60, 40, 40, 60]))
parms.set_plain_modulus(PlainModulus.Batching(8192, 20))

context = SEALContext(parms)
keygen = KeyGenerator(context)
sk = keygen.secret_key()
pk = keygen.create_public_key()
encryptor = Encryptor(context, pk)
evaluator = Evaluator(context)
decryptor = Decryptor(context, sk)

x_plain = Plaintext(str(3))
x_encrypted = encryptor.encrypt(x_plain)
x_encrypted2 = Ciphertext(x_encrypted)
evaluator.add_inplace(x_encrypted, x_encrypted)

res1 = decryptor.invariant_noise_budget(x_encrypted)
print(res1)  # 111

sk.set_parms(parms)
print(sk)  # <seal.SecretKey object at 0x000002055B0A1470>
sk1 = pickle.dumps(sk)
print(len(sk1))  # 349891
sk2 = pickle.loads(sk1)
print(sk2)  # <seal.SecretKey object at 0x000002055B1B89F0>

decryptor2 = Decryptor(context, sk2)
res2 = decryptor.invariant_noise_budget(x_encrypted)
print(res2)  # 111

Hava you rebuild the SEAL lib after running the py helper.py?

lidh15 commented 3 years ago

I tried your demo code, yes there is no segmentation fault. In fact I only see the error in certain functions in my codes, this is strange and I think I need some more time to figure out what happened.