# What's the best way to use openFHE on two sides?

**URL:** <https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380>\
**Category:** FHE Use Cases\
**Created:** [February 23, 2023, 3:59pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380 "2023-02-23T15:59:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 23, 2023, 3:59pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/1 "2023-02-23T15:59:38Z")

</div>

Dear,  
I am contacting you because I am a beginner on openFHE library. Indeed, as part of my sensitive data protection POC, I need to use the openFHE library on two sides : Owner server side and Provider server side.

**Owner server side**

1. Generate and share Keys

2. Encrypt and send data

3. Decrypt data with privatekey

**Provider server side**

Receive encryptedData1, encryptedData2 from Owner Server

Create ComputeContext

ComputeContext.Insert?(evalMultkey);

ComputeContext.Insert?(evalSumkey);

ComputeContext.Insert?(evalAtIndexkey);

Compute HE operations :

```
    EvalMult(encryptedData1, encryptedData2)
    EvalAtIndex(? , index)
    EvalAddInPlace(?,?)

```

Send encryptedResult to Owner Server

My question is: How do I initialize OpenFHE’s cryptoContexts : encryptContext?, decryptContext?, ComputeContext ? and what’s the best way to implement my use case with openFHE?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://openfhe.discourse.group/u/saroja)\
**Post date:** [February 23, 2023, 4:18pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/2 "2023-02-23T16:18:25Z")

</div>

See the following examples for BFV, BGV, and CKKS.

- [BFV](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-integers-serial.cpp)
- [BGV](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-integers-serial-bgvrns.cpp)
- [CKKS](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-real-numbers-serial.cpp)

The owner server side would serialize the created CryptoContexts into files, and the provider server side would deserialize the files back into CryptoContexts

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [February 23, 2023, 4:29pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/3 "2023-02-23T16:29:33Z")

</div>

If you’re looking for a “larger scale” example, check out the material we’ve put out for [palisade-serial-examples](https://gitlab.com/palisade/palisade-serial-examples/-/tree/master/). Just remember that Palisade != OpenFHE so not everything there will transfer over (in terms of creating the cryptocontexts, etc) but the core idea of a two-party system is there.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 23, 2023, 4:38pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/4 "2023-02-23T16:38:08Z")

</div>

> [@gbmamat](#):
>
> `Share evalMultkey, evalSumkey, evalAtIndexkey keys with provider server side`

Thanks #saroja for your reply

According your response don’t need to load evalMultkey, evalSumkey, evalAtIndexkey keys on provider server side?

---

<div class="post-metadata">

**Author:** ![saroja](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@saroja](https://openfhe.discourse.group/u/saroja)\
**Post date:** [February 23, 2023, 4:45pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/5 "2023-02-23T16:45:28Z")

</div>

You also need to serialize and deserialize any keys you are using! The examples linked above show how to do this with:

- `SerializeToFile` and `DeserializeFromFile` for the cryptocontext, public key, private key, and ciphertexts
- `SerializeEvalMultKey` and `DeserializeEvalMultKey` for the evalMultkey
- `SerializeEvalAutomorphismKey` and `DeserializeEvalAutomorphismKey` for the evalRotatekey

---

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 23, 2023, 4:47pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/6 "2023-02-23T16:47:32Z")

</div>

Thanks @iquah for your reply.

There are not way to share cryptoContext as base64 text instead file?  
And how to load shared keys : privateKey, publicKey,evalMultkey, evalSumkey, evalAtIndexkey ?

---

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 23, 2023, 5:05pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/7 "2023-02-23T17:05:35Z")

</div>

Thanks for help.  
I’ll trying this way.

---

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 23, 2023, 5:09pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/8 "2023-02-23T17:09:18Z")

</div>

Thanks so much for links.

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [February 23, 2023, 5:11pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/9 "2023-02-23T17:11:31Z")

</div>

> There are not way to share cryptoContext as base64 text instead file?

We provide capabilities to serialize to both json and binary strings. You’d then transmit the string to the receiver who then deserializes these strings.

```auto
std::stringstream s;
Serial::Serialize(serverCC, s, SerType::BINARY);

```

See [Program Listing for File cryptocontext-ser.h — OpenFHE documentation](https://openfhe-development.readthedocs.io/en/latest/api/program_listing_file_pke_include_cryptocontext-ser.h.html) for various functions that we provide.

> And how to load shared keys : privateKey, publicKey,evalMultkey, evalSumkey, evalAtIndexkey ?

Those keys require an already-created cryptocontext. You use the cryptocontext to handle the serialization and deserialization of those objects. See the link I just shared to see what functions exist and what you can call

---

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 23, 2023, 5:27pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/10 "2023-02-23T17:27:33Z")

</div>

> [@saroja](#):
>
> `SerializeEvalMultKey`

I’m checked openFHE docs about Serialization.

> <https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-integers-serial.cpp>

Hope this helps someone else with the same needs

---

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 23, 2023, 5:29pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/11 "2023-02-23T17:29:40Z")

</div>

Thanks again for your helps.

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [February 24, 2023, 3:29pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/12 "2023-02-24T15:29:53Z")

</div>

Hi, I wrote the serialization examples repo in palisade [PALISADE / PALISADE serial examples · GitLab](https://gitlab.com/palisade/palisade-serial-examples). It contains examples of two separate heavyweight processes that do what you want. At least it shows the things that need to be done in order to share a crypto context between two programs. Just don’t share the secret key!

I do not think it would be too much effort to port it to OpenFHE, especially if you use the serialization examples Saroja pointed you to – however all those examples are only for a single thread, and do not reveal all the required operations needed to get things to work correctly.

When you have two cooperating sequential processes they need to talk via a protocal that provides syncrhonisation, as well as just sending data back and forth. IIRC there is a file+mutex version and a simple socket I/O version using an open source library with simple ASIO sockets.

You _always_ have to serialise the crypto objects though. they are not simple arrays of integegers, but actually C++ object with control information and are ususally implemented as smart shared pointers as well. Serialization automagically converts _all_ the object information into a json (huge but barely readable) or binary (as small as it can get but no readable) buffer which gets written to a file or socket.

Hope this will help  
Dave

---

<div class="post-metadata">

**Author:** ![gbmamat](https://avatars.discourse-cdn.com/v4/letter/g/898d66/32.png) [@gbmamat](https://openfhe.discourse.group/u/gbmamat)\
**Post date:** [February 24, 2023, 3:49pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/13 "2023-02-24T15:49:45Z")

</div>

Thanks @dcousins for your reply.

In my use case, i need share crypo Objects, pk, sk, evalK as stringstream stored on database  
I think File Serialize/Deserialize IO/Socket are very expensive for my use case.

Ghislain

---

<div class="post-metadata">

**Author:** ![Nightingale](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/nightingale/32/93_2.png) [@Nightingale](https://openfhe.discourse.group/u/Nightingale)\
**Post date:** [March 6, 2023, 11:17am UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/14 "2023-03-06T11:17:15Z")

</div>

Hi, I am trying to use real\_socket\_server example on PALISADE / PALISADE serial examples. when ı run it, i take segmentation fault.  
in the server  
_SERVER: creating acceptor for 8080  
SERVER: accepting socket  
SERVER: sending CC and Keys  
SERVER: sending cryptocontext  
streamed to buffer 5 bytes  
SERVER: send size to socket  
SERVER: sent 8 bytes to socket  
SERVER: sending data to socket  
SERVER: sent 5 data bytes to socket  
SERVER: sending Public key  
streamed to buffer 2623680 bytes  
SERVER: send size to socket  
SERVER: sent 8 bytes to socket  
SERVER: sending data to socket  
Exception: send: Broken pipe  
SERVER: Total time: 27003 mSec_  
in the client side  
_CLIENT: connecting to localhost:8080  
CLIENT: connected  
CLIENT: reading length from socket  
CLIENT: read 8 bytes from socket  
CLIENT: will read 5 bytes from socket  
CLIENT: read 5 bytes CC from socket  
Segmentation fault_

I have 16gb RAM and ı use WSL(ubuntu).

do you know how can ı solve this problem?

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [March 6, 2023, 12:48pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/15 "2023-03-06T12:48:31Z")

</div>

You can compile with the debug flags on and run the client in gdb to identify where it is segfaulting.

The server side is a message that the client died and took the socket with them.

Not sure if 16 GB ram is sufficient you might be running out of memory, can you check with top or htop?

---

<div class="post-metadata">

**Author:** ![Nightingale](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/nightingale/32/93_2.png) [@Nightingale](https://openfhe.discourse.group/u/Nightingale)\
**Post date:** [March 7, 2023, 2:04pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/16 "2023-03-07T14:04:29Z")

</div>

thank you for answer.

I checked the memory, the basic example use approximately 2gb RAM. When i compile with the debug mode, the fault is arised from :  
in client.cpp → CC clientCC= recvCC(s);

 ![Ekran görüntüsü 2023-03-07 144422](https://canada1.discourse-cdn.com/flex031/uploads/openfhe/original/1X/11ece8650f2541813b8212086810d73c880a46a1.png)  
 ![Ekran görüntüsü 2023-03-07 163410](https://canada1.discourse-cdn.com/flex031/uploads/openfhe/original/1X/ca189cb9701ddc14e3d47fbb82898236a2251535.png)

Could it be due to WSL?

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [March 8, 2023, 2:46pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/17 "2023-03-08T14:46:10Z")

</div>

I personally never use wsl, and so I never tested this example in that environment. What is happening here is that the message that is coming back from the sever may be empty or corrupted. I am a bit confused, in that it loos like openfhe code and you said you were using palisade?

---

<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:** [March 10, 2023, 5:54pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/18 "2023-03-10T17:54:48Z")

</div>

I just looked at this thread. I am using `wsl` and typically get the same behavior with it as with a native Ubuntu build. As far as I can see, the PALISADE serialization example is being run against OpenFHE. I would not expect the PALISADE serialization example to work correctly as many changes were made in OpenFHE since it forked off from PALISADE at the beginning of 2022. Why not use serialization examples from [openfhe-development/src/pke/examples at main · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/tree/main/src/pke/examples)?

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [March 15, 2023, 6:10pm UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/19 "2023-03-15T18:10:20Z")

</div>

the question is related to the communication between the two programs. That was why the palisade version was brought up. – but that said the palisade code is NOT interoperable with the openfhe code.

---

<div class="post-metadata">

**Author:** ![Nightingale](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/nightingale/32/93_2.png) [@Nightingale](https://openfhe.discourse.group/u/Nightingale)\
**Post date:** [March 20, 2023, 10:06am UTC](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380/20 "2023-03-20T10:06:16Z")

</div>

Thank you.

Before I sent you the error, I made changes to the Palisade code to make it work in OpenFHE (according to [How To Migrate A User Project From Palisade To OpenFHE](https://openfhe-development.readthedocs.io/en/latest/sphinx_rsts/misc/z_migration_guide.html)). I also solved my above problem with a new change. it is working now.

[Next page](https://openfhe.discourse.group/t/whats-the-best-way-to-use-openfhe-on-two-sides/380.md?page=2)
