-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoly.py
144 lines (121 loc) · 3.41 KB
/
poly.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
#This file contains several helper functions of polynomial arithmetic
import numpy as np
from sympy.ntheory import primitive_root
import math
###############################
# NTT / invNTT
###############################
def brv(x, b=32):
r = 0
for i in range(b):
r |= ((x >> i) & 1) << (b - 1 - i)
return r
def compute_zetas(root_of_unity, p, log_deg):
x = root_of_unity
zetas = [None for _ in range(1 << log_deg)]
for u in range(1 << log_deg):
zetas[u] = pow(x, brv(u, log_deg), p)
return zetas
def ntt(f, p, zetas=None):
if zetas is None:
zetas, _ = get_roots(p, len(f))
F = [int(f) for f in f]
deg = len(f)
l = deg // 2
k = 1
while l > 0:
for s in range(0, deg, 2 * l):
zeta = zetas[k]
k += 1
for j in range(s, s + l):
t = (F[j + l] * zeta) % p
F[j + l] = (F[j] - t) % p
F[j] = (F[j] + t) % p
l = l // 2
return F
def intt(f, p, zetas=None):
if zetas is None:
_, zetas = get_roots(p, len(f))
F = [int(f) for f in f]
deg = len(f)
l = 1
k = deg - 1
while l < deg:
for s in reversed(range(0, deg, 2 * l)):
zeta = zetas[k]
k -= 1
for j in range(s, s + l):
t = F[j]
F[j] = (t + F[j + l]) % p
F[j + l] = (t - F[j + l]) % p
F[j + l] = (F[j + l] * zeta) % p
l = l * 2
ideg = pow(deg, p - 2, p)
F = [(f * ideg) % p for f in F]
return F
def nttadj(u, p):
zetas, izetas = get_roots(p, len(u))
ui = intt(u, p, izetas)
return ntt(adjoint(ui), p, zetas)
def get_roots(p, n):
logn = int(math.log2(n))
g0 = primitive_root(p)
b = (p - 1) // (2 * n)
g0 = (g0**b) % p
zetas = compute_zetas(g0, p, logn)
izetas = [pow(z, p - 2, p) for z in zetas]
return zetas, izetas
########################################
# Polynomial operations
########################################
def poly_mul_ntt(p1, p2, p):
n = len(p1)
zetas, izetas = get_roots(p, n)
p1ntt = ntt(p1, p, zetas)
p2ntt = ntt(p2, p, zetas)
rntt = [(a * b) % p for a, b in zip(p1ntt, p2ntt)]
m = intt(rntt, p, izetas)
for i in range(n):
if m[i] > (p - 1) // 2:
m[i] -= p
return m
def poly_mul_schoolbook(p1, p2, p):
deg = len(p1)
r = [0 for _ in range(deg)]
for i in range(deg):
for j in range(deg):
ij = i + j
if ij >= deg:
r[ij % deg] -= ((p1[i] * p2[j])) % p
else:
r[ij % deg] += ((p1[i] * p2[j])) % p
r = [x % p for x in r]
return r
def adjoint(u):
ustar = u.copy()
n = len(u)
for i in range(1, n):
ustar[i] = -u[n - i]
return ustar
def poly_sub(p0, p1):
return [p0 - p1 for (p0, p1) in zip(p0, p1)]
def poly_add(p0, p1):
return [p0 + p1 for (p0, p1) in zip(p0, p1)]
########################################
# Polynomial properties
########################################
def infnorm(poly):
return max([abs(p) for p in poly])
def bytes_to_poly(h, n):
h0 = [None] * n
for i in range(n):
h0[i] = (h[i // 8] >> (i % 8)) & 1
return h0
def l2norm(x):
x = [x * x for x in x]
return sum(x)
def isinvertible(poly, p):
if p == 2:
return (np.sum(poly) % 2) == 1
polyntt = ntt(poly, p)
return all([c != 0 for c in polyntt])