Documentation on the global management of Crypto Contexts

Hi everybody,

First of all, this is an opinionated question after struggling with the library a little bit. It is related to some aspects of how the library works (non related to cryptography) which I have had to struggle with and I find them important to understand, but not intuitive without proper documentation. Maybe I did not find it.

Is the way Crypto Contexts are managed globally in OpenFHE documented somewhere? Personally, it seems to me one of the most fundamental aspects of how OpenFHE works that should be taken into account in any mature development and should be made known in any introduction to the technology.

When I first encounter the GenCryptoContext function, check the examples and read the comments in the header file, I expect it to create a new context, managed through the return value. It is not intuitive (to me) to understand that there is a global vector that keeps it in memory during program execution and that the next time you invoke GenCryptoContext with parameters with the same value it will return exactly the same shared pointer.

Similarly, I can expect that when I generate some EvalKey for some context, that EvalKey is somehow associated with the context. It is not intuitive (to me) to have methods that, from one context, return EvalKeys that only apply to other contexts. Only when I start to perceive unexpected behaviors and dig into the code, I discover that there is a global map where all the EvalKeys are managed sine die (regardless of their context) and that it is the Key Tags that actually allow you to determine the keys that apply in each case.

Maybe it is documented somewhere and I have not found it. Or it is not intuitive only for me. But my intuition leads me to think about other behavior. And it’s not that I don’t understand why this approach applies, once I figure out how it works. It’s simply that, if not understood, it can lead to wrong design decisions when moving from the single-file example to a real use case with several contexts, peers, etc.

Thank you very much and congratulations for your development.

Thank you for the questions and comments. I would like to note that the documentation and introductory articles primarily focus on explaining how to use FHE and the OpenFHE API. The question you are asking is on the the advanced use of the library. For these, it is better either to search using Welcome to OpenFHE’s documentation! — OpenFHE documentation or look at the header/source files directly.

How key objects are stored within a cryptocontext is described in the header file for cryptocontext (openfhe-development/src/pke/include/cryptocontext.h at main · openfheorg/openfhe-development · GitHub) or doxygen documentation from it Template Class CryptoContextImpl — OpenFHE documentation (searchable via ReadTheDocs).

The implementation for deduplicating cryptocontexts can be examined at openfhe-development/src/pke/lib/cryptocontextfactory.cpp at main · openfheorg/openfhe-development · GitHub The doxygen-generated documentation at Template Class CryptoContextFactory — OpenFHE documentation can also be useful.

I also suggest reading OpenFHE discourse topics on the same issues. The Discourse documentation should be treated as additional OpenFHE documentation. See, for example, Is CryptoContext global?.