Tracking slots of Ciphertexts through operations

Hello everyone,

I am currently implementing OpenFHE as a back end for a machine learning tool, which can do inference on encrypted inputs using the onnx data format. For this we call different programs that each handle key generation, encryption, inference and decryption meaning that the ciphertexts are serialized and deserialized after the inference step. During the decryption process the slot size of the output node is required, in order to only get an output of the correct size. Is there any way to track this?
I naively thought that this could be achieved using the getSlots and setSlots method for ciphertexts, but this gives weird powers of two, when having linear layers.

Thanks in advance

Hi @LHenke,

At a high level, we only track the number of slots for scenarios with sparse CKKS packing (it works with power-of-two subrings in this case). By default, we assume full packing in CKKS, BGV, and BFV. SetSlots/GetSlots is used only to tell OpenFHE the subring dimension size (note that this feature is supported only in CKKS).

How many slots are actually used in a ciphertext and which slots are empty are assumed to be the responsibility of the application developer (not tracked in OpenFHE to prevent unnecessary fata leakage). You can use the SetLength method (as demonstrated in many OpenFHE examples), to tell OpenFHE how many slots of the plaintext storing the decryption results to output using standard output stream operations.

Ok, I mean the issue here is that the used number of slots need to be preserved during serialization and deserialization, and I am hesitant to further add arguments to the decryption program, since it further complicates usage. But your response was still very helpful.
I have seen the Metadata member in the documentation, but did not find a lot of info on it. Is it possible to add information to a ciphertext object with that, and also make it serializable with that?

Thanks in advance

Yes, you can use the ciphertext metadata for this.

For examples of how to use the ciphertext metadata, I suggest the following unit tests:

Note that a test MetaData class was created for the unit tests: Serialization for it is enabled using

Thanks a lot for the responses!


I just wanted to add that this worked really well, thank you.

1 Like