forked from kimlaine/bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
polycore.h
375 lines (355 loc) · 11.9 KB
/
polycore.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include <cstdint>
#include <stdexcept>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <limits>
#include "seal/util/common.h"
#include "seal/util/uintcore.h"
#include "seal/util/pointer.h"
namespace seal
{
namespace util
{
SEAL_NODISCARD inline std::string poly_to_hex_string(
const std::uint64_t *value, std::size_t coeff_count,
std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (coeff_uint64_count && coeff_count && !value)
{
throw std::invalid_argument("value");
}
#endif
std::ostringstream result;
bool empty = true;
value += util::mul_safe(coeff_count - 1, coeff_uint64_count);
while (coeff_count--)
{
if (is_zero_uint(value, coeff_uint64_count))
{
value -= coeff_uint64_count;
continue;
}
if (!empty)
{
result << " + ";
}
result << uint_to_hex_string(value, coeff_uint64_count);
if (coeff_count)
{
result << "x^" << coeff_count;
}
empty = false;
value -= coeff_uint64_count;
}
if (empty)
{
result << "0";
}
return result.str();
}
SEAL_NODISCARD inline std::string poly_to_dec_string(
const std::uint64_t *value, std::size_t coeff_count,
std::size_t coeff_uint64_count, MemoryPool &pool)
{
#ifdef SEAL_DEBUG
if (coeff_uint64_count && coeff_count && !value)
{
throw std::invalid_argument("value");
}
#endif
std::ostringstream result;
bool empty = true;
value += coeff_count - 1;
while (coeff_count--)
{
if (is_zero_uint(value, coeff_uint64_count))
{
value -= coeff_uint64_count;
continue;
}
if (!empty)
{
result << " + ";
}
result << uint_to_dec_string(value, coeff_uint64_count, pool);
if (coeff_count)
{
result << "x^" << coeff_count;
}
empty = false;
value -= coeff_uint64_count;
}
if (empty)
{
result << "0";
}
return result.str();
}
SEAL_NODISCARD inline auto allocate_poly(std::size_t coeff_count,
std::size_t coeff_uint64_count, MemoryPool &pool)
{
return allocate_uint(
util::mul_safe(coeff_count, coeff_uint64_count), pool);
}
inline void set_zero_poly(std::size_t coeff_count,
std::size_t coeff_uint64_count, std::uint64_t* result)
{
#ifdef SEAL_DEBUG
if (!result && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
set_zero_uint(util::mul_safe(coeff_count, coeff_uint64_count), result);
}
SEAL_NODISCARD inline auto allocate_zero_poly(
std::size_t coeff_count, std::size_t coeff_uint64_count,
MemoryPool &pool)
{
return allocate_zero_uint(
util::mul_safe(coeff_count, coeff_uint64_count), pool);
}
SEAL_NODISCARD inline std::uint64_t *get_poly_coeff(
std::uint64_t *poly, std::size_t coeff_index,
std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (!poly)
{
throw std::invalid_argument("poly");
}
#endif
return poly + util::mul_safe(coeff_index, coeff_uint64_count);
}
SEAL_NODISCARD inline const std::uint64_t *get_poly_coeff(
const std::uint64_t *poly, std::size_t coeff_index,
std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (!poly)
{
throw std::invalid_argument("poly");
}
#endif
return poly + util::mul_safe(coeff_index, coeff_uint64_count);
}
inline void set_poly_poly(const std::uint64_t *poly,
std::size_t coeff_count, std::size_t coeff_uint64_count,
std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
if (!result && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
set_uint_uint(poly,
util::mul_safe(coeff_count, coeff_uint64_count), result);
}
SEAL_NODISCARD inline bool is_zero_poly(
const std::uint64_t *poly, std::size_t coeff_count,
std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
#endif
return is_zero_uint(poly,
util::mul_safe(coeff_count, coeff_uint64_count));
}
SEAL_NODISCARD inline bool is_equal_poly_poly(
const std::uint64_t *operand1, const std::uint64_t *operand2,
std::size_t coeff_count, std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (!operand1 && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("operand1");
}
if (!operand2 && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("operand2");
}
#endif
return is_equal_uint_uint(operand1, operand2,
util::mul_safe(coeff_count, coeff_uint64_count));
}
inline void set_poly_poly(const std::uint64_t *poly, std::size_t poly_coeff_count,
std::size_t poly_coeff_uint64_count, std::size_t result_coeff_count,
std::size_t result_coeff_uint64_count, std::uint64_t *result)
{
#ifdef SEAL_DEBUG
if (!poly && poly_coeff_count && poly_coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
if (!result && result_coeff_count && result_coeff_uint64_count)
{
throw std::invalid_argument("result");
}
#endif
if (!result_coeff_uint64_count || !result_coeff_count)
{
return;
}
std::size_t min_coeff_count = std::min(poly_coeff_count, result_coeff_count);
for (std::size_t i = 0; i < min_coeff_count; i++,
poly += poly_coeff_uint64_count, result += result_coeff_uint64_count)
{
set_uint_uint(poly, poly_coeff_uint64_count, result_coeff_uint64_count, result);
}
set_zero_uint(util::mul_safe(
result_coeff_count - min_coeff_count, result_coeff_uint64_count), result);
}
SEAL_NODISCARD inline bool is_one_zero_one_poly(
const std::uint64_t *poly, std::size_t coeff_count,
std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
#endif
if (coeff_count == 0 || coeff_uint64_count == 0)
{
return false;
}
if (!is_equal_uint(get_poly_coeff(poly, 0, coeff_uint64_count),
coeff_uint64_count, 1))
{
return false;
}
if (!is_equal_uint(get_poly_coeff(poly, coeff_count - 1, coeff_uint64_count),
coeff_uint64_count, 1))
{
return false;
}
if (coeff_count > 2 &&
!is_zero_poly(poly + coeff_uint64_count,
coeff_count - 2, coeff_uint64_count))
{
return false;
}
return true;
}
SEAL_NODISCARD inline std::size_t get_significant_coeff_count_poly(
const std::uint64_t *poly, std::size_t coeff_count,
std::size_t coeff_uint64_count)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
#endif
if (coeff_count == 0)
{
return 0;
}
poly += util::mul_safe(coeff_count - 1, coeff_uint64_count);
for (std::size_t i = coeff_count; i; i--)
{
if (!is_zero_uint(poly, coeff_uint64_count))
{
return i;
}
poly -= coeff_uint64_count;
}
return 0;
}
SEAL_NODISCARD inline auto duplicate_poly_if_needed(
const std::uint64_t *poly, std::size_t coeff_count,
std::size_t coeff_uint64_count, std::size_t new_coeff_count,
std::size_t new_coeff_uint64_count, bool force, MemoryPool &pool)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
#endif
if (!force && coeff_count >= new_coeff_count &&
coeff_uint64_count == new_coeff_uint64_count)
{
return ConstPointer<std::uint64_t>::Aliasing(poly);
}
auto allocation(allocate_poly(
new_coeff_count, new_coeff_uint64_count, pool));
set_poly_poly(poly, coeff_count, coeff_uint64_count, new_coeff_count,
new_coeff_uint64_count, allocation.get());
return ConstPointer<std::uint64_t>(std::move(allocation));
}
SEAL_NODISCARD inline bool are_poly_coefficients_less_than(
const std::uint64_t *poly, std::size_t coeff_count,
std::size_t coeff_uint64_count, const std::uint64_t *compare,
std::size_t compare_uint64_count)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count && coeff_uint64_count)
{
throw std::invalid_argument("poly");
}
if (!compare && compare_uint64_count > 0)
{
throw std::invalid_argument("compare");
}
#endif
if (coeff_count == 0)
{
return true;
}
if (compare_uint64_count == 0)
{
return false;
}
if (coeff_uint64_count == 0)
{
return true;
}
for (; coeff_count--; poly += coeff_uint64_count)
{
if (compare_uint_uint(poly, coeff_uint64_count, compare,
compare_uint64_count) >= 0)
{
return false;
}
}
return true;
}
SEAL_NODISCARD inline bool are_poly_coefficients_less_than(
const std::uint64_t *poly, std::size_t coeff_count,
std::uint64_t compare)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count)
{
throw std::invalid_argument("poly");
}
#endif
if (coeff_count == 0)
{
return true;
}
for (; coeff_count--; poly++)
{
if (*poly >= compare)
{
return false;
}
}
return true;
}
}
}