3535
3636#include " openfhe.h"
3737// header files needed for serialization
38- #include " metadata-ser.h"
3938#include " ciphertext-ser.h"
4039#include " cryptocontext-ser.h"
4140#include " key/key-ser.h"
42- #include " scheme/bfvrns/bfvrns-ser.h"
43- #include " scheme/bgvrns/bgvrns-ser.h"
44- #include " scheme/ckksrns/ckksrns-ser.h"
4541
4642using namespace lbcrypto ;
4743namespace py = pybind11;
@@ -51,33 +47,30 @@ PYBIND11_MAKE_OPAQUE(std::map<uint32_t, EvalKey<DCRTPoly>>);
5147
5248
5349template <typename ST>
54- bool SerializeEvalMultKeyWrapper (const std::string &filename, const ST &sertype, std::string id)
55- {
50+ bool SerializeEvalMultKeyWrapper (const std::string& filename, const ST& sertype, std::string id) {
5651 std::ofstream outfile (filename, std::ios::out | std::ios::binary);
5752 bool res = CryptoContextImpl<DCRTPoly>::SerializeEvalMultKey<ST>(outfile, sertype, id);
5853 outfile.close ();
5954 return res;
6055}
6156
6257template <typename ST>
63- bool SerializeEvalAutomorphismKeyWrapper (const std::string& filename, const ST& sertype, std::string id)
64- {
58+ bool SerializeEvalAutomorphismKeyWrapper (const std::string& filename, const ST& sertype, std::string id) {
6559 std::ofstream outfile (filename, std::ios::out | std::ios::binary);
6660 bool res = CryptoContextImpl<DCRTPoly>::SerializeEvalAutomorphismKey<ST>(outfile, sertype, id);
6761 outfile.close ();
6862 return res;
6963}
7064
7165template <typename ST>
72- bool DeserializeEvalMultKeyWrapper (const std::string &filename, const ST &sertype)
73- {
66+ bool DeserializeEvalMultKeyWrapper (const std::string& filename, const ST& sertype) {
7467 std::ifstream emkeys (filename, std::ios::in | std::ios::binary);
75- if (!emkeys.is_open ())
76- {
68+ if (!emkeys.is_open ()) {
7769 std::cerr << " I cannot read serialization from " << filename << std::endl;
7870 }
7971 bool res = CryptoContextImpl<DCRTPoly>::DeserializeEvalMultKey<ST>(emkeys, sertype);
80- return res; }
72+ return res;
73+ }
8174
8275template <typename T, typename ST>
8376std::tuple<T, bool > DeserializeFromFileWrapper (const std::string& filename, const ST& sertype) {
@@ -101,10 +94,14 @@ std::string SerializeToStringWrapper(const T& obj, const ST& sertype) {
10194
10295template <typename T, typename ST>
10396py::bytes SerializeToBytesWrapper (const T& obj, const ST& sertype) {
104- std::ostringstream oss (std::ios::binary);
97+ // let strbuf be dynamically allocated as we may be dealing with large keys
98+ auto strbuf = std::make_unique<std::stringbuf>(std::ios::out | std::ios::binary);
99+ std::ostream oss (strbuf.get ());
100+
105101 Serial::Serialize<T>(obj, oss, sertype);
106- std::string str = oss.str ();
107- return py::bytes (str);
102+
103+ const std::string& str = strbuf->str ();
104+ return py::bytes (str.data (), str.size ());
108105}
109106
110107template <typename T, typename ST>
@@ -125,18 +122,20 @@ CryptoContext<DCRTPoly> DeserializeCCFromStringWrapper(const std::string& str, c
125122
126123template <typename T, typename ST>
127124T DeserializeFromBytesWrapper (const py::bytes& bytes, const ST& sertype) {
128- T obj;
129- std::string str (bytes);
125+ std::string str{static_cast <std::string>(bytes)};
130126 std::istringstream iss (str, std::ios::binary);
127+
128+ T obj;
131129 Serial::Deserialize<T>(obj, iss, sertype);
132130 return obj;
133131}
134132
135133template <typename ST>
136134CryptoContext<DCRTPoly> DeserializeCCFromBytesWrapper (const py::bytes& bytes, const ST& sertype) {
137- CryptoContext<DCRTPoly> obj;
138- std::string str (bytes);
135+ std::string str{static_cast <std::string>(bytes)};
139136 std::istringstream iss (str, std::ios::binary);
137+
138+ CryptoContext<DCRTPoly> obj;
140139 Serial::Deserialize<DCRTPoly>(obj, iss, sertype);
141140 return obj;
142141}
@@ -153,15 +152,17 @@ std::string SerializeEvalMultKeyToStringWrapper(const ST& sertype, const std::st
153152
154153template <typename ST>
155154py::bytes SerializeEvalMultKeyToBytesWrapper (const ST& sertype, const std::string& id) {
156- std::ostringstream oss (std::ios::binary);
157- bool res = CryptoContextImpl<DCRTPoly>::SerializeEvalMultKey (oss, sertype, id);
158- if (!res) {
155+ // let strbuf be dynamically allocated as we may be dealing with large keys
156+ auto strbuf = std::make_unique<std::stringbuf>(std::ios::out | std::ios::binary);
157+ std::ostream oss (strbuf.get ());
158+
159+ if (!CryptoContextImpl<DCRTPoly>::SerializeEvalMultKey (oss, sertype, id)) {
159160 throw std::runtime_error (" Failed to serialize EvalMultKey" );
160161 }
161- std::string str = oss.str ();
162- return py::bytes (str);
163- }
164162
163+ const std::string& str = strbuf->str ();
164+ return py::bytes (str.data (), str.size ());
165+ }
165166
166167template <typename ST>
167168std::string SerializeEvalAutomorphismKeyToStringWrapper (const ST& sertype, const std::string& id) {
@@ -173,15 +174,18 @@ std::string SerializeEvalAutomorphismKeyToStringWrapper(const ST& sertype, const
173174 return oss.str ();
174175}
175176
176-
177177template <typename ST>
178178py::bytes SerializeEvalAutomorphismKeyToBytesWrapper (const ST& sertype, const std::string& id) {
179- std::ostringstream oss (std::ios::binary);
180- bool res = CryptoContextImpl<DCRTPoly>::SerializeEvalAutomorphismKey (oss, sertype, id);
181- if (!res) {
179+ // let strbuf be dynamically allocated as we may be dealing with large keys
180+ auto strbuf = std::make_unique<std::stringbuf>(std::ios::out | std::ios::binary);
181+ std::ostream oss (strbuf.get ());
182+
183+ if (!CryptoContextImpl<DCRTPoly>::SerializeEvalAutomorphismKey (oss, sertype, id)) {
182184 throw std::runtime_error (" Failed to serialize EvalAutomorphismKey" );
183185 }
184- return oss.str ();
186+
187+ const std::string& str = strbuf->str ();
188+ return py::bytes (str.data (), str.size ());
185189}
186190
187191template <typename ST>
@@ -194,11 +198,11 @@ void DeserializeEvalMultKeyFromStringWrapper(const std::string& data, const ST&
194198}
195199
196200template <typename ST>
197- void DeserializeEvalMultKeyFromBytesWrapper (const std::string& data , const ST& sertype) {
198- std::string str (data) ;
201+ void DeserializeEvalMultKeyFromBytesWrapper (const py::bytes& bytes , const ST& sertype) {
202+ std::string str{ static_cast <std::string>(bytes)} ;
199203 std::istringstream iss (str, std::ios::binary);
200- bool res = CryptoContextImpl<DCRTPoly>::DeserializeEvalMultKey<ST>(iss, sertype);
201- if (!res ) {
204+
205+ if (!CryptoContextImpl<DCRTPoly>::DeserializeEvalMultKey<ST>(iss, sertype) ) {
202206 throw std::runtime_error (" Failed to deserialize EvalMultKey" );
203207 }
204208}
@@ -214,11 +218,11 @@ void DeserializeEvalAutomorphismKeyFromStringWrapper(const std::string& data, co
214218}
215219
216220template <typename ST>
217- void DeserializeEvalAutomorphismKeyFromBytesWrapper (const std::string& data , const ST& sertype) {
218- std::string str (data) ;
221+ void DeserializeEvalAutomorphismKeyFromBytesWrapper (const py::bytes& bytes , const ST& sertype) {
222+ std::string str{ static_cast <std::string>(bytes)} ;
219223 std::istringstream iss (str, std::ios::binary);
220- bool res = CryptoContextImpl<DCRTPoly>::DeserializeEvalAutomorphismKey<ST>(iss, sertype);
221- if (!res ) {
224+
225+ if (!CryptoContextImpl<DCRTPoly>::DeserializeEvalAutomorphismKey<ST>(iss, sertype) ) {
222226 throw std::runtime_error (" Failed to deserialize EvalAutomorphismKey" );
223227 }
224228}
@@ -272,17 +276,19 @@ void bind_serialization(pybind11::module &m) {
272276 m.def (" DeserializeEvalKeyString" , &DeserializeFromStringWrapper<EvalKey<DCRTPoly>, SerType::SERJSON>,
273277 py::arg (" str" ), py::arg (" sertype" ));
274278 m.def (" Serialize" , &SerializeToBytesWrapper<std::shared_ptr<std::map<uint32_t , EvalKey<DCRTPoly>>>, SerType::SERJSON>,
275- py::arg (" obj" ), py::arg (" sertype" ));
279+ py::arg (" obj" ), py::arg (" sertype" ));
276280 m.def (" DeserializeEvalKeyMapString" , &DeserializeFromBytesWrapper<std::shared_ptr<std::map<uint32_t , EvalKey<DCRTPoly>>>, SerType::SERJSON>,
277- py::arg (" str" ), py::arg (" sertype" ));
281+ py::arg (" str" ), py::arg (" sertype" ));
278282
279283 m.def (" SerializeEvalMultKeyString" , &SerializeEvalMultKeyToStringWrapper<SerType::SERJSON>,
280284 py::arg (" sertype" ), py::arg (" id" ) = " " );
281- m.def (" DeserializeEvalMultKeyString" , &DeserializeEvalMultKeyFromStringWrapper<SerType::SERJSON>,
285+ m.def (" DeserializeEvalMultKeyString" ,
286+ static_cast <void (*)(const std::string&, const SerType::SERJSON&)>(&DeserializeEvalMultKeyFromStringWrapper<SerType::SERJSON>),
282287 py::arg (" data" ), py::arg (" sertype" ));
283288 m.def (" SerializeEvalAutomorphismKeyString" , &SerializeEvalAutomorphismKeyToStringWrapper<SerType::SERJSON>,
284289 py::arg (" sertype" ), py::arg (" id" ) = " " );
285- m.def (" DeserializeEvalAutomorphismKeyString" , &DeserializeEvalAutomorphismKeyFromStringWrapper<SerType::SERJSON>,
290+ m.def (" DeserializeEvalAutomorphismKeyString" ,
291+ static_cast <void (*)(const std::string&, const SerType::SERJSON&)>(&DeserializeEvalAutomorphismKeyFromStringWrapper<SerType::SERJSON>),
286292 py::arg (" data" ), py::arg (" sertype" ));
287293
288294 // Binary Serialization
@@ -333,16 +339,18 @@ void bind_serialization(pybind11::module &m) {
333339 m.def (" DeserializeEvalKeyString" , &DeserializeFromBytesWrapper<EvalKey<DCRTPoly>, SerType::SERBINARY>,
334340 py::arg (" str" ), py::arg (" sertype" ));
335341 m.def (" Serialize" , &SerializeToBytesWrapper<std::shared_ptr<std::map<uint32_t , EvalKey<DCRTPoly>>>, SerType::SERBINARY>,
336- py::arg (" obj" ), py::arg (" sertype" ));
342+ py::arg (" obj" ), py::arg (" sertype" ));
337343 m.def (" DeserializeEvalKeyMapString" , &DeserializeFromBytesWrapper<std::shared_ptr<std::map<uint32_t , EvalKey<DCRTPoly>>>, SerType::SERBINARY>,
338- py::arg (" str" ), py::arg (" sertype" ));
344+ py::arg (" str" ), py::arg (" sertype" ));
339345
340346 m.def (" SerializeEvalMultKeyString" , &SerializeEvalMultKeyToBytesWrapper<SerType::SERBINARY>,
341347 py::arg (" sertype" ), py::arg (" id" ) = " " );
342- m.def (" DeserializeEvalMultKeyString" , &DeserializeEvalMultKeyFromBytesWrapper<SerType::SERBINARY>,
343- py::arg (" data" ), py::arg (" sertype" ));
348+ m.def (" DeserializeEvalMultKeyString" ,
349+ static_cast <void (*)(const py::bytes&, const SerType::SERBINARY&)>(&DeserializeEvalMultKeyFromBytesWrapper<SerType::SERBINARY>),
350+ py::arg (" bytes" ), py::arg (" sertype" ));
344351 m.def (" SerializeEvalAutomorphismKeyString" , &SerializeEvalAutomorphismKeyToBytesWrapper<SerType::SERBINARY>,
345352 py::arg (" sertype" ), py::arg (" id" ) = " " );
346- m.def (" DeserializeEvalAutomorphismKeyString" , &DeserializeEvalAutomorphismKeyFromBytesWrapper<SerType::SERBINARY>,
347- py::arg (" data" ), py::arg (" sertype" ));
353+ m.def (" DeserializeEvalAutomorphismKeyString" ,
354+ static_cast <void (*)(const py::bytes&, const SerType::SERBINARY&)>(&DeserializeEvalAutomorphismKeyFromBytesWrapper<SerType::SERBINARY>),
355+ py::arg (" bytes" ), py::arg (" sertype" ));
348356}
0 commit comments