-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNTT.cpp
More file actions
369 lines (311 loc) · 8.35 KB
/
Copy pathNTT.cpp
File metadata and controls
369 lines (311 loc) · 8.35 KB
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
#include <intrin.h>
#include <immintrin.h>
#include <memory>
#include <map>
#include <thread>
#include "NTT.h"
#include <future>
#pragma intrinsic(_umul128)
#pragma intrinsic(_udiv128)
class NNT // Number theoretic transform
{
public:
NNT() { r = 0; L = 0; p = 0; W = 0; iW = 0; rN = 0; WW = NULL; iWW = NULL; NN = 0; }
~NNT() { _free(); }
// Main interface
void NTT(uint64_t* dst, uint64_t* src, int threads); // uint64_t dst[n] = fast NTT(uint64_t src[n])
void iNTT(uint64_t* dst, uint64_t* src, int threads); // uint64_t dst[n] = fast INTT(uint64_t src[n])
// Modular arithmetics
uint64_t mod(uint64_t a);
uint64_t modadd(uint64_t a, uint64_t b);
uint64_t modsub(uint64_t a, uint64_t b);
uint64_t modmul(uint64_t a, uint64_t b);
uint64_t modpow(uint64_t a, uint64_t b);
bool init(uint64_t n); // init r,L,p,W,iW,rN
private:
// Internals
void _free(); // Free precomputed W,iW powers tables
void _alloc(uint64_t n); // Allocate and precompute W,iW powers tables
// Helper functions
void NTT_fast(uint64_t* dst, uint64_t* src, uint64_t n, uint64_t w, int threads); // uint64_t dst[n] = fast NTT(uint64_t src[n])
void NTT_fast(uint64_t* dst, uint64_t* src, uint64_t n, uint64_t* w2, uint64_t i2);
private:
uint64_t r, L, p, N;
uint64_t W, iW, rN; // W=(r^L) mod p, iW=inverse W, rN = inverse N
uint64_t* WW, * iWW, NN; // Precomputed (W,iW)^(0,..,NN-1) powers
};
void NNT::_free()
{
NN = 0;
if (WW) delete[] WW; WW = NULL;
if (iWW) delete[] iWW; iWW = NULL;
}
void NNT::_alloc(uint64_t n)
{
/*
WW = new uint64_t[n];
WW[0] = 1;
for (size_t i = 1; i < n; i++) {
WW[i] = modmul(WW[i - 1], W);
}
iWW = new uint64_t[n];
iWW[0] = 1;
for (size_t i = 1; i < n; i++) {
iWW[i] = modmul(iWW[i - 1], iW);
}
*/
NN = n;
}
void NNT::NTT(uint64_t* dst, uint64_t* src, int threads)
{
NTT_fast(dst, src, N, W, threads);
}
void NNT::iNTT(uint64_t* dst, uint64_t* src, int threads)
{
NTT_fast(dst, src, N, iW, threads);
for (uint64_t i = 0; i < N; i++) dst[i] = modmul(dst[i], rN);
}
uint64_t modmul(uint64_t a, uint64_t b, uint64_t p)
{
uint64_t r, s = _umul128(a, b, &r);
(void)_udiv128(r, s, p, &r);
return r;
}
uint64_t modpow(uint64_t a, uint64_t b, uint64_t p) {
uint64_t result = 1;
while (b > 0) {
if (b & 1)
result = modmul(result, a, p);
b >>= 1;
a = modmul(a, a, p);
}
return result;
}
#include <vector>
uint64_t generator(uint64_t p) {
std::vector<uint64_t> fact;
uint64_t phi = p - 1, n = phi;
for (uint64_t i = 2; i * i <= n; ++i)
if (n % i == 0) {
fact.push_back(i);
while (n % i == 0)
n /= i;
}
if (n > 1)
fact.push_back(n);
for (uint64_t res = 2; res <= p; ++res) {
bool ok = true;
for (size_t i = 0; i < fact.size() && ok; ++i)
ok &= modpow(res, phi / fact[i], p) != 1;
if (ok) return res;
}
return -1;
}
std::map<int, uint64_t> primes = {
{11, 9223372036854675457},
{12, 9223372036854497281},
{13, 9223372036854497281},
{14, 9223372036854497281},
{15, 9223372036853661697},
{16, 9223372036853661697},
{17, 9223372036844421121},
{18, 9223372036836950017},
{19, 9223372036836950017},
{20, 9223372036836950017},
{21, 9223372036752015361},
{22, 9223372036737335297},
{23, 9223372036737335297},
{24, 9223372036737335297},
{25, 9223372036083023873},
{26, 9223372035915251713},
{27, 9223372035915251713},
{28, 9223372034170421249},
{29, 9223372034170421249},
{30, 9223372006790004737},
{31, 9223372006790004737},
{32, 9223372006790004737},
{33, 9223371564408373249},
{34, 9223371280940531713},
{35, 9223371280940531713},
{36, 9223371280940531713},
{37, 9223369837831520257},
{38, 9223369837831520257},
{39, 9223369837831520257},
{40, 9223369837831520257},
{41, 9223369837831520257},
{42, 9223341250529198081}
};
bool PrimeQ(uint64_t w) {
uint64_t s = ((uint64_t)sqrt(w) + 1);
for (uint64_t i = 2; i <= s; i++) {
if (w % i == 0) return false;
}
return true;
}
bool NNT::init(uint64_t n)
{
/*
L = (1ull << (64 - (int)round(log2(n)))) - 1;
while (PrimeQ(L * n + 1) == false) L--;
p = L * n + 1;
r = generator(p);
printf("Prime: %llu\n", p);
*/
p = primes[(int)round(log2(n))];
r = generator(p);
L = (p - 1) / n;
N = n; // Size of vectors [uint64_ts]
W = modpow(r, L); // Wn for NTT
iW = modpow(r, p - 1 - L); // Wn for INTT
rN = modpow(n, p - 2); // Scale for INTT
_alloc(n >> 1); // Precompute W,iW powers
return true;
}
void NNT::NTT_fast(uint64_t* dst, uint64_t* src, uint64_t n, uint64_t w, int threads)
{
if (n <= 1) { if (n == 1) dst[0] = src[0]; return; }
uint64_t i, j, a0, a1, n2 = n >> 1, w2 = modmul(w, w);
// Reorder even,odd
for (i = 0, j = 0; i < n2; i++, j += 2) dst[i] = src[j];
for (j = 1; i < n; i++, j += 2) dst[i] = src[j];
// Recursion
if (threads == 1) {
NTT_fast(src, dst, n2, w2, 1); // Even
NTT_fast(src + n2, dst + n2, n2, w2, 1); // Odd
}
else {
int tds0 = threads / 2;
int tds1 = threads - tds0;
void (NNT::*func_ptr)(uint64_t *, uint64_t *, uint64_t, uint64_t, int) = &NNT::NTT_fast;
auto x = std::async(std::launch::async, func_ptr, this, src, dst, n2, w2, tds0); // Even
NTT_fast(src + n2, dst + n2, n2, w2, tds1); // Odd
x.wait();
}
// Restore results
for (w2 = 1, i = 0, j = n2; i < n2; i++, j++, w2 = modmul(w2, w))
{
a0 = src[i];
a1 = modmul(src[j], w2);
dst[i] = modadd(a0, a1);
dst[j] = modsub(a0, a1);
}
}
void NNT::NTT_fast(uint64_t* dst, uint64_t* src, uint64_t n, uint64_t* w2, uint64_t i2)
{
if (n <= 1) { if (n == 1) dst[0] = src[0]; return; }
uint64_t i, j, a0, a1, n2 = n >> 1;
// Reorder even,odd
for (i = 0, j = 0; i < n2; i++, j += 2) dst[i] = src[j];
for (j = 1; i < n; i++, j += 2) dst[i] = src[j];
// Recursion
i = i2 << 1;
NTT_fast(src, dst, n2, w2, i); // Even
NTT_fast(src + n2, dst + n2, n2, w2, i); // Odd
// Restore results
for (i = 0, j = n2; i < n2; i++, j++, w2 += i2)
{
a0 = src[i];
a1 = modmul(src[j], *w2);
dst[i] = modadd(a0, a1);
dst[j] = modsub(a0, a1);
}
}
uint64_t NNT::mod(uint64_t a)
{
if (a > p) a -= p;
return a;
}
uint64_t NNT::modadd(uint64_t a, uint64_t b)
{
uint64_t d;
bool cy = _addcarry_u64(0, a, b, &d);
if (cy) d -= p;
if (d >= p) d -= p;
return d;
}
uint64_t NNT::modsub(uint64_t a, uint64_t b)
{
uint64_t d;
bool cy = _subborrow_u64(0, a, b, &d);
if (cy) d += p;
return d;
}
uint64_t NNT::modmul(uint64_t a, uint64_t b)
{
uint64_t r, s = _umul128(a, b, &r);
(void)_udiv128(r, s, p, &r);
return r;
}
uint64_t NNT::modpow(uint64_t a, uint64_t b) {
uint64_t result = 1;
while (b > 0) {
if (b & 1)
result = modmul(result, a);
b >>= 1;
a = modmul(a, a);
}
return result;
}
std::map<int, NNT> ntts;
void ntt_ensure_table(int k)
{
if (k <= 26) return;
if (ntts.find(k) != ntts.end()) return;
ntt_ensure_table(k - 1);
ntts.insert({ k, NNT() });
ntts[k].init(1ull << k);
}
void ntt_forward(uint64_t* T, int k, int threads)
{
NNT& nnt = ntts[k];
uint64_t* D = new uint64_t[(1ull << k)];
nnt.NTT(D, T, threads);
memcpy(T, D, sizeof(uint64_t) * (1ull << k));
delete[] D;
}
void ntt_inverse(uint64_t* T, int k, int threads)
{
NNT& nnt = ntts[k];
uint64_t* D = new uint64_t[(1ull << k)];
nnt.iNTT(D, T, threads);
memcpy(T, D, sizeof(uint64_t) * (1ull << k));
delete[] D;
}
void ntt_pointwise(uint64_t* T, uint64_t* A, int k)
{
NNT& nnt = ntts[k];
for (int i = 0; i < (1 << k); i++) T[i] = nnt.modmul(T[i], A[i]);
}
void int_to_ntt(uint64_t* T, int k, const uint32_t* A, size_t AL)
{
for (size_t i = 0; i < AL; i++) {
uint32_t X = A[i];
T[2 * i] = X % 65536;
X /= 65536;
T[2 * i + 1] = X % 65536;
}
for (size_t i = 2 * AL; i < (1ull << k); i++) {
T[i] = 0;
}
}
void ntt_to_int(uint64_t* T, int k, uint32_t* A, size_t AL)
{
// Compute Scaling Factor
size_t fft_length = (size_t)1 << k;
// Since there are 9 digits per word and we want to put 3 digits per
// point, the length of the transform must be at least 3 times the word
// length of the input.
if (fft_length < 2 * AL)
throw "FFT length is too small.";
uint64_t carry = 0;
for (size_t c = 0; c < AL; c++) {
uint32_t word;
carry += T[2 * c]; // Add to carry
word = carry % 65536; // Get 3 digits.
carry /= 65536;
carry += T[2 * c + 1]; // Add to carry
word += (carry % 65536) * 65536; // Get 3 digits.
carry /= 65536;
A[c] = word;
}
}