Skip to content

Commit 608717f

Browse files
204 fix py byte wrappers (#205)
* Improved the code for the *BytesWrapper functions, fixed input parameters for some functions, added Compress to binding * Cleanup and formatting --------- Co-authored-by: Dmitriy Suponitskiy <[email protected]>
1 parent 6ca82a9 commit 608717f

File tree

9 files changed

+86
-74
lines changed

9 files changed

+86
-74
lines changed

src/include/binfhe/binfhecontext_wrapper.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28-
#ifndef BINFHE_CRYPTOCONTEXT_BINDINGS_H
29-
#define BINFHE_CRYPTOCONTEXT_BINDINGS_H
28+
#ifndef __BINFHECONTEXT_WRAPPER_H__
29+
#define __BINFHECONTEXT_WRAPPER_H__
3030

3131
#include <pybind11/pybind11.h>
3232
#include <pybind11/stl.h>
@@ -66,4 +66,5 @@ NativeInteger StaticFunction(NativeInteger m, NativeInteger p);
6666
// extern py::function static_f;
6767

6868
LWECiphertext EvalFuncWrapper(BinFHEContext &self, ConstLWECiphertext &ct, const std::vector<uint64_t> &LUT);
69-
#endif // BINFHE_CRYPTOCONTEXT_BINDINGS_H
69+
70+
#endif // __BINFHECONTEXT_WRAPPER_H__

src/include/binfhe_bindings.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28-
#ifndef BINFHE_BINDINGS_H
29-
#define BINFHE_BINDINGS_H
28+
#ifndef __BINFHE_BINDINGS_H__
29+
#define __BINFHE_BINDINGS_H__
3030

3131
#include <pybind11/pybind11.h>
3232

3333
void bind_binfhe_enums(pybind11::module &m);
3434
void bind_binfhe_context(pybind11::module &m);
3535
void bind_binfhe_keys(pybind11::module &m);
3636
void bind_binfhe_ciphertext(pybind11::module &m);
37-
#endif // BINFHE_BINDINGS_H
37+
#endif // __BINFHE_BINDINGS_H__

src/include/docstrings/binfhecontext_docs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28-
#ifndef BINFHECONTEXT_DOCSTRINGS_H
29-
#define BINFHECONTEXT_DOCSTRINGS_H
28+
#ifndef __BINFHECONTEXT_DOCS_H
29+
#define __BINFHECONTEXT_DOCS_H
3030

3131
// GenerateBinFHEContext
3232
const char* binfhe_GenerateBinFHEContext_parset_docs = R"pbdoc(
@@ -199,4 +199,4 @@ const char* binfhe_SerializedObjectName_docs = R"pbdoc(
199199
:return: object name
200200
:rtype: std::string
201201
)pbdoc";
202-
#endif // BINFHECONTEXT_DOCSTRINGS_H
202+
#endif // __BINFHECONTEXT_DOCS_H

src/include/docstrings/ciphertext_docs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28-
#ifndef CIPHERTEXT_DOCSTRINGS_H
29-
#define CIPHERTEXT_DOCSTRINGS_H
28+
#ifndef __CIPHERTEXT_DOCS_H__
29+
#define __CIPHERTEXT_DOCS_H__
3030

3131
// GetLevel
3232
const char* ctx_GetLevel_docs = R"pbdoc(
@@ -58,4 +58,4 @@ const char* cc_RemoveElement_docs = R"pbdoc(
5858
:param index: The index of the element to remove.
5959
:type index: int
6060
)pbdoc";
61-
#endif // CIPHERTEXT_DOCSTRINGS_H
61+
#endif // __CIPHERTEXT_DOCS_H__

src/include/docstrings/cryptocontext_docs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28-
#ifndef CRYPTOCONTEXT_DOCSTRINGS_H
29-
#define CRYPTOCONTEXT_DOCSTRINGS_H
28+
#ifndef __CRYPTOCONTEXT_DOCS_H__
29+
#define __CRYPTOCONTEXT_DOCS_H__
3030

3131
#include "pybind11/pybind11.h"
3232
#include "pybind11/attr.h"
@@ -1385,4 +1385,4 @@ const char* cc_DeserializeEvalMultKey_docs = R"pbdoc(
13851385
)pbdoc";
13861386

13871387

1388-
#endif //CRYPTOCONTEXT_DOCSTRINGS_H
1388+
#endif // __CRYPTOCONTEXT_DOCS_H__

src/include/docstrings/cryptoparameters_docs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28-
#ifndef CRYPTOPARAMS_DOCSTRINGS_H
29-
#define CRYPTOPARAMS_DOCSTRINGS_H
28+
#ifndef __CRYPTOPARAMETERS_DOCS_H__
29+
#define __CRYPTOPARAMETERS_DOCS_H__
3030

3131
const char* ccparams_doc = R"doc(
3232
Crypto parameters for the BFV, BGV and CKKS scheme.
@@ -72,4 +72,4 @@ const char* cc_GetScalingFactorReal_docs = R"pbdoc(
7272
)pbdoc";
7373

7474

75-
#endif // CRYPTOPARAMS_DOCSTRINGS_H
75+
#endif // __CRYPTOPARAMETERS_DOCS_H__

src/include/docstrings/plaintext_docs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2626
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727

28-
#ifndef PLAINTEXT_DOCSTRINGS_H
29-
#define PLAINTEXT_DOCSTRINGS_H
28+
#ifndef __PLAINTEXT_DOCS_H__
29+
#define __PLAINTEXT_DOCS_H__
3030

3131
// GetScalingFactor
3232
const char* ptx_GetScalingFactor_docs = R"doc(
@@ -133,4 +133,4 @@ const char* ptx_GetRealPackedValue_docs = R"pbdoc(
133133
)pbdoc";
134134

135135

136-
#endif // PLAINTEXT_DOCSTRINGS_H
136+
#endif // __PLAINTEXT_DOCS_H__

src/lib/bindings.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,14 +759,17 @@ void bind_crypto_context(py::module &m)
759759
std::vector<std::shared_ptr<lbcrypto::CiphertextImpl<DCRTPoly> > >&
760760
) const>(
761761
&CryptoContextImpl<DCRTPoly>::EvalLinearWSumMutable),
762-
py::arg("ciphertext"),
763-
py::arg("coefficients"))
762+
py::arg("ciphertext"),
763+
py::arg("coefficients"))
764764
.def("EvalLinearWSum",
765765
static_cast<lbcrypto::Ciphertext<DCRTPoly> (lbcrypto::CryptoContextImpl<DCRTPoly>::*)(
766766
std::vector<std::shared_ptr<const lbcrypto::CiphertextImpl<DCRTPoly> > >&,const std::vector<double>&) const>(
767767
&CryptoContextImpl<DCRTPoly>::EvalLinearWSum),
768-
py::arg("ciphertext"),
769-
py::arg("coefficients"))
768+
py::arg("ciphertext"),
769+
py::arg("coefficients"))
770+
.def("Compress", &CryptoContextImpl<DCRTPoly>::Compress,
771+
py::arg("ciphertext"),
772+
py::arg("towersLeft"))
770773
.def("EvalMultMany", &CryptoContextImpl<DCRTPoly>::EvalMultMany,
771774
py::arg("ciphertextVec"))
772775
.def("EvalAddManyInPlace", &CryptoContextImpl<DCRTPoly>::EvalAddManyInPlace,

src/lib/pke/serialization.cpp

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@
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

4642
using namespace lbcrypto;
4743
namespace py = pybind11;
@@ -51,33 +47,30 @@ PYBIND11_MAKE_OPAQUE(std::map<uint32_t, EvalKey<DCRTPoly>>);
5147

5248

5349
template <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

6257
template <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

7165
template <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

8275
template <typename T, typename ST>
8376
std::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

10295
template <typename T, typename ST>
10396
py::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

110107
template <typename T, typename ST>
@@ -125,18 +122,20 @@ CryptoContext<DCRTPoly> DeserializeCCFromStringWrapper(const std::string& str, c
125122

126123
template <typename T, typename ST>
127124
T 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

135133
template <typename ST>
136134
CryptoContext<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

154153
template <typename ST>
155154
py::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

166167
template <typename ST>
167168
std::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-
177177
template <typename ST>
178178
py::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

187191
template <typename ST>
@@ -194,11 +198,11 @@ void DeserializeEvalMultKeyFromStringWrapper(const std::string& data, const ST&
194198
}
195199

196200
template <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

216220
template <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

Comments
 (0)