Error in EvalSumRows "Input evaluation key map is empty"

Hi guys,
I am facing this error when I tried to use EvalSumRows. I checked my code and it contains the key map generated but when I run, it still show the key map is empty.
It is noted that I updated the version of openfhe-development and openfhe-python and face this error. Please help me with this situation!

Please include a minimum working example reproducing the problem and we will examine it to determine if EvalSumRows is used correctly or whether there is a bug.

For the cryptocontext, I used the code from logistictraining python example

def create_crypto(
        crypto_hparams: Dict,
        bootstrap_hparams: Optional[Dict] = None
) -> Tuple[openfhe.CryptoContext, openfhe.KeyPair, int]:
    ################################################
    # Setting the crypto parameters
    ################################################
    # CKKS cryptographic parameters
    if openfhe.get_native_int() == 128:
        logging.info("Running in 128-bit mode")
        scaling_mod_size = crypto_hparams["128_scaling_mod_size"]
        first_mod = crypto_hparams["128_first_mod"]
    else:
        logging.info("Running in 64-bit mode")
        scaling_mod_size = crypto_hparams["64_scaling_mod_size"]
        first_mod = crypto_hparams["64_first_mod"]   

    # Define the parameter object that will be used in the cryptocontext creation
    cc_params = openfhe.CCParamsCKKSRNS()
    # If running in bootstrap mode, this value gets overwritten!
    mult_depth = crypto_hparams["mult_depth"]

    if crypto_hparams["run_bootstrap"]:
        sk_dist = getattr(openfhe, bootstrap_hparams["secret_key_dist"] )
        level_budget = bootstrap_hparams["level_budget"]
        bsgs_dim = bootstrap_hparams["bsgs_dim"]

        if openfhe.get_native_int() == 128:
            levels_before_bootstrap = bootstrap_hparams["levels_before_bootstrap_128bit"]
        else:
            levels_before_bootstrap = bootstrap_hparams["levels_before_bootstrap_64bit"]

        approx_bootstrap_depth = bootstrap_hparams["approx_bootstrap_depth"]
        mult_depth = levels_before_bootstrap + openfhe.FHECKKSRNS.GetBootstrapDepth(
            approx_bootstrap_depth, level_budget, sk_dist
        )
        logger.info(f"Bootstrap final depth: {mult_depth}")
        cc_params.SetSecretKeyDist(sk_dist)
        
    # Setting is ordered based on the reference C++ implementation
    # https://github.com/openfheorg/openfhe-logreg-training-examples/blob/main/lr_nag.cpp#L239
    cc_params.SetMultiplicativeDepth(mult_depth)
    cc_params.SetScalingModSize(scaling_mod_size)
    cc_params.SetBatchSize(crypto_hparams["batch_size"])
    cc_params.SetSecurityLevel(getattr(openfhe, crypto_hparams["security_level"]))
    cc_params.SetRingDim(crypto_hparams["ring_dimensionality"])
    cc_params.SetScalingTechnique(getattr(openfhe, crypto_hparams["rescale_method"]))
    cc_params.SetKeySwitchTechnique(getattr(openfhe, crypto_hparams["keyswitch_method"]))
    cc_params.SetNumLargeDigits(crypto_hparams["num_large_digits"])
    cc_params.SetFirstModSize(first_mod)
    cc_params.SetMaxRelinSkDeg(crypto_hparams["max_relin_sk_deg"])

    logger.info("Generating CC")
    cc: openfhe.CryptoContext = openfhe.GenCryptoContext(cc_params)
    logger.info("Enabling Crypto Features")
    cc.Enable(openfhe.PKESchemeFeature.PKE)
    cc.Enable(openfhe.PKESchemeFeature.LEVELEDSHE)
    cc.Enable(openfhe.PKESchemeFeature.ADVANCEDSHE)
    cc.Enable(openfhe.PKESchemeFeature.KEYSWITCH)

    logger.info("Generating Keypair")
    keys: openfhe.KeyPair = cc.KeyGen()
    logger.info("Generating Mult Key and Sum Key")
    cc.EvalMultKeyGen(keys.secretKey)
    cc.EvalSumKeyGen(keys.secretKey)
    logger.info("Generating Rotate Key")
    cc.EvalRotateKeyGen(keys.secretKey,[1,0])

    # Based on the number of occupied slots, we do some manipulation of the
    #   data
    num_slots = crypto_hparams["batch_size"]

    return cc, keys, num_slots, mult_depth

Then, I defined the function

def EvalSumRows(x):
    return [cc.EvalSumRows(i,row_size,eval_sum_row_keys) for i in x]
row_size = 32
eval_sum_row_keys = cc.EvalSumRowsKeyGen(keys.secretKey, rowSize=row_size)

When I use this function, it had the error “Input evaluation key map is empty”

From the past, I can run this code on the older version but when I updated to lastest version, it has this error

Hi @ypolyakov , I just tested some case it the error may come from parameters.SetSecurityLevel(HEStd_NotSet). When I do not use the securitylevel set up, the EvalSumRows() can be used properly but when I use it, it throw that errors.
Can you check with the dev team and explain me the reason for that?

What’s the order you’re calling the functions in? Please put code that we can copy-paste and immediately run.

Hi @ManhBui

I am not having any problems using the latest versions of OpenFHE and Python API (from the main branches). It works fine for HEStd_NotSet (the example I ran is provided below). Most likely, there is a problem with your code.

BTW, why are you generating a key with index 0? It will never get used as the rotation by 0 is the input ciphertext itself.

from openfhe import *

mult_depth = 1
scale_mod_size = 50
batch_size = 8

parameters = CCParamsCKKSRNS()
parameters.SetMultiplicativeDepth(mult_depth)
parameters.SetScalingModSize(scale_mod_size)
parameters.SetBatchSize(batch_size)
parameters.SetSecurityLevel(HEStd_NotSet)
parameters.SetRingDim(4096)

cc = GenCryptoContext(parameters)
cc.Enable(PKESchemeFeature.PKE)
cc.Enable(PKESchemeFeature.KEYSWITCH)
cc.Enable(PKESchemeFeature.LEVELEDSHE)
cc.Enable(PKESchemeFeature.ADVANCEDSHE)

print("The CKKS scheme is using ring dimension: " + str(cc.GetRingDimension()))

keys = cc.KeyGen()
cc.EvalMultKeyGen(keys.secretKey)
cc.EvalRotateKeyGen(keys.secretKey, [1, -2])
eval_sum_row_keys = cc.EvalSumRowsKeyGen(keys.secretKey, rowSize=8)

x1 = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
x2 = [5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25]

ptx1 = cc.MakeCKKSPackedPlaintext(x1)
ptx2 = cc.MakeCKKSPackedPlaintext(x2)

print("Input x1: " + str(ptx1))
print("Input x2: " + str(ptx2))

# Encrypt the encoded vectors
c1 = cc.Encrypt(keys.publicKey, ptx1)
c2 = cc.Encrypt(keys.publicKey, ptx2)

# Step 4: Evaluation
# Homomorphic additions
c_add = cc.EvalAdd(c1, c2)
# Homomorphic subtraction
c_sub = cc.EvalSub(c1, c2)
# Homomorphic scalar multiplication
c_scalar = cc.EvalMult(c1,4)
# Homomorphic multiplication
c_mult = cc.EvalMult(c1, c2)
# Homomorphic rotations
c_rot1 = cc.EvalRotate(c1, 1)
c_rot2 = cc.EvalRotate(c1, -2)

c_rot3 = cc.EvalSumRows(c1, 8, eval_sum_row_keys)

# Step 5: Decryption and output
# Decrypt the result of additions
ptAdd = cc.Decrypt(c_add,keys.secretKey)

# We set the precision to 8 decimal digits for a nicer output.
# If you want to see the error/noise introduced by CKKS, bump it up
# to 15 and it should become visible.

precision = 8
print("Results of homomorphic computations:")
result = cc.Decrypt(c1, keys.secretKey)
result.SetLength(batch_size)
print("x1 = " + str(result))
print("Estimated precision in bits: " + str(result.GetLogPrecision()))

# Decrypt the result of scalar multiplication
result = cc.Decrypt(c_scalar,keys.secretKey)
result.SetLength(batch_size)
print("4 * x1 = " + str(result))

# Decrypt the result of multiplication
result = cc.Decrypt(c_mult,keys.secretKey)
result.SetLength(batch_size)
print("x1 * x2 = " + str(result))

# Decrypt the result of rotations
result = cc.Decrypt(c_rot1,keys.secretKey)
result.SetLength(batch_size)
print("In rotations, very small outputs (~10^-10 here) correspond to 0's:")
print("x1 rotated by 1 = " + str(result))

result = cc.Decrypt(c_rot2,keys.secretKey)
result.SetLength(batch_size)
print("x1 rotated by -2 = " + str(result))

result = cc.Decrypt(c_rot3,keys.secretKey)
result.SetLength(batch_size)
print("EvalSumRown for  = " + str(result))







Hi @ypolyakov , after trying to test with the latest version many time, I decided to downgrade the version to openfhe-development v1.1.2 and pythonwrapper with 0.8.4
So far, my project code can run perfectly in v1.1.2 (or below) with the same code and the same logic. Maybe after this project, I will try to test again with the latest version and notify you if it works. Thank you very much for supporting me!