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

**URL:** <https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774>\
**Category:** Library Questions\
**Created:** [December 13, 2024, 5:46pm UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774 "2024-12-13T17:46:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antox](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@Antox](https://openfhe.discourse.group/u/Antox)\
**Post date:** [December 13, 2024, 5:46pm UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774/1 "2024-12-13T17:46:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [December 14, 2024, 12:58am UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774/2 "2024-12-14T00:58:35Z")

</div>

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

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

```

This might resolve the problem.

---

<div class="post-metadata">

**Author:** ![Antox](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@Antox](https://openfhe.discourse.group/u/Antox)\
**Post date:** [December 14, 2024, 4:52pm UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774/4 "2024-12-14T16:52:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [December 14, 2024, 6:02pm UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774/5 "2024-12-14T18:02:43Z")

</div>

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

OpenFHE includes an [example](https://github.com/openfheorg/openfhe-development/blob/v1.2.3/src/pke/examples/tckks-interactive-mp-bootstrapping-Chebyshev.cpp) 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.

---

<div class="post-metadata">

**Author:** ![Antox](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@Antox](https://openfhe.discourse.group/u/Antox)\
**Post date:** [December 14, 2024, 6:05pm UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774/6 "2024-12-14T18:05:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Antox](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@Antox](https://openfhe.discourse.group/u/Antox)\
**Post date:** [December 15, 2024, 7:29pm UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774/7 "2024-12-15T19:29:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)\
**Post date:** [December 16, 2024, 1:50pm UTC](https://openfhe.discourse.group/t/use-of-evalsum-in-serialization-for-the-multiparty-threshold-fhe-setting/1774/8 "2024-12-16T13:50:36Z")

</div>

I suggest checking the following two topics:

- [How to properly serialize evalSumKeys in threshold fhe? - #2 by ypolyakov](https://openfhe.discourse.group/t/how-to-properly-serialize-evalsumkeys-in-threshold-fhe/609/2)
- [Examples for PRE and Threshold FHE are now available](https://openfhe.discourse.group/t/examples-for-pre-and-threshold-fhe-are-now-available/916)

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](https://github.com/openfheorg/openfhe-network-examples)
