forked from microsoft/SEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializable.h
144 lines (122 loc) · 5.29 KB
/
serializable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/serialization.h"
#include "seal/util/defines.h"
#include "seal/util/streambuf.h"
#include <ios>
#include <iostream>
namespace seal
{
/**
Class to represent a serializable object. Some functions return serializable
objects rather than normal objects. For example, Encryptor can be used in
symmetric-key mode to create symmetric-key ciphertexts, where half of the
ciphertext data is pseudo-random and can be generated from a seed, reducing
the size of the newly created ciphertext object by nearly 50%. This makes
sense when, e.g., the ciphertexts need to be communicated from a client to
a server for encrypted computation. When loading an serializable object,
if a seed was used, the seed will be automatically expanded.
Serializable objects also expose the save_size function that behaves just
as the save_size functions of other objects in Microsoft SEAL: it returns
an upper bound on the size of a buffer needed to hold the serialized data.
The following illustrates the use of serializable objects:
+--------------------------+
| Serializable<GaloisKeys> | Size ~1 MB (example)
+------------+-------------+
|
| Serializable<GaloisKeys>::save
v
+---------------+
| Stream/Buffer | Size ~1 MB (example)
+-------+-------+
|
|
v
+---------+
| Network | Minimized communication
+----+----+
|
| GaloisKeys::load
v
+------------+
| GaloisKeys | Size 2 MB (example)
+------------+
*/
template <class T>
class Serializable
{
friend class KeyGenerator;
friend class Encryptor;
public:
/**
Creates a new serializable object by copying a given one.
@param[in] copy The serializable object to copy from
*/
Serializable(const Serializable<T> ©) = default;
/**
Creates a new serializable object by moving a given one.
@param[in] source The serializable object to move from
*/
Serializable(Serializable<T> &&source) = default;
/**
Moves a given serializable object to the current one.
@param[in] assign The serializable object to move from
*/
Serializable &operator=(Serializable<T> &&assign) = default;
/**
Copies a given serializable object to the current one.
@param[in] copy The serializable object to copy from
*/
Serializable &operator=(const Serializable<T> ©) = default;
/**
Returns an upper bound on the size of the serializable object, as if it
was written to an output stream.
@param[in] compr_mode The compression mode
@throws std::invalid_argument if the compression mode is not supported
@throws std::logic_error if the size does not fit in the return type
*/
SEAL_NODISCARD inline std::streamoff save_size(
compr_mode_type compr_mode = Serialization::compr_mode_default) const
{
return obj_.save_size(compr_mode);
}
/**
Saves the serializable object to an output stream. The output is in binary
format and not human-readable. The output stream must have the "binary"
flag set.
@param[out] stream The stream to save the serializable object to
@param[in] compr_mode The desired compression mode
@throws std::invalid_argument if the compression mode is not supported
@throws std::logic_error if the data to be saved is invalid, or if
compression failed
@throws std::runtime_error if I/O operations failed
*/
inline std::streamoff save(
std::ostream &stream, compr_mode_type compr_mode = Serialization::compr_mode_default) const
{
return obj_.save(stream, compr_mode);
}
/**
Saves the serializable object to a given memory location. The output is in
binary format and is not human-readable.
@param[out] out The memory location to write the serializable object to
@param[in] size The number of bytes available in the given memory location
@param[in] compr_mode The desired compression mode
@throws std::invalid_argument if out is null or if size is too small to
contain a SEALHeader, or if the compression mode is not supported
@throws std::logic_error if the data to be saved is invalid, or if
compression failed
@throws std::runtime_error if I/O operations failed
*/
inline std::streamoff save(
seal_byte *out, std::size_t size, compr_mode_type compr_mode = Serialization::compr_mode_default) const
{
return obj_.save(out, size, compr_mode);
}
private:
Serializable(T &&obj) : obj_(std::move(obj))
{}
T obj_;
};
} // namespace seal