Replicate inner product result to each element of the result

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)

The simple way:
Your inner product result is in the first slot. So you can do the following:

  1. Create a binary-mask plaintext object of the same batch size, with the first slot set to 1 and the remaining slots set to 0.
  2. Multiply the inner product result by the mask to zero out unnecessary slots.
  3. Then call EvalSum to sum all slots in the resultant ciphertext
    Note, you need to set the batch_size in parameters in your code above.

The hard way:
Use contrib/slot-replication at main · openfheorg/contrib · GitHub

It looks like the issue was the batch_size parameter. Once it was set correctly in parameters, even EvalInnerProduct replicated the result for each slot, without the need for masking and EvalSum.

Thank you for you help!