# Serialization of Plaintexts (or DCRTPoly)

**URL:** <https://openfhe.discourse.group/t/serialization-of-plaintexts-or-dcrtpoly/1093>\
**Category:** Library Questions\
**Tags:** questions\
**Created:** [February 27, 2024, 7:25pm UTC](https://openfhe.discourse.group/t/serialization-of-plaintexts-or-dcrtpoly/1093 "2024-02-27T19:25:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swanhong](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@swanhong](https://openfhe.discourse.group/u/swanhong)\
**Post date:** [February 27, 2024, 7:25pm UTC](https://openfhe.discourse.group/t/serialization-of-plaintexts-or-dcrtpoly/1093/1 "2024-02-27T19:25:24Z")

</div>

Hi there,

I’m looking for a way to serialize/deserialize plaintext (as a polynomial). I found a thread discussing this([Serializing a Plaintext](https://openfhe.discourse.group/t/serializing-a-plaintext/623)), 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!

---

<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:** [February 28, 2024, 6:32pm UTC](https://openfhe.discourse.group/t/serialization-of-plaintexts-or-dcrtpoly/1093/2 "2024-02-28T18:32:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![swanhong](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@swanhong](https://openfhe.discourse.group/u/swanhong)\
**Post date:** [February 28, 2024, 10:17pm UTC](https://openfhe.discourse.group/t/serialization-of-plaintexts-or-dcrtpoly/1093/3 "2024-02-28T22:17:12Z")

</div>

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:

```auto
    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 

```

---

<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 1, 2024, 3:58am UTC](https://openfhe.discourse.group/t/serialization-of-plaintexts-or-dcrtpoly/1093/4 "2024-03-01T03:58:26Z")

</div>

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](https://github.com/openfheorg/openfhe-development/blob/v1.1.2/src/pke/include/encoding/packedencoding.h#L69) and unit tests at [openfhe-development/src/pke/unittest/UnitTestEncoding.cpp at v1.1.2 · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/blob/v1.1.2/src/pke/unittest/UnitTestEncoding.cpp)

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>`.
