TenSEAL icon indicating copy to clipboard operation
TenSEAL copied to clipboard

Example of plaintext matrix multiplied by ciphertext vector

Open yooopan opened this issue 3 years ago • 2 comments

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.

yooopan avatar Dec 19 '21 17:12 yooopan

@bcebere

yooopan avatar Dec 22 '21 02:12 yooopan

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.

MartinNoc avatar May 24 '22 11:05 MartinNoc