# CKKS deserialisation

**URL:** <https://openfhe.discourse.group/t/ckks-deserialisation/719>\
**Category:** Library Questions\
**Created:** [July 26, 2023, 6:31pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719 "2023-07-26T18:31:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![miiip](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@miiip](https://openfhe.discourse.group/u/miiip)\
**Post date:** [July 26, 2023, 6:31pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/1 "2023-07-26T18:31:48Z")

</div>

Hi all,

I’m trying to deserialize a CKKS ciphertext to a JSON string and I receive the following error:

> Blockquote  
> Trying to save an unregistered polymorphic type (lbcrypto::CryptoParametersCKKSRNS).  
> Make sure your type is registered with CEREAL\_REGISTER\_TYPE and that the archive you are using was included (and registered with CEREAL\_REGISTER\_ARCHIVE) prior to calling CEREAL\_REGISTER\_TYPE.  
> If your type is already registered and you still see this error, you may need to use CEREAL\_REGISTER\_DYNAMIC\_INIT.

I read [this](https://openfhe.discourse.group/t/how-to-save-ckkss-ciphertext-in-database/247) post and [this](https://openfhe.discourse.group/t/serializing-a-plaintext/623) post but I wasn’t able to find a solution.

Thank you!

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [July 26, 2023, 6:46pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/2 "2023-07-26T18:46:28Z")

</div>

Please include your code

---

<div class="post-metadata">

**Author:** ![miiip](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@miiip](https://openfhe.discourse.group/u/miiip)\
**Post date:** [July 26, 2023, 6:57pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/3 "2023-07-26T18:57:42Z")

</div>

```auto
TIC(t);
    std::vector<Ciphertext<DCRTPoly>> ciphertexts;
    for(int idx = 0; idx < plaintexts.size(); idx++) {
        Ciphertext<DCRTPoly> ciphertext= cc->Encrypt(publicKey, plaintexts[idx]);
        std::string jsonCiphertext = Serial::SerializeToString(ciphertext);
        std::cout << jsonCiphertext << std::endl;
        ciphertexts.push_back(ciphertext);
    }
    processingTime = TOC(t);

```

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [July 26, 2023, 7:01pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/4 "2023-07-26T19:01:52Z")

</div>

Hmm, that’s interesting why it’s not working… Please follow [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and I’ll dig more into it.

Thank you!

---

<div class="post-metadata">

**Author:** ![miiip](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@miiip](https://openfhe.discourse.group/u/miiip)\
**Post date:** [July 26, 2023, 7:09pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/5 "2023-07-26T19:09:01Z")

</div>

Thank you for your quick reply. Here is the complete code (it’s made from the [original example](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-real-numbers.cpp))

```auto
#include "openfhe.h"

using namespace lbcrypto;

int main() {

    uint32_t multDepth = 1;
    uint32_t scaleModSize = 50;
    uint32_t batchSize = 8;
    CCParams<CryptoContextCKKSRNS> parameters;
    parameters.SetMultiplicativeDepth(multDepth);
    parameters.SetScalingModSize(scaleModSize);
    parameters.SetBatchSize(batchSize);

    CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
    cc->Enable(PKE);
    cc->Enable(KEYSWITCH);
    cc->Enable(LEVELEDSHE);
    auto keys = cc->KeyGen();
    cc->EvalMultKeyGen(keys.secretKey);
    std::vector<double> x = {0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0};
    Plaintext ptxt = cc->MakeCKKSPackedPlaintext(x);
    auto c = cc->Encrypt(keys.publicKey, ptxt);
    std::string jsonCiphertext = Serial::SerializeToString(c);
    std::cout << jsonCiphertext << std::endl;
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![narger](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/narger/32/420_2.png) [@narger](https://openfhe.discourse.group/u/narger)\
**Post date:** [July 27, 2023, 3:20pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/6 "2023-07-27T15:20:29Z")

</div>

I serialize like this:

```auto
Serial::SerializeToFile(filename, v, SerType::BINARY);

```

where `filename` is a `std::string` and `v` is a `Ciphertext<DCRTPoly>`, then de-serialization as follows:

```auto
Ciphertext<DCRTPoly> result;

if (!Serial::DeserializeFromFile(filename, result, SerType::BINARY))
    cerr << "Could not find \"" << filename << "\"" << endl;

```

and it works like a charm. I am using CKKS, too.

---

<div class="post-metadata">

**Author:** ![miiip](https://avatars.discourse-cdn.com/v4/letter/m/a183cd/32.png) [@miiip](https://openfhe.discourse.group/u/miiip)\
**Post date:** [July 28, 2023, 6:28am UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/7 "2023-07-28T06:28:59Z")

</div>

Thank you!

I modified the code and the error still persists. Here is the update:

```auto
#include "openfhe.h"

using namespace lbcrypto;

int main() {

    uint32_t multDepth = 1;
    uint32_t scaleModSize = 50;
    uint32_t batchSize = 8;
    CCParams<CryptoContextCKKSRNS> parameters;
    parameters.SetMultiplicativeDepth(multDepth);
    parameters.SetScalingModSize(scaleModSize);
    parameters.SetBatchSize(batchSize);

    CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
    cc->Enable(PKE);
    cc->Enable(KEYSWITCH);
    cc->Enable(LEVELEDSHE);
    auto keys = cc->KeyGen();
    cc->EvalMultKeyGen(keys.secretKey);
    std::vector<double> x = {0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0};
    Plaintext ptxt = cc->MakeCKKSPackedPlaintext(x);
    Ciphertext<DCRTPoly> c = cc->Encrypt(keys.publicKey, ptxt);
    std::string filename = "output.txt";
    Serial::SerializeToFile(filename, c, SerType::BINARY);
    return 0;
}

```

---

<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:** [July 29, 2023, 2:17pm UTC](https://openfhe.discourse.group/t/ckks-deserialisation/719/8 "2023-07-29T14:17:26Z")

</div>

@miiip Please look at OpenFHE serialization examples, e.g., [https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-integers-serial.cpp](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-integers-serial.cpp). When serialization is needed, several header files with suffix “-ser.h” need to be included. Otherwise, the errors like the ones you listed appear. The last header file is scheme-specific (you would need to replace bfvrns with ckksrns).
