Hi, I’m trying to perform an inner product and then apply a Chebyshev function approximation to the result. However, for the final result of my operation (as the last operation will be an element-wise multiplication), I need either to replicate the inner product result to each slot of the ciphertext, or to extract the result in the first slot and use it as a scalar.
What is the suggested way to do this?
Below you can find a mwe
import numpy as np
from openfhe import *
parameters = CCParamsCKKSRNS()
parameters.SetSecurityLevel(SecurityLevel.HEStd_128_classic)
scaling_mod_size = 50
parameters.SetScalingModSize(scaling_mod_size)
mult_depth = 10
batch_size = 4
parameters.SetMultiplicativeDepth(mult_depth)
cc = GenCryptoContext(parameters)
cc.Enable(PKESchemeFeature.PKE)
cc.Enable(PKESchemeFeature.KEYSWITCH)
cc.Enable(PKESchemeFeature.LEVELEDSHE)
cc.Enable(PKESchemeFeature.ADVANCEDSHE)
key_pair = cc.KeyGen()
secret_key = key_pair.secretKey
public_key = key_pair.publicKey
cc.EvalMultKeyGen(secret_key)
cc.EvalRotateKeyGen(secret_key, [1,2,4])
v1 = [1,2,3,4]
v2 = [5,6,7,8]
# Normalize v1 and v2
v1 = np.array(v1)
norm1 = np.linalg.norm(v1)
normalized_v1 = np.divide(v1, norm1)
v2 = np.array(v2)
norm2 = np.linalg.norm(v2)
normalized_v2 = np.divide(v2, norm2)
ptx1 = cc.MakeCKKSPackedPlaintext(normalized_v1)
ptx2 = cc.MakeCKKSPackedPlaintext(normalized_v2)
ctx1 = cc.Encrypt(public_key, ptx1)
ctx2 = cc.Encrypt(public_key, ptx2)
ctx_cosine_similarity = cc.EvalInnerProduct(ctx1, ctx2, batch_size)
# further operations with EvalChebyshevFunction
res = cc.Decrypt(ctx_cosine_similarity, secret_key)
res.SetLength(batch_size)
print(res)