Skip to content

Commit 890be7b

Browse files
committed
dump and load from bytes
1 parent 9fc2af6 commit 890be7b

File tree

1 file changed

+87
-7
lines changed

1 file changed

+87
-7
lines changed

src/wrapper.cpp

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ PYBIND11_MODULE(seal, m)
4949
parms.load(in);
5050
in.close();
5151
})
52+
.def("dumpb", [](const EncryptionParameters &parms){
53+
std::stringstream out(std::ios::binary | std::ios::out);
54+
parms.save(out);
55+
return py::bytes(out.str());
56+
})
57+
.def("loadb", [](EncryptionParameters &parms, const py::bytes &bytes){
58+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
59+
parms.load(in);
60+
})
61+
// remove pickle support?
5262
.def(py::pickle(
5363
[](const EncryptionParameters &parms){
5464
std::stringstream out_stream(std::ios::binary | std::ios::out);
@@ -183,7 +193,17 @@ PYBIND11_MODULE(seal, m)
183193
})
184194
.def("save_size", [](const Plaintext &plain){
185195
return plain.save_size();
186-
});
196+
})
197+
.def("dumpb", [](const Plaintext &plain){
198+
std::stringstream out(std::ios::binary | std::ios::out);
199+
plain.save(out);
200+
return py::bytes(out.str());
201+
})
202+
.def("loadb", [](Plaintext &plain, const SEALContext &context, const py::bytes &bytes){
203+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
204+
plain.load(context, in);
205+
})
206+
;
187207

188208
// ciphertext.h
189209
py::class_<Ciphertext>(m, "Ciphertext")
@@ -215,7 +235,17 @@ PYBIND11_MODULE(seal, m)
215235
})
216236
.def("save_size", [](const Ciphertext &cipher){
217237
return cipher.save_size();
218-
});
238+
})
239+
.def("dumpb", [](const Ciphertext &cipher){
240+
std::stringstream out(std::ios::binary | std::ios::out);
241+
cipher.save(out);
242+
return py::bytes(out.str());
243+
})
244+
.def("loadb", [](Ciphertext &cipher, const SEALContext &context, const py::bytes &bytes){
245+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
246+
cipher.load(context, in);
247+
})
248+
;
219249

220250
// secretkey.h
221251
py::class_<SecretKey>(m, "SecretKey")
@@ -231,7 +261,17 @@ PYBIND11_MODULE(seal, m)
231261
std::ifstream in(path, std::ifstream::binary);
232262
sk.load(context, in);
233263
in.close();
234-
});
264+
})
265+
.def("dumpb", [](const SecretKey &sk){
266+
std::stringstream out(std::ios::binary | std::ios::out);
267+
sk.save(out);
268+
return py::bytes(out.str());
269+
})
270+
.def("loadb", [](SecretKey &sk, const SEALContext &context, const py::bytes &bytes){
271+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
272+
sk.load(context, in);
273+
})
274+
;
235275

236276
// publickey.h
237277
py::class_<PublicKey>(m, "PublicKey")
@@ -247,7 +287,17 @@ PYBIND11_MODULE(seal, m)
247287
std::ifstream in(path, std::ifstream::binary);
248288
pk.load(context, in);
249289
in.close();
250-
});
290+
})
291+
.def("dumpb", [](const PublicKey &pk){
292+
std::stringstream out(std::ios::binary | std::ios::out);
293+
pk.save(out);
294+
return py::bytes(out.str());
295+
})
296+
.def("loadb", [](PublicKey &pk, const SEALContext &context, const py::bytes &bytes){
297+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
298+
pk.load(context, in);
299+
})
300+
;
251301

252302
// kswitchkeys.h
253303
py::class_<KSwitchKeys>(m, "KSwitchKeys")
@@ -264,7 +314,17 @@ PYBIND11_MODULE(seal, m)
264314
std::ifstream in(path, std::ifstream::binary);
265315
ksk.load(context, in);
266316
in.close();
267-
});
317+
})
318+
.def("dumpb", [](const KSwitchKeys &ksk){
319+
std::stringstream out(std::ios::binary | std::ios::out);
320+
ksk.save(out);
321+
return py::bytes(out.str());
322+
})
323+
.def("loadb", [](KSwitchKeys &ksk, const SEALContext &context, const py::bytes &bytes){
324+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
325+
ksk.load(context, in);
326+
})
327+
;
268328

269329
// relinKeys.h
270330
py::class_<RelinKeys, KSwitchKeys>(m, "RelinKeys")
@@ -283,7 +343,17 @@ PYBIND11_MODULE(seal, m)
283343
std::ifstream in(path, std::ifstream::binary);
284344
rk.load(context, in);
285345
in.close();
286-
});
346+
})
347+
.def("dumpb", [](const RelinKeys &rk){
348+
std::stringstream out(std::ios::binary | std::ios::out);
349+
rk.save(out);
350+
return py::bytes(out.str());
351+
})
352+
.def("loadb", [](RelinKeys &rk, const SEALContext &context, const py::bytes &bytes){
353+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
354+
rk.load(context, in);
355+
})
356+
;
287357

288358
// galoisKeys.h
289359
py::class_<GaloisKeys, KSwitchKeys>(m, "GaloisKeys")
@@ -302,7 +372,17 @@ PYBIND11_MODULE(seal, m)
302372
std::ifstream in(path, std::ifstream::binary);
303373
gk.load(context, in);
304374
in.close();
305-
});
375+
})
376+
.def("dumpb", [](const GaloisKeys &gk){
377+
std::stringstream out(std::ios::binary | std::ios::out);
378+
gk.save(out);
379+
return py::bytes(out.str());
380+
})
381+
.def("loadb", [](GaloisKeys &gk, const SEALContext &context, const py::bytes &bytes){
382+
std::stringstream in(bytes, std::ios::binary | std::ios::in);
383+
gk.load(context, in);
384+
})
385+
;
306386

307387
// keygenerator.h
308388
py::class_<KeyGenerator>(m, "KeyGenerator")

0 commit comments

Comments
 (0)