Serialization of Homomorphic Encryption Elements

--

In any program, we can have data objects and which are represented in a binary format. These reside in memory. But what if we need to send a binary object over a network connection or save it in a text format? Well, for this, we can serialize our objects. One library we can use to serialize is Cereal [here]:

This library does not contain C++ code, and only requires the integration of hpp files. This allows for easy integration into the code. The hpp headers include:

access.hpp
cereal.hpp
macros.hpp
specialize.hpp
version.hpp

With the integration with OpenFHE, we simply integrate headers which will integrate with the Cereal headers:

#include "ciphertext-ser.h"
#include "cryptocontext-ser.h"
#include "key/key-ser.h"
#include "scheme/ckksrns/ckksrns-ser.h"

With this, we integrate the Serial library to either convert to a string or to a file:

auto str = Serial::SerializeToString(  cryptoContext);

if (!Serial::SerializeToFile("./cryptocontext.bin", cryptoContext, SerType::BINARY)) {
std::cerr << "Error serializing cc" << std::endl;
return 1;
}

Once serialized, we can then just call deserialization methods to reverse the process.

A fuller example is [here]:

#include <openfhe.h>
using namespace lbcrypto;
using namespace std;
#include <iomanip>
#include <tuple>
#include <unistd.h>
#include "ciphertext-ser.h"
#include "cryptocontext-ser.h"
#include "key/key-ser.h"
#include "scheme/ckksrns/ckksrns-ser.h"

#include <iostream>
int main(int argc, char *argv[]) {
int64_t x=0;
int64_t y=0;
string type="BFV";
uint64_t mod = 65537;

if (argc>1) {
std::istringstream iss(argv[1]);
iss >> mod;
}
CCParams<CryptoContextBFVRNS> parameters;
parameters.SetPlaintextModulus(mod);
parameters.SetMultiplicativeDepth(2);
CryptoContext<DCRTPoly> cryptoContext = GenCryptoContext(parameters);
cryptoContext->Enable(PKE);
cryptoContext->Enable(KEYSWITCH);
cryptoContext->Enable(LEVELEDSHE);

KeyPair<DCRTPoly> keyPair;
// Generate a public/private key pair
keyPair = cryptoContext->KeyGen();
std::cout << "The key pair has been generated." << std::endl;
auto str = Serial::SerializeToString( cryptoContext);
cout << "Crypto Context (First 2,000 characters):\n" << str.substr(0,2000) << endl;

if (!Serial::SerializeToFile("./cryptocontext.bin", cryptoContext, SerType::BINARY)) {
std::cerr << "Error serializing pk" << std::endl;
return 1;
}
if (!Serial::SerializeToFile("./cryptoContext.txt", cryptoContext, SerType::JSON)) {
std::cerr << "Error serializing pk" << std::endl;
return 1;
}


return(0);

}

A sample run is [here]:

{
"value0": {
"polymorphic_id": 1073741824,
"ptr_wrapper": {
"id": 2147483649,
"data": {
"cereal_class_version": 1,
"cc": {
"polymorphic_id": 2147483649,
"polymorphic_name": "lbcrypto::CryptoParametersBFVRNS",
"ptr_wrapper": {
"id": 2147483650,
"data": {
"cereal_class_version": 0,
"value0": {
"cereal_class_version": 0,
"value0": {
"cereal_class_version": 0,
"value0": {
"cereal_class_version": 0,
"elp": {
"polymorphic_id": 1073741824,
"ptr_wrapper": {
"id": 2147483651,
"data": {
"cereal_class_version": 1,
"value0": {
"cereal_class_version": 1,
"co": 16384,
"rd": 8192,
"cm": {
"cereal_class_version": 1,
"v": [
3458500651430690817,
216172783388817408,
4503599627369408
],
"m": 180
},
"ru": {
"v": [
0
],
"m": 0
},
"bm": {
"v": [
0
],
"m": 0
},
"br": {
"v": [
0
],
"m": 0
}
},
"p": [
{
"polymorphic_id": 1073741824,
"ptr_wrapper": {
"id": 2147483652,
"data": {
"cereal_class_version": 1,
"value0": {
"cereal_class_version": 1,
"co": 16384,
"rd": 8192,
"cm": {
"cereal_class_version": 1,
"v": 1152921504606830593
},
"ru": {
"v": 25959043411404
},
"bm": {
"v": 0
},
"br": {
"v": 0
}
}
}
}
},
{
"polymorphic_id": 1073741824,
"ptr_wrapper": {
"id": 2147483653,
"data": {
"value0": {
"co": 16384,
"rd": 8192,
"cm": {
"v": 1152921504606748673
},
"ru": {
"v": 100406242475323
},
"bm": {
"v": 0
},
"br": {
"v": 0
}
}
}
}
},
{
"polymorphic_id": 1073741824,
"ptr_wrapper": {
"id": 2147483654,
"data": {
"value0": {
"co": 16384,
"rd": 8192,
"cm": {
"v": 1152921504606683137
},
"ru": {
"v": 45474351589225
},
"bm": {
"v": 0
},
"br": {
"v": 0
}
}
}
}
}
]
}
}
},
"enp": {
"polymorphic_id": 1073741824,
"ptr_wrapper": {
"id": 2147483655,
"data": {
"cereal_class_version": 0,
"m": 65537,
"ru": {
"v": 0
},
"bm": {
"v": 0
},
"bru": {
"v": 0
},
"g": 0,
"bs": 8192
}
}
}
},
"dp": 3.190000057220459,
"am": 36.0,
"ns": 1,
"rw": 0,
"md": 2,
"mo": 1,
"pmo": 1,
"mmo": 1,
"exm": 0,
"dnm": 0,
"slv": 0,
"fdp": 0.0,
"ss": 0.0,
"aq": 0.0,
"tp": 1
},
"ks": 1,
"rs": 4,
"encs": 0,
"muls": 3,
"dnum": 2,
"ab": 60,
"eb": 0,
"ccl": 3
}
}
}
},
"kt": {
"polymorphic_id": 2147483650,
"polymorphic_name": "lbcrypto::SchemeBFVRNS",
"ptr_wrapper": {
"id": 2147483656,
"data": {
"cereal_class_version": 0,
"value0": {
"cereal_class_version": 0,
"value0": {
"cereal_class_version": 0,
"fhe": {
"polymorphic_id": 0
},
"schswitch": {
"polymorphic_id": 0
},
"enabled": 11
}
}
}
}
},
"si": 2
}
}
}
}

You will find other examples here:

--

--

Prof Bill Buchanan OBE FRSE
ASecuritySite: When Bob Met Alice

Professor of Cryptography. Serial innovator. Believer in fairness, justice & freedom. Based in Edinburgh. Old World Breaker. New World Creator. Building trust.