-
Notifications
You must be signed in to change notification settings - Fork 713
/
polyarithsmallmod.cpp
371 lines (343 loc) · 12 KB
/
polyarithsmallmod.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "seal/util/polyarithsmallmod.h"
#include "seal/util/uintarith.h"
#include "seal/util/uintcore.h"
#ifdef SEAL_USE_INTEL_HEXL
#include "hexl/hexl.hpp"
#endif
using namespace std;
namespace seal
{
namespace util
{
void modulo_poly_coeffs(ConstCoeffIter poly, std::size_t coeff_count, const Modulus &modulus, CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count > 0)
{
throw std::invalid_argument("poly");
}
if (!result && coeff_count > 0)
{
throw std::invalid_argument("result");
}
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
#endif
#ifdef SEAL_USE_INTEL_HEXL
intel::hexl::EltwiseReduceMod(result, poly, coeff_count, modulus.value(), modulus.value(), 1);
#else
SEAL_ITERATE(
iter(poly, result), coeff_count, [&](auto I) { get<1>(I) = barrett_reduce_64(get<0>(I), modulus); });
#endif
}
void add_poly_coeffmod(
ConstCoeffIter operand1, ConstCoeffIter operand2, std::size_t coeff_count, const Modulus &modulus,
CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!operand1 && coeff_count > 0)
{
throw std::invalid_argument("operand1");
}
if (!operand2 && coeff_count > 0)
{
throw std::invalid_argument("operand2");
}
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (!result && coeff_count > 0)
{
throw std::invalid_argument("result");
}
#endif
const uint64_t modulus_value = modulus.value();
#ifdef SEAL_USE_INTEL_HEXL
intel::hexl::EltwiseAddMod(&result[0], &operand1[0], &operand2[0], coeff_count, modulus_value);
#else
SEAL_ITERATE(iter(operand1, operand2, result), coeff_count, [&](auto I) {
#ifdef SEAL_DEBUG
if (get<0>(I) >= modulus_value)
{
throw std::invalid_argument("operand1");
}
if (get<1>(I) >= modulus_value)
{
throw std::invalid_argument("operand2");
}
#endif
std::uint64_t sum = get<0>(I) + get<1>(I);
get<2>(I) = SEAL_COND_SELECT(sum >= modulus_value, sum - modulus_value, sum);
});
#endif
}
void sub_poly_coeffmod(
ConstCoeffIter operand1, ConstCoeffIter operand2, std::size_t coeff_count, const Modulus &modulus,
CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!operand1 && coeff_count > 0)
{
throw std::invalid_argument("operand1");
}
if (!operand2 && coeff_count > 0)
{
throw std::invalid_argument("operand2");
}
if (modulus.is_zero())
{
throw std::invalid_argument("modulus");
}
if (!result && coeff_count > 0)
{
throw std::invalid_argument("result");
}
#endif
const uint64_t modulus_value = modulus.value();
#ifdef SEAL_USE_INTEL_HEXL
intel::hexl::EltwiseSubMod(result, operand1, operand2, coeff_count, modulus_value);
#else
SEAL_ITERATE(iter(operand1, operand2, result), coeff_count, [&](auto I) {
#ifdef SEAL_DEBUG
if (get<0>(I) >= modulus_value)
{
throw std::invalid_argument("operand1");
}
if (get<1>(I) >= modulus_value)
{
throw std::invalid_argument("operand2");
}
#endif
unsigned long long temp_result;
std::int64_t borrow = sub_uint64(get<0>(I), get<1>(I), &temp_result);
get<2>(I) = temp_result + (modulus_value & static_cast<std::uint64_t>(-borrow));
});
#endif
}
void add_poly_scalar_coeffmod(
ConstCoeffIter poly, size_t coeff_count, uint64_t scalar, const Modulus &modulus, CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count > 0)
{
throw invalid_argument("poly");
}
if (!result && coeff_count > 0)
{
throw invalid_argument("result");
}
if (modulus.is_zero())
{
throw invalid_argument("modulus");
}
if (scalar >= modulus.value())
{
throw invalid_argument("scalar");
}
#endif
#ifdef SEAL_USE_INTEL_HEXL
intel::hexl::EltwiseAddMod(result, poly, scalar, coeff_count, modulus.value());
#else
SEAL_ITERATE(iter(poly, result), coeff_count, [&](auto I) {
const uint64_t x = get<0>(I);
get<1>(I) = add_uint_mod(x, scalar, modulus);
});
#endif
}
void sub_poly_scalar_coeffmod(
ConstCoeffIter poly, size_t coeff_count, uint64_t scalar, const Modulus &modulus, CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count > 0)
{
throw invalid_argument("poly");
}
if (!result && coeff_count > 0)
{
throw invalid_argument("result");
}
if (modulus.is_zero())
{
throw invalid_argument("modulus");
}
if (scalar >= modulus.value())
{
throw invalid_argument("scalar");
}
#endif
#ifdef SEAL_USE_INTEL_HEXL
intel::hexl::EltwiseSubMod(result, poly, scalar, coeff_count, modulus.value());
#else
SEAL_ITERATE(iter(poly, result), coeff_count, [&](auto I) {
const uint64_t x = get<0>(I);
get<1>(I) = sub_uint_mod(x, scalar, modulus);
});
#endif
}
void multiply_poly_scalar_coeffmod(
ConstCoeffIter poly, size_t coeff_count, MultiplyUIntModOperand scalar, const Modulus &modulus,
CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!poly && coeff_count > 0)
{
throw invalid_argument("poly");
}
if (!result && coeff_count > 0)
{
throw invalid_argument("result");
}
if (modulus.is_zero())
{
throw invalid_argument("modulus");
}
#endif
#ifdef SEAL_USE_INTEL_HEXL
intel::hexl::EltwiseFMAMod(&result[0], &poly[0], scalar.operand, nullptr, coeff_count, modulus.value(), 8);
#else
SEAL_ITERATE(iter(poly, result), coeff_count, [&](auto I) {
const uint64_t x = get<0>(I);
get<1>(I) = multiply_uint_mod(x, scalar, modulus);
});
#endif
}
void dyadic_product_coeffmod(
ConstCoeffIter operand1, ConstCoeffIter operand2, size_t coeff_count, const Modulus &modulus,
CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!operand1)
{
throw invalid_argument("operand1");
}
if (!operand2)
{
throw invalid_argument("operand2");
}
if (!result)
{
throw invalid_argument("result");
}
if (coeff_count == 0)
{
throw invalid_argument("coeff_count");
}
if (modulus.is_zero())
{
throw invalid_argument("modulus");
}
#endif
#ifdef SEAL_USE_INTEL_HEXL
intel::hexl::EltwiseMultMod(&result[0], &operand1[0], &operand2[0], coeff_count, modulus.value(), 4);
#else
const uint64_t modulus_value = modulus.value();
const uint64_t const_ratio_0 = modulus.const_ratio()[0];
const uint64_t const_ratio_1 = modulus.const_ratio()[1];
SEAL_ITERATE(iter(operand1, operand2, result), coeff_count, [&](auto I) {
// Reduces z using base 2^64 Barrett reduction
unsigned long long z[2], tmp1, tmp2[2], tmp3, carry;
multiply_uint64(get<0>(I), get<1>(I), z);
// Multiply input and const_ratio
// Round 1
multiply_uint64_hw64(z[0], const_ratio_0, &carry);
multiply_uint64(z[0], const_ratio_1, tmp2);
tmp3 = tmp2[1] + add_uint64(tmp2[0], carry, &tmp1);
// Round 2
multiply_uint64(z[1], const_ratio_0, tmp2);
carry = tmp2[1] + add_uint64(tmp1, tmp2[0], &tmp1);
// This is all we care about
tmp1 = z[1] * const_ratio_1 + tmp3 + carry;
// Barrett subtraction
tmp3 = z[0] - tmp1 * modulus_value;
// Claim: One more subtraction is enough
get<2>(I) = SEAL_COND_SELECT(tmp3 >= modulus_value, tmp3 - modulus_value, tmp3);
});
#endif
}
uint64_t poly_infty_norm_coeffmod(ConstCoeffIter operand, size_t coeff_count, const Modulus &modulus)
{
#ifdef SEAL_DEBUG
if (!operand && coeff_count > 0)
{
throw invalid_argument("operand");
}
if (modulus.is_zero())
{
throw invalid_argument("modulus");
}
#endif
// Construct negative threshold (first negative modulus value) to compute absolute values of coeffs.
uint64_t modulus_neg_threshold = (modulus.value() + 1) >> 1;
// Mod out the poly coefficients and choose a symmetric representative from
// [-modulus,modulus). Keep track of the max.
uint64_t result = 0;
SEAL_ITERATE(operand, coeff_count, [&](auto I) {
uint64_t poly_coeff = barrett_reduce_64(I, modulus);
if (poly_coeff >= modulus_neg_threshold)
{
poly_coeff = modulus.value() - poly_coeff;
}
if (poly_coeff > result)
{
result = poly_coeff;
}
});
return result;
}
void negacyclic_shift_poly_coeffmod(
ConstCoeffIter poly, size_t coeff_count, size_t shift, const Modulus &modulus, CoeffIter result)
{
#ifdef SEAL_DEBUG
if (!poly)
{
throw invalid_argument("poly");
}
if (!result)
{
throw invalid_argument("result");
}
if (poly == result)
{
throw invalid_argument("result cannot point to the same value as poly");
}
if (modulus.is_zero())
{
throw invalid_argument("modulus");
}
if (util::get_power_of_two(static_cast<uint64_t>(coeff_count)) < 0)
{
throw invalid_argument("coeff_count");
}
if (shift >= coeff_count)
{
throw invalid_argument("shift");
}
#endif
// Nothing to do
if (shift == 0)
{
set_uint(poly, coeff_count, result);
return;
}
uint64_t index_raw = shift;
uint64_t coeff_count_mod_mask = static_cast<uint64_t>(coeff_count) - 1;
for (size_t i = 0; i < coeff_count; i++, poly++, index_raw++)
{
uint64_t index = index_raw & coeff_count_mod_mask;
if (!(index_raw & static_cast<uint64_t>(coeff_count)) || !*poly)
{
result[index] = *poly;
}
else
{
result[index] = modulus.value() - *poly;
}
}
}
} // namespace util
} // namespace seal