I’m trying to understand better how eval keys should be managed in OpenFHE. Assume that we have the following scenario. We run a typical client-server application, where the client sends eval keys, the server does some costly operation, returns the result to the client and finally clears the keys using this function. I tried to run two copies of the same code at the same time and at the moment the first process finishes then the second one crashes since it does not have eval keys inside the cc anymore.
Two important notes:
We want to clear the keys in the server since they are big and we do not want to keep them in memory waiting for client requests.
Notice that this problem does not happen when you have calls from different clients with different keys, in that case you just clear the keys with the corresponding keytag.
The whole problem would go away if there was a way to check if some other process is using the same keys in OpenFHE. Is there such a function?
I am not sure I fully understand the setup. Are you saying you are running the same executable (within the same process)? It gets two client requests. It gets the keys from a client request and executes the code, then it clears the eval keys. Then it gets another client request (I guess without the eval keys) and it is not able to compute, right?
In general, you can deserialize eval keys whenever you want (if you had cleared them from the memory). Otherwise, you can keep the keys in the memory longer. Basically you either have the eval keys in the memory or on persistent storage (if the latter is used to minimize the memory usage). The standard OpenFHE serialization/deserialization mechanism can be used for this.
Yes, I run a process which is able to create a thread for each client request. Considering your solution, I think that clearing the keys in the first thread and then loading the keys in the second is inefficient in comparison to just having a way to check if the keys are being used and not clear them if so. Also, as far as I understand, if you want to scale the application, having all the keys in memory quickly becomes a problem. Please correct me if I’m wrong here.
Is there a way in OpenFHE for a thread to know that some other thread is using the same CC and therefore it should not release the keys at the end?
I think there is some confusion here. If OpenFHE is run in the multithreaded setting (default), all threads can access the same evaluation keys (cryptocontexts). If OpenFHE is run using multiple processes (rather than different threads within the same process), then these processes will not be able to see each other’s evaluation key by default. You can probably play with shared memory, but I would rather recommend using one OpenFHE process for all client requests (and using threads internally to distinguish between requests).