-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsometric_GS.sage
318 lines (246 loc) · 10.4 KB
/
Isometric_GS.sage
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
attach NTRU.sage
#Rotate k times a NTRU lattice vector
#The rotation r is defined as follows: r(f,g) = (x*f mod (x^n+1), x*g mod (x^n+1))
def NTRU_Rotate(v,F,k):
#This algorithm performs operations over F. One can typically take F to be ZZ, QQ or RR.
n = len(v)//2;
w = vector(F, 2*n);
for i in [0..n-1-k]:
w[i+k] = v[i];
w[n+i+k] = v[n+i];
for i in range(k):
w[i] = -v[n-k+i];
w[n+i] = -v[2*n-k+i];
return w;
#Algorithm performing Gram-Schmidt Decomposition over NTRU lattices using the principles of [GHN06] and [LP15].
#Optimized versions of Algorithms 3,4 of [LP15]. See also section 7 of [LP15].
#This algorithm performs operations over the field F. One can typically take F to be QQ or RR.
def Generic_NTRU_GSD(A,F,q):
n = A.nrows()//2;
assert(A.nrows()==A.ncols());
B = matrix(F,2*n,2*n);
C = vector(F,2*n);
D = vector(F,2*n);
CD = vector(F,2*n);
M = matrix(F,2*n,2*n);
X = matrix(F,2*n,2*n);
Y = matrix(F,2*n,2*n);
B[0] = A[0];
v = A[0];
C[0] = NTRU_Rotate(B[0],F,1)*v;
D[0] = v*v;
CD[0] = C[0]/D[0];
for i in [1..n-1]:
B[i] = NTRU_Rotate(B[i-1],F,1) - CD[i-1]*v;
v = v - CD[i-1]*NTRU_Rotate(B[i-1],F,1);
Y[n,i] = NTRU_Rotate(A[n],F,1)*v; #This is computed now to be used later in the GSD.
D[i] = D[i-1] - C[i-1]*CD[i-1];
C[i] = NTRU_Rotate(B[i],F,1)*A[0];
CD[i] = C[i]/D[i];
for i in range(n):
k = q/D[i];
for j in range(n):
B[2*n-1-i,2*n-1-j] = k*B[i,j];
B[2*n-1-i,j] = -k*B[i,2*n-1-j];
D[2*n-1-i] = q*k;
vv = B[n,1];
for i in range(n-1):
assert(vv!=0);
CD[n+i] = (B[n+i,0]-B[n+i+1,1])/vv;
vv = vv - CD[n+i]*B[n+i,0];
for i in range(2*n):
M[i,i] = 1;
#We fill the upper-left quadrant:
for i in range(n):
X[i,i] = D[i];
for i in [1..n-1]:
X[i,0] = A[i]*B[0];
Y[i,0] = NTRU_Rotate(A[i],F,1)*A[0];
for j in [1..i-1]:
X[i,j] = X[i-1,j-1] - CD[j-1]*Y[i-1,j-1];
Y[i,j] = Y[i,j-1] - CD[j-1]*X[i,j-1];
#We fill the lower-left quadrant (under its diagonal):
for i in [n..2*n-1]:
X[i,0] = A[i]*B[0];
Y[i,0] = NTRU_Rotate(A[i],F,1)*A[0];
for j in [1..i-n]:
X[i,j] = X[i-1,j-1] - CD[j-1]*Y[i-1,j-1];
Y[i,j] = Y[i,j-1] - CD[j-1]*X[i,j-1];
#We fill the lower-left quadrant (over its diagonal):
for j in range(n):
X[n,j] = A[n]*B[j];
for i in [n+1..j+n-1]:
X[i,j] = X[i-1,j-1] - CD[j-1]*Y[i-1,j-1];
Y[i,j] = Y[i,j-1] - CD[j-1]*X[i,j-1];
#We fill the lower-right quadrant:
for i in [n..2*n-1]:
X[i,n] = A[i]*B[n];
Y[i,n] = NTRU_Rotate(A[i],F,1)*B[n];
for j in [n+1..i]:
X[i,j] = X[i-1,j-1] - CD[j-1]*Y[i-1,j-1];
Y[i,j] = Y[i,j-1] - CD[j-1]*X[i,j-1];
for i in range(2*n):
for j in [0..i-1]:
M[i,j] = X[i,j]/D[j];
return (B,M);
#Algorithm performing Gram-Schmidt Decomposition over NTRU lattices using the principles of [GHN06] and [LP15].
#Optimized versions of Algorithms 6,7 of [LP15]. See also section 7 of [LP15].
#This algorithm performs only exact operations over the ring of integers ZZ
def Integer_NTRU_GSD(A,q):
n = A.nrows()//2;
b = matrix(ZZ,2*n,2*n);
c = vector(ZZ,2*n);
d = vector(ZZ,2*n+1);
xx = matrix(ZZ,2*n,2*n);
y = matrix(ZZ,2*n,2*n);
d[2*n] = 1; #This is the sentinel value for 'd[-1]'
b[0] = A[0];
v = A[0];
c[0] = NTRU_Rotate(A[0],ZZ,1)*v;
d[0] = v*v;
for i in [1..n-1]:
b[i] = (d[i-1]*NTRU_Rotate(b[i-1],ZZ,1) - c[i-1]*v)/d[i-2];
v = (d[i-1]*v - c[i-1]*NTRU_Rotate(b[i-1],ZZ,1))/d[i-2];
y[n,i] = NTRU_Rotate(A[n],ZZ,1)*v; #This is computed now to be used later in the GSD.
d[i] = (d[i-1]*d[i-1] - c[i-1]*c[i-1])/d[i-2];
c[i] = A[0]*NTRU_Rotate(b[i],ZZ,1);
#Using symplecticity (see [GHN06]) to compute the second half of the GSO
q2 = 1;
for i in range(n):
q2 *= (q**2);
d[n+i] = q2*d[n-2-i];
#Using symplecticity (see [GHN06]) to compute the second half of the GSO
for i in range(n):
k = q**(2*n-2*i-1);
for j in range(n):
b[2*n-1-i,2*n-1-j] = k*b[i,j];
b[2*n-1-i,j] = -k*b[i,2*n-1-j];
#Can be computed faster (see Generic_NTRU_GSD)
for i in range(n-1):
c[n+i] = (b[N]*NTRU_Rotate(b[n+i],ZZ,1))/d[n-1];
#We fill the diagonal of xx:
for i in range(2*n):
xx[i,i] = d[i];
#We fill the upper-left quadrant of xx:
for i in [1..N-1]:
xx[i,0] = A[i]*b[0];
y[i,0] = NTRU_Rotate(A[i],ZZ,1)*A[0];
for j in [1..i-1]:
xx[i,j] = (d[j-1]*xx[i-1,j-1] - c[j-1]*y[i-1,j-1])/d[j-2];
y[i,j] = (d[j-1]*y[i ,j-1] - c[j-1]*xx[i ,j-1])/d[j-2];
#We fill the lower-left quadrant of xx (under its diagonal):
for i in [n..2*n-1]:
xx[i,0] = A[i]*b[0];
y[i,0] = NTRU_Rotate(A[i],ZZ,1)*A[0];
for j in [1..i-n]:
xx[i,j] = (d[j-1]*xx[i-1,j-1] - c[j-1]*y[i-1,j-1])/d[j-2];
y[i,j] = (d[j-1]*y[i ,j-1] - c[j-1]*xx[i ,j-1])/d[j-2];
#We fill the lower-left quadrant of xx (over its diagonal):
for j in range(n):
xx[n,j] = A[n]*b[j];
for i in [n+1..j+n-1]:
xx[i,j] = (d[j-1]*xx[i-1,j-1] - c[j-1]*y[i-1,j-1])/d[j-2];
y[i,j] = (d[j-1]*y[i ,j-1] - c[j-1]*xx[i ,j-1])/d[j-2];
#We fill the lower-right quadrant of xx:
for i in [n..2*n-1]:
xx[i,n] = A[i]*b[n];
y[i,n] = NTRU_Rotate(A[i],ZZ,1)*b[n];
for j in [n+1..i-1]:
xx[i,j] = (d[j-1]*xx[i-1,j-1] - c[j-1]*y[i-1,j-1])/d[j-2];
y[i,j] = (d[j-1]*y[i ,j-1] - c[j-1]*xx[i ,j-1])/d[j-2];
return xx;
#Allows to compute multiple dot products at the same time when the second term is constant and the first term is NTRU_Rotate(a,i) for i in range(n)
#Useful for algorithm Optimized_Integer_NTRU_GSD
def Multiple_Dot_Products(a,b,F):
assert(len(a)==len(b));
n = len(a)//2;
phi = x^n + 1;
a1 = sum(a[i ]*(x**i) for i in range(n));
a2 = sum(a[i+n]*(x**i) for i in range(n));
b1 = sum(b[i ]*(x**i) for i in range(n));
b2 = sum(b[i+n]*(x**i) for i in range(n));
a1 = Reverse(a1,n);
a2 = Reverse(a2,n);
a1 = a1.change_ring(F);
a2 = a2.change_ring(F);
b1 = b1.change_ring(F);
b2 = b2.change_ring(F);
return (a1*b1 + a2*b2)%phi;
#Algorithm performing Gram-Schmidt Decomposition over NTRU lattices using the principles of [GHN06] and [LP15].
#Optimized versions of Algorithms 6,7 of [LP15]. See also section 7 of [LP15].
#This algorithm performs only exact operations over the ring of integers ZZ
#Compared to Integer_NTRU_GSD(), this algorithm introduces further optimizations:
# - replacing standard division / with .divide_knowing_divisible_by()
# - maximizing the use of symplecticity to compute for free or avoid using the second half of c, d
# - using symplecticity to avoid computing the second half of b
# - using a new procedure Multiple_Dot_Products() to compute multiple dot products at the same time
def Optimized_Integer_NTRU_GSD(A,q):
n = A.nrows()//2;
b = matrix(ZZ,2*n,2*n);
c = vector(ZZ,2*n);
d = vector(ZZ,2*n+1);
xx = matrix(ZZ,2*n,2*n);
y = matrix(ZZ,2*n,2*n);
d[2*n] = 1; #This is the sentinel value for 'd[-1]'
b[0] = A[0];
v = A[0];
c[0] = NTRU_Rotate(A[0],ZZ,1)*v;
d[0] = v*v;
for i in [1..n-1]:
b[i] = d[i-1]*NTRU_Rotate(b[i-1],ZZ,1) - c[i-1]*v;
v = d[i-1]*v - c[i-1]*NTRU_Rotate(b[i-1],ZZ,1);
for j in range(2*n):
b[i,j] = b[i,j].divide_knowing_divisible_by(d[i-2]);
v[j] = v[j].divide_knowing_divisible_by(d[i-2]);
y[n,i] = NTRU_Rotate(A[n],ZZ,1)*v; #This is computed now to be used later in the GSD.
d[i] = (d[i-1]*d[i-1] - c[i-1]*c[i-1]).divide_knowing_divisible_by(d[i-2]);
c[i] = A[0]*NTRU_Rotate(b[i],ZZ,1);
#Using symplecticity (see [GHN06]) to compute for free the second half of d
q2 = 1;
for i in range(n):
q2 *= (q**2);
d[n+i] = q2*d[n-2-i];
#We fill the diagonal of xx:
for i in range(2*n):
xx[i,i] = d[i];
#We fill the upper-left quadrant of xx:
#We could avoid to compute one of the two MDP, but it wouldn't change much to the timings
MDP1 = Multiple_Dot_Products(A[0],A[0],ZZ);
MDP2 = Multiple_Dot_Products(NTRU_Rotate(A[0],ZZ,1),A[0],ZZ);
for i in [1..n-1]:
xx[i,0] = MDP1[i];
y[i,0] = MDP2[i];
for j in [1..i-1]:
xx[i,j] = (d[j-1]*xx[i-1,j-1] - c[j-1]*y[i-1,j-1]).divide_knowing_divisible_by(d[j-2]);
y[i,j] = (d[j-1]*y[i ,j-1] - c[j-1]*xx[i ,j-1]).divide_knowing_divisible_by(d[j-2]);
#We fill the lower-left quadrant of xx (under its diagonal):
MDP1 = Multiple_Dot_Products(A[n],A[0],ZZ);
MDP2 = Multiple_Dot_Products(NTRU_Rotate(A[n],ZZ,1),A[0],ZZ);
for i in [n..2*n-1]:
xx[i,0] = MDP1[i-n];
y[i,0] = MDP2[i-n];
for j in [1..i-n]:
xx[i,j] = (d[j-1]*xx[i-1,j-1] - c[j-1]*y[i-1,j-1]).divide_knowing_divisible_by(d[j-2]);
y[i,j] = (d[j-1]*y[i ,j-1] - c[j-1]*xx[i ,j-1]).divide_knowing_divisible_by(d[j-2]);
#We fill the lower-left quadrant of xx (over its diagonal):
for j in range(n):
xx[n,j] = A[n]*b[j];
for i in [n+1..j+n-1]:
xx[i,j] = (d[j-1]*xx[i-1,j-1] - c[j-1]*y[i-1,j-1]).divide_knowing_divisible_by(d[j-2]);
y[i,j] = (d[j-1]*y[i ,j-1] - c[j-1]*xx[i ,j-1]).divide_knowing_divisible_by(d[j-2]);
#We fill the lower-right quadrant of xx:
#In addition to using Multiple_Dot_Products, we find a way to avoid having to use the c[j],d[j],b[j] for j>=n
#This is done using the symmetries of the GSD of a symplectic matrix
Ap = vector(ZZ,2*n);
for j in range(n):
Ap[2*n-1-j] = A[n,j];
Ap[j] = -A[n,2*n-1-j];
MDP1 = -q*Multiple_Dot_Products(Ap,b[n-1],ZZ);
MDP2 = -q*Multiple_Dot_Products(NTRU_Rotate(Ap,ZZ,1),b[n-1],ZZ);
for i in [n..2*n-1]:
xx[i,n] = MDP1[i-n];
y[i,n] = MDP2[i-n];
for j in [n+1..i-1]:
xx[i,j] = (q*q*(d[2*n-1-j]*xx[i-1,j-1] - c[2*n-1-j]*y[i-1,j-1])).divide_knowing_divisible_by(d[2*n-j]);
y[i,j] = (q*q*(d[2*n-1-j]*y[i,j-1] - c[2*n-1-j]*xx[i,j-1])).divide_knowing_divisible_by(d[2*n-j]);
return xx;