OpenMined / TenSEAL

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

encrypted1 and encrypted2 parameter mismatch #449

Open Propantheline opened 1 year ago

Propantheline commented 1 year ago

Description

The encrypted 564 matrix is multiplied with 64784 plaintext matrix by the mm() function, and it reports ValueError: encrypted1 and encrypted2 parameter mismatch.

Confusion

The encrypted matrix is multiplyable with np.random.rand(64,784), and mnist's data(64*784) will report an error, why is that?

xuefeng-xu commented 1 year ago

Hi @Propantheline, I came across similar error, here is my code

import tenseal as ts

ctx = ts.context(ts.SCHEME_TYPE.CKKS,
                 poly_modulus_degree=8192,
                 coeff_mod_bit_sizes=[35,27,27,35])
ctx.global_scale = pow(2, 27)
ctx.generate_galois_keys()

x = [[0.1, 0.1], [0.1, 0.0]]
# x = [[0.1, 0.1], [0.1, 0.1]]

w = ts.ckks_tensor(ctx, [[0.1, 0.1], [0.1, 0.1]])
z = w.mm(x)
d = z.mm(x)

When x = [[0.1, 0.1], [0.1, 0.0]], I got the error below (no error when x = [[0.1, 0.1], [0.1, 0.1]])

Traceback (most recent call last):
  File "ten.py", line 14, in <module>
    d = z.mm(x)
  File "/Users/xxf/miniconda3/envs/ph/lib/python3.8/site-packages/tenseal/tensors/ckkstensor.py", line 118, in mm
    result = self.data.mm(other)
ValueError: encrypted1 and encrypted2 parameter mismatch

It seems that it's 0.0 cause this error since MNIST data also contains zeros.

Propantheline commented 1 year ago

Maybe you could check the shapes of x, w, and z, or you could increase the number of coefficients in coeff_mod_bit_sizes, which determines the number of homomorphic multiplications performed.

xuefeng-xu commented 1 year ago

Maybe you could check the shapes of x, w, and z, or you could increase the number of coefficients in coeff_mod_bit_sizes, which determines the number of homomorphic multiplications performed.

The shape of x, w, z are all (2,2). And I have try different ckks params, such as increase the number of coefficients, but I still got this error.

xuefeng-xu commented 1 year ago

Description

The encrypted 5_64 matrix is multiplied with 64_784 plaintext matrix by the mm() function, and it reports ValueError: encrypted1 and encrypted2 parameter mismatch.

Confusion

The encrypted matrix is multiplyable with np.random.rand(64,784), and mnist's data(64*784) will report an error, why is that?

BTW, did you solve this error?

Propantheline commented 1 year ago

Sorry, I forgot how I did it then, and it didn't seem to fix it, I encrypted the plaintext matrix again and multiplied it with the ciphertext matrix to get no error. What I understand is to keep the ciphertext scale on a level, or the parameters don't match.

xuefeng-xu commented 1 year ago

Sorry, I forgot how I did it then, and it didn't seem to fix it, I encrypted the plaintext matrix again and multiplied it with the ciphertext matrix to get no error. What I understand is to keep the ciphertext scale on a level, or the parameters don't match.

Thanks, it seems that TenSEAL does relin, rescale, and mod switch automatically. I still wonder why I got this error. Anyway, here is another script which receives a parameter mismatch error.

import tenseal as ts

ctx = ts.context(ts.SCHEME_TYPE.CKKS,
                 poly_modulus_degree=32768,
                 coeff_mod_bit_sizes=[60,40,40,40,60])
ctx.global_scale = pow(2, 40)
ctx.generate_galois_keys()

x = [[0.1, 0.1], [0.1, 0.0]]
w = ts.ckks_tensor(ctx, [[0.1, 0.1], [0.1, 0.1]])
z = w.mm(x)

t = [[1., 0.], [0., 1.]]
# t = [[1., 0.0001], [0.0001, 1.]]
d = z * t
d.sum()

If t = [[1., 0.], [0., 1.]], I will get an error. But if t = [[1., 0.0001], [0.0001, 1.]], it's all good. The only difference is the first t contains 0..

Traceback (most recent call last):
  File "tt.py", line 16, in <module>
    d.sum()
  File "/Users/xxf/miniconda3/envs/ph/lib/python3.8/site-packages/tenseal/tensors/abstract_tensor.py", line 153, in sum
    return self._wrap(self.data.sum(axis))
ValueError: encrypted1 and encrypted2 parameter mismatch