Huelse / SEAL-Python

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

plaintext decode out of range #40

Closed tedw-tech closed 3 years ago

tedw-tech commented 3 years ago

I set up the SEAL context with the following values:

params = seal.EncryptionParameters(seal.scheme_type.BFV) poly_modulus_degree = 16384 params.set_poly_modulus_degree(poly_modulus_degree) params.set_coeff_modulus(seal.CoeffModulus.BFVDefault(poly_modulus_degree)) plain_mod = 2424833 params.set_plain_modulus(plain_mod)

I am also using the IntegerEncoder, like this: encoder = seal.IntegerEncoder(context) value1 = -7 plain1 = seal.Plaintext(encoder.encode(value1)) value2 = 25 plain2 = seal.Plaintext(encoder.encode(value2))

Afterwards, I perform some computations (squarings and multiplications). While the noise budget stays in the double digits, decoding the result to int becomes imposible as I get a Value error.

Traceback (most recent call last): File "/.../Python-Tests/test.py", line 551, in tmp = encoder.decode_int64(plain_result) ValueError: output out of range

Shouldn't the result remain in a decoding-range for int64 if it is set to the value above? I also noticed that with smaller input values, I can still decode, but the value is not mod plaintext_modulus either. Am I missing something about the purpose of the plain_modulus?

Huelse commented 3 years ago

Hello, I have tried some tests with v3.4.5, and it's just working fine. I guess there are some mistakes in your evaluator? Here is my code

from seal import *
from seal_helper import *

print_example_banner("Example: BFV Basics")

parms = EncryptionParameters(scheme_type.BFV)
poly_modulus_degree = 4096  # you can use yours, it just fast
parms.set_poly_modulus_degree(poly_modulus_degree)
parms.set_coeff_modulus(CoeffModulus.BFVDefault(poly_modulus_degree))
parms.set_plain_modulus(1024)  #  any positive integer, maybe a prime number and keep it small will be better in theory

context = SEALContext.Create(parms)
print_parameters(context)
evaluator = Evaluator(context)
encoder = IntegerEncoder(context)
keygen = KeyGenerator(context)
public_key = keygen.public_key()
secret_key = keygen.secret_key()
encryptor = Encryptor(context, public_key)
decryptor = Decryptor(context, secret_key)

value1 = -7
plain1 = Plaintext(encoder.encode(value1))
value2 = 25
plain2 = Plaintext(encoder.encode(value2))

x1 = Ciphertext()
encryptor.encrypt(plain1, x1)
evaluator.add_plain_inplace(x1, plain2)
evaluator.multiply_plain_inplace(x1, plain2)

p1 = decryptor.decrypt(x1)
v1 = encoder.decode_int64(p1)
print(v1)  # 450
tedw-tech commented 3 years ago

Hey, thanks for the quick reply!

I just ran that code and it does work, however, when I add another multiplication (after the already existing one), the output is 11250, shouldn't it be 1010? Or am I confusing the plaintext modulus here with what would happen if I used batching?

Huelse commented 3 years ago

(25-7)x25x25=11250, I think it's right. For the detail, you can refer to the notes from the example. or the paper, which implements the BFV scheme.

tedw-tech commented 3 years ago

Ah, sorry, I got something mixed up with the parameters! Never mind, so no problem after all.