Use of EvalSum in Serialization for the Multiparty (Threshold FHE) Setting

I’m having problems using EvalSum after serializing and deserializing the CrytoContext as I get the following error:

RuntimeError: /openfhe-development/src/pke/lib/cryptocontext.cpp:146 You need to use EvalSumKeyGen so that you have EvalSumKeys available for this ID

I set the cryptocontext like this:
def generate_k1_k2(scaleModSize,batchSize, multDepth):
parameters = CCParamsCKKSRNS()
parameters.SetScalingModSize(scaleModSize)
parameters.SetBatchSize(batchSize)
parameters.SetMultiplicativeDepth(multDepth)

# CryptoContext generation
node1CC = GenCryptoContext(parameters)
node1CC.Enable(PKE)
node1CC.Enable(KEYSWITCH)
node1CC.Enable(LEVELEDSHE)
node1CC.Enable(ADVANCEDSHE)
node1CC.Enable(MULTIPARTY)

 ## Perform Key Generation Operation
############################################################

print("Running key generation (used for source data)...")

# Round 1 (party A)

print("Round 1 (party A) started.")

kp1 = node1CC.KeyGen()

# Generate evalmult key part for A
evalMultKey = node1CC.KeySwitchGen(kp1.secretKey, kp1.secretKey)

# Generate evalsum key part for A
node1CC.EvalSumKeyGen(kp1.secretKey)
evalSumKeys = node1CC.GetEvalSumKeyMap(kp1.secretKey.GetKeyTag())

print("Round 1 of key generation completed.")

# Round 2 (party B)

print("Round 2 (party B) started.")

print("Joint public key for (s_a + s_b) is generated...")
kp2 = node1CC.MultipartyKeyGen(kp1.publicKey)

evalMultKey2 = node1CC.MultiKeySwitchGen(kp2.secretKey, kp2.secretKey, evalMultKey)

print("Joint evaluation multiplication key for (s_a + s_b) is generated...")
evalMultAB = node1CC.MultiAddEvalKeys(evalMultKey, evalMultKey2, kp2.publicKey.GetKeyTag())

print("Joint evaluation multiplication key (s_a + s_b) is transformed into s_b*(s_a + s_b)...")
evalMultBAB = node1CC.MultiMultEvalKey(kp2.secretKey, evalMultAB, kp2.publicKey.GetKeyTag())

evalSumKeysB = node1CC.MultiEvalSumKeyGen(kp2.secretKey, evalSumKeys, kp2.publicKey.GetKeyTag())

print("Joint evaluation summation key for (s_a + s_b) is generated...")
evalSumKeysJoin = node1CC.MultiAddEvalSumKeys(evalSumKeys, evalSumKeysB, kp2.publicKey.GetKeyTag())

node1CC.InsertEvalSumKey(evalSumKeysJoin)

print("Round 2 of key generation completed.")

print("Round 3 (party A) started.")

print("Joint key (s_a + s_b) is transformed into s_a*(s_a + s_b)...")
evalMultAAB = node1CC.MultiMultEvalKey(kp1.secretKey, evalMultAB, kp2.publicKey.GetKeyTag())

print("Computing the final evaluation multiplication key for (s_a + s_b)*(s_a + s_b)...")
evalMultFinal = node1CC.MultiAddEvalMultKeys(evalMultAAB, evalMultBAB, evalMultAB.GetKeyTag())

node1CC.InsertEvalMultKey([evalMultFinal])
if not  SerializeToFile(f"{mylocalfolder}/{ccLocation}", node1CC,  BINARY):
    raise Exception("Exception writing cryptocontext to cryptocontext.txt")

Can anyone help me?

Is this a single-party application?
Try to generate the EvalSumKeys before serializing the crypto-context as follows:

KeyPair<DCRTPoly> kp = cryptoContext->KeyGen();
cryptoContext->EvalSumKeyGen(kp.secretKey);

This might resolve the problem.

No it’s a multiparty application, what could I do?

First, let’s make sure you generate evalSumKeys correctly.

OpenFHE includes an example of Threshold-CKKS (3 parties) which includes logic for generating evalSumKeys (but without serialization).
First, I would try the example without serialization to ensure you generate evalSumKeys correctly. Then you can add the serialization logic.

I tried this example without serialization and it works. The problem is with the serialization

Sorry, can you suggest something to me? I’ve been using openfhe for a while now and I can’t figure out where the problem is

I suggest checking the following two topics:

The following repository may also be helpful: GitHub - openfheorg/openfhe-network-examples: OpenFHE Experiments in Encrypted Network Control and Secure Data Distribution with Proxy Re-Encryption