5
5
#include < pybind11/stl.h>
6
6
#include " seal/seal.h"
7
7
#include < fstream>
8
+ #include " base64.h"
8
9
9
10
using namespace std ;
10
11
using namespace seal ;
@@ -18,6 +19,44 @@ PYBIND11_MAKE_OPAQUE(std::vector<std::int64_t>);
18
19
19
20
using parms_id_type = std::array<std::uint64_t , 4 >;
20
21
22
+ template <class T >
23
+ py::tuple serialize (T &c)
24
+ {
25
+ std::stringstream output (std::ios::binary | std::ios::out);
26
+ c.save (output);
27
+ std::string cipherstr = output.str ();
28
+ std::string base64_encoded_cipher = base64_encode (reinterpret_cast <const unsigned char *>(cipherstr.c_str ()), cipherstr.length ());
29
+ return py::make_tuple (base64_encoded_cipher);
30
+ }
31
+
32
+ template <class T >
33
+ T deserialize (py::tuple t)
34
+ {
35
+ if (t.size () != 1 )
36
+ throw std::runtime_error (" (Pickle) Invalid input tuple!" );
37
+ T c = T ();
38
+ std::string cipherstr_encoded = t[0 ].cast <std::string>();
39
+ std::string cipherstr_decoded = base64_decode (cipherstr_encoded);
40
+ std::stringstream input (std::ios::binary | std::ios::in);
41
+ input.str (cipherstr_decoded);
42
+ c.load (input);
43
+ return c;
44
+ }
45
+
46
+ template <class T >
47
+ T deserialize_context (py::tuple t)
48
+ {
49
+ if (t.size () != 2 )
50
+ throw std::runtime_error (" (Pickle) Invalid input tuple!" );
51
+ T c = T ();
52
+ std::string cipherstr_encoded = t[1 ].cast <std::string>();
53
+ std::string cipherstr_decoded = base64_decode (cipherstr_encoded);
54
+ std::stringstream input (std::ios::binary | std::ios::in);
55
+ input.str (cipherstr_decoded);
56
+ c.load (t[0 ].cast <std::shared_ptr<SEALContext>>(), input);
57
+ return c;
58
+ }
59
+
21
60
PYBIND11_MODULE (seal, m)
22
61
{
23
62
m.doc () = " Microsoft SEAL (3.4.5) For Python. From https://github.com/Huelse/SEAL-Python" ;
@@ -61,7 +100,8 @@ PYBIND11_MODULE(seal, m)
61
100
std::ifstream in (path, std::ifstream::binary);
62
101
p.load (in);
63
102
in.close ();
64
- });
103
+ })
104
+ .def (py::pickle (&serialize<EncryptionParameters>, &deserialize<EncryptionParameters>));
65
105
66
106
// context.h
67
107
py::class_<EncryptionParameterQualifiers, std::unique_ptr<EncryptionParameterQualifiers, py::nodelete>>(m, " EncryptionParameterQualifiers" )
@@ -147,7 +187,8 @@ PYBIND11_MODULE(seal, m)
147
187
std::ifstream in (path, std::ifstream::binary);
148
188
c.load (context, in);
149
189
in.close ();
150
- });
190
+ })
191
+ .def (py::pickle (&serialize<Plaintext>, &deserialize_context<Plaintext>));
151
192
152
193
// ciphertext.h
153
194
py::class_<Ciphertext>(m, " Ciphertext" )
@@ -177,7 +218,8 @@ PYBIND11_MODULE(seal, m)
177
218
std::ifstream in (path, std::ifstream::binary);
178
219
c.load (context, in);
179
220
in.close ();
180
- });
221
+ })
222
+ .def (py::pickle (&serialize<Ciphertext>, &deserialize_context<Ciphertext>));
181
223
182
224
// secretkey.h
183
225
py::class_<SecretKey>(m, " SecretKey" )
@@ -192,7 +234,8 @@ PYBIND11_MODULE(seal, m)
192
234
std::ifstream in (path, std::ifstream::binary);
193
235
c.load (context, in);
194
236
in.close ();
195
- });
237
+ })
238
+ .def (py::pickle (&serialize<SecretKey>, &deserialize_context<SecretKey>));
196
239
197
240
// publickey.h
198
241
py::class_<PublicKey>(m, " PublicKey" )
@@ -207,7 +250,8 @@ PYBIND11_MODULE(seal, m)
207
250
std::ifstream in (path, std::ifstream::binary);
208
251
c.load (context, in);
209
252
in.close ();
210
- });
253
+ })
254
+ .def (py::pickle (&serialize<PublicKey>, &deserialize_context<PublicKey>));
211
255
212
256
// kswitchkeys.h
213
257
py::class_<KSwitchKeys>(m, " KSwitchKeys" )
@@ -222,7 +266,8 @@ PYBIND11_MODULE(seal, m)
222
266
std::ifstream in (path, std::ifstream::binary);
223
267
c.load (context, in);
224
268
in.close ();
225
- });
269
+ })
270
+ .def (py::pickle (&serialize<KSwitchKeys>, &deserialize_context<KSwitchKeys>));
226
271
227
272
// relinKeys.h
228
273
py::class_<RelinKeys, KSwitchKeys>(m, " RelinKeys" )
@@ -237,7 +282,8 @@ PYBIND11_MODULE(seal, m)
237
282
std::ifstream in (path, std::ifstream::binary);
238
283
c.load (context, in);
239
284
in.close ();
240
- });
285
+ })
286
+ .def (py::pickle (&serialize<RelinKeys>, &deserialize_context<RelinKeys>));
241
287
242
288
// galoisKeys.h
243
289
py::class_<GaloisKeys, KSwitchKeys>(m, " GaloisKeys" )
@@ -252,7 +298,8 @@ PYBIND11_MODULE(seal, m)
252
298
std::ifstream in (path, std::ifstream::binary);
253
299
c.load (context, in);
254
300
in.close ();
255
- });
301
+ })
302
+ .def (py::pickle (&serialize<GaloisKeys>, &deserialize_context<GaloisKeys>));
256
303
257
304
// keygenerator.h
258
305
py::class_<KeyGenerator>(m, " KeyGenerator" )
0 commit comments