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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/plaintext.h"
#include "seal/util/common.h"
using namespace std;
using namespace seal::util;
namespace seal
{
namespace
{
bool is_dec_char(char c)
{
return c >= '0' && c <= '9';
}
int get_dec_value(char c)
{
return c - '0';
}
int get_coeff_length(const char *poly)
{
int length = 0;
while (is_hex_char(*poly))
{
length++;
poly++;
}
return length;
}
int get_coeff_power(const char *poly, int *power_length)
{
int length = 0;
if (*poly == '\0')
{
*power_length = 0;
return 0;
}
if (*poly != 'x')
{
return -1;
}
poly++;
length++;
if (*poly != '^')
{
return -1;
}
poly++;
length++;
int power = 0;
while (is_dec_char(*poly))
{
power *= 10;
power += get_dec_value(*poly);
poly++;
length++;
}
*power_length = length;
return power;
}
int get_plus(const char *poly)
{
if (*poly == '\0')
{
return 0;
}
if (*poly++ != ' ')
{
return -1;
}
if (*poly++ != '+')
{
return -1;
}
if (*poly != ' ')
{
return -1;
}
return 3;
}
} // namespace
Plaintext &Plaintext::operator=(const string &hex_poly)
{
if (is_ntt_form())
{
throw logic_error("cannot set an NTT transformed Plaintext");
}
if (unsigned_gt(hex_poly.size(), numeric_limits<int>::max()))
{
throw invalid_argument("hex_poly too long");
}
int length = safe_cast<int>(hex_poly.size());
// Determine size needed to store string coefficient.
int assign_coeff_count = 0;
int assign_coeff_bit_count = 0;
int pos = 0;
int last_power = safe_cast<int>(min(data_.max_size(), safe_cast<size_t>(numeric_limits<int>::max())));
const char *hex_poly_ptr = hex_poly.data();
while (pos < length)
{
// Determine length of coefficient starting at pos.
int coeff_length = get_coeff_length(hex_poly_ptr + pos);
if (coeff_length == 0)
{
throw invalid_argument("unable to parse hex_poly");
}
// Determine bit length of coefficient.
int coeff_bit_count = get_hex_string_bit_count(hex_poly_ptr + pos, coeff_length);
if (coeff_bit_count > assign_coeff_bit_count)
{
assign_coeff_bit_count = coeff_bit_count;
}
pos += coeff_length;
// Extract power-term.
int power_length = 0;
int power = get_coeff_power(hex_poly_ptr + pos, &power_length);
if (power == -1 || power >= last_power)
{
throw invalid_argument("unable to parse hex_poly");
}
if (assign_coeff_count == 0)
{
assign_coeff_count = power + 1;
}
pos += power_length;
last_power = power;
// Extract plus (unless it is the end).
int plus_length = get_plus(hex_poly_ptr + pos);
if (plus_length == -1)
{
throw invalid_argument("unable to parse hex_poly");
}
pos += plus_length;
}
// If string is empty, then done.
if (assign_coeff_count == 0 || assign_coeff_bit_count == 0)
{
set_zero();
return *this;
}
// Resize polynomial.
if (assign_coeff_bit_count > bits_per_uint64)
{
throw invalid_argument("hex_poly has too large coefficients");
}
resize(safe_cast<size_t>(assign_coeff_count));
// Populate polynomial from string.
pos = 0;
last_power = safe_cast<int>(coeff_count());
while (pos < length)
{
// Determine length of coefficient starting at pos.
const char *coeff_start = hex_poly_ptr + pos;
int coeff_length = get_coeff_length(coeff_start);
pos += coeff_length;
// Extract power-term.
int power_length = 0;
int power = get_coeff_power(hex_poly_ptr + pos, &power_length);
pos += power_length;
// Extract plus (unless it is the end).
int plus_length = get_plus(hex_poly_ptr + pos);
pos += plus_length;
// Zero coefficients not set by string.
for (int zero_power = last_power - 1; zero_power > power; --zero_power)
{
data_[static_cast<size_t>(zero_power)] = 0;
}
// Populate coefficient.
uint64_t *coeff_ptr = data_.begin() + power;
hex_string_to_uint(coeff_start, coeff_length, size_t(1), coeff_ptr);
last_power = power;
}
// Zero coefficients not set by string.
for (int zero_power = last_power - 1; zero_power >= 0; --zero_power)
{
data_[static_cast<size_t>(zero_power)] = 0;
}
return *this;
}
void Plaintext::save_members(ostream &stream) const
{
auto old_except_mask = stream.exceptions();
try
{
// Throw exceptions on std::ios_base::badbit and std::ios_base::failbit
stream.exceptions(ios_base::badbit | ios_base::failbit);
stream.write(reinterpret_cast<const char *>(&parms_id_), sizeof(parms_id_type));
uint64_t coeff_count64 = static_cast<uint64_t>(coeff_count_);
stream.write(reinterpret_cast<const char *>(&coeff_count64), sizeof(uint64_t));
stream.write(reinterpret_cast<const char *>(&scale_), sizeof(double));
data_.save(stream, compr_mode_type::none);
}
catch (const ios_base::failure &)
{
stream.exceptions(old_except_mask);
throw runtime_error("I/O error");
}
catch (...)
{
stream.exceptions(old_except_mask);
throw;
}
stream.exceptions(old_except_mask);
}
void Plaintext::load_members(const SEALContext &context, istream &stream, SEAL_MAYBE_UNUSED SEALVersion version)
{
// Verify parameters
if (!context.parameters_set())
{
throw invalid_argument("encryption parameters are not set correctly");
}
Plaintext new_data(data_.pool());
auto old_except_mask = stream.exceptions();
try
{
// Throw exceptions on std::ios_base::badbit and std::ios_base::failbit
stream.exceptions(ios_base::badbit | ios_base::failbit);
parms_id_type parms_id{};
stream.read(reinterpret_cast<char *>(&parms_id), sizeof(parms_id_type));
uint64_t coeff_count64 = 0;
stream.read(reinterpret_cast<char *>(&coeff_count64), sizeof(uint64_t));
double scale = 0;
stream.read(reinterpret_cast<char *>(&scale), sizeof(double));
// Set the metadata
new_data.parms_id_ = parms_id;
new_data.coeff_count_ = safe_cast<size_t>(coeff_count64);
new_data.scale_ = scale;
// Checking the validity of loaded metadata
// Note: We allow pure key levels here! This is to allow load_members
// to be used also when loading derived objects like SecretKey. This
// further means that functions reading in Plaintext objects must check
// that for those use-cases the Plaintext truly is at the data level
// if it is supposed to be. In other words, one cannot assume simply
// based on load_members succeeding that the Plaintext is valid for
// computations.
if (!is_metadata_valid_for(new_data, context, true))
{
throw logic_error("plaintext data is invalid");
}
// Reserve memory now that the metadata is checked for validity.
new_data.data_.reserve(new_data.coeff_count_);
// Load the data. Note that we are supplying also the expected maximum
// size of the loaded DynArray. This is an important security measure to
// prevent a malformed DynArray from causing arbitrarily large memory
// allocations.
new_data.data_.load(stream, new_data.coeff_count_);
// Verify that the buffer is correct
if (!is_buffer_valid(new_data))
{
throw logic_error("plaintext data is invalid");
}
}
catch (const ios_base::failure &)
{
stream.exceptions(old_except_mask);
throw runtime_error("I/O error");
}
catch (...)
{
stream.exceptions(old_except_mask);
throw;
}
stream.exceptions(old_except_mask);
swap(*this, new_data);
}
} // namespace seal