Serialization of Plaintexts (or DCRTPoly)

Hi there,

I’m looking for a way to serialize/deserialize plaintext (as a polynomial). I found a thread discussing this(Serializing a Plaintext), and @ypolyakov suggested using DCRTPoly with GetElement() to store plaintext. However, I can’t find any function that supports serialization of DCRTPoly. Can you explain how to do this, example code for this? Or any plan to add plaintext serialization? Thanks!

Here is an example: openfhe-development/src/core/unittest/UnitTestSerialize.cpp at v1.1.2 · openfheorg/openfhe-development · GitHub Element would get replaced with DCRTPoly. Generally speaking, Serialize and SerializeToFile can take an object of any class that supports serialization, e.g., DCRTPoly.

Got it, I found that Serialize function works well for DCRTPoly. Then can you explain one more: when I deserialize DCRTPoly, how can I convert it into Plaintext format? I couldn’t find any MakePlaintext function that works with DCRTPoly yet. For instance, I want to run following example:

    vector<int64_t> plain_vec(slot_count, 0);

    // encode
    Plaintext plain;
    plain = cryptoContext->MakePackedPlaintext(plain_vec);
    DCRTPoly poly = plain->GetElement<DCRTPoly>();
    // serialize
    std::stringstream s;
    Serial::Serialize(poly, s, SerType::JSON);
    // deserialize
    DCRTPoly deser;
    Serial::Deserialize(deser, s, SerType::JSON);

    // want to define new plaintext from deserialized DCRTPoly
    Plaintext plain_new = // something with deser

    // encrypt plaintext
    Ciphertext<DCRTPoly> ct = cryptoContext→Encrypt(secretKey, plain_new);

    // and so on 

In general, I would not recommend serializing plaintexts as they are an inflated (significantly expanded) version of the input data. It is more efficient to serialize a vector of integers directly and send it (assuming it is trusted environment), and then call MakePackedPlaintext at the party that will be using the plaintexts. This can always be done as a precomputation (offline). This is the main reason why we did not add explicit serialization for Plaintext classes.

To still deserialize a plaintext in OpenFHE, I suggest looking at constructors at openfhe-development/src/pke/include/encoding/packedencoding.h at v1.1.2 · openfheorg/openfhe-development · GitHub and unit tests at openfhe-development/src/pke/unittest/UnitTestEncoding.cpp at v1.1.2 · openfheorg/openfhe-development · GitHub

Basically you will need to create a new plaintext with all necessary parameters (read from the original plaintext; these will also need to be serialized separately) and then set the vector to the deserialized value using GetElement()<DCRTPoly>.