-
Notifications
You must be signed in to change notification settings - Fork 709
/
dwthandler.h
362 lines (327 loc) · 16.3 KB
/
dwthandler.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#include "seal/memorymanager.h"
#include "seal/modulus.h"
#include "seal/util/defines.h"
#include "seal/util/iterator.h"
#include "seal/util/pointer.h"
#include "seal/util/uintarithsmallmod.h"
#include "seal/util/uintcore.h"
#include <stdexcept>
namespace seal
{
namespace util
{
/**
Provides an interface to all necessary arithmetic of the number structure that specializes a DWTHandler.
*/
template <typename ValueType, typename RootType, typename ScalarType>
class Arithmetic
{
public:
ValueType add(const ValueType &a, const ValueType &b) const;
ValueType sub(const ValueType &a, const ValueType &b) const;
ValueType mul_root(const ValueType &a, const RootType &r) const;
ValueType mul_scalar(const ValueType &a, const ScalarType &s) const;
RootType mul_root_scalar(const RootType &r, const ScalarType &s) const;
ValueType guard(const ValueType &a) const;
};
/**
Provides an interface that performs the fast discrete weighted transform (DWT) and its inverse that are used to
accelerate polynomial multiplications, batch multiple messages into a single plaintext polynomial. This class
template is specialized with integer modular arithmetic for DWT over integer quotient rings, and is used in
polynomial multiplications and BatchEncoder. It is also specialized with double-precision complex arithmetic for
DWT over the complex field, which is used in CKKSEncoder.
@par The discrete weighted transform (DWT) is a variantion on the discrete Fourier transform (DFT) over
arbitrary rings involving weighing the input before transforming it by multiplying element-wise by a weight
vector, then weighing the output by another vector. The DWT can be used to perform negacyclic convolution on
vectors just like how the DFT can be used to perform cyclic convolution. The DFT of size n requires a primitive
n-th root of unity, while the DWT for negacyclic convolution requires a primitive 2n-th root of unity, \psi.
In the forward DWT, the input is multiplied element-wise with an incrementing power of \psi, the forward DFT
transform uses the 2n-th primitve root of unity \psi^2, and the output is not weighed. In the backward DWT, the
input is not weighed, the backward DFT transform uses the 2n-th primitve root of unity \psi^{-2}, and the output
is multiplied element-wise with an incrementing power of \psi^{-1}.
@par A fast Fourier transform is an algorithm that computes the DFT or its inverse. The Cooley-Tukey FFT reduces
the complexity of the DFT from O(n^2) to O(n\log{n}). The DFT can be interpretted as evaluating an (n-1)-degree
polynomial at incrementing powers of a primitive n-th root of unity, which can be accelerated by FFT algorithms.
The DWT evaluates incrementing odd powers of a primitive 2n-th root of unity, and can also be accelerated by
FFT-like algorithms implemented in this class.
@par Algorithms implemented in this class are based on algorithms 1 and 2 in the paper by Patrick Longa and
Michael Naehrig (https://eprint.iacr.org/2016/504.pdf) with three modifications. First, we generalize in this
class the algorithms to DWT over arbitrary rings. Second, the powers of \psi^{-1} used by the IDWT are stored
in a scrambled order (in contrast to bit-reversed order in paper) to create coalesced memory accesses. Third,
the multiplication with 1/n in the IDWT is merged to the last iteration, saving n/2 multiplications. Last, we
unroll the loops to create coalesced memory accesses to input and output vectors. In earlier versions of SEAL,
the mutiplication with 1/n is done by merging a multiplication of 1/2 in all interations, which is slower than
the current method on CPUs but more efficient on some hardware architectures.
@par The order in which the powers of \psi^{-1} used by the IDWT are stored is unnatural but efficient:
the i-th slot stores the (reverse_bits(i - 1, log_n) + 1)-th power of \psi^{-1}.
*/
template <typename ValueType, typename RootType, typename ScalarType>
class DWTHandler
{
public:
DWTHandler()
{}
DWTHandler(const Arithmetic<ValueType, RootType, ScalarType> &num_struct) : arithmetic_(num_struct)
{}
/**
Performs in place a fast multiplication with the DWT matrix.
Accesses to powers of root is coalesced.
Accesses to values is not coalesced without loop unrolling.
@param[values] inputs in normal order, outputs in bit-reversed order
@param[log_n] log 2 of the DWT size
@param[roots] powers of a root in bit-reversed order
@param[scalar] an optional scalar that is multiplied to all output values
*/
void transform_to_rev(
ValueType *values, int log_n, const RootType *roots, const ScalarType *scalar = nullptr) const
{
// constant transform size
size_t n = size_t(1) << log_n;
// registers to hold temporary values
RootType r;
ValueType u;
ValueType v;
// pointers for faster indexing
ValueType *x = nullptr;
ValueType *y = nullptr;
// variables for indexing
std::size_t gap = n >> 1;
std::size_t m = 1;
for (; m < (n >> 1); m <<= 1)
{
std::size_t offset = 0;
if (gap < 4)
{
for (std::size_t i = 0; i < m; i++)
{
r = *++roots;
x = values + offset;
y = x + gap;
for (std::size_t j = 0; j < gap; j++)
{
u = arithmetic_.guard(*x);
v = arithmetic_.mul_root(*y, r);
*x++ = arithmetic_.add(u, v);
*y++ = arithmetic_.sub(u, v);
}
offset += gap << 1;
}
}
else
{
for (std::size_t i = 0; i < m; i++)
{
r = *++roots;
x = values + offset;
y = x + gap;
for (std::size_t j = 0; j < gap; j += 4)
{
u = arithmetic_.guard(*x);
v = arithmetic_.mul_root(*y, r);
*x++ = arithmetic_.add(u, v);
*y++ = arithmetic_.sub(u, v);
u = arithmetic_.guard(*x);
v = arithmetic_.mul_root(*y, r);
*x++ = arithmetic_.add(u, v);
*y++ = arithmetic_.sub(u, v);
u = arithmetic_.guard(*x);
v = arithmetic_.mul_root(*y, r);
*x++ = arithmetic_.add(u, v);
*y++ = arithmetic_.sub(u, v);
u = arithmetic_.guard(*x);
v = arithmetic_.mul_root(*y, r);
*x++ = arithmetic_.add(u, v);
*y++ = arithmetic_.sub(u, v);
}
offset += gap << 1;
}
}
gap >>= 1;
}
if (scalar != nullptr)
{
RootType scaled_r;
for (std::size_t i = 0; i < m; i++)
{
r = *++roots;
scaled_r = arithmetic_.mul_root_scalar(r, *scalar);
u = arithmetic_.mul_scalar(arithmetic_.guard(values[0]), *scalar);
v = arithmetic_.mul_root(values[1], scaled_r);
values[0] = arithmetic_.add(u, v);
values[1] = arithmetic_.sub(u, v);
values += 2;
}
}
else
{
for (std::size_t i = 0; i < m; i++)
{
r = *++roots;
u = arithmetic_.guard(values[0]);
v = arithmetic_.mul_root(values[1], r);
values[0] = arithmetic_.add(u, v);
values[1] = arithmetic_.sub(u, v);
values += 2;
}
}
}
/**
Performs in place a fast multiplication with the DWT matrix.
Accesses to powers of root is coalesced.
Accesses to values is not coalesced without loop unrolling.
@param[values] inputs in bit-reversed order, outputs in normal order
@param[roots] powers of a root in scrambled order
@param[scalar] an optional scalar that is multiplied to all output values
*/
void transform_from_rev(
ValueType *values, int log_n, const RootType *roots, const ScalarType *scalar = nullptr) const
{
// constant transform size
size_t n = size_t(1) << log_n;
// registers to hold temporary values
RootType r;
ValueType u;
ValueType v;
// pointers for faster indexing
ValueType *x = nullptr;
ValueType *y = nullptr;
// variables for indexing
std::size_t gap = 1;
std::size_t m = n >> 1;
for (; m > 1; m >>= 1)
{
std::size_t offset = 0;
if (gap < 4)
{
for (std::size_t i = 0; i < m; i++)
{
r = *++roots;
x = values + offset;
y = x + gap;
for (std::size_t j = 0; j < gap; j++)
{
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
}
offset += gap << 1;
}
}
else
{
for (std::size_t i = 0; i < m; i++)
{
r = *++roots;
x = values + offset;
y = x + gap;
for (std::size_t j = 0; j < gap; j += 4)
{
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
}
offset += gap << 1;
}
}
gap <<= 1;
}
if (scalar != nullptr)
{
r = *++roots;
RootType scaled_r = arithmetic_.mul_root_scalar(r, *scalar);
x = values;
y = x + gap;
if (gap < 4)
{
for (std::size_t j = 0; j < gap; j++)
{
u = arithmetic_.guard(*x);
v = *y;
*x++ = arithmetic_.mul_scalar(arithmetic_.guard(arithmetic_.add(u, v)), *scalar);
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), scaled_r);
}
}
else
{
for (std::size_t j = 0; j < gap; j += 4)
{
u = arithmetic_.guard(*x);
v = *y;
*x++ = arithmetic_.mul_scalar(arithmetic_.guard(arithmetic_.add(u, v)), *scalar);
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), scaled_r);
u = arithmetic_.guard(*x);
v = *y;
*x++ = arithmetic_.mul_scalar(arithmetic_.guard(arithmetic_.add(u, v)), *scalar);
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), scaled_r);
u = arithmetic_.guard(*x);
v = *y;
*x++ = arithmetic_.mul_scalar(arithmetic_.guard(arithmetic_.add(u, v)), *scalar);
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), scaled_r);
u = arithmetic_.guard(*x);
v = *y;
*x++ = arithmetic_.mul_scalar(arithmetic_.guard(arithmetic_.add(u, v)), *scalar);
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), scaled_r);
}
}
}
else
{
r = *++roots;
x = values;
y = x + gap;
if (gap < 4)
{
for (std::size_t j = 0; j < gap; j++)
{
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
}
}
else
{
for (std::size_t j = 0; j < gap; j += 4)
{
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
u = *x;
v = *y;
*x++ = arithmetic_.guard(arithmetic_.add(u, v));
*y++ = arithmetic_.mul_root(arithmetic_.sub(u, v), r);
}
}
}
}
private:
Arithmetic<ValueType, RootType, ScalarType> arithmetic_;
};
} // namespace util
} // namespace seal