-
Notifications
You must be signed in to change notification settings - Fork 4
/
affft.py
415 lines (329 loc) · 9.48 KB
/
affft.py
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from gf2_poly import *
def s(k,x,mul=gf832_mul):
if k==0:
return x
tmp = s(k-1,x,mul)
return mul(tmp,tmp)^ tmp
def fft_constants(k,a):
if k==0:
return [[s(k,a)]]
l1 = fft_constants(k-1,a)
l2 = fft_constants(k-1,a^(1<<k))
return [[s(k,a)]] + [ l1[i] + l2[i] for i in range(k)]
def fft(coeffs, k, a, mul = gf832_mul):
if k==-1:
return [c for c in coeffs ]
assert len(coeffs) == 2**(k+1)
w = s(k,a,mul)
p0 = list(coeffs[0:len(coeffs)//2])
p1 = list(coeffs[len(coeffs)//2::])
h0 = [ c0 ^ mul(c1,w) for c0,c1 in zip(p0,p1)]
h1 = [ c0 ^ c1 for c0,c1 in zip(h0,p1)]
return fft(h0,k-1,a,mul) + fft(h1,k-1,a^(1<<k),mul)
"""
h0 = p0+w p1
h1 = p0+(w+1)p1
p1 = h0 + h1
p0 = h0 + w p1
"""
def ifft(points, k, a, mul = gf832_mul):
if k==-1:
return [c for c in points]
w = s(k,a,mul)
h0 = ifft(list(points[0:len(points)//2]),k-1,a,mul)
h1 = ifft(list(points[len(points)//2::]),k-1,a^(1<<k),mul)
p1 = [ c0 ^ c1 for c0,c1 in zip(h0,h1)]
p0 = [ c0 ^ mul(c1,w) for c0,c1 in zip(h0,p1)]
return p0 + p1
def affft(coeffs, k, a, mul = gf832_mul, n=0):
if k==-1:
return [c for c in coeffs ]
w = s(k,a,mul)
p0 = list(coeffs[0:len(coeffs)//2])
p1 = list(coeffs[len(coeffs)//2::])
h0 = [ c0 ^ mul(c1,w) for c0,c1 in zip(p0,p1)]
h1 = [ c0 ^ c1 for c0,c1 in zip(h0,p1)]
if n==0:
return [affft(h0,k-1,a,mul,n) , affft(h1,k-1,a^(1<<(k)),mul,n+1)]
#assert len(coeffs) == 2**(k+1)
#do encode
if 2**((n).bit_length()-1) == n:
return affft(h0,k-1,a,mul,n+1)
return affft(h0,k-1,a,mul,n+1) + affft(h1,k-1,a^(1<<k),mul,n+1)
def iaffft(points, k, a, mul = gf832_mul, inv = gf832_inv, n=0):
if k==-1:
return points
return [c for c in points ]
w = s(k,a,mul)
if n==0:
h0 = iaffft(points[0],k-1,a,mul,inv,n)
h1 = iaffft(points[1],k-1,a^(1<<k),mul,inv,n+1)
p1 = [ c0 ^ c1 for c0,c1 in zip(h0,h1)]
p0 = [ c0 ^ mul(c1,w) for c0,c1 in zip(h0,p1)]
return p0+p1
#do decode
if 2**(n.bit_length()-1) == n:
h1 = iaffft(points,k-1,a,mul,inv,n+1)
c1 = (w) >> n
c0 = (w) & (2**n-1)
"""
aa = a + b*(c1 x + c0) = (a+b*c0) + (b*c1) *x
u v
b = v * c1^(-1)
a = u + b*c0
"""
p0=[]
p1=[]
for aa in h1:
u = aa & (2**n-1)
v = aa >> n
#b = mul(v,inv(c1))
b = v
a = mul(b,c0) ^ u
p0.append(a)
p1.append(b)
return p0+p1
h0 = iaffft(points[0:len(points)//2],k-1,a,mul,inv,n+1)
h1 = iaffft(points[len(points)//2::],k-1,a^(1<<k),mul,inv,n+1)
p1 = [ c0 ^ c1 for c0,c1 in zip(h0,h1)]
p0 = [ c0 ^ mul(c1,w) for c0,c1 in zip(h0,p1)]
return p0 + p1
def affft2(coeffs, k, a, mul = gf832_mul, n=0):
if k==-1:
return [c for c in coeffs ]
w = s(k,a,mul)
if n==0:
return affft2(coeffs,k,a^(1<<(k+1)),mul,n+1)
assert len(coeffs) == 2**(k+1)
p0 = list(coeffs[0:len(coeffs)//2])
p1 = list(coeffs[len(coeffs)//2::])
h0 = [ c0 ^ mul(c1,w) for c0,c1 in zip(p0,p1)]
h1 = [ c0 ^ c1 for c0,c1 in zip(h0,p1)]
#do encode
if 2**((n).bit_length()-1) == n:
return affft2(h1,k-1,a^(1<<k),mul,n+1)
return affft2(h0,k-1,a,mul,n+1) + affft2(h1,k-1,a^(1<<k),mul,n+1)
def iaffft2(points, k, a, mul = gf832_mul, inv = gf832_inv, n=0):
if k==-1:
return [c for c in points ]
w = s(k,a,mul)
if n==0:
return iaffft2(points,k,a^(1<<(k+1)),mul,inv,n+1)
#do decode
if 2**(n.bit_length()-1) == n:
h1 = iaffft2(points,k-1,a^(1<<k),mul,inv,n+1)
c1 = (w^1) >> n
c0 = (w^1) & (2**n-1)
"""
aa = a + b*(c1 x + c0) = (a+b*c0) + (b*c1) *x
u v
b = v * c1^(-1)
a = u + b*c0
"""
p0=[]
p1=[]
for aa in h1:
u = aa & (2**n-1)
v = aa >> n
#b = mul(v,inv(c1))
b = v
a = mul(b,c0) ^ u
p0.append(a)
p1.append(b)
return p0+p1
h0 = iaffft2(list(points[0:len(points)//2]),k-1,a,mul,inv,n+1)
h1 = iaffft2(list(points[len(points)//2::]),k-1,a^(1<<k),mul,inv,n+1)
p1 = [ c0 ^ c1 for c0,c1 in zip(h0,h1)]
p0 = [ c0 ^ mul(c1,w) for c0,c1 in zip(h0,p1)]
return p0 + p1
def s_poly(k):
if k==0:
return [0,1]
else:
p = s_poly(k-1)
return gf2_poly_add(gf2_poly_mul(p,p),p)
def sparse_s_poly(k):
if k==0:
return [1]
else:
p = sparse_s_poly(k-1)
#sparse mul
ret = set()
q=list(p)
if 0 in q:
q.remove(0)
else:
q.insert(0,0)
for i in p:
for j in q:
if i+j not in ret:
ret.add(i+j)
else:
ret.remove(i+j)
return sorted(list(ret))
def basis_conversion(coeffs,k):
if k==0:
return coeffs
p1,p0 = gf2_poly_div(coeffs,s_poly(k))
return basis_conversion(extend_zero(p0,2**k),k-1) + basis_conversion(extend_zero(p1,2**k),k-1)
def basis_conversion_count(coeffs,k):
if k==0:
return coeffs,0
p1,p0,count = gf2_poly_div_count(coeffs,s_poly(k))
b0 = basis_conversion_count(extend_zero(p0,2**k),k-1)
b1 = basis_conversion_count(extend_zero(p1,2**k),k-1)
return b0[0]+b1[0], b0[1]+b1[1]+count
def ibasis_conversion(coeffs,k):
if k==0:
return coeffs
p0 = ibasis_conversion(list(coeffs[0:len(coeffs)//2]),k-1)
p1 = ibasis_conversion(list(coeffs[len(coeffs)//2::]),k-1)
return gf2_poly_add(gf2_poly_mul(p1,s_poly(k)),p0)
def test_basis_conversion(k):
n=2**(k+1)
a=gf2_rand(n)
b=ibasis_conversion(basis_conversion(a,k),k)
print(a)
print(b)
def test_fft(k):
n=2**(k+1)
a=gf256_rand(n)
fa = fft(a,k,0)
ifa = ifft(fa,k,0)
print(a)
print(ifa)
def test_affft(k):
n=2**(k+1)
a=gf2_rand(n)
fa = affft(a,k,0)
print(fa)
ifa = iaffft(fa,k,0)
print(a)
print(ifa)
def poly_mul_by_fft(a,b,k,mul=gf832_mul):
aa = basis_conversion(a,k)
bb = basis_conversion(b,k)
faa = fft(aa,k,0,mul)
fbb = fft(bb,k,0,mul)
faabb = point_mul(faa,fbb,mul)
aabb = ifft(faabb,k,0,mul)
ab = ibasis_conversion(aabb,k)
return ab
def poly_mul_by_affft(a,b,k,mul=gf832_mul):
aa = basis_conversion(a,k)
bb = basis_conversion(b,k)
faa = affft(aa,k,0,mul)
fbb = affft(bb,k,0,mul)
faabb = point_mul(faa,fbb,mul)
print("point mul result",faabb)
aabb = iaffft(faabb,k,0,mul)
ab = ibasis_conversion(aabb,k)
return ab
def poly_mul_by_affft2(a,b,k,mul=gf832_mul):
aa = basis_conversion(a,k)
bb = basis_conversion(b,k)
faa = affft2(aa,k,0,mul)
fbb = affft2(bb,k,0,mul)
faabb = point_mul(faa,fbb,mul)
print("point mul result",faabb)
aabb = iaffft2(faabb,k,0,mul)
ab = ibasis_conversion(aabb,k)
return ab
def point_mul(a,b,mul=gf832_mul):
if isinstance(a, list):
return [point_mul(u,v,mul) for u,v in zip(a,b)]
else:
return mul(a,b)
def test_poly_mul_fft(k):
n=2**k
a=extend_zero(gf2_rand(n),2*n)
b=extend_zero(gf2_rand(n),2*n)
print(a,b)
result = poly_mul_by_fft(a,b,k)
print (result)
ref_result = gf2_poly_mul(a,b)
ref_result = ref_result[0:len(ref_result)//2+1]
print (ref_result)
def test_poly_mul_affft(k):
n=2**k
a=extend_zero(gf2_rand(n),2*n)
b=extend_zero(gf2_rand(n),2*n)
print(a,b)
result = poly_mul_by_affft(a,b,k)
print (" ",result)
ref_result = gf2_poly_mul(a,b)
ref_result = ref_result[0:len(ref_result)//2+1]
#print("ref pmul",affft(basis_conversion(ref_result,k),k,0))
print ("ref ",ref_result)
return ref_result == result
def test_poly_mul_affft2(k):
n=2**k
a=extend_zero(gf2_rand(n),2*n)
b=extend_zero(gf2_rand(n),2*n)
print(a,b)
result = poly_mul_by_affft2(a,b,k)
print (" ",result)
ref_result = gf2_poly_mul(a,b)
ref_result = ref_result[0:len(ref_result)//2+1]
#print("ref pmul",affft(basis_conversion(ref_result,k),k,0))
print ("ref ",ref_result)
return ref_result == result
def ft(coeffs,k):
return eval_poly([i for i in range(2**(k+1))],coeffs)
def test_fft_bc(k):
a = gf2_rand(2**(k+1))
print(a)
aa = basis_conversion(a,k)
faa = fft(aa,k,0)
fa = ft(a,k)
print(faa)
print(fa)
print(ibasis_conversion((ifft(faa,k,0)),k))
def test_affft_bc(k):
a = gf2_rand(2**(k+1))
print(a)
aa = basis_conversion(a,k)
faa = affft(aa,k,0)
print(faa)
print(ibasis_conversion((iaffft(faa,k,0)),k))
def test_affft_fft(k):
a = gf2_rand(2**(k+1))
print(a)
aa = basis_conversion(a,k)
fa = ft(a,k)
faa = fft(aa,k,0)
ffaa = affft(aa,k,0)
print(fa)
print(faa)
print(ffaa)
dp = dict()
# 2**(n+1) points
def rec(n):
if n in dp:
return dp[n]
if n==0:
dp[n] = 0
return 0
if n==1:
dp[n] = 1
return 1
m = 0
while 2**(m+1) <=n:
m=m+1
ret = rec(n-2**m)+rec(2**m-1)+n-2**m+1
dp[n] = ret
return ret
def nbc(n):
if n==1:
return 0
if n==0:
return 0
d = 0
while 2**(d+1) <n:
d=d+1
m = 2**d
"""
x^{2^m}+x
"""
ret = nbc(n-m)+nbc(m)+n-m
return ret