-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlll_reduce.c
224 lines (187 loc) · 5.15 KB
/
lll_reduce.c
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
/*=============================================================================
The LLL-Reduction Algorithm
Authors
-------
* Grady Williams ([email protected])
* Chris Swierczewski ([email protected])
=============================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/*
gram_schmidt
Numerically stable Gram-Schmidt algorithm.
Given a set of `n` vectors `b_1, ..., b_n` construct a collection of
orthogonal vectors `b*_1, ..., b*_n` spanning the same space. The vectors are
stored as **columns** of b. THIS SHOULD BE CHANGED IN THE FUTURE TO FOLLOW
STANDARD C PRACTICES FOR STORING VECTORS.
Parameters
----------
b : double[:]
An array of `n` vectors each of size `n`.
n : int
Returns
-------
b : double[:]
Orthogonalized vectors.
mu : double[:]
Matrix of orthogonalization parameters
mu_ij = <bi, b*j> / <b*j, b*j>
B : double[:]
Square norms of orthogonalized vectors.
*/
void gram_schmidt(double* b, double* mu, double* B, int n)
{
double numerator;
double dot;
int i,j,k;
double* b_star = (double*)malloc(n*n*sizeof(double));
double* temp = (double*)malloc(n*sizeof(double));
for (i = 0; i < n; i++)
{
// (1) copy non-orthogonal vector: b*_i = b_i
for (k = 0; k < n; k++)
b_star[k*n + i] = b[k*n + i];
// temp keeps track of the shift in the gram schmidt algorithm
for (k = 0; k < n; k++)
temp[k] = 0;
// for each previously computed b*j perform the steps:
//
// (2) mu_ij = <bi, b*j> / B[j] (compute shift coefficient)
// (3) b*i = b*i - mu_ij*b*j (shift by each previous b*j)
//
// note that this is not performed in the first iteration when i=0
for (j = 0; j < i; j++)
{
// (2) compute mu_ij = <b_i, b*_j> / <b*_j, b*_j>
numerator = 0;
for (k = 0; k < n; k++)
numerator += b[k*n + i] * b_star[k*n + j];
mu[i*n + j] = numerator / B[j];
// printf("\nnumerator = %f \t B[%d] = %f", numerator, j, B[j]);
// (3) shift b*i by - mu_ij b*j
for (k = 0; k < n; k++)
b_star[k*n + i] -= mu[i*n + j] * b_star[k*n + j];
}
// (4) store the dot product Bi = <b*i, b*i>
dot = 0;
for (k = 0; k < n; k++)
dot += b_star[k*n + i]*b_star[k*n + i];
B[i] = dot;
}
free(b_star);
free(temp);
}
/*
nearest_integer_shift
Performs operation (*) on p. 521 of [LLL].
Parameters
----------
mu : double[:,:]
b : double[:,:]
k,l : int
n : int
Size of each vector.
lc : double
LLL parameter.
*/
void nearest_integer_shift(double* mu, double* b, int k, int l,
int n, double lc)
{
int i,j;
double r = round(mu[k*n + l]);
if (fabs(mu[k*n+l]) > lc)
{
// shift bk by (-r*bl)
for (i = 0; i < n; i++)
b[i*n + k] -= r*b[i*n + l];
// shift mu_kj by (-r*mu_lj) for j=0,...,l-2
for (j = 0; j < l-1; j++)
mu[k*n + j] -= r*mu[l*n + j];
// shift mu_kl by (-r)
mu[k*n + l] -= r;
}
}
/*
lll_reduce
Performs Lenstra-Lenstra-Lovasv reduction on a given lattice n in
n-dimensional real space. The input matrix b is in the usual C-ordering but
the algorithm works on the columns. (This should be rewritten in a future
update for performance purposes.)
Parameters
----------
b : double[:]
Input array / `n x n` matrix.
n : int
lc,uc : double
The LLL parameters.
Returns
-------
b : double[:]
The LLL reduction of the columns of the input `b`.
*/
void lll_reduce(double* b, int n, double lc, double uc)
{
int i,j,k,l;
double tmp, B_tmp, mu_tmp;
double swap_condition;
double* mu = (double*)calloc(n*n, sizeof(double));
double* B = (double*)calloc(n, sizeof(double));
// initialize mu and B with zeros
for (i = 0; i < n*n; i++)
mu[i] = 0;
for (i = 0; i < n; i++)
B[i] = 0;
// orthogonalize the columns of b and obtain the scaling factors B and mu
gram_schmidt(b,mu,B,n);
k = 1;
while (k < n)
{
nearest_integer_shift(mu, b, k, k-1, n, lc);
swap_condition = (uc - mu[k*n + (k-1)]*mu[k*n + (k-1)])*B[k-1];
if (B[k] < swap_condition)
{
// set the "constant parameters" for this round
mu_tmp = mu[k*n + (k-1)];
B_tmp = B[k] + mu_tmp*mu_tmp*B[k-1];
// scale and swap mu and B values
mu[k*n + (k-1)] = mu_tmp*B[k-1] / B_tmp;
B[k] = B[k-1]*B[k] / B_tmp;
B[k-1] = B_tmp;
// swap b_(k-1) and b_k
for (i = 0; i < n; i++)
{
tmp = b[i*n + k];
b[i*n + k] = b[i*n + (k-1)];
b[i*n + (k-1)] = tmp;
}
// swap mu_(k-1),j and mu_k,j for j = 0,...,k-3
for (j = 0; j < k-2; j++)
{
tmp = mu[k*n + j];
mu[k*n + j] = mu[(k-1)*n + j];
mu[(k-1)*n + j] = tmp;
}
// perform the linear transformation for i = k,...,n-1
for (i = k; i < n; i++)
{
tmp = mu[i*n + (k-1)] - mu_tmp*mu[i*n + k];
mu[i*n + (k-1)] = mu[i*n + k] + mu[k*n + (k-1)] * tmp;
mu[i*n + k] = tmp;
}
if (k > 1)
k -= 1;
}
else
{
// perform the integer shift for l = k-3, ..., 0
for (l = k-3; l >= 0; l--)
{
nearest_integer_shift(mu, b, k, l, n, lc);
}
k += 1;
}
}
free(mu);
free(B);
}