forked from algebraic-solving/msolve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifting.c
326 lines (291 loc) · 10.3 KB
/
lifting.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
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
/* This file is part of msolve.
*
* msolve is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* msolve is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with msolve. If not, see <https://www.gnu.org/licenses/>
*
* Authors:
* Jérémy Berthomieu
* Christian Eder
* Mohab Safey El Din */
/* Initialization of sparse fglm matrix for crt */
static inline void crt_mpz_matfglm_initset(crt_mpz_matfglm_t crt_mat,
sp_matfglm_t *mod_mat){
crt_mat->ncols = mod_mat->ncols;
crt_mat->nrows = mod_mat->nrows;
crt_mat->dense_mat = calloc(crt_mat->ncols*crt_mat->nrows,
sizeof(mpz_t));
uint64_t sz = crt_mat->nrows * crt_mat->ncols;
for(uint64_t i = 0; i < sz; i++){
mpz_init_set_ui(crt_mat->dense_mat[i], mod_mat->dense_mat[i]);
}
long diff = crt_mat->ncols - crt_mat->nrows;
crt_mat->triv_idx = calloc(diff, sizeof(uint32_t));
crt_mat->triv_pos = calloc(diff, sizeof(uint32_t));
crt_mat->dense_idx = calloc(crt_mat->nrows, sizeof(uint32_t));
crt_mat->dst = calloc(crt_mat->nrows, sizeof(uint32_t));
for(long i = 0; i < diff; i++){
crt_mat->triv_idx[i]= mod_mat->triv_idx[i];
crt_mat->triv_pos[i]= mod_mat->triv_pos[i];
}
for(long i = 0; i < crt_mat->nrows; i++){
crt_mat->dense_idx[i] = mod_mat->dense_idx[i];
crt_mat->dst[i] = mod_mat->dst[i];
}
}
/* Initialization of sparse mpz fglm matrix */
static inline void mpz_matfglm_initset(mpz_matfglm_t mpz_mat,
sp_matfglm_t *mod_mat){
mpz_mat->ncols = mod_mat->ncols;
mpz_mat->nrows = mod_mat->nrows;
mpz_mat->dense_mat = calloc(mpz_mat->ncols*mpz_mat->nrows,
sizeof(mpz_t));
uint64_t sz = mpz_mat->nrows * mpz_mat->ncols;
for(uint64_t i = 0; i < sz; i++){
mpz_init_set_ui(mpz_mat->dense_mat[i], 0);
}
mpz_mat->denoms = calloc(mpz_mat->nrows,
sizeof(mpz_t));
for(uint64_t i = 0; i < mpz_mat->nrows; i++){
mpz_init_set_ui(mpz_mat->denoms[i], 1);
}
long diff = mpz_mat->ncols - mpz_mat->nrows;
mpz_mat->triv_idx = calloc(diff, sizeof(uint32_t));
mpz_mat->triv_pos = calloc(diff, sizeof(uint32_t));
mpz_mat->dense_idx = calloc(mpz_mat->nrows, sizeof(uint32_t));
mpz_mat->dst = calloc(mpz_mat->nrows, sizeof(uint32_t));
for(long i = 0; i < diff; i++){
mpz_mat->triv_idx[i]= mod_mat->triv_idx[i];
mpz_mat->triv_pos[i]= mod_mat->triv_pos[i];
}
for(long i = 0; i < mpz_mat->nrows; i++){
mpz_mat->dense_idx[i] = mod_mat->dense_idx[i];
mpz_mat->dst[i] = mod_mat->dst[i];
}
}
/* Initialization of sparse fglm matrix for rational reconstruction */
static inline void mpq_matfglm_initset(mpq_matfglm_t mpq_mat,
sp_matfglm_t *mod_mat){
mpq_mat->ncols = mod_mat->ncols;
mpq_mat->nrows = mod_mat->nrows;
mpq_mat->dense_mat = calloc(2*mpq_mat->ncols*mpq_mat->nrows,
sizeof(mpz_t));
uint64_t nc = 2*mpq_mat->ncols;
for(uint32_t i = 0; i < mpq_mat->nrows; i++){
uint64_t c = 2*i*mpq_mat->ncols;
for(uint32_t j = 0; j < nc; j++){
mpz_init_set_ui(mpq_mat->dense_mat[c+j], 0);
j++;
mpz_init_set_ui(mpq_mat->dense_mat[c+j], 1);
}
}
long diff = mpq_mat->ncols - mpq_mat->nrows;
mpq_mat->triv_idx = calloc(diff, sizeof(uint32_t));
mpq_mat->triv_pos = calloc(diff, sizeof(uint32_t));
mpq_mat->dense_idx = calloc(mpq_mat->nrows, sizeof(uint32_t));
mpq_mat->dst = calloc(mpq_mat->nrows, sizeof(uint32_t));
for(long i = 0; i < diff; i++){
mpq_mat->triv_idx[i]= mod_mat->triv_idx[i];
mpq_mat->triv_pos[i]= mod_mat->triv_pos[i];
}
for(long i = 0; i < mpq_mat->nrows; i++){
mpq_mat->dense_idx[i] = mod_mat->dense_idx[i];
mpq_mat->dst[i] = mod_mat->dst[i];
}
}
static inline void trace_det_initset(trace_det_fglm_mat_t trace_det,
uint32_t trace_mod, uint32_t det_mod,
uint32_t tridx, uint32_t detidx){
mpz_init_set_ui(trace_det->trace_crt, trace_mod);
mpz_init_set_ui(trace_det->det_crt, det_mod);
mpz_init_set_ui(trace_det->trace_num, 0);
mpz_init_set_ui(trace_det->trace_den, 1);
mpz_init_set_ui(trace_det->det_num, 0);
mpz_init_set_ui(trace_det->det_den, 1);
mpz_init(trace_det->tmp);
trace_det->check_trace = 0;
trace_det->check_det = 0;
trace_det->done_trace = 0;
trace_det->done_det = 0;
trace_det->trace_idx = tridx;
trace_det->det_idx = detidx;
}
static inline void trace_det_clear(trace_det_fglm_mat_t trace_det){
mpz_clear(trace_det->trace_crt);
mpz_clear(trace_det->det_crt);
mpz_clear(trace_det->trace_num);
mpz_clear(trace_det->trace_den);
mpz_clear(trace_det->det_num);
mpz_clear(trace_det->det_den);
mpz_clear(trace_det->tmp);
}
static inline void crt_lift_trace_det(trace_det_fglm_mat_t trace_det,
uint32_t trace_mod, uint32_t det_mod,
mpz_t modulus, mpz_t prod,
uint32_t prime){
mpz_CRT_ui(trace_det->trace_crt, trace_det->trace_crt,
modulus, trace_mod, prime, prod, trace_det->tmp, 1);
mpz_CRT_ui(trace_det->det_crt, trace_det->det_crt,
modulus, det_mod, prime, prod, trace_det->tmp, 1);
}
static inline void crt_lift_dense_rows(mpz_t *rows, uint32_t *mod_rows,
const uint64_t sz,
mpz_t modulus,
mpz_t prod,
int32_t prime,
mpz_t tmp,
const int nthrds){
len_t i;
for(i = 0; i < sz; i++){
mpz_CRT_ui(rows[i], rows[i], modulus,
mod_rows[i], prime, prod, tmp, 1);
}
}
static inline void crt_lift_mat(crt_mpz_matfglm_t mat, sp_matfglm_t *mod_mat,
mpz_t modulus, mpz_t prod_crt,
const int32_t prime,
mpz_t tmp,
const int nthrds){
/*assumes prod_crt = modulus * prime */
const uint64_t sz = mat->nrows * mat->ncols;
crt_lift_dense_rows(mat->dense_mat, mod_mat->dense_mat,
sz, modulus, prod_crt, prime, tmp, nthrds);
}
static inline void build_mpz_matrix(mpq_matfglm_t mpq_mat, mpz_matfglm_t mpz_mat){
fprintf(stderr, "TODO\n");
}
static inline int rat_recon_trace_det(trace_det_fglm_mat_t trace_det,
rrec_data_t recdata, mpz_t modulus,
mpz_t rnum, mpz_t rden){
int b = ratrecon(rnum, rden, trace_det->trace_crt, modulus, recdata);
if(b == 1){
mpz_set(trace_det->trace_num, rnum);
mpz_set(trace_det->trace_den, rden);
}
else
{
return 0;
}
b = ratrecon(rnum, rden, trace_det->det_crt, modulus, recdata);
if(b == 1){
mpz_set(trace_det->det_num, rnum);
mpz_set(trace_det->det_den, rden);
}
else{
return 0;
}
return 1;
}
static inline int check_trace(trace_det_fglm_mat_t trace_det,
uint32_t trace_mod,
uint32_t prime){
uint32_t lc = mpz_fdiv_ui(trace_det->trace_den, prime);
lc = mod_p_inverse_32(lc, prime);
uint64_t c = mpz_fdiv_ui(trace_det->trace_num, prime);
c *= lc;
c = c % prime;
return (c == trace_mod);
}
static inline int check_det(trace_det_fglm_mat_t trace_det,
uint32_t det_mod,
uint32_t prime){
uint32_t lc = mpz_fdiv_ui(trace_det->det_den, prime);
lc = mod_p_inverse_32(lc, prime);
uint64_t c = mpz_fdiv_ui(trace_det->det_num, prime);
c *= lc;
c = c % prime;
return (c==det_mod);
}
#define NEW 1
static inline int64_t rat_recon_dense_rows(mpq_matfglm_t mpq_mat,
crt_mpz_matfglm_t crt_mat,
mpz_matfglm_t mpz_mat,
mpz_t modulus, rrec_data_t rdata,
mpz_t rnum, mpz_t rden,
long *matrec){
const uint32_t nrows = crt_mat->nrows;
const uint32_t ncols = crt_mat->ncols;
int64_t cnt = 0;
#ifdef NEW
mpz_t lcm, coef;
mpz_init(lcm);
mpz_init(coef);
#endif
for(uint32_t i = 0; i < nrows; i++){
uint64_t c = i*ncols;
#ifdef NEW
mpz_set_ui(lcm, 1);
#endif
for(uint32_t j = 0; j < ncols; j++){
int b = 1;
if(*matrec <= c+j){
#ifdef NEW
mpz_mul(coef, crt_mat->dense_mat[c+j], lcm);
mpz_mod(coef, coef, modulus);
b = ratrecon(rnum, rden, coef,
modulus, rdata);
#else
b = ratrecon(rnum, rden, crt_mat->dense_mat[c+j],
modulus, rdata);
#endif
if(b == 1){
mpz_set(mpq_mat->dense_mat[2*c+2*j], rnum);
mpz_set(mpq_mat->dense_mat[2*c+2*j+1], rden);
mpz_lcm(lcm, lcm, rden);
cnt++;
}
else{
if(cnt > *matrec+1){
fprintf(stderr, "<%.2f%%>", (100*(((double)cnt)/ncols))/nrows );
}
*matrec = c;
#ifdef NEW
mpz_clear(lcm);
mpz_clear(coef);
#endif
return c;
}
}
else{
cnt++;
}
}
}
fprintf(stderr, "<100.0%%>\n");
build_mpz_matrix(mpq_mat, mpz_mat);
*matrec = cnt;
#ifdef NEW
mpz_clear(coef);
mpz_clear(lcm);
#endif
return cnt;
}
void initialize_rrec_data(rrec_data_t recdata){
mpz_init(recdata->r0);
mpz_init(recdata->r1);
mpz_init(recdata->t0);
mpz_init(recdata->t1);
mpz_init(recdata->q);
mpz_init(recdata->tmp);
mpz_init(recdata->N);
mpz_init(recdata->D);
}
void free_rrec_data(rrec_data_t recdata){
mpz_clear(recdata->r0);
mpz_clear(recdata->r1);
mpz_clear(recdata->t0);
mpz_clear(recdata->t1);
mpz_clear(recdata->q);
mpz_clear(recdata->tmp);
mpz_clear(recdata->N);
mpz_clear(recdata->D);
}