Skip to content

Commit 6ca82a9

Browse files
dsuponitskiydsuponitskiy-dualityYuriy Polyakov
authored
Additional serialization methods (#203)
* Made Serialize/Deserialize functions available for EvalKeyMap * Additional pair of overloaded Serialize/Deserialize functions * Revert "Additional pair of overloaded Serialize/Deserialize functions" This reverts commit e1bf413. * compilation works * updated the example * Removed commented code * Added SerializeToFile() and organized header files --------- Co-authored-by: Dmitriy Suponitskiy <[email protected]> Co-authored-by: Yuriy Polyakov <[email protected]>
1 parent 9994095 commit 6ca82a9

File tree

7 files changed

+166
-118
lines changed

7 files changed

+166
-118
lines changed

src/include/bindings.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
//==================================================================================
12
// BSD 2-Clause License
2-
3-
// Copyright (c) 2023, OpenFHE
4-
3+
//
4+
// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors
5+
//
56
// All rights reserved.
6-
7+
//
8+
// Author TPOC: [email protected]
9+
//
710
// Redistribution and use in source and binary forms, with or without
811
// modification, are permitted provided that the following conditions are met:
9-
12+
//
1013
// 1. Redistributions of source code must retain the above copyright notice, this
1114
// list of conditions and the following disclaimer.
12-
15+
//
1316
// 2. Redistributions in binary form must reproduce the above copyright notice,
1417
// this list of conditions and the following disclaimer in the documentation
1518
// and/or other materials provided with the distribution.
16-
19+
//
1720
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1821
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1922
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,9 +27,9 @@
2427
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2528
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2629
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27-
28-
#ifndef OPENFHE_BINDINGS_H
29-
#define OPENFHE_BINDINGS_H
30+
//==================================================================================
31+
#ifndef __BINDINGS_H__
32+
#define __BINDINGS_H__
3033

3134
#include <pybind11/pybind11.h>
3235

@@ -39,4 +42,5 @@ void bind_ciphertext(pybind11::module &m);
3942
void bind_serialization(pybind11::module &m);
4043
void bind_schemes(pybind11::module &m);
4144
void bind_sch_swch_params(pybind11::module &m);
42-
#endif // OPENFHE_BINDINGS_H
45+
46+
#endif // __BINDINGS_H__

src/include/pke/cryptocontext_wrapper.h

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
//==================================================================================
12
// BSD 2-Clause License
2-
3-
// Copyright (c) 2023, OpenFHE
4-
3+
//
4+
// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors
5+
//
56
// All rights reserved.
6-
7+
//
8+
// Author TPOC: [email protected]
9+
//
710
// Redistribution and use in source and binary forms, with or without
811
// modification, are permitted provided that the following conditions are met:
9-
12+
//
1013
// 1. Redistributions of source code must retain the above copyright notice, this
1114
// list of conditions and the following disclaimer.
12-
15+
//
1316
// 2. Redistributions in binary form must reproduce the above copyright notice,
1417
// this list of conditions and the following disclaimer in the documentation
1518
// and/or other materials provided with the distribution.
16-
19+
//
1720
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1821
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1922
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,23 +27,14 @@
2427
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2528
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2629
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//==================================================================================
31+
#ifndef __CRYPTOCONTEXT_WRAPPER_H__
32+
#define __CRYPTOCONTEXT_WRAPPER_H__
2733

28-
#ifndef OPENFHE_CRYPTOCONTEXT_BINDINGS_H
29-
#define OPENFHE_CRYPTOCONTEXT_BINDINGS_H
30-
31-
#include "bindings.h"
3234
#include "openfhe.h"
3335

34-
#include <pybind11/pybind11.h>
35-
#include <pybind11/stl.h>
36-
#include <vector>
37-
#include <algorithm>
38-
#include <complex>
39-
40-
41-
namespace py = pybind11;
4236
using namespace lbcrypto;
43-
using ParmType = typename DCRTPoly::Params;
37+
4438

4539
Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly> &self,
4640
ConstCiphertext<DCRTPoly> ciphertext);
@@ -69,4 +63,4 @@ const usint GetDigitSizeWrapper(CryptoContext<DCRTPoly>& self);
6963

7064
void ClearEvalMultKeysWrapper();
7165

72-
#endif // OPENFHE_CRYPTOCONTEXT_BINDINGS_H
66+
#endif // __CRYPTOCONTEXT_WRAPPER_H__

src/include/pke/serialization.h

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
//==================================================================================
12
// BSD 2-Clause License
2-
3-
// Copyright (c) 2023, OpenFHE
4-
3+
//
4+
// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors
5+
//
56
// All rights reserved.
6-
7+
//
8+
// Author TPOC: [email protected]
9+
//
710
// Redistribution and use in source and binary forms, with or without
811
// modification, are permitted provided that the following conditions are met:
9-
12+
//
1013
// 1. Redistributions of source code must retain the above copyright notice, this
1114
// list of conditions and the following disclaimer.
12-
15+
//
1316
// 2. Redistributions in binary form must reproduce the above copyright notice,
1417
// this list of conditions and the following disclaimer in the documentation
1518
// and/or other materials provided with the distribution.
16-
19+
//
1720
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1821
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1922
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -24,16 +27,12 @@
2427
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2528
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2629
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27-
28-
#ifndef OPENFHE_SERIALIZATION_BINDINGS_H
29-
#define OPENFHE_SERIALIZATION_BINDINGS_H
30+
//==================================================================================
31+
#ifndef __SERIALIZATION_H__
32+
#define __SERIALIZATION_H__
3033

3134
#include <pybind11/pybind11.h>
32-
#include "openfhe.h"
33-
#include "bindings.h"
3435

35-
using namespace lbcrypto;
36-
namespace py = pybind11;
3736

3837
template <typename ST>
3938
bool SerializeEvalMultKeyWrapper(const std::string& filename, const ST& sertype, std::string id);
@@ -48,25 +47,25 @@ template <typename T, typename ST>
4847
std::string SerializeToStringWrapper(const T& obj, const ST& sertype);
4948

5049
template <typename T, typename ST>
51-
py::bytes SerializeToBytesWrapper(const T& obj, const ST& sertype);
50+
pybind11::bytes SerializeToBytesWrapper(const T& obj, const ST& sertype);
5251

5352
template <typename T, typename ST>
5453
T DeserializeFromStringWrapper(const std::string& str, const ST& sertype);
5554

5655
template <typename T, typename ST>
57-
T DeserializeFromBytesWrapper(const py::bytes& bytes, const ST& sertype);
56+
T DeserializeFromBytesWrapper(const pybind11::bytes& bytes, const ST& sertype);
5857

5958
template <typename ST>
6059
std::string SerializeEvalMultKeyToStringWrapper(const ST& sertype, const std::string& id);
6160

6261
template <typename ST>
63-
py::bytes SerializeEvalMultKeyToBytesWrapper(const ST& sertype, const std::string& id);
62+
pybind11::bytes SerializeEvalMultKeyToBytesWrapper(const ST& sertype, const std::string& id);
6463

6564
template <typename ST>
6665
std::string SerializeEvalAutomorphismKeyToStringWrapper(const ST& sertype, const std::string& id);
6766

6867
template <typename ST>
69-
py::bytes SerializeEvalAutomorphismKeyToBytesWrapper(const ST& sertype, const std::string& id);
68+
pybind11::bytes SerializeEvalAutomorphismKeyToBytesWrapper(const ST& sertype, const std::string& id);
7069

7170
template <typename ST>
7271
void DeserializeEvalMultKeyFromStringWrapper(const std::string& data, const ST& sertype);
@@ -80,4 +79,4 @@ void DeserializeEvalAutomorphismKeyFromStringWrapper(const std::string& data, co
8079
template <typename ST>
8180
void DeserializeEvalAutomorphismKeyFromBytesWrapper(const std::string& data, const ST& sertype);
8281

83-
#endif // OPENFHE_SERIALIZATION_BINDINGS_H
82+
#endif // __SERIALIZATION_H__

src/lib/bindings.cpp

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,57 @@
1-
#include <pybind11/pybind11.h>
1+
//==================================================================================
2+
// BSD 2-Clause License
3+
//
4+
// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors
5+
//
6+
// All rights reserved.
7+
//
8+
// Author TPOC: [email protected]
9+
//
10+
// Redistribution and use in source and binary forms, with or without
11+
// modification, are permitted provided that the following conditions are met:
12+
//
13+
// 1. Redistributions of source code must retain the above copyright notice, this
14+
// list of conditions and the following disclaimer.
15+
//
16+
// 2. Redistributions in binary form must reproduce the above copyright notice,
17+
// this list of conditions and the following disclaimer in the documentation
18+
// and/or other materials provided with the distribution.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//==================================================================================
31+
#include "bindings.h"
32+
233
#include <pybind11/stl.h>
334
#include <pybind11/stl_bind.h>
435
#include <pybind11/complex.h>
536
#include <pybind11/functional.h>
637
#include <pybind11/operators.h>
738
#include <pybind11/iostream.h>
8-
#include <iostream>
9-
#include <map>
39+
1040
#include "openfhe.h"
41+
1142
#include "key/key-ser.h"
12-
#include "bindings.h"
13-
#include "cryptocontext_wrapper.h"
1443
#include "binfhe_bindings.h"
44+
45+
#include "cryptocontext_wrapper.h"
1546
#include "cryptocontext_docs.h"
1647
#include "cryptoparameters_docs.h"
1748
#include "plaintext_docs.h"
1849
#include "ciphertext_docs.h"
19-
#include "serialization.h"
2050

2151
using namespace lbcrypto;
2252
namespace py = pybind11;
53+
54+
// disable the PYBIND11 template-based conversion for this type
2355
PYBIND11_MAKE_OPAQUE(std::map<usint, EvalKey<DCRTPoly>>);
2456

2557
template <typename T>
@@ -100,6 +132,12 @@ void bind_parameters(py::module &m,const std::string name)
100132

101133
void bind_crypto_context(py::module &m)
102134
{
135+
//Parameters Type
136+
/*TODO (Oliveira): If we expose Poly's and ParmType, this block will go somewhere else */
137+
using ParmType = typename DCRTPoly::Params;
138+
using ParmTypePtr = std::shared_ptr<ParmType>;
139+
py::class_<ParmType, ParmTypePtr>(m, "ParmType");
140+
103141
py::class_<CryptoContextImpl<DCRTPoly>, std::shared_ptr<CryptoContextImpl<DCRTPoly>>>(m, "CryptoContext")
104142
.def(py::init<>())
105143
.def("GetKeyGenLevel", &CryptoContextImpl<DCRTPoly>::GetKeyGenLevel, cc_GetKeyGenLevel_docs)
@@ -1028,15 +1066,8 @@ void bind_enums_and_constants(py::module &m)
10281066
m.attr("HEStd_256_classic") = py::cast(SecurityLevel::HEStd_256_classic);
10291067
m.attr("HEStd_NotSet") = py::cast(SecurityLevel::HEStd_NotSet);
10301068

1031-
//Parameters Type
1032-
/*TODO (Oliveira): If we expose Poly's and ParmType, this block will go somewhere else */
1033-
using ParmType = typename DCRTPoly::Params;
1034-
py::class_<ParmType, std::shared_ptr<ParmType>>(m, "ParmType");
1035-
10361069
//NATIVEINT function
10371070
m.def("get_native_int", &get_native_int);
1038-
1039-
10401071
}
10411072

10421073
void bind_keys(py::module &m)

src/lib/binfhe_bindings.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1+
//==================================================================================
12
// BSD 2-Clause License
2-
3-
// Copyright (c) 2023, OpenFHE
4-
3+
//
4+
// Copyright (c) 2014-2025, NJIT, Duality Technologies Inc. and other contributors
5+
//
56
// All rights reserved.
6-
7+
//
8+
// Author TPOC: [email protected]
9+
//
710
// Redistribution and use in source and binary forms, with or without
811
// modification, are permitted provided that the following conditions are met:
9-
10-
// 1. Redistributions of source code must retain the above copyright notice,
11-
// this
12+
//
13+
// 1. Redistributions of source code must retain the above copyright notice, this
1214
// list of conditions and the following disclaimer.
13-
15+
//
1416
// 2. Redistributions in binary form must reproduce the above copyright notice,
1517
// this list of conditions and the following disclaimer in the documentation
1618
// and/or other materials provided with the distribution.
17-
19+
//
1820
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1921
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20-
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21-
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22-
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23-
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24-
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25-
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26-
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27-
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28-
// POSSIBILITY OF SUCH DAMAGE.
29-
22+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
//==================================================================================
3031
#include "binfhe_bindings.h"
32+
33+
#include <pybind11/operators.h>
34+
35+
#include "openfhe.h"
3136
#include "binfhecontext.h"
3237
#include "binfhecontext_docs.h"
3338
#include "binfhecontext_wrapper.h"
34-
#include "openfhe.h"
39+
3540
#include "cereal/archives/binary.hpp"
36-
#include "cereal/archives/portable_binary.hpp"
37-
#include "core/utils/serial.h"
38-
#include <iostream>
39-
#include <pybind11/operators.h>
40-
#include <pybind11/pybind11.h>
41+
// #include "cereal/archives/portable_binary.hpp"
42+
// #include "core/utils/serial.h"
4143

4244
using namespace lbcrypto;
4345
namespace py = pybind11;

0 commit comments

Comments
 (0)