TenSEAL
TenSEAL copied to clipboard
Example of plaintext matrix multiplied by ciphertext vector
Question
Example of plaintext matrix multiplied by ciphertext vector:
import numpy as np
import tenseal as ts
key_length = 4096
bits_scale = 24
coefficient_modulus = [30, bits_scale, bits_scale, 30]
context = ts.context(
ts.SCHEME_TYPE.CKKS,poly_modulus_degree=key_length,
coeff_mod_bit_sizes= coefficient_modulus
)
context.global_scale = pow(2, bits_scale)
def decrypt(enc):
return enc.decrypt().tolist()
X = np.random.randint(1, 10, (32, 31))
X = ts.plain_tensor(X)
y = [0.5] *31
enc_y = ts.ckks_tensor(context, y)
result = X * enc_y
print("result shape {}".format(result.shape))
print("Decrypted result: {}.".format(decrypt(result)))
why result shape is (32,31),not (32,1).
Intuitively understand from the matrix multiplication, matrix A with shape(32,31), matrix B with shape(31,1), A*B should get a (32,1) matrix after multiplying.
@bcebere
Hello @napoay,
the * (star) operator performs element-wise multiplication.
If you want to do a vector-matrix multiplication, you should use
enc_y.mm(X)
or
enc_y @ X
or
enc_y.dot(X[0])
if you want a single row-column product.