Annotation of OpenXM_contrib2/asir2018/engine/H.c, Revision 1.3
1.1 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.3 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2018/engine/H.c,v 1.2 2018/09/28 08:20:28 noro Exp $
1.1 noro 49: */
50: #include "ca.h"
51: #include "inline.h"
52: #include "base.h"
53: #include <math.h>
54:
55: #if 1
56: #define Mulum mulum
57: #define Divum divum
58: #define Mulsum mulsum
59: #define Gcdum gcdum
60: #endif
61:
62: #define FCTR 0
63: #define SQFR 1
64: #define DDD 2
65: #define NEWDDD 3
66:
67: LUM LUMALLOC();
68:
69: void berle(int index,int count,P f,ML *listp)
70: {
71: UM wf,wf1,wf2,wfs,gcd;
72: ML flist;
73: int fn,fn1,fm,m,n,fhd;
74: register int i,j,inv,hd,*ptr,*ptr1;
75:
76: n = UDEG(f);
77: wf = W_UMALLOC(n); wf1 = W_UMALLOC(n); wf2 = W_UMALLOC(n);
78: wfs = W_UMALLOC(n); gcd = W_UMALLOC(n);
79: for ( j = 0, fn = n + 1; (j < count) && (fn > 1); ) {
80: m = sprime[index++];
81: if ( !remqi((Q)UCOEF(f),m) )
82: continue;
83: ptoum(m,f,wf); cpyum(wf,wf1);
84: diffum(m,wf1,wf2); gcdum(m,wf1,wf2,gcd);
85: if ( DEG(gcd) > 0 )
86: continue;
87: hd = COEF(wf)[n]; inv = invm(hd,m);
88: for ( i = n, ptr = COEF(wf); i >= 0; i-- )
89: ptr[i] = ( ptr[i] * inv ) % m;
90: fn1 = berlecnt(m,wf);
91: if ( fn1 < fn ) {
92: fn = fn1; fm = m; fhd = hd;
93: for ( i = n, ptr = COEF(wf), ptr1 = COEF(wfs); i >= 0; i-- )
94: ptr1[i] = ptr[i];
95: }
96: j++;
97: }
98: DEG(wfs) = n;
99: *listp = flist = MLALLOC(fn); flist->n = fn; flist->mod = fm;
100: /* berlemain(fm,wfs,(UM *)flist->c); */
101: if ( fm == 2 )
102: berlemain(fm,wfs,(UM *)flist->c);
103: else
104: newddd(fm,wfs,(UM *)flist->c);
105: for ( i = DEG((UM)(flist->c[0])),
106: ptr = COEF((UM)(flist->c[0])),
107: hd = fhd, m = fm; i >= 0; i-- )
108: ptr[i] = ( ptr[i] * hd ) % m;
109: }
110:
111: int berlecnt(int mod,UM f)
112: {
113: register int i,j,**c;
114: int d,dr,n;
115: UM w,q;
116: int **almat();
117:
118: n = DEG(f); c = almat(n,n);
119: w = W_UMALLOC(mod + n); q = W_UMALLOC(mod + n);
120: for ( i = 1; ( d = ( mod * i ) ) < n; i++ )
121: c[d][i - 1] = 1;
122: DEG(w) = d; COEF(w)[d] = 1;
123: for ( j = d - 1; j >= 0; j-- )
124: COEF(w)[j] = 0;
125: for ( ; ( i < n ) && ( ( dr = divum(mod,w,f,q) ) != -1 ); i++ ) {
126: for ( j = dr; j >= 0; j-- )
127: COEF(w)[j + mod] = c[j][i - 1] = COEF(w)[j];
128: for ( j = mod - 1; j >= 0; j-- )
129: COEF(w)[j] = 0;
130: DEG(w) = dr + mod;
131: }
132: for ( i = 1; i < n; i++ )
133: c[i][i - 1] = ( c[i][i - 1] + mod - 1 ) % mod;
134: return berlecntmain(mod,n,n-1,c);
135: }
136:
137: /* XXX berlecntmain should not be used for large mod */
138:
139: int berlecntmain(int mod,int n,int m,int **c)
140: {
141: register int *p1,*p2,i,j,k,l,a;
142: int *tmp,inv;
143: int cfs;
144:
145: for ( cfs = 1, j = k = 0; j < m; j++ ) {
146: for ( i = k; ( n > i ) && ( c[i][j] == 0 ); i++ );
147: if ( i == n ) {
148: cfs++; continue;
149: }
150: if ( i != k ) {
151: tmp = c[i]; c[i] = c[k]; c[k] = tmp;
152: }
153: p1 = c[k]; inv = invm((p1[j] + mod) % mod,mod);
154: for ( l = j; l < m; l++ )
155: p1[l] = ( p1[l] * inv ) % mod;
156: for ( i = k + 1; i < n; c[i][j] = 0, i++ )
157: if ( i != k && ( a = -c[i][j] ) )
158: for ( l = j + 1, p2 = c[i]; l < m; l++ )
159: p2[l] = (a*p1[l] + p2[l]) % mod;
160: k++;
161: }
162: return ( cfs );
163: }
164:
165: UM *berlemain(int mod,UM f,UM *fp)
166: {
167: UM wg,ws,wf,f0,gcd,q;
168: int n;
169: register int i;
170:
171: n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
172: if ( DEG(wg) <= 0 ) {
173: f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
174: return ( fp );
175: }
176: f0 = W_UMALLOC(n); cpyum(f,f0);
177: ws = W_UMALLOC(n); wf = W_UMALLOC(n);
178: q = W_UMALLOC(n); gcd = W_UMALLOC(n);
179: for ( i = 0; i < mod; i++ ) {
180: cpyum(f0,wf); cpyum(wg,ws);
181: COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
182: gcdum(mod,wf,ws,gcd);
183: if ( DEG(gcd) > 0 ) {
184: if ( DEG(gcd) < n ) {
185: divum(mod,f0,gcd,q); f0 = q; fp = berlemain(mod,gcd,fp);
186: }
187: break;
188: }
189: }
190: fp = berlemain(mod,f0,fp);
191: return ( fp );
192: }
193:
194: void hensel(int index,int count,P f,ML *listp)
195: {
196: register int i,j;
197: int q,n,bound;
198: int *p;
199: int **pp;
200: ML blist,clist,bqlist,cqlist,rlist;
201: UM *b;
202: LUM fl,tl;
203: LUM *l;
204:
205: if ( UDEG(f) == 1 ) {
206: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
207: return;
208: }
209: berle(index,count,f,&blist);
210: if ( blist->n == 1 ) {
211: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
212: return;
213: }
214: gcdgen(f,blist,&clist); henprep(f,blist,clist,&bqlist,&cqlist);
215: n = bqlist->n; q = bqlist->mod;
216: bqlist->bound = cqlist->bound = bound = mignotte(q,f);
217: if ( bound == 1 ) {
218: *listp = rlist = MLALLOC(n);
219: rlist->n = n; rlist->mod = q; rlist->bound = bound;
220: for ( i = 0, b = (UM *)bqlist->c, l = (LUM *)rlist->c; i < n; i++ ) {
221: tl = LUMALLOC(DEG(b[i]),1); l[i] = tl; p = COEF(b[i]);
222: for ( j = 0, pp = COEF(tl); j <= DEG(tl); j++ )
223: pp[j][0] = p[j];
224: }
225: } else {
226: W_LUMALLOC((int)UDEG(f),bound,fl);
227: ptolum(q,bound,f,fl); henmain(fl,bqlist,cqlist,listp);
228: }
229: }
230:
231: void hensel2(int index,int count,P f,ML *listp)
232: {
233: register int i,j;
234: int mod,q,n,bound,dx;
235: ML blist,clist,bqlist,cqlist,rlist;
236: UM fm,qfm,gm,qgm,hm,qhm,qam,qbm,w;
237: UM *b;
238: LUM fl,tl;
239: int k;
240:
241: dx = UDEG(f);
242: if ( dx == 1 ) {
243: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
244: return;
245: }
246: berle(index,count,f,&blist);
247: n = blist->n;
248: mod = blist->mod;
249:
250: if ( n == 1 ) {
251: *listp = blist = MLALLOC(1); blist->n = 1; blist->c[0] = 0;
252: return;
253: }
254:
255: /* find k s.t. mod^k <= 2^27 < mod^(k+1); set q = mod^k */
256: for ( k = 1, q = mod; q <= ((1<<27)/mod); q *= mod, k++ );
257:
258: /* mignotte bound */
259: bound = mignotte(q,f);
260:
261: *listp = rlist = MLALLOC(n);
262: rlist->n = n;
263: rlist->mod = q;
264: rlist->bound = bound;
265:
266: if ( bound == 1 ) {
267: gcdgen(f,blist,&clist);
268: henprep(f,blist,clist,&bqlist,&cqlist);
269:
270: for ( i = 0, b = (UM *)bqlist->c; i < n; i++ ) {
271: COEF(rlist)[i] = tl = LUMALLOC(DEG(b[i]),1);
272: for ( j = 0; j <= DEG(tl); j++ )
273: COEF(tl)[j][0] = COEF(b[i])[j];
274: COEF(rlist)[i] = tl;
275: }
276: } else {
277: /* fl = f mod q */
278: fl = LUMALLOC(dx,bound);
279: ptolum(q,bound,f,fl);
280: /* fm = f mod mod */
281: fm = W_UMALLOC(dx);
282: ptoum(mod,f,fm);
283: /* fm = f mod q */
284: qfm = W_UMALLOC(dx);
285: ptoum(q,f,qfm);
286:
287: gm = W_UMALLOC(dx); qgm = W_UMALLOC(dx);
288: hm = W_UMALLOC(dx); qhm = W_UMALLOC(dx);
289: qam = W_UMALLOC(dx); qbm = W_UMALLOC(dx);
290: w = W_UMALLOC(dx);
291: for ( i = 0; i < n-1; i++ ) {
292: cpyum(COEF(blist)[i],gm);
293: cpyum(fm,w);
294: divum(mod,w,gm,hm);
295:
296: /* find am,bm s.t. qam*qgm+qbm*qhm=1 mod q, qgm=gm mod mod, qhm=hm mod mod */
297: henprep2(mod,q,k,qfm,gm,hm,qgm,qhm,qam,qbm);
298:
299: henmain2(fl,qgm,qhm,qam,qbm,q,bound,&tl);
300: rlist->c[i] = (pointer)tl;
301: cpyum(hm,fm);
302: cpyum(qhm,qfm);
303: }
304: rlist->c[i] = fl;
305: }
306: }
307:
308: /*
309: f = g0*h0 mod m -> f = gk*hk mod m^(bound), f is replaced by hk
310: */
311:
312: void henmain2(LUM f,UM g0,UM h0,UM a0,UM b0,int m,int bound,LUM *gp)
313: {
314: int n,dg,dh,i,k,j,dg1,dh1;
315: UM wu,wr,ws,wt,q,wh1,wg1,wc,wd,we,wz;
316: LUM wb0,wb1,wb2,fk,gk,hk;
317:
318: n = DEG(f); dg = DEG(g0); dh = DEG(h0);
319:
320: W_LUMALLOC(n,bound,wb0);
321: W_LUMALLOC(n,bound,wb1);
322: W_LUMALLOC(n,bound,wb2);
323:
324: wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n);
325: wr = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
326: q = W_UMALLOC(2*n);
327: wh1 = W_UMALLOC(2*n); wg1 = W_UMALLOC(2*n);
328:
329: /* gk = g0 */
330: gk = LUMALLOC(n,bound);
331: DEG(gk) = dg;
332: for ( i = 0; i <= dg; i++ )
333: COEF(gk)[i][0] = COEF(g0)[i];
334:
335: /* hk = h0 */
336: W_LUMALLOC(n,bound,hk);
337: DEG(hk) = dh;
338: for ( i = 0; i <= dh; i++ )
339: COEF(hk)[i][0] = COEF(h0)[i];
340:
341: /* fk = gk*hk */
342: W_LUMALLOC(n,bound,fk);
343: mullum(m,bound,gk,hk,fk);
344:
345: wc = W_UMALLOC(2*n); wd = W_UMALLOC(2*n);
346: we = W_UMALLOC(2*n); wz = W_UMALLOC(2*n);
347:
348: #if 0
349: mulum(m,a0,g0,wc);
350: mulum(m,b0,h0,wd);
351: addum(m,wc,wd,wz);
352: if ( DEG(wz) != 0 || COEF(wz)[0] != 1 )
353: error("henmain2 : cannot happen(extgcd)");
354: #endif
355:
356: #if 1
357: fprintf(stderr,"bound=%d\n",bound);
358: #endif
359: for ( k = 1; k < bound; k++ ) {
360: #if 1
361: fprintf(stderr,".");
362: #endif
363:
364: #if defined(VISUAL) || defined(__MINGW32__)
365: check_intr();
366: #endif
367: /* at this point, f = gk*hk mod y^k */
368:
369: #if 0
370: for ( j = 0; j < k; j++ )
371: for ( i = 0; i <= n; i++ )
372: if ( COEF(f)[i][j] != COEF(f)[i][j] )
373: error("henmain2 : cannot happen(f=fk)");
374: #endif
375:
376: /* wt = (f-gk*hk)/y^k */
377: for ( i = 0; i <= n; i++ )
378: COEF(ws)[i] = COEF(f)[i][k];
379: degum(ws,n);
380: for ( i = 0; i <= n; i++ )
381: COEF(wu)[i] = COEF(fk)[i][k];
382: degum(wu,n);
383: subum(m,ws,wu,wt);
384:
385: /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
386: mulum(m,a0,wt,wh1); DEG(wh1) = divum(m,wh1,h0,q);
387: mulum(m,wh1,g0,wc); subum(m,wt,wc,wd); DEG(wd) = divum(m,wd,h0,wg1);
388:
389: /* check */
390: #if 0
391: if ( DEG(wd) >= 0 || DEG(wg1) > dg )
392: error("henmain2 : cannot happen(adj)");
393:
394: mulum(m,wg1,h0,wc); mulum(m,wh1,g0,wd); addum(m,wc,wd,we);
395: subum(m,we,wt,wz);
396: if ( DEG(wz) >= 0 )
397: error("henmain2 : cannot happen(coef)");
398: #endif
399:
400: /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod m^bound */
401:
402: /* wb0 = wh1*y^k */
403: clearlum(n,bound,wb0);
404: DEG(wb0) = dh1 = DEG(wh1);
405: for ( i = 0; i <= dh1; i++ )
406: COEF(wb0)[i][k] = COEF(wh1)[i];
407:
408: /* wb2 = gk*wb0 mod y^bound */
409: clearlum(n,bound,wb2);
410: mullum(m,bound,gk,wb0,wb2);
411:
412: /* fk += wb2 */
413: addtolum(m,bound,wb2,fk);
414:
415: /* wb1 = wg1*y^k */
416: clearlum(n,bound,wb1);
417: DEG(wb1) = dg1 = DEG(wg1);
418: for ( i = 0; i <= n; i++ )
419: COEF(wb1)[i][k] = COEF(wg1)[i];
420:
421: /* wb2 = hk*wb1 mod y^bound */
422: clearlum(n,bound,wb2);
423: mullum(m,bound,hk,wb1,wb2);
424:
425: /* fk += wb2 */
426: addtolum(m,bound,wb2,fk);
427:
428: /* fk += wg1*wh1*y^(2*k) mod y^bound) */
429: if ( 2*k < bound ) {
430: clearlum(n,bound,wb2);
431: mullum(m,bound,wb0,wb1,wb2);
432: addtolum(m,bound,wb2,fk);
433: }
434:
435: /* gk += wg1*y^k, hk += wh1*y^k */
436: for ( i = 0; i <= DEG(wg1); i++ )
437: COEF(gk)[i][k] = COEF(wg1)[i];
438: for ( i = 0; i <= DEG(wh1); i++ )
439: COEF(hk)[i][k] = COEF(wh1)[i];
440: }
441: #if 1
442: fprintf(stderr,"\n");
443: #endif
444: *gp = gk;
445: clearlum(n,bound,f);
446: DEG(f) = dh;
447: for ( i = 0; i <= dh; i++ )
448: for ( j = 0; j < bound; j++ )
449: COEF(f)[i][j] = COEF(hk)[i][j];
450: }
451:
452: void clearlum(int n,int bound,LUM f)
453: {
454: int i;
455:
456: for ( i = 0; i <= n; i++ )
457: bzero(COEF(f)[i],bound*sizeof(int));
458: }
459:
460: /* g += f */
461:
462: void addtolum(int m,int bound,LUM f,LUM g)
463: {
464: int n,i;
465:
466: n = DEG(f);
467: for ( i = 0; i <= n; i++ )
468: addpadic(m,bound,(unsigned int *)COEF(f)[i],(unsigned int *)COEF(g)[i]);
469: }
470:
471: void hsq(int index,int count,P f,int *nindex,DCP *dcp)
472: {
473: register int i,j,k;
474: register int **pp,**fpp;
475: register int *px,*py;
476: int **wpp;
1.3 ! noro 477: int n,dr,m,b,e,np,dt;
! 478: unsigned int tmp;
1.1 noro 479: LUM fpa,wb0,wb1,lcpa,tpa,tlum;
480: struct oDUM *dct;
481: UM wt,wq0,wq,wr,wm,wm0,wa,ws,wb;
482: LUM *llist,*ll;
483: UM *dlist,*l,*c;
484: ML list,fp,cfp;
485: DCP dc;
486:
487: sqfrum(index,count,f,nindex,&dct,&fp);
488: np = fp->n; m = fp->mod;
489: if ( ( np == 1 ) && ( dct[0].n == 1 ) ) {
490: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0; *dcp = dc;
491: return;
492: }
493: for ( i = 0, dt = 0; i < np; i++ )
494: dt = MAX(DEG(dct[i].f),dt);
495: b = mig(m,dt,f); fp->bound = b;
496: if ( np == 1 ) {
497: nthrootchk(f,dct,fp,dcp);
498: return;
499: }
500: list = W_MLALLOC(np); list->n = np; list->mod = m; list->bound = 1;
501: for ( i = 0, ll = (LUM *)list->c; i < np; i++ ) {
502: W_LUMALLOC(DEG(dct[i].f),b,ll[i]);
503: for ( j = 0, px = COEF(dct[i].f), pp = COEF(ll[i]);
504: j <= DEG(ll[i]); j++ )
505: pp[j][0] = px[j];
506: }
507: dtestsql(f,list,dct,&dc);
508: if ( dc ) {
509: *dcp = dc;
510: return;
511: }
512: n = UDEG(f);
513: W_LUMALLOC(n,b,fpa); W_LUMALLOC(0,b,lcpa);
514: W_LUMALLOC(n,b,wb0); W_LUMALLOC(n,b,wb1);
515: W_LUMALLOC(n,b,tpa);
516: wt = W_UMALLOC(n); ws = W_UMALLOC(n);
517: wr = W_UMALLOC(n);
518: wq = W_UMALLOC(2*n); wq0 = W_UMALLOC(n);
519: wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
520: wa = W_UMALLOC(2*n);
521: ptolum(m,b,f,fpa); DEG(lcpa) = 0;
522: for ( i = 0, pp = COEF(lcpa), fpp = COEF(fpa); i < b; i++ )
523: pp[0][i] = fpp[n][i];
524: gcdgen(f,fp,&cfp);
525: llist = (LUM *) ALLOCA(np*sizeof(LUM));
526: dlist = (UM *) ALLOCA(np*sizeof(UM));
527: l = (UM *)fp->c; c = (UM *)cfp->c;
528: for ( i = 0; i < np; i++ ) {
529: W_LUMALLOC(DEG(l[i]),b,llist[i]);
530: for ( j = DEG(l[i]), pp = COEF(llist[i]), px = COEF(l[i]); j >= 0; j-- )
531: pp[j][0] = px[j];
532: if ( ( e = dct[i].n ) != 1 ) {
533: wb = dct[i].f;
534: dlist[i] = W_UMALLOC(DEG(wb)*e); cpyum(l[i],dlist[i]);
535: divum(m,dlist[i],wb,wq); DEG(dlist[i])= DEG(wq);
536: for ( k = 0; k <= DEG(wq); k++ )
537: COEF(dlist[i])[k] = dmb(m,COEF(wq)[k],e,&tmp);
538: }
539: }
540: for ( i = 1; i < b; i++ ) {
541: mullum(m,i+1,lcpa,llist[0],wb0);
542: for ( j = 1; j < np; j++ ) {
543: mullum(m,i+1,llist[j],wb0,wb1);
544: tlum = wb0; wb0 = wb1; wb1 = tlum;
545: }
546: for ( j = n, px = COEF(wt), pp = COEF(fpa), wpp = COEF(wb0);
547: j >= 0; j-- )
548: px[j] = ( pp[j][i] - wpp[j][i] + m ) % m;
549: degum(wt,n);
550: for ( j = n, px = COEF(wq0); j >= 0; j-- )
551: px[j] = 0;
552: for ( j = 1; j < np; j++ ) {
553: mulum(m,wt,c[j],wm); dr = divum(m,wm,l[j],wq);
554: for ( k = DEG(wq), px = COEF(wq0), py = COEF(wq); k >= 0; k-- )
555: px[k] = ( px[k] + py[k] ) % m;
556: for ( k = dr, pp = COEF(llist[j]), px = COEF(wm); k >= 0; k-- )
557: pp[k][i] = px[k];
558: }
559: degum(wq0,n); mulum(m,wq0,l[0],wm);
560: mulum(m,wt,c[0],wm0); addum(m,wm,wm0,wa);
561: for ( j = DEG(wa), pp = COEF(llist[0]), px = COEF(wa); j >= 0; j-- )
562: pp[j][i] = px[j];
563: for ( j = n, px = COEF(wq0); j >= 0; j-- )
564: px[j] = 0;
565: for ( j = 0; j < np; j++ )
566: if ( dct[j].n == 1 )
567: for ( k = 0,
568: pp = COEF(llist[j]),
569: wpp = COEF(((LUM *)list->c)[j]);
570: k <= DEG(llist[j]); k++ )
571: wpp[k][i] = pp[k][i];
572: else {
573: pwrlum(m,i+1,((LUM *)list->c)[j],dct[j].n,tpa);
574: for ( k = 0,
575: pp = COEF(llist[j]),
576: wpp = COEF(tpa);
577: k <= DEG(l[j]); k++ )
578: COEF(wt)[k] = (pp[k][i]-wpp[k][i]+m)%m;
579: degum(wt,DEG(l[j])); dr = divum(m,wt,dlist[j],ws);
580: if ( dr >= 0 ) {
581: *dcp = 0;
582: return;
583: } else
584: for ( k = 0,
585: pp = COEF(((LUM *)list->c)[j]);
586: k <= DEG(ws); k++ )
587: pp[k][i] = COEF(ws)[k];
588: }
589: list->bound = i+1; dtestsql(f,list,dct,&dc);
590: if ( dc ) {
591: *dcp = dc;
592: return;
593: }
594: }
595: *dcp = 0;
596: }
597:
598: void gcdgen(P f,ML blist,ML *clistp)
599: {
600: register int i;
601: int n,d,mod,np;
602: UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
603: UM *in,*out;
604: ML clist;
605:
606: n = UDEG(f); mod = blist->mod; np = blist->n;
607: d = 2*n;
608: q = W_UMALLOC(d); wf = W_UMALLOC(d);
609: wm = W_UMALLOC(d); wx = W_UMALLOC(d);
610: wy = W_UMALLOC(d); wu = W_UMALLOC(d);
611: wv = W_UMALLOC(d); wg = W_UMALLOC(d);
612: wa = W_UMALLOC(d); wb = W_UMALLOC(d);
613: ptoum(mod,f,wf); DEG(wg) = 0; COEF(wg)[0] = 1;
614: *clistp = clist = MLALLOC(np); clist->mod = mod; clist->n = np;
615: for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
616: divum(mod,wf,in[i],q); tum = wf; wf = q; q = tum;
617: cpyum(wf,wx); cpyum(in[i],wy);
618: eucum(mod,wx,wy,wa,wb); mulum(mod,wa,wg,wm);
619: DEG(wm) = divum(mod,wm,in[i],q); out[i] = UMALLOC(DEG(wm));
620: cpyum(wm,out[i]); mulum(mod,q,wf,wu);
621: mulum(mod,wg,wb,wv); addum(mod,wu,wv,wg);
622: }
623: }
624:
625: /* find a,b s.t. qa*qg+qb*qh=1 mod q, qg=g mod mod, qh=h mod mod */
626: /* q = mod^k */
627:
628: void henprep2(int mod,int q,int k,UM f,UM g,UM h,UM qg,UM qh,UM qa,UM qb)
629: {
630: int n;
631: UM wg,wh,wa,wb;
632: ML bl,cl,bql,cql;
633: P ff;
634:
635: n = DEG(f);
636: wg = W_UMALLOC(2*n); wh = W_UMALLOC(2*n);
637: wa = W_UMALLOC(2*n); wb = W_UMALLOC(2*n);
638: cpyum(g,wg); cpyum(h,wh);
639:
640: /* wa*g+wb*h = 1 mod mod */
641: eucum(mod,wg,wh,wa,wb);
642:
643: #if 0
644: /* check */
645: wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
646: mulum(mod,wa,g,wt);
647: mulum(mod,wb,h,ws);
648: addum(mod,wt,ws,wu);
649: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
650: error("henprep 1");
651: #endif
652:
653: bl = MLALLOC(2); bl->n = 2; bl->mod = mod; bl->c[0] = g; bl->c[1] = h;
654: cl = MLALLOC(2); cl->n = 2; cl->mod = mod; cl->c[0] = wb; cl->c[1] = wa;
655: umtop(CO->v,f,&ff); /* XXX */
656: henprep(ff,bl,cl,&bql,&cql); /* XXX */
657:
658: cpyum(bql->c[0],qg); cpyum(bql->c[1],qh);
659: cpyum(cql->c[0],qb); cpyum(cql->c[1],qa);
660:
661: #if 0
662: /* check */
663: mulum(q,qa,qg,wt);
664: mulum(q,qb,qh,ws);
665: addum(q,wt,ws,wu);
666: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
667: error("henprep 2");
668: #endif
669: }
670:
671: /*
672: henprep(f,blist,clist,&bqlist,&cqlist);
673: */
674:
675: void henprep(P f,ML blist,ML clist,ML *bqlistp,ML *cqlistp)
676: {
677: register int i,j,k,*px,*py,*pz;
678: int n,pmax,dr,tmp,p,p1,mod,np,b,q;
679: UM w,wm,wn,wa,wt,wq,wf,quot,tum,*in,*inc,*out,*outc;
680: ML bqlist,cqlist;
681:
682: n = UDEG(f); p = mod = blist->mod; np = blist->n;
683: /* for ( b = 1, q = mod; q <= (unsigned int)(LBASE / (L)mod); q *= mod, b++ ); */
684: for ( b = 1, q = mod; q <= ((1<<27) / mod); q *= mod, b++ );
685: w = W_UMALLOC(n); ptoum(q,f,w);
686: wm = W_UMALLOC(2*n); wn = W_UMALLOC(2*n);
687: wa = W_UMALLOC(2*n); wt = W_UMALLOC(2*n);
688: wq = W_UMALLOC(2*n); wf = W_UMALLOC(2*n);
689: quot = W_UMALLOC(2*n);
690: *bqlistp = bqlist = MLALLOC(np); *cqlistp = cqlist = MLALLOC(np);
691: for ( i = 0; i < n+2; i++ )
692: COEF(wq)[i] = 0;
693: for ( i = 0,
694: in = (UM *)blist->c, inc = (UM *)clist->c,
695: out = (UM *)bqlist->c, outc = (UM *)cqlist->c;
696: i < np; i++ ) {
697: out[i] = C_UMALLOC(n+1); cpyum(in[i],out[i]);
698: outc[i] = C_UMALLOC(n+1); cpyum(inc[i],outc[i]);
699: }
700: for ( pmax = 1, i = b; i > 0; i-- )
701: pmax *= mod;
702: for ( i = 1; i < b; i++, p = p1 ) {
703: cpyum(out[0],wm);
704: for ( j = 1; j < np; j++ ) {
705: mulum(pmax,wm,out[j],wn);
706: tum = wm; wm = wn; wn = tum;
707: }
708: for ( j = n, px = COEF(w), py = COEF(wm), pz = COEF(wt); j >= 0; j-- ) {
709: tmp = ( ( px[j] - py[j] ) / p ) % mod;
710: pz[j] = ( tmp >= 0? tmp : tmp + mod );
711: }
712: degum(wt,n);
713: for ( j = 1; j < np; j++ ) {
714: mulum(mod,wt,inc[j],wm); dr = divum(mod,wm,in[j],quot);
715: for ( k = DEG(quot); k >= 0; k-- )
716: COEF(wq)[k] = ( COEF(wq)[k] + COEF(quot)[k] ) % mod;
717: for ( k = dr, px = COEF(out[j]), py = COEF(wm); k >= 0; k-- )
718: px[k] += p * py[k];
719: }
720: degum(wq,n); mulum(mod,wq,in[0],wm);
721: mulum(mod,wt,inc[0],wn); addum(mod,wm,wn,wa);
722: for ( j = DEG(wa), px = COEF(out[0]), py = COEF(wa); j >= 0; j-- )
723: px[j] += p * py[j];
724: for ( j = n, px = COEF(wq); j >= 0; j-- )
725: px[j] = 0;
726: p1 = p * mod;
727: for ( j = n, px = COEF(wt); j >= 1; j-- )
728: px[j] = 0;
729: px[0] = 1;
730: for ( j = 0; j < np; j++ ) {
731: cpyum(w,wf);
732: for ( k = DEG(wf), px = COEF(wf); k >= 0; k-- )
733: px[k] %= p1;
734: divum(p1,wf,out[j],quot); mulum(p1,outc[j],quot,wm);
735: for ( k = DEG(wm), px = COEF(wt), py = COEF(wm); k >= 0; k-- )
736: px[k] = ( px[k] - py[k] ) % p1;
737: }
738: degum(wt,n);
739: for ( j = DEG(wt), px = COEF(wt); j >= 0; j-- )
740: px[j] = ((tmp=(px[j]/p)%mod)>= 0?tmp:tmp + mod);
741: for ( j = 0; j < np; j++ ) {
742: mulum(mod,wt,outc[j],wm); dr = divum(mod,wm,in[j],quot);
743: for ( k = dr, px = COEF(outc[j]), py = COEF(wm); k >= 0; k-- )
744: px[k] += p * py[k];
745: degum(outc[j],MAX(DEG(outc[j]),dr));
746: }
747: }
748: bqlist->n = cqlist->n = np;
749: bqlist->mod = cqlist->mod = q;
750: }
751:
752: /*
753: henmain(fl,bqlist,cqlist,listp)
754: */
755:
756: void henmain(LUM f,ML bqlist,ML cqlist,ML *listp)
757: {
758: register int i,j,k;
759: int *px,*py;
760: int **pp,**pp1;
761: int n,np,mod,bound,dr,tmp;
762: UM wt,wq0,wq,wr,wm,wm0,wa,q;
763: LUM wb0,wb1,tlum;
764: UM *b,*c;
765: LUM *l;
766: ML list;
767:
768: n = DEG(f); np = bqlist->n; mod = bqlist->mod; bound = bqlist->bound;
769: *listp = list = MLALLOC(n);
770: list->n = np; list->mod = mod; list->bound = bound;
771: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
772: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
773: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
774: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
775: b = (UM *)bqlist->c; c = (UM *)cqlist->c; l = (LUM *)list->c;
776: for ( i = 0; i < np; i++ ) {
777: l[i] = LUMALLOC(DEG(b[i]),bound);
778: for ( j = DEG(b[i]), pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
779: pp[j][0] = px[j];
780: }
781: #if 0
782: fprintf(stderr,"bound=%d\n",bound);
783: #endif
784: for ( i = 1; i < bound; i++ ) {
785: #if 0
786: fprintf(stderr,".");
787: #endif
788: #if defined(VISUAL) || defined(__MINGW32__)
789: check_intr();
790: #endif
791: mullum(mod,i+1,l[0],l[1],wb0);
792: for ( j = 2; j < np; j++ ) {
793: mullum(mod,i+1,l[j],wb0,wb1);
794: tlum = wb0; wb0 = wb1; wb1 = tlum;
795: }
796: for ( j = n, px = COEF(wt); j >= 0; j-- )
797: px[j] = 0;
798: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
799: tmp = ( pp[j][i] - pp1[j][i] ) % mod;
800: COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
801: }
802: degum(wt,n);
803: for ( j = n, px = COEF(wq0); j >= 0; j-- )
804: px[j] = 0;
805: for ( j = 1; j < np; j++ ) {
806: mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
807: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
808: px[k] = ( px[k] + py[k] ) % mod;
809: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
810: pp[k][i] = px[k];
811: }
812: degum(wq0,n); mulum(mod,wq0,b[0],wm);
813: mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
814: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
815: pp[j][i] = px[j];
816: for ( j = n, px = COEF(wq0); j >= 0; j-- )
817: px[j] = 0;
818: }
819: #if 0
820: fprintf(stderr,"\n");
821: #endif
822: }
823:
824: /*
825: henmain_incremental(fl,bqlist,cqlist,start)
826: fl = bqlist[0]*... mod q^start
827: */
828:
829: void henmain_incremental(LUM f,LUM *bqlist,ML cqlist,
830: int np, int mod, int start, int bound)
831: {
832: register int i,j,k;
833: int *px,*py;
834: int **pp,**pp1;
835: int n,dr,tmp;
836: UM wt,wq0,wq,wr,wm,wm0,wa,q;
837: LUM wb0,wb1,tlum;
838: UM *b,*c;
839: LUM *l;
840: ML list;
841:
842: n = DEG(f);
843: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
844: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
845: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
846: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
847: c = (UM *)cqlist->c; l = bqlist;
848: b = (UM *)ALLOCA(n*sizeof(UM));
849: for ( i = 0; i < np; i++ ) {
850: j = DEG(l[i]);
851: b[i] = W_UMALLOC(j);
852: DEG(b[i]) = j;
853: for ( pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
854: px[j] = pp[j][0];
855: }
856: #if 0
857: fprintf(stderr,"bound=%d\n",bound);
858: #endif
859: for ( i = start; i < bound; i++ ) {
860: #if 0
861: fprintf(stderr,".");
862: #endif
863: mullum(mod,i+1,l[0],l[1],wb0);
864: for ( j = 2; j < np; j++ ) {
865: mullum(mod,i+1,l[j],wb0,wb1);
866: tlum = wb0; wb0 = wb1; wb1 = tlum;
867: }
868: for ( j = n, px = COEF(wt); j >= 0; j-- )
869: px[j] = 0;
870: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
871: tmp = ( pp[j][i] - pp1[j][i] ) % mod;
872: COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
873: }
874: degum(wt,n);
875: for ( j = n, px = COEF(wq0); j >= 0; j-- )
876: px[j] = 0;
877: for ( j = 1; j < np; j++ ) {
878: mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
879: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
880: px[k] = ( px[k] + py[k] ) % mod;
881: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
882: pp[k][i] = px[k];
883: }
884: degum(wq0,n); mulum(mod,wq0,b[0],wm);
885: mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
886: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
887: pp[j][i] = px[j];
888: for ( j = n, px = COEF(wq0); j >= 0; j-- )
889: px[j] = 0;
890: }
891: #if 0
892: fprintf(stderr,"\n");
893: #endif
894: }
895:
896: static double M;
897: static int E;
898:
899: int mig(int q,int d,P f)
900: {
901: mpz_t z;
902: int s;
903: mp_limb_t m;
904: DCP dc;
905:
906: for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
907: z[0] = BDY((Z)COEF(dc))[0]; s = mpz_size(z); m = mpz_getlimbn(z,s-1);
908: sqad(m,(s-1)*sizeof(mp_limb_t)*8);
909: }
910: if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
911: z[0] = BDY((Z)COEF(DC(f)))[0]; s = mpz_size(z); m = mpz_getlimbn(z,s-1);
912: M *= ((double)m)+1; E += (s-1) * sizeof(mp_limb_t)*8;
913: return (int)ceil( (0.31*(E+d+1)+log10((double)M)) / log10((double)q) );
914: }
915:
916: int mignotte(int q,P f)
917: {
918: return mig(q,UDEG(f),f);
919: }
920:
921: /* man*2^exp */
922:
923: void sqad(mp_limb_t man,int exp)
924: {
925: int e,sqe;
926: mp_limb_t t;
927: double man1,d,sqm;
928: int diff;
929:
930: if ( man+1 == 0 ) {
931: e = sizeof(mp_limb_t)*8; man1 = 1.0;
932: } else {
933: man += 1;
934: for ( e = 0, t = man; t; e++, t >>= 1 );
935: e--; d = (double)(1<<e);
936: man1 = ((double)man)/d;
937: }
938: exp += e; sqm = man1 * man1; sqe = 2 * exp;
939: if ( sqm >= 2.0 ) {
940: sqm /= 2.0; sqe++;
941: }
942: diff = E - sqe;
943: if ( diff > 18 )
944: return;
945: if ( diff < -18 ) {
946: M = sqm; E = sqe;
947: return;
948: }
949: if ( diff >= 0 )
950: M += (sqm / (double)(1<<diff));
951: else {
952: M = ( ( M / (double)(1<<-diff)) + sqm ); E = sqe;
953: }
954: if ( M >= 2.0 ) {
955: M /= 2.0; E++;
956: }
957: }
958:
959: void ptolum(int q,int bound,P f,LUM fl)
960: {
961: DCP dc;
962: int i,j;
963: int **pp;
964: int d,br,s;
965: unsigned int r;
966: int *c;
967: unsigned int *w;
968: Z z;
969:
970: for ( dc = DC(f), pp = COEF(fl); dc; dc = NEXT(dc) ) {
971: absz((Z)COEF(dc),&z);
972: d = ztonadic(q,z,&w);
1.2 noro 973: c = pp[ZTOS(DEG(dc))];
1.1 noro 974: for ( i = 0; i < d; i++ ) c[i] = w[i];
975: for ( ; i < bound; i++ ) c[i] = 0;
976: if ( sgnz((Z)COEF(dc)) < 0 )
977: for (i = 0, br = 0; i < bound; i++ ) {
978: if ( ( s = -(c[i] + br) ) < 0 ) {
979: c[i] = s + q; br = 1;
980: } else {
981: c[i] = 0; br = 0;
982: }
983: }
984: }
985: }
986:
987: void modfctrp(P p,int mod,int flag,DCP *dcp)
988: {
989: int cm,n,i,j,k;
990: DCP dc,dc0;
991: P zp;
992: Q c;
993: Z q;
994: UM mp;
995: UM *tl;
996: struct oDUM *udc,*udc1;
997:
998: if ( !p ) {
999: *dcp = 0; return;
1000: }
1001: ptozp(p,1,&c,&zp);
1002: if ( !INT(c) || !(cm = remqi(c,mod)) ) {
1003: *dcp = 0; return;
1004: }
1005: mp = W_UMALLOC(UDEG(p));
1006: ptoum(mod,zp,mp);
1007: if ( (n = DEG(mp)) < 0 ) {
1008: *dcp = 0; return;
1009: } else if ( n == 0 ) {
1.2 noro 1010: cm = dmar(cm,COEF(mp)[0],0,mod); STOZ(cm,q);
1.1 noro 1011: NEWDC(dc); COEF(dc) = (P)q; DEG(dc) = ONE;
1012: NEXT(dc) = 0; *dcp = dc;
1013: return;
1014: }
1015: if ( COEF(mp)[n] != 1 ) {
1016: cm = dmar(cm,COEF(mp)[n],0,mod);
1017: i = invm(COEF(mp)[n],mod);
1018: for ( j = 0; j <= n; j++ )
1019: COEF(mp)[j] = dmar(COEF(mp)[j],i,0,mod);
1020: }
1021: W_CALLOC(n+1,struct oDUM,udc);
1022: gensqfrum(mod,mp,udc);
1023: switch ( flag ) {
1024: case FCTR:
1025: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1026: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1027: for ( i = 0,j = 0; udc[i].f; i++ )
1028: if ( DEG(udc[i].f) == 1 ) {
1029: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1030: } else {
1031: bzero((char *)tl,(n+1)*sizeof(UM));
1032: berlemain(mod,udc[i].f,tl);
1033: for ( k = 0; tl[k]; k++, j++ ) {
1034: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1035: }
1036: }
1037: udc = udc1; break;
1038: case SQFR:
1039: break;
1040: case DDD:
1041: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1042: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1043: for ( i = 0,j = 0; udc[i].f; i++ )
1044: if ( DEG(udc[i].f) == 1 ) {
1045: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1046: } else {
1047: bzero((char *)tl,(n+1)*sizeof(UM));
1048: ddd(mod,udc[i].f,tl);
1049: for ( k = 0; tl[k]; k++, j++ ) {
1050: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1051: }
1052: }
1053: udc = udc1; break;
1054: case NEWDDD:
1055: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1056: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1057: for ( i = 0,j = 0; udc[i].f; i++ )
1058: if ( DEG(udc[i].f) == 1 ) {
1059: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1060: } else {
1061: bzero((char *)tl,(n+1)*sizeof(UM));
1062: if ( mod == 2 )
1063: berlemain(mod,udc[i].f,tl);
1064: else
1065: newddd(mod,udc[i].f,tl);
1066: for ( k = 0; tl[k]; k++, j++ ) {
1067: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1068: }
1069: }
1070: udc = udc1; break;
1071: }
1.2 noro 1072: NEWDC(dc0); STOZ(cm,q); COEF(dc0) = (P)q; DEG(dc0) = ONE; dc = dc0;
1.1 noro 1073: for ( n = 0; udc[n].f; n++ ) {
1074: NEWDC(NEXT(dc)); dc = NEXT(dc);
1.2 noro 1075: STOZ(udc[n].n,DEG(dc)); umtop(VR(p),udc[n].f,&COEF(dc));
1.1 noro 1076: }
1077: NEXT(dc) = 0; *dcp = dc0;
1078: }
1079:
1080: void gensqfrum(int mod,UM p,struct oDUM *dc)
1081: {
1082: int n,i,j,d;
1083: UM t,s,g,f,f1,b;
1084:
1085: if ( (n = DEG(p)) == 1 ) {
1086: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
1087: return;
1088: }
1089: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
1090: f = W_UMALLOC(n); f1 = W_UMALLOC(n); b = W_UMALLOC(n);
1091: diffum(mod,p,t); cpyum(p,s); Gcdum(mod,t,s,g);
1092: if ( !DEG(g) ) {
1093: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
1094: return;
1095: }
1096: cpyum(p,b); cpyum(p,t); Divum(mod,t,g,f);
1097: for ( i = 0, d = 0; DEG(f); i++ ) {
1098: while ( 1 ) {
1099: cpyum(b,t);
1100: if ( Divum(mod,t,f,s) >= 0 )
1101: break;
1102: else {
1103: cpyum(s,b); d++;
1104: }
1105: }
1106: cpyum(b,t); cpyum(f,s); Gcdum(mod,t,s,f1);
1107: Divum(mod,f,f1,s); cpyum(f1,f);
1108: dc[i].f = UMALLOC(DEG(s)); cpyum(s,dc[i].f); dc[i].n = d;
1109: }
1110: if ( DEG(b) > 0 ) {
1111: d = 1;
1112: while ( 1 ) {
1113: cpyum(b,t);
1114: for ( j = DEG(t); j >= 0; j-- )
1115: if ( COEF(t)[j] && (j % mod) )
1116: break;
1117: if ( j >= 0 )
1118: break;
1119: else {
1120: DEG(s) = DEG(t)/mod;
1121: for ( j = 0; j <= DEG(t); j++ )
1122: COEF(s)[j] = COEF(t)[j*mod];
1123: cpyum(s,b); d *= mod;
1124: }
1125: }
1126: gensqfrum(mod,b,dc+i);
1127: for ( j = i; dc[j].f; j++ )
1128: dc[j].n *= d;
1129: }
1130: }
1131:
1132: #if 0
1133: void srchum(int mod,UM p1,UM p2,UM gr)
1134: {
1135: UM m,m1,m2,q,r,t,g1,g2;
1136: int lc,d,d1,d2,i,j,k,l,l1,l2,l3,tmp,adj;
1137: V v;
1138:
1139: d = MAX(DEG(p1),DEG(p2));
1140: g1 = W_UMALLOC(d); g2 = W_UMALLOC(d);
1141: bzero((char *)g1,(d+2)*sizeof(int)); bzero((char *)g2,(d+2)*sizeof(int));
1142: if ( d == DEG(p1) ) {
1143: cpyum(p1,g1); cpyum(p2,g2);
1144: } else {
1145: cpyum(p1,g2); cpyum(p2,g1);
1146: }
1147: if ( ( d1 = DEG(g1) ) > ( d2 = DEG(g2) ) ) {
1148: j = d1 - 1; adj = 1;
1149: } else
1150: j = d2;
1151: lc = 1;
1152: r = W_UMALLOC(d1+d2); q = W_UMALLOC(d1+d2);
1153: m1 = W_UMALLOC(d1+d2); t = W_UMALLOC(d1+d2);
1154: bzero((char *)r,(d1+d2+2)*sizeof(int)); bzero((char *)q,(d1+d2+2)*sizeof(int));
1155: bzero((char *)m1,(d1+d2+2)*sizeof(int)); bzero((char *)t,(d1+d2+2)*sizeof(int));
1156: m = W_UMALLOC(0); bzero((char *)m,2*sizeof(int));
1157: adj = pwrm(mod,COEF(g2)[DEG(g2)],DEG(g1));
1158: DEG(m) = 0; COEF(m)[0] = invm(COEF(g2)[DEG(g2)],mod);
1159: Mulum(mod,g2,m,r); cpyum(r,g2);
1160: while ( 1 ) {
1161: if ( ( k = DEG(g2) ) < 0 ) {
1162: DEG(gr) = -1;
1163: return;
1164: }
1165: if ( k == j ) {
1166: if ( k == 0 ) {
1167: DEG(m) = 0; COEF(m)[0] = adj;
1168: Mulum(mod,g2,m,gr);
1169: return;
1170: } else {
1171: DEG(m) = 0;
1172: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
1173: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,t);
1174: DEG(m) = 0; COEF(m)[0] = dmb(mod,lc,lc,&tmp);
1175: Divum(mod,r,m,q); cpyum(g2,g1); cpyum(q,g2);
1176: lc = COEF(g1)[DEG(g1)]; j = k - 1;
1177: }
1178: } else {
1179: d = j - k;
1180: DEG(m) = 0; COEF(m)[0] = pwrm(mod,COEF(g2)[DEG(g2)],d);
1181: Mulum(mod,g2,m,m1); l = pwrm(mod,lc,d);
1182: DEG(m) = 0; COEF(m)[0] = l; Divum(mod,m1,m,t);
1183: if ( k == 0 ) {
1184: DEG(m) = 0; COEF(m)[0] = adj;
1185: Mulum(mod,t,m,gr);
1186: return;
1187: } else {
1188: DEG(m) = 0;
1189: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
1190: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,q);
1191: l1 = dmb(mod,lc,lc,&tmp); l2 = dmb(mod,l,l1,&tmp);
1192: DEG(m) = 0; COEF(m)[0] = l2;
1193: Divum(mod,r,m,q); cpyum(t,g1); cpyum(q,g2);
1194: if ( d % 2 )
1195: for ( i = DEG(g2); i >= 0; i-- )
1196: COEF(g2)[i] = ( mod - COEF(g2)[i] ) % mod;
1197: lc = COEF(g1)[DEG(g1)]; j = k - 1;
1198: }
1199: }
1200: }
1201: }
1202:
1203: UM *resberle(int mod,UM f,UM *fp)
1204: {
1205: UM w,wg,ws,wf,f0,gcd,q,res;
1206: int n;
1207: register int i;
1208:
1209: n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
1210: if ( DEG(wg) <= 0 ) {
1211: f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
1212: return ( fp );
1213: }
1214: f0 = W_UMALLOC(n); cpyum(f,f0);
1215: ws = W_UMALLOC(n); wf = W_UMALLOC(n);
1216: q = W_UMALLOC(n); gcd = W_UMALLOC(n);
1217: res = W_UMALLOC(2*n);
1218: srchum(mod,f,wg,res);
1219: for ( i = 0; i < mod; i++ ) {
1220: if ( substum(mod,res,i) )
1221: continue;
1222: cpyum(f0,wf); cpyum(wg,ws);
1223: COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
1224: Gcdum(mod,wf,ws,gcd);
1225: if ( DEG(gcd) > 0 ) {
1226: if ( DEG(gcd) < n ) {
1227: Divum(mod,f0,gcd,q); f0 = q; fp = resberle(mod,gcd,fp);
1228: }
1229: break;
1230: }
1231: }
1232: fp = resberle(mod,f0,fp);
1233: return ( fp );
1234: }
1235:
1236: int substum(int mod,UM p,int a)
1237: {
1238: int i,j,s;
1239: int *c;
1240:
1241: if ( DEG(p) < 0 )
1242: return 0;
1243: if ( DEG(p) == 0 )
1244: return COEF(p)[0];
1245: for ( i = DEG(p), c = COEF(p), s = c[i]; i >= 0; ) {
1246: for ( j = i--; (i>=0) && !c[i]; i-- );
1247: if ( i >= 0 )
1248: s = (s*pwrm(mod,a,j-i)%mod+c[i])%mod;
1249: else
1250: s = s*pwrm(mod,a,j)%mod;
1251: }
1252: return s;
1253: }
1254: #endif
1255:
1256: void ddd(int mod,UM f,UM *r)
1257: {
1258: register int i,j;
1259: int d,n;
1260: UM q,s,t,u,v,w,g,x,m;
1261: UM *base;
1262:
1263: n = DEG(f);
1264: if ( n == 1 ) {
1265: r[0] = UMALLOC(1); cpyum(f,r[0]); r[1] = 0; return;
1266: }
1267: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1268: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1269: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1270: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1271: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1272: for ( i = 2; i < n; i++ ) {
1273: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1274: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1275: }
1276: v = W_UMALLOC(n); cpyum(f,v);
1277: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1278: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1279: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1280: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1281: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1282: if ( COEF(w)[i] ) {
1283: Mulsum(mod,base[i],COEF(w)[i],s);
1284: addum(mod,s,t,u); cpyum(u,t);
1285: }
1286: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1287: if ( DEG(g) >= 1 ) {
1288: canzas(mod,g,d,base,r+j); j += DEG(g)/d;
1289: Divum(mod,v,g,q); cpyum(q,v);
1290: DEG(w) = Divum(mod,w,v,q);
1291: for ( i = 0; i < DEG(v); i++ )
1292: DEG(base[i]) = Divum(mod,base[i],v,q);
1293: }
1294: }
1295: if ( DEG(v) ) {
1296: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
1297: }
1298: r[j] = 0;
1299: }
1300:
1301: void canzas(int mod,UM f,int d,UM *base,UM *r)
1302: {
1303: UM t,s,u,w,g,o,q;
1304: Z n1,n2,n3,n4,n5,z;
1305: UM *b;
1306: int n,m,i;
1307:
1308: if ( DEG(f) == d ) {
1309: r[0] = UMALLOC(d); cpyum(f,r[0]);
1310: return;
1311: } else {
1312: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
1313: for ( i = 0, m = 0; i < n; i++ )
1314: m = MAX(m,DEG(base[i]));
1315: q = W_UMALLOC(m);
1316: for ( i = 0; i < n; i++ ) {
1317: b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
1318: DEG(b[i]) = Divum(mod,b[i],f,q);
1319: }
1320: t = W_UMALLOC(2*d);
1321: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
1322: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
1323: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
1.2 noro 1324: STOZ(mod,n1); STOZ(d,z); pwrz(n1,z,&n2); subz(n2,ONE,&n3);
1325: STOZ(2,n4); divz(n3,n4,&n5);
1.1 noro 1326: while ( 1 ) {
1327: randum(mod,2*d,t); spwrum0(mod,f,t,n5,s);
1328: subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
1329: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
1330: canzas(mod,g,d,b,r);
1331: cpyum(f,w); Divum(mod,w,g,s);
1332: canzas(mod,s,d,b,r+DEG(g)/d);
1333: return;
1334: }
1335: }
1336: }
1337: }
1338:
1339: void randum(int mod,int d,UM p)
1340: {
1341: unsigned int n;
1342: int i;
1343:
1344: n = ((unsigned int)random()) % d; DEG(p) = n; COEF(p)[n] = 1;
1345: for ( i = 0; i < (int)n; i++ )
1346: COEF(p)[i] = ((unsigned int)random()) % mod;
1347: }
1348:
1349: void pwrmodum(int mod,UM p,int e,UM f,UM pr)
1350: {
1351: UM wt,ws,q;
1352:
1353: if ( e == 0 ) {
1354: DEG(pr) = 0; COEF(pr)[0] = 1;
1355: } else if ( DEG(p) < 0 )
1356: DEG(pr) = -1;
1357: else if ( e == 1 ) {
1358: q = W_UMALLOC(DEG(p)); cpyum(p,pr);
1359: DEG(pr) = divum(mod,pr,f,q);
1360: } else if ( DEG(p) == 0 ) {
1361: DEG(pr) = 0; COEF(pr)[0] = pwrm(mod,COEF(p)[0],e);
1362: } else {
1363: wt = W_UMALLOC(2*DEG(f)); ws = W_UMALLOC(2*DEG(f));
1364: q = W_UMALLOC(2*DEG(f));
1365: pwrmodum(mod,p,e/2,f,wt);
1366: if ( !(e%2) ) {
1367: mulum(mod,wt,wt,pr); DEG(pr) = divum(mod,pr,f,q);
1368: } else {
1369: mulum(mod,wt,wt,ws); DEG(ws) = divum(mod,ws,f,q);
1370: mulum(mod,ws,p,pr); DEG(pr) = divum(mod,pr,f,q);
1371: }
1372: }
1373: }
1374:
1375: void spwrum0(int mod,UM m,UM f,Z e,UM r)
1376: {
1377: UM t,s,q;
1378: Z e1,rem,two;
1379:
1380: if ( !e ) {
1381: DEG(r) = 0; COEF(r)[0] = 1;
1382: } else if ( UNIZ(e) )
1383: cpyum(f,r);
1384: else {
1.2 noro 1385: STOZ(2,two);
1.1 noro 1386: divqrz(e,two,&e1,&rem);
1387: t = W_UMALLOC(2*DEG(m)); spwrum0(mod,m,f,e1,t);
1388: s = W_UMALLOC(2*DEG(m)); q = W_UMALLOC(2*DEG(m));
1389: Mulum(mod,t,t,s); DEG(s) = Divum(mod,s,m,q);
1390: if ( rem ) {
1391: Mulum(mod,s,f,t); DEG(t) = Divum(mod,t,m,q); cpyum(t,r);
1392: } else
1393: cpyum(s,r);
1394: }
1395: }
1396:
1397: void mult_mod_tab(UM p,int mod,UM *tab,UM r,int d)
1398: {
1399: UM w,w1,c;
1400: int n,i;
1401: int *pc;
1402:
1403: w = W_UMALLOC(d); w1 = W_UMALLOC(d);
1404: c = W_UMALLOC(1); DEG(c) = 0;
1405: n = DEG(p); DEG(r) = -1;
1406: for ( i = 0, pc = COEF(p); i <= n; i++ )
1407: if ( pc[i] ) {
1408: COEF(c)[0] = pc[i];
1409: mulum(mod,tab[i],c,w);
1410: addum(mod,r,w,w1);
1411: cpyum(w1,r);
1412: }
1413: }
1414:
1415: void make_qmat(UM p,int mod,UM *tab,int ***mp)
1416: {
1417: int n,i,j;
1418: int *c;
1419: UM q,r;
1420: int **mat;
1421:
1422: n = DEG(p);
1423: *mp = mat = almat(n,n);
1424: for ( j = 0; j < n; j++ ) {
1425: r = W_UMALLOC(DEG(tab[j])); q = W_UMALLOC(DEG(tab[j]));
1426: cpyum(tab[j],r); DEG(r) = divum(mod,r,p,q);
1427: for ( i = 0, c = COEF(r); i <= DEG(r); i++ )
1428: mat[i][j] = c[i];
1429: }
1430: for ( i = 0; i < n; i++ )
1431: mat[i][i] = (mat[i][i]+mod-1) % mod;
1432: }
1433:
1434: void null_mod(int **mat,int mod,int n,int *ind)
1435: {
1436: int i,j,l,s,h,inv;
1437: int *t,*u;
1438:
1439: bzero((char *)ind,n*sizeof(int));
1440: ind[0] = 0;
1441: for ( i = j = 0; j < n; i++, j++ ) {
1442: for ( ; j < n; j++ ) {
1443: for ( l = i; l < n; l++ )
1444: if ( mat[l][j] )
1445: break;
1446: if ( l < n ) {
1447: t = mat[i]; mat[i] = mat[l]; mat[l] = t; break;
1448: } else
1449: ind[j] = 1;
1450: }
1451: if ( j == n )
1452: break;
1453: inv = invm(mat[i][j],mod);
1454: for ( s = j, t = mat[i]; s < n; s++ )
1455: t[s] = dmar(t[s],inv,0,mod);
1456: for ( l = 0; l < n; l++ ) {
1457: if ( l == i )
1458: continue;
1459: for ( s = j, u = mat[l], h = (mod-u[j])%mod; s < n; s++ )
1460: u[s] = dmar(h,t[s],u[s],mod);
1461: }
1462: }
1463: }
1464:
1465: void null_to_sol(int **mat,int *ind,int mod,int n,UM *r)
1466: {
1467: int i,j,k,l;
1468: int *c;
1469: UM w;
1470:
1471: for ( i = 0, l = 0; i < n; i++ ) {
1472: if ( !ind[i] )
1473: continue;
1474: w = UMALLOC(n);
1475: for ( j = k = 0, c = COEF(w); j < n; j++ )
1476: if ( ind[j] )
1477: c[j] = 0;
1478: else
1479: c[j] = mat[k++][i];
1480: c[i] = mod-1;
1481: for ( j = n; j >= 0; j-- )
1482: if ( c[j] )
1483: break;
1484: DEG(w) = j;
1485: r[l++] = w;
1486: }
1487: }
1488: /*
1489: make_qmat(p,mod,tab,mp)
1490: null_mod(mat,mod,n,ind)
1491: null_to_sol(mat,ind,mod,n,r)
1492: */
1493:
1494: void newddd(int mod,UM f,UM *r)
1495: {
1496: register int i,j;
1497: int d,n;
1498: UM q,s,t,u,v,w,g,x,m;
1499: UM *base;
1500:
1501: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1502: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1503: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1504: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1505: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1506: for ( i = 2; i < n; i++ ) {
1507: /* fprintf(stderr,"i=%d\n",i); */
1508: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1509: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1510: }
1511: v = W_UMALLOC(n); cpyum(f,v);
1512: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1513: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1514: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1515: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1516: /* fprintf(stderr,"d=%d\n",d); */
1517: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1518: if ( COEF(w)[i] ) {
1519: Mulsum(mod,base[i],COEF(w)[i],s);
1520: addum(mod,s,t,u); cpyum(u,t);
1521: }
1522: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1523: if ( DEG(g) >= 1 ) {
1524: berlekamp(g,mod,d,base,r+j); j += DEG(g)/d;
1525: Divum(mod,v,g,q); cpyum(q,v);
1526: DEG(w) = Divum(mod,w,v,q);
1527: for ( i = 0; i < DEG(v); i++ )
1528: DEG(base[i]) = Divum(mod,base[i],v,q);
1529: }
1530: }
1531: if ( DEG(v) ) {
1532: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
1533: }
1534: r[j] = 0;
1535: }
1536:
1537: int nfctr_mod(UM f,int mod)
1538: {
1539: register int i,j;
1540: int d,n;
1541: UM q,s,t,u,v,w,g,x,m;
1542: UM *base;
1543:
1544: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1545: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1546: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1547: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1548: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1549: for ( i = 2; i < n; i++ ) {
1550: /* fprintf(stderr,"i=%d\n",i); */
1551: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1552: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1553: }
1554: v = W_UMALLOC(n); cpyum(f,v);
1555: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1556: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1557: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1558: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1559: /* fprintf(stderr,"d=%d\n",d); */
1560: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1561: if ( COEF(w)[i] ) {
1562: Mulsum(mod,base[i],COEF(w)[i],s);
1563: addum(mod,s,t,u); cpyum(u,t);
1564: }
1565: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1566: if ( DEG(g) >= 1 ) {
1567: j += DEG(g)/d;
1568: Divum(mod,v,g,q); cpyum(q,v);
1569: DEG(w) = Divum(mod,w,v,q);
1570: for ( i = 0; i < DEG(v); i++ )
1571: DEG(base[i]) = Divum(mod,base[i],v,q);
1572: }
1573: }
1574: if ( DEG(v) ) j++;
1575: return j;
1576: }
1577:
1578: int irred_check(UM f,int mod)
1579: {
1580: register int i,j;
1581: int d,n;
1582: UM q,s,t,u,v,w,g,x,m,f1,b;
1583: UM *base;
1584:
1585: if ( (n = DEG(f)) == 1 )
1586: return 1;
1587: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
1588: f1 = W_UMALLOC(n); b = W_UMALLOC(n);
1589: diffum(mod,f,t); cpyum(f,s); Gcdum(mod,t,s,g);
1590: if ( DEG(g) )
1591: return 0;
1592:
1593: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1594: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1595: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1596: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1597: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1598: for ( i = 2; i < n; i++ ) {
1599: /* fprintf(stderr,"i=%d\n",i); */
1600: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1601: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1602: }
1603: v = W_UMALLOC(n); cpyum(f,v);
1604: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1605: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1606: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1607: for ( j = 0, d = 1; 2*d <= n; d++ ) {
1608: /* fprintf(stderr,"d=%d\n",d); */
1609: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1610: if ( COEF(w)[i] ) {
1611: Mulsum(mod,base[i],COEF(w)[i],s);
1612: addum(mod,s,t,u); cpyum(u,t);
1613: }
1614: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1615: if ( DEG(g) >= 1 )
1616: return 0;
1617: }
1618: return 1;
1619: }
1620:
1621: int berlekamp(UM p,int mod,int df,UM *tab,UM *r)
1622: {
1623: int n,i,j,k,nf,d,nr;
1624: int **mat;
1625: int *ind;
1626: UM mp,w,q,gcd,w1,w2;
1627: UM *u;
1628: int *root;
1629:
1630: n = DEG(p);
1631: ind = ALLOCA(n*sizeof(int));
1632: make_qmat(p,mod,tab,&mat);
1633: null_mod(mat,mod,n,ind);
1634: for ( i = 0, d = 0; i < n; i++ )
1635: if ( ind[i] )
1636: d++;
1637: if ( d == 1 ) {
1638: r[0] = UMALLOC(n); cpyum(p,r[0]); return 1;
1639: }
1640: u = ALLOCA(d*sizeof(UM *));
1641: r[0] = UMALLOC(n); cpyum(p,r[0]);
1642: null_to_sol(mat,ind,mod,n,u);
1643: root = ALLOCA(d*sizeof(int));
1644: w = W_UMALLOC(n); mp = W_UMALLOC(d);
1645: w1 = W_UMALLOC(n); w2 = W_UMALLOC(n);
1646: for ( i = 1, nf = 1; i < d; i++ ) {
1647: minipoly_mod(mod,u[i],p,mp);
1648: nr = find_root(mod,mp,root);
1649: for ( j = 0; j < nf; j++ ) {
1650: if ( DEG(r[j]) == df )
1651: continue;
1652: for ( k = 0; k < nr; k++ ) {
1653: cpyum(u[i],w1); cpyum(r[j],w2);
1654: COEF(w1)[0] = (mod-root[k]) % mod;
1655: gcdum(mod,w1,w2,w);
1656: if ( DEG(w) > 0 && DEG(w) < DEG(r[j]) ) {
1657: gcd = UMALLOC(DEG(w));
1658: q = UMALLOC(DEG(r[j])-DEG(w));
1659: cpyum(w,gcd); divum(mod,r[j],w,q);
1660: r[j] = q; r[nf++] = gcd;
1661: }
1662: if ( nf == d )
1663: return d;
1664: }
1665: }
1666: }
1667: /* NOTREACHED */
1668: error("berlekamp : cannot happen");
1669: return -1;
1670: }
1671:
1672: void minipoly_mod(int mod,UM f,UM p,UM mp)
1673: {
1674: struct p_pair *list,*l,*l1,*lprev;
1675: int n,d;
1676: UM u,p0,p1,np0,np1,q,w;
1677:
1678: list = (struct p_pair *)MALLOC(sizeof(struct p_pair));
1679: list->p0 = u = W_UMALLOC(0); DEG(u) = 0; COEF(u)[0] = 1;
1680: list->p1 = W_UMALLOC(0); cpyum(list->p0,list->p1);
1681: list->next = 0;
1682: n = DEG(p); w = UMALLOC(2*n);
1683: p0 = UMALLOC(2*n); cpyum(list->p0,p0);
1684: p1 = UMALLOC(2*n); cpyum(list->p1,p1);
1685: q = W_UMALLOC(2*n);
1686: while ( 1 ) {
1687: COEF(p0)[DEG(p0)] = 0; DEG(p0)++; COEF(p0)[DEG(p0)] = 1;
1688: mulum(mod,f,p1,w); DEG(w) = divum(mod,w,p,q); cpyum(w,p1);
1689: np0 = UMALLOC(n); np1 = UMALLOC(n);
1690: lnf_mod(mod,n,p0,p1,list,np0,np1);
1691: if ( DEG(np1) < 0 ) {
1692: cpyum(np0,mp); return;
1693: } else {
1694: l1 = (struct p_pair *)MALLOC(sizeof(struct p_pair));
1695: l1->p0 = np0; l1->p1 = np1;
1696: for ( l = list, lprev = 0, d = DEG(np1);
1697: l && (DEG(l->p1) > d); lprev = l, l = l->next );
1698: if ( lprev ) {
1699: lprev->next = l1; l1->next = l;
1700: } else {
1701: l1->next = list; list = l1;
1702: }
1703: }
1704: }
1705: }
1706:
1707: void lnf_mod(int mod,int n,UM p0,UM p1,struct p_pair *list,UM np0,UM np1)
1708: {
1709: int inv,h,d1;
1710: UM t0,t1,s0,s1;
1711: struct p_pair *l;
1712:
1713: cpyum(p0,np0); cpyum(p1,np1);
1714: t0 = W_UMALLOC(n); t1 = W_UMALLOC(n);
1715: s0 = W_UMALLOC(n); s1 = W_UMALLOC(n);
1716: for ( l = list; l; l = l->next ) {
1717: d1 = DEG(np1);
1718: if ( d1 == DEG(l->p1) ) {
1719: inv = invm((mod-COEF(l->p1)[d1])%mod,mod);
1720: h = dmar(COEF(np1)[d1],inv,0,mod);
1721: mulsum(mod,l->p0,h,t0); addum(mod,np0,t0,s0); cpyum(s0,np0);
1722: mulsum(mod,l->p1,h,t1); addum(mod,np1,t1,s1); cpyum(s1,np1);
1723: }
1724: }
1725: }
1726:
1727: int find_root(int mod,UM p,int *root)
1728: {
1729: UM *r;
1730: int i,j;
1731:
1732: r = ALLOCA((DEG(p)+1)*sizeof(UM));
1733: ddd(mod,p,r);
1734: for ( i = 0, j = 0; r[i]; i++ )
1735: if ( DEG(r[i]) == 1 )
1736: root[j++] = (mod - COEF(r[i])[0]) % mod;
1737: return j;
1738: }
1739:
1740: void showum(UM p)
1741: {
1742: int i;
1743: int *c;
1744:
1745: for ( i = DEG(p), c = COEF(p); i >= 0; i-- )
1746: if ( c[i] )
1747: printf("+%dx^%d",c[i],i);
1748: printf("\n");
1749: }
1750:
1751: void showumat(int **mat,int n)
1752: {
1753: int i,j;
1754:
1755: for ( i = 0; i < n; i++ ) {
1756: for ( j = 0; j < n; j++ )
1757: printf("%d ",mat[i][j]);
1758: printf("\n");
1759: }
1760: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>