Annotation of OpenXM_contrib2/asir2000/engine/H.c, Revision 1.7
1.2 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
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 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.7 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/H.c,v 1.6 2001/10/09 01:36:09 noro Exp $
1.2 noro 49: */
1.1 noro 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:
1.6 noro 69: void berle(int index,int count,P f,ML *listp)
1.1 noro 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 ( !rem(NM((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:
1.6 noro 111: int berlecnt(int mod,UM f)
1.1 noro 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:
1.6 noro 139: int berlecntmain(int mod,int n,int m,int **c)
1.1 noro 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:
1.6 noro 165: UM *berlemain(int mod,UM f,UM *fp)
1.1 noro 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:
1.6 noro 194: void hensel(int index,int count,P f,ML *listp)
1.1 noro 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:
1.6 noro 231: void hensel2(int index,int count,P f,ML *listp)
1.4 noro 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;
1.6 noro 239: int k;
1.4 noro 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:
1.6 noro 312: void henmain2(LUM f,UM g0,UM h0,UM a0,UM b0,int m,int bound,LUM *gp)
1.4 noro 313: {
314: int n,dg,dh,i,k,j,dg1,dh1;
1.6 noro 315: UM wu,wr,ws,wt,q,wh1,wg1,wc,wd,we,wz;
1.4 noro 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:
1.5 noro 356: #if 1
1.4 noro 357: fprintf(stderr,"bound=%d\n",bound);
1.5 noro 358: #endif
1.4 noro 359: for ( k = 1; k < bound; k++ ) {
1.5 noro 360: #if 1
1.4 noro 361: fprintf(stderr,".");
1.5 noro 362: #endif
1.4 noro 363: /* at this point, f = gk*hk mod y^k */
364:
365: #if 0
366: for ( j = 0; j < k; j++ )
367: for ( i = 0; i <= n; i++ )
368: if ( COEF(f)[i][j] != COEF(f)[i][j] )
369: error("henmain2 : cannot happen(f=fk)");
370: #endif
371:
372: /* wt = (f-gk*hk)/y^k */
373: for ( i = 0; i <= n; i++ )
374: COEF(ws)[i] = COEF(f)[i][k];
375: degum(ws,n);
376: for ( i = 0; i <= n; i++ )
377: COEF(wu)[i] = COEF(fk)[i][k];
378: degum(wu,n);
379: subum(m,ws,wu,wt);
380:
381: /* compute wf1,wg1 s.t. wh1*g0+wg1*h0 = wt */
382: mulum(m,a0,wt,wh1); DEG(wh1) = divum(m,wh1,h0,q);
383: mulum(m,wh1,g0,wc); subum(m,wt,wc,wd); DEG(wd) = divum(m,wd,h0,wg1);
384:
385: /* check */
386: #if 0
387: if ( DEG(wd) >= 0 || DEG(wg1) > dg )
388: error("henmain2 : cannot happen(adj)");
389:
390: mulum(m,wg1,h0,wc); mulum(m,wh1,g0,wd); addum(m,wc,wd,we);
391: subum(m,we,wt,wz);
392: if ( DEG(wz) >= 0 )
393: error("henmain2 : cannot happen(coef)");
394: #endif
395:
396: /* fk += ((wg1*hk+wh1*gk)*y^k+wg1*wh1*y^(2*k) mod m^bound */
397:
398: /* wb0 = wh1*y^k */
399: clearlum(n,bound,wb0);
400: DEG(wb0) = dh1 = DEG(wh1);
401: for ( i = 0; i <= dh1; i++ )
402: COEF(wb0)[i][k] = COEF(wh1)[i];
403:
404: /* wb2 = gk*wb0 mod y^bound */
405: clearlum(n,bound,wb2);
406: mullum(m,bound,gk,wb0,wb2);
407:
408: /* fk += wb2 */
409: addtolum(m,bound,wb2,fk);
410:
411: /* wb1 = wg1*y^k */
412: clearlum(n,bound,wb1);
413: DEG(wb1) = dg1 = DEG(wg1);
414: for ( i = 0; i <= n; i++ )
415: COEF(wb1)[i][k] = COEF(wg1)[i];
416:
417: /* wb2 = hk*wb1 mod y^bound */
418: clearlum(n,bound,wb2);
419: mullum(m,bound,hk,wb1,wb2);
420:
421: /* fk += wb2 */
422: addtolum(m,bound,wb2,fk);
423:
424: /* fk += wg1*wh1*y^(2*k) mod y^bound) */
425: if ( 2*k < bound ) {
426: clearlum(n,bound,wb2);
427: mullum(m,bound,wb0,wb1,wb2);
428: addtolum(m,bound,wb2,fk);
429: }
430:
431: /* gk += wg1*y^k, hk += wh1*y^k */
432: for ( i = 0; i <= DEG(wg1); i++ )
433: COEF(gk)[i][k] = COEF(wg1)[i];
434: for ( i = 0; i <= DEG(wh1); i++ )
435: COEF(hk)[i][k] = COEF(wh1)[i];
436: }
1.5 noro 437: #if 1
1.4 noro 438: fprintf(stderr,"\n");
1.5 noro 439: #endif
1.4 noro 440: *gp = gk;
441: clearlum(n,bound,f);
442: DEG(f) = dh;
443: for ( i = 0; i <= dh; i++ )
444: for ( j = 0; j < bound; j++ )
445: COEF(f)[i][j] = COEF(hk)[i][j];
446: }
447:
1.6 noro 448: void clearlum(int n,int bound,LUM f)
1.4 noro 449: {
450: int i;
451:
452: for ( i = 0; i <= n; i++ )
453: bzero(COEF(f)[i],bound*sizeof(int));
454: }
455:
456: /* g += f */
457:
1.6 noro 458: void addtolum(int m,int bound,LUM f,LUM g)
1.4 noro 459: {
460: int n,i;
461:
462: n = DEG(f);
463: for ( i = 0; i <= n; i++ )
464: addpadic(m,bound,COEF(f)[i],COEF(g)[i]);
465: }
466:
1.6 noro 467: void hsq(int index,int count,P f,int *nindex,DCP *dcp)
1.1 noro 468: {
469: register int i,j,k;
470: register int **pp,**fpp;
471: register int *px,*py;
472: int **wpp;
473: int n,dr,tmp,m,b,e,np,dt;
474: LUM fpa,wb0,wb1,lcpa,tpa,tlum;
475: struct oDUM *dct;
476: UM wt,wq0,wq,wr,wm,wm0,wa,ws,wb;
477: LUM *llist,*ll;
478: UM *dlist,*l,*c;
479: ML list,fp,cfp;
480: DCP dc;
481:
482: sqfrum(index,count,f,nindex,&dct,&fp);
483: np = fp->n; m = fp->mod;
484: if ( ( np == 1 ) && ( dct[0].n == 1 ) ) {
485: NEWDC(dc); DEG(dc) = ONE; COEF(dc) = f; NEXT(dc) = 0; *dcp = dc;
486: return;
487: }
488: for ( i = 0, dt = 0; i < np; i++ )
489: dt = MAX(DEG(dct[i].f),dt);
490: b = mig(m,dt,f); fp->bound = b;
491: if ( np == 1 ) {
492: nthrootchk(f,dct,fp,dcp);
493: return;
494: }
495: list = W_MLALLOC(np); list->n = np; list->mod = m; list->bound = 1;
496: for ( i = 0, ll = (LUM *)list->c; i < np; i++ ) {
497: W_LUMALLOC(DEG(dct[i].f),b,ll[i]);
498: for ( j = 0, px = COEF(dct[i].f), pp = COEF(ll[i]);
499: j <= DEG(ll[i]); j++ )
500: pp[j][0] = px[j];
501: }
502: dtestsql(f,list,dct,&dc);
503: if ( dc ) {
504: *dcp = dc;
505: return;
506: }
507: n = UDEG(f);
508: W_LUMALLOC(n,b,fpa); W_LUMALLOC(0,b,lcpa);
509: W_LUMALLOC(n,b,wb0); W_LUMALLOC(n,b,wb1);
510: W_LUMALLOC(n,b,tpa);
511: wt = W_UMALLOC(n); ws = W_UMALLOC(n);
512: wr = W_UMALLOC(n);
513: wq = W_UMALLOC(2*n); wq0 = W_UMALLOC(n);
514: wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
515: wa = W_UMALLOC(2*n);
516: ptolum(m,b,f,fpa); DEG(lcpa) = 0;
517: for ( i = 0, pp = COEF(lcpa), fpp = COEF(fpa); i < b; i++ )
518: pp[0][i] = fpp[n][i];
519: gcdgen(f,fp,&cfp);
520: llist = (LUM *) ALLOCA(np*sizeof(LUM));
521: dlist = (UM *) ALLOCA(np*sizeof(UM));
522: l = (UM *)fp->c; c = (UM *)cfp->c;
523: for ( i = 0; i < np; i++ ) {
524: W_LUMALLOC(DEG(l[i]),b,llist[i]);
525: for ( j = DEG(l[i]), pp = COEF(llist[i]), px = COEF(l[i]); j >= 0; j-- )
526: pp[j][0] = px[j];
527: if ( ( e = dct[i].n ) != 1 ) {
528: wb = dct[i].f;
529: dlist[i] = W_UMALLOC(DEG(wb)*e); cpyum(l[i],dlist[i]);
530: divum(m,dlist[i],wb,wq); DEG(dlist[i])= DEG(wq);
531: for ( k = 0; k <= DEG(wq); k++ )
532: COEF(dlist[i])[k] = dmb(m,COEF(wq)[k],e,&tmp);
533: }
534: }
535: for ( i = 1; i < b; i++ ) {
536: mullum(m,i+1,lcpa,llist[0],wb0);
537: for ( j = 1; j < np; j++ ) {
538: mullum(m,i+1,llist[j],wb0,wb1);
539: tlum = wb0; wb0 = wb1; wb1 = tlum;
540: }
541: for ( j = n, px = COEF(wt), pp = COEF(fpa), wpp = COEF(wb0);
542: j >= 0; j-- )
543: px[j] = ( pp[j][i] - wpp[j][i] + m ) % m;
544: degum(wt,n);
545: for ( j = n, px = COEF(wq0); j >= 0; j-- )
546: px[j] = 0;
547: for ( j = 1; j < np; j++ ) {
548: mulum(m,wt,c[j],wm); dr = divum(m,wm,l[j],wq);
549: for ( k = DEG(wq), px = COEF(wq0), py = COEF(wq); k >= 0; k-- )
550: px[k] = ( px[k] + py[k] ) % m;
551: for ( k = dr, pp = COEF(llist[j]), px = COEF(wm); k >= 0; k-- )
552: pp[k][i] = px[k];
553: }
554: degum(wq0,n); mulum(m,wq0,l[0],wm);
555: mulum(m,wt,c[0],wm0); addum(m,wm,wm0,wa);
556: for ( j = DEG(wa), pp = COEF(llist[0]), px = COEF(wa); j >= 0; j-- )
557: pp[j][i] = px[j];
558: for ( j = n, px = COEF(wq0); j >= 0; j-- )
559: px[j] = 0;
560: for ( j = 0; j < np; j++ )
561: if ( dct[j].n == 1 )
562: for ( k = 0,
563: pp = COEF(llist[j]),
564: wpp = COEF(((LUM *)list->c)[j]);
565: k <= DEG(llist[j]); k++ )
566: wpp[k][i] = pp[k][i];
567: else {
568: pwrlum(m,i+1,((LUM *)list->c)[j],dct[j].n,tpa);
569: for ( k = 0,
570: pp = COEF(llist[j]),
571: wpp = COEF(tpa);
572: k <= DEG(l[j]); k++ )
573: COEF(wt)[k] = (pp[k][i]-wpp[k][i]+m)%m;
574: degum(wt,DEG(l[j])); dr = divum(m,wt,dlist[j],ws);
575: if ( dr >= 0 ) {
576: *dcp = 0;
577: return;
578: } else
579: for ( k = 0,
580: pp = COEF(((LUM *)list->c)[j]);
581: k <= DEG(ws); k++ )
582: pp[k][i] = COEF(ws)[k];
583: }
584: list->bound = i+1; dtestsql(f,list,dct,&dc);
585: if ( dc ) {
586: *dcp = dc;
587: return;
588: }
589: }
590: *dcp = 0;
591: }
592:
1.6 noro 593: void gcdgen(P f,ML blist,ML *clistp)
1.1 noro 594: {
595: register int i;
596: int n,d,mod,np;
597: UM wf,wm,wx,wy,wu,wv,wa,wb,wg,q,tum;
598: UM *in,*out;
599: ML clist;
600:
601: n = UDEG(f); mod = blist->mod; np = blist->n;
602: d = 2*n;
603: q = W_UMALLOC(d); wf = W_UMALLOC(d);
604: wm = W_UMALLOC(d); wx = W_UMALLOC(d);
605: wy = W_UMALLOC(d); wu = W_UMALLOC(d);
606: wv = W_UMALLOC(d); wg = W_UMALLOC(d);
607: wa = W_UMALLOC(d); wb = W_UMALLOC(d);
608: ptoum(mod,f,wf); DEG(wg) = 0; COEF(wg)[0] = 1;
609: *clistp = clist = MLALLOC(np); clist->mod = mod; clist->n = np;
610: for ( i = 0, in = (UM *)blist->c, out = (UM *)clist->c; i < np; i++ ) {
611: divum(mod,wf,in[i],q); tum = wf; wf = q; q = tum;
612: cpyum(wf,wx); cpyum(in[i],wy);
613: eucum(mod,wx,wy,wa,wb); mulum(mod,wa,wg,wm);
614: DEG(wm) = divum(mod,wm,in[i],q); out[i] = UMALLOC(DEG(wm));
615: cpyum(wm,out[i]); mulum(mod,q,wf,wu);
616: mulum(mod,wg,wb,wv); addum(mod,wu,wv,wg);
617: }
618: }
619:
1.4 noro 620: /* find a,b s.t. qa*qg+qb*qh=1 mod q, qg=g mod mod, qh=h mod mod */
621: /* q = mod^k */
622:
1.6 noro 623: void henprep2(int mod,int q,int k,UM f,UM g,UM h,UM qg,UM qh,UM qa,UM qb)
1.4 noro 624: {
625: int n;
626: UM wg,wh,wa,wb;
627: ML bl,cl,bql,cql;
628: P ff;
629:
630: n = DEG(f);
631: wg = W_UMALLOC(2*n); wh = W_UMALLOC(2*n);
632: wa = W_UMALLOC(2*n); wb = W_UMALLOC(2*n);
633: cpyum(g,wg); cpyum(h,wh);
634:
635: /* wa*g+wb*h = 1 mod mod */
636: eucum(mod,wg,wh,wa,wb);
637:
638: #if 0
639: /* check */
640: wt = W_UMALLOC(2*n); ws = W_UMALLOC(2*n); wu = W_UMALLOC(2*n);
641: mulum(mod,wa,g,wt);
642: mulum(mod,wb,h,ws);
643: addum(mod,wt,ws,wu);
644: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
645: error("henprep 1");
646: #endif
647:
648: bl = MLALLOC(2); bl->n = 2; bl->mod = mod; bl->c[0] = g; bl->c[1] = h;
649: cl = MLALLOC(2); cl->n = 2; cl->mod = mod; cl->c[0] = wb; cl->c[1] = wa;
650: umtop(CO->v,f,&ff); /* XXX */
651: henprep(ff,bl,cl,&bql,&cql); /* XXX */
652:
653: cpyum(bql->c[0],qg); cpyum(bql->c[1],qh);
654: cpyum(cql->c[0],qb); cpyum(cql->c[1],qa);
655:
656: #if 0
657: /* check */
658: mulum(q,qa,qg,wt);
659: mulum(q,qb,qh,ws);
660: addum(q,wt,ws,wu);
661: if ( DEG(wu) != 0 || COEF(wu)[0] != 1 )
662: error("henprep 2");
663: #endif
664: }
665:
1.1 noro 666: /*
1.4 noro 667: henprep(f,blist,clist,&bqlist,&cqlist);
1.1 noro 668: */
669:
1.6 noro 670: void henprep(P f,ML blist,ML clist,ML *bqlistp,ML *cqlistp)
1.1 noro 671: {
672: register int i,j,k,*px,*py,*pz;
673: int n,pmax,dr,tmp,p,p1,mod,np,b,q;
674: UM w,wm,wn,wa,wt,wq,wf,quot,tum,*in,*inc,*out,*outc;
675: ML bqlist,cqlist;
676:
677: n = UDEG(f); p = mod = blist->mod; np = blist->n;
678: /* for ( b = 1, q = mod; q <= (unsigned int)(LBASE / (L)mod); q *= mod, b++ ); */
679: for ( b = 1, q = mod; q <= ((1<<27) / mod); q *= mod, b++ );
680: w = W_UMALLOC(n); ptoum(q,f,w);
681: wm = W_UMALLOC(2*n); wn = W_UMALLOC(2*n);
682: wa = W_UMALLOC(2*n); wt = W_UMALLOC(2*n);
683: wq = W_UMALLOC(2*n); wf = W_UMALLOC(2*n);
684: quot = W_UMALLOC(2*n);
685: *bqlistp = bqlist = MLALLOC(np); *cqlistp = cqlist = MLALLOC(np);
686: for ( i = 0; i < n+2; i++ )
687: COEF(wq)[i] = 0;
688: for ( i = 0,
689: in = (UM *)blist->c, inc = (UM *)clist->c,
690: out = (UM *)bqlist->c, outc = (UM *)cqlist->c;
691: i < np; i++ ) {
692: out[i] = C_UMALLOC(n+1); cpyum(in[i],out[i]);
693: outc[i] = C_UMALLOC(n+1); cpyum(inc[i],outc[i]);
694: }
695: for ( pmax = 1, i = b; i > 0; i-- )
696: pmax *= mod;
697: for ( i = 1; i < b; i++, p = p1 ) {
698: cpyum(out[0],wm);
699: for ( j = 1; j < np; j++ ) {
700: mulum(pmax,wm,out[j],wn);
701: tum = wm; wm = wn; wn = tum;
702: }
703: for ( j = n, px = COEF(w), py = COEF(wm), pz = COEF(wt); j >= 0; j-- ) {
704: tmp = ( ( px[j] - py[j] ) / p ) % mod;
705: pz[j] = ( tmp >= 0? tmp : tmp + mod );
706: }
707: degum(wt,n);
708: for ( j = 1; j < np; j++ ) {
709: mulum(mod,wt,inc[j],wm); dr = divum(mod,wm,in[j],quot);
710: for ( k = DEG(quot); k >= 0; k-- )
711: COEF(wq)[k] = ( COEF(wq)[k] + COEF(quot)[k] ) % mod;
712: for ( k = dr, px = COEF(out[j]), py = COEF(wm); k >= 0; k-- )
713: px[k] += p * py[k];
714: }
715: degum(wq,n); mulum(mod,wq,in[0],wm);
716: mulum(mod,wt,inc[0],wn); addum(mod,wm,wn,wa);
717: for ( j = DEG(wa), px = COEF(out[0]), py = COEF(wa); j >= 0; j-- )
718: px[j] += p * py[j];
719: for ( j = n, px = COEF(wq); j >= 0; j-- )
720: px[j] = 0;
721: p1 = p * mod;
722: for ( j = n, px = COEF(wt); j >= 1; j-- )
723: px[j] = 0;
724: px[0] = 1;
725: for ( j = 0; j < np; j++ ) {
726: cpyum(w,wf);
727: for ( k = DEG(wf), px = COEF(wf); k >= 0; k-- )
728: px[k] %= p1;
729: divum(p1,wf,out[j],quot); mulum(p1,outc[j],quot,wm);
730: for ( k = DEG(wm), px = COEF(wt), py = COEF(wm); k >= 0; k-- )
731: px[k] = ( px[k] - py[k] ) % p1;
732: }
733: degum(wt,n);
734: for ( j = DEG(wt), px = COEF(wt); j >= 0; j-- )
735: px[j] = ((tmp=(px[j]/p)%mod)>= 0?tmp:tmp + mod);
736: for ( j = 0; j < np; j++ ) {
737: mulum(mod,wt,outc[j],wm); dr = divum(mod,wm,in[j],quot);
738: for ( k = dr, px = COEF(outc[j]), py = COEF(wm); k >= 0; k-- )
739: px[k] += p * py[k];
740: degum(outc[j],MAX(DEG(outc[j]),dr));
741: }
742: }
743: bqlist->n = cqlist->n = np;
744: bqlist->mod = cqlist->mod = q;
745: }
746:
747: /*
748: henmain(fl,bqlist,cqlist,listp)
749: */
750:
1.6 noro 751: void henmain(LUM f,ML bqlist,ML cqlist,ML *listp)
1.1 noro 752: {
753: register int i,j,k;
754: int *px,*py;
755: int **pp,**pp1;
756: int n,np,mod,bound,dr,tmp;
757: UM wt,wq0,wq,wr,wm,wm0,wa,q;
758: LUM wb0,wb1,tlum;
759: UM *b,*c;
760: LUM *l;
761: ML list;
762:
763: n = DEG(f); np = bqlist->n; mod = bqlist->mod; bound = bqlist->bound;
764: *listp = list = MLALLOC(n);
765: list->n = np; list->mod = mod; list->bound = bound;
766: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
767: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
768: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
769: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
770: b = (UM *)bqlist->c; c = (UM *)cqlist->c; l = (LUM *)list->c;
771: for ( i = 0; i < np; i++ ) {
772: l[i] = LUMALLOC(DEG(b[i]),bound);
773: for ( j = DEG(b[i]), pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
774: pp[j][0] = px[j];
775: }
1.5 noro 776: #if 0
1.4 noro 777: fprintf(stderr,"bound=%d\n",bound);
1.5 noro 778: #endif
1.1 noro 779: for ( i = 1; i < bound; i++ ) {
1.5 noro 780: #if 0
1.4 noro 781: fprintf(stderr,".");
1.5 noro 782: #endif
1.1 noro 783: mullum(mod,i+1,l[0],l[1],wb0);
784: for ( j = 2; j < np; j++ ) {
785: mullum(mod,i+1,l[j],wb0,wb1);
786: tlum = wb0; wb0 = wb1; wb1 = tlum;
787: }
788: for ( j = n, px = COEF(wt); j >= 0; j-- )
789: px[j] = 0;
790: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
791: tmp = ( pp[j][i] - pp1[j][i] ) % mod;
792: COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
793: }
794: degum(wt,n);
795: for ( j = n, px = COEF(wq0); j >= 0; j-- )
796: px[j] = 0;
797: for ( j = 1; j < np; j++ ) {
798: mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
799: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
800: px[k] = ( px[k] + py[k] ) % mod;
801: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
802: pp[k][i] = px[k];
803: }
804: degum(wq0,n); mulum(mod,wq0,b[0],wm);
805: mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
806: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
807: pp[j][i] = px[j];
808: for ( j = n, px = COEF(wq0); j >= 0; j-- )
809: px[j] = 0;
810: }
1.5 noro 811: #if 0
1.4 noro 812: fprintf(stderr,"\n");
1.5 noro 813: #endif
1.1 noro 814: }
815:
1.7 ! noro 816: /*
! 817: henmain_incremental(fl,bqlist,cqlist,start)
! 818: fl = bqlist[0]*... mod q^start
! 819: */
! 820:
! 821: void henmain_incremental(LUM f,LUM *bqlist,ML cqlist,
! 822: int np, int mod, int start, int bound)
! 823: {
! 824: register int i,j,k;
! 825: int *px,*py;
! 826: int **pp,**pp1;
! 827: int n,dr,tmp;
! 828: UM wt,wq0,wq,wr,wm,wm0,wa,q;
! 829: LUM wb0,wb1,tlum;
! 830: UM *b,*c;
! 831: LUM *l;
! 832: ML list;
! 833:
! 834: n = DEG(f);
! 835: W_LUMALLOC(n,bound,wb0); W_LUMALLOC(n,bound,wb1);
! 836: wt = W_UMALLOC(n); wq0 = W_UMALLOC(n); wq = W_UMALLOC(n);
! 837: wr = W_UMALLOC(n); wm = W_UMALLOC(2*n); wm0 = W_UMALLOC(2*n);
! 838: wa = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
! 839: c = (UM *)cqlist->c; l = bqlist;
! 840: b = (UM *)ALLOCA(n*sizeof(UM));
! 841: for ( i = 0; i < np; i++ ) {
! 842: j = DEG(l[i]);
! 843: b[i] = W_UMALLOC(j);
! 844: DEG(b[i]) = j;
! 845: for ( pp = COEF(l[i]), px = COEF(b[i]); j >= 0; j-- )
! 846: px[j] = pp[j][0];
! 847: }
! 848: #if 0
! 849: fprintf(stderr,"bound=%d\n",bound);
! 850: #endif
! 851: for ( i = start; i < bound; i++ ) {
! 852: #if 0
! 853: fprintf(stderr,".");
! 854: #endif
! 855: mullum(mod,i+1,l[0],l[1],wb0);
! 856: for ( j = 2; j < np; j++ ) {
! 857: mullum(mod,i+1,l[j],wb0,wb1);
! 858: tlum = wb0; wb0 = wb1; wb1 = tlum;
! 859: }
! 860: for ( j = n, px = COEF(wt); j >= 0; j-- )
! 861: px[j] = 0;
! 862: for ( j = n, pp = COEF(f), pp1 = COEF(wb0); j >= 0; j-- ) {
! 863: tmp = ( pp[j][i] - pp1[j][i] ) % mod;
! 864: COEF(wt)[j] = ( tmp < 0 ? tmp + mod : tmp );
! 865: }
! 866: degum(wt,n);
! 867: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 868: px[j] = 0;
! 869: for ( j = 1; j < np; j++ ) {
! 870: mulum(mod,wt,c[j],wm); dr = divum(mod,wm,b[j],q);
! 871: for ( k = DEG(q), px = COEF(wq0), py = COEF(q); k >= 0; k-- )
! 872: px[k] = ( px[k] + py[k] ) % mod;
! 873: for ( k = dr, pp = COEF(l[j]), px = COEF(wm); k >= 0; k-- )
! 874: pp[k][i] = px[k];
! 875: }
! 876: degum(wq0,n); mulum(mod,wq0,b[0],wm);
! 877: mulum(mod,wt,c[0],wm0); addum(mod,wm,wm0,wa);
! 878: for ( j = DEG(wa), pp = COEF(l[0]), px = COEF(wa); j >= 0; j-- )
! 879: pp[j][i] = px[j];
! 880: for ( j = n, px = COEF(wq0); j >= 0; j-- )
! 881: px[j] = 0;
! 882: }
! 883: #if 0
! 884: fprintf(stderr,"\n");
! 885: #endif
! 886: }
! 887:
1.1 noro 888: static double M;
889: static int E;
890:
1.6 noro 891: int mignotte(int q,P f)
1.1 noro 892: {
893: int p;
894: unsigned int *b;
895: N c;
896: DCP dc;
897:
898: for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
899: c = NM((Q)COEF(dc)); p = PL(c); b = BD(c);
900: sqad(b[p-1],(p-1)*BSH);
901: }
902: if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
903: c = NM((Q)COEF(DC(f))); p = PL(c); M *= ((double)BD(c)[p-1]+1.0); E += (p-1) * BSH;
904: return (int)ceil( (0.31*(E+UDEG(f)+1)+log10((double)M)) / log10((double)q) );
905: }
906:
1.6 noro 907: int mig(int q,int d,P f)
1.1 noro 908: {
909: int p;
910: unsigned int *b;
911: N c;
912: DCP dc;
913:
914: for ( dc = DC(f), M = 0, E = 0; dc; dc = NEXT(dc) ) {
915: c = NM((Q)COEF(dc)); p = PL(c); b = BD(c);
916: sqad(b[p-1],(p-1)*BSH);
917: }
918: if (E % 2) M *= 2; M = ceil(sqrt(M)); E /= 2;
919: c = NM((Q)COEF(DC(f))); p = PL(c);
920: M *= (BD(c)[p-1]+1); E += (p-1) * BSH;
921: return (int)ceil( (0.31*(E+d+1)+log10((double)M)) / log10((double)q) );
922: }
923:
1.6 noro 924: void sqad(unsigned int man,int exp)
1.1 noro 925: {
926: int e,sqe;
927: unsigned int t;
928: double man1,d,sqm;
929: int diff;
930:
931: if ( man == BMASK ) {
932: e = BSH; man1 = 1.0;
933: } else {
934: man += 1;
935: for ( e = 0, t = man; t; e++, t >>= 1 );
936: e--; d = (double)(1<<e);
937: man1 = ((double)man)/d;
938: }
939: exp += e; sqm = man1 * man1; sqe = 2 * exp;
940: if ( sqm >= 2.0 ) {
941: sqm /= 2.0; sqe++;
942: }
943: diff = E - sqe;
944: if ( diff > 18 )
945: return;
946: if ( diff < -18 ) {
947: M = sqm; E = sqe;
948: return;
949: }
950: if ( diff >= 0 )
951: M += (sqm / (double)(1<<diff));
952: else {
953: M = ( ( M / (double)(1<<-diff)) + sqm ); E = sqe;
954: }
955: if ( M >= 2.0 ) {
956: M /= 2.0; E++;
957: }
958: }
959:
1.6 noro 960: void ptolum(int q,int bound,P f,LUM fl)
1.1 noro 961: {
962: DCP dc;
963: int i,j;
964: int **pp;
965: int d,br,s;
966: unsigned int r;
967: int *c;
968: unsigned int *m,*w;
969:
970: for ( dc = DC(f), pp = COEF(fl); dc; dc = NEXT(dc) ) {
971: d = PL(NM((Q)COEF(dc))); m = BD(NM((Q)COEF(dc)));
972: c = pp[QTOS(DEG(dc))]; w = (unsigned int *)W_ALLOC(d);
973: for ( i = 0; i < d; i++ )
974: w[i] = m[i];
1.7 ! noro 975: for ( i = 0; i < bound && d >= 1; ) {
1.1 noro 976: for ( j = d - 1, r = 0; j >= 0; j-- ) {
977: DSAB(q,r,w[j],w[j],r)
978: }
979: c[i++] = (int)r;
980: if ( !w[d-1] )
981: d--;
982: }
983: if ( SGN((Q)COEF(dc)) < 0 )
984: for (i = 0, br = 0; i < bound; i++ )
985: if ( ( s = -(c[i] + br) ) < 0 ) {
986: c[i] = s + q; br = 1;
987: } else {
988: c[i] = 0; br = 0;
989: }
990: }
991: }
992:
1.6 noro 993: void modfctrp(P p,int mod,int flag,DCP *dcp)
1.1 noro 994: {
995: int cm,n,i,j,k;
996: DCP dc,dc0;
997: P zp;
998: Q c,q;
999: UM mp;
1000: UM *tl;
1001: struct oDUM *udc,*udc1;
1002:
1003: if ( !p ) {
1004: *dcp = 0; return;
1005: }
1006: ptozp(p,1,&c,&zp);
1007: if ( DN(c) || !(cm = rem(NM(c),mod)) ) {
1008: *dcp = 0; return;
1009: }
1010: mp = W_UMALLOC(UDEG(p));
1011: ptoum(mod,zp,mp);
1012: if ( (n = DEG(mp)) < 0 ) {
1013: *dcp = 0; return;
1014: } else if ( n == 0 ) {
1015: cm = dmar(cm,COEF(mp)[0],0,mod); STOQ(cm,q);
1016: NEWDC(dc); COEF(dc) = (P)q; DEG(dc) = ONE;
1017: NEXT(dc) = 0; *dcp = dc;
1018: return;
1019: }
1020: if ( COEF(mp)[n] != 1 ) {
1021: cm = dmar(cm,COEF(mp)[n],0,mod);
1022: i = invm(COEF(mp)[n],mod);
1023: for ( j = 0; j <= n; j++ )
1024: COEF(mp)[j] = dmar(COEF(mp)[j],i,0,mod);
1025: }
1026: W_CALLOC(n+1,struct oDUM,udc);
1027: gensqfrum(mod,mp,udc);
1028: switch ( flag ) {
1029: case FCTR:
1030: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1031: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1032: for ( i = 0,j = 0; udc[i].f; i++ )
1033: if ( DEG(udc[i].f) == 1 ) {
1034: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1035: } else {
1036: bzero((char *)tl,(n+1)*sizeof(UM));
1037: berlemain(mod,udc[i].f,tl);
1038: for ( k = 0; tl[k]; k++, j++ ) {
1039: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1040: }
1041: }
1042: udc = udc1; break;
1043: case SQFR:
1044: break;
1045: case DDD:
1046: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1047: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1048: for ( i = 0,j = 0; udc[i].f; i++ )
1049: if ( DEG(udc[i].f) == 1 ) {
1050: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1051: } else {
1052: bzero((char *)tl,(n+1)*sizeof(UM));
1053: ddd(mod,udc[i].f,tl);
1054: for ( k = 0; tl[k]; k++, j++ ) {
1055: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1056: }
1057: }
1058: udc = udc1; break;
1059: case NEWDDD:
1060: tl = (UM *)ALLOCA((n+1)*sizeof(UM));
1061: W_CALLOC(DEG(mp)+1,struct oDUM,udc1);
1062: for ( i = 0,j = 0; udc[i].f; i++ )
1063: if ( DEG(udc[i].f) == 1 ) {
1064: udc1[j].f = udc[i].f; udc1[j].n = udc[i].n; j++;
1065: } else {
1066: bzero((char *)tl,(n+1)*sizeof(UM));
1067: if ( mod == 2 )
1068: berlemain(mod,udc[i].f,tl);
1069: else
1070: newddd(mod,udc[i].f,tl);
1071: for ( k = 0; tl[k]; k++, j++ ) {
1072: udc1[j].f = tl[k]; udc1[j].n = udc[i].n;
1073: }
1074: }
1075: udc = udc1; break;
1076: }
1077: NEWDC(dc0); STOQ(cm,q); COEF(dc0) = (P)q; DEG(dc0) = ONE; dc = dc0;
1078: for ( n = 0; udc[n].f; n++ ) {
1079: NEWDC(NEXT(dc)); dc = NEXT(dc);
1080: STOQ(udc[n].n,DEG(dc)); umtop(VR(p),udc[n].f,&COEF(dc));
1081: }
1082: NEXT(dc) = 0; *dcp = dc0;
1083: }
1084:
1.6 noro 1085: void gensqfrum(int mod,UM p,struct oDUM *dc)
1.1 noro 1086: {
1087: int n,i,j,d;
1088: UM t,s,g,f,f1,b;
1089:
1090: if ( (n = DEG(p)) == 1 ) {
1091: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
1092: return;
1093: }
1094: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
1095: f = W_UMALLOC(n); f1 = W_UMALLOC(n); b = W_UMALLOC(n);
1096: diffum(mod,p,t); cpyum(p,s); Gcdum(mod,t,s,g);
1097: if ( !DEG(g) ) {
1098: dc[0].f = UMALLOC(DEG(p)); cpyum(p,dc[0].f); dc[0].n = 1;
1099: return;
1100: }
1101: cpyum(p,b); cpyum(p,t); Divum(mod,t,g,f);
1102: for ( i = 0, d = 0; DEG(f); i++ ) {
1103: while ( 1 ) {
1104: cpyum(b,t);
1105: if ( Divum(mod,t,f,s) >= 0 )
1106: break;
1107: else {
1108: cpyum(s,b); d++;
1109: }
1110: }
1111: cpyum(b,t); cpyum(f,s); Gcdum(mod,t,s,f1);
1112: Divum(mod,f,f1,s); cpyum(f1,f);
1113: dc[i].f = UMALLOC(DEG(s)); cpyum(s,dc[i].f); dc[i].n = d;
1114: }
1115: if ( DEG(b) > 0 ) {
1116: d = 1;
1117: while ( 1 ) {
1118: cpyum(b,t);
1119: for ( j = DEG(t); j >= 0; j-- )
1120: if ( COEF(t)[j] && (j % mod) )
1121: break;
1122: if ( j >= 0 )
1123: break;
1124: else {
1125: DEG(s) = DEG(t)/mod;
1126: for ( j = 0; j <= DEG(t); j++ )
1127: COEF(s)[j] = COEF(t)[j*mod];
1128: cpyum(s,b); d *= mod;
1129: }
1130: }
1131: gensqfrum(mod,b,dc+i);
1132: for ( j = i; dc[j].f; j++ )
1133: dc[j].n *= d;
1134: }
1135: }
1136:
1137: #if 0
1.6 noro 1138: void srchum(int mod,UM p1,UM p2,UM gr)
1.1 noro 1139: {
1140: UM m,m1,m2,q,r,t,g1,g2;
1141: int lc,d,d1,d2,i,j,k,l,l1,l2,l3,tmp,adj;
1142: V v;
1143:
1144: d = MAX(DEG(p1),DEG(p2));
1145: g1 = W_UMALLOC(d); g2 = W_UMALLOC(d);
1146: bzero((char *)g1,(d+2)*sizeof(int)); bzero((char *)g2,(d+2)*sizeof(int));
1147: if ( d == DEG(p1) ) {
1148: cpyum(p1,g1); cpyum(p2,g2);
1149: } else {
1150: cpyum(p1,g2); cpyum(p2,g1);
1151: }
1152: if ( ( d1 = DEG(g1) ) > ( d2 = DEG(g2) ) ) {
1153: j = d1 - 1; adj = 1;
1154: } else
1155: j = d2;
1156: lc = 1;
1157: r = W_UMALLOC(d1+d2); q = W_UMALLOC(d1+d2);
1158: m1 = W_UMALLOC(d1+d2); t = W_UMALLOC(d1+d2);
1159: bzero((char *)r,(d1+d2+2)*sizeof(int)); bzero((char *)q,(d1+d2+2)*sizeof(int));
1160: bzero((char *)m1,(d1+d2+2)*sizeof(int)); bzero((char *)t,(d1+d2+2)*sizeof(int));
1161: m = W_UMALLOC(0); bzero((char *)m,2*sizeof(int));
1162: adj = pwrm(mod,COEF(g2)[DEG(g2)],DEG(g1));
1163: DEG(m) = 0; COEF(m)[0] = invm(COEF(g2)[DEG(g2)],mod);
1164: Mulum(mod,g2,m,r); cpyum(r,g2);
1165: while ( 1 ) {
1166: if ( ( k = DEG(g2) ) < 0 ) {
1167: DEG(gr) = -1;
1168: return;
1169: }
1170: if ( k == j ) {
1171: if ( k == 0 ) {
1172: DEG(m) = 0; COEF(m)[0] = adj;
1173: Mulum(mod,g2,m,gr);
1174: return;
1175: } else {
1176: DEG(m) = 0;
1177: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
1178: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,t);
1179: DEG(m) = 0; COEF(m)[0] = dmb(mod,lc,lc,&tmp);
1180: Divum(mod,r,m,q); cpyum(g2,g1); cpyum(q,g2);
1181: lc = COEF(g1)[DEG(g1)]; j = k - 1;
1182: }
1183: } else {
1184: d = j - k;
1185: DEG(m) = 0; COEF(m)[0] = pwrm(mod,COEF(g2)[DEG(g2)],d);
1186: Mulum(mod,g2,m,m1); l = pwrm(mod,lc,d);
1187: DEG(m) = 0; COEF(m)[0] = l; Divum(mod,m1,m,t);
1188: if ( k == 0 ) {
1189: DEG(m) = 0; COEF(m)[0] = adj;
1190: Mulum(mod,t,m,gr);
1191: return;
1192: } else {
1193: DEG(m) = 0;
1194: COEF(m)[0] = pwrm(mod,COEF(g2)[k],DEG(g1)-k+1);
1195: Mulum(mod,g1,m,r); DEG(r) = Divum(mod,r,g2,q);
1196: l1 = dmb(mod,lc,lc,&tmp); l2 = dmb(mod,l,l1,&tmp);
1197: DEG(m) = 0; COEF(m)[0] = l2;
1198: Divum(mod,r,m,q); cpyum(t,g1); cpyum(q,g2);
1199: if ( d % 2 )
1200: for ( i = DEG(g2); i >= 0; i-- )
1201: COEF(g2)[i] = ( mod - COEF(g2)[i] ) % mod;
1202: lc = COEF(g1)[DEG(g1)]; j = k - 1;
1203: }
1204: }
1205: }
1206: }
1207:
1.6 noro 1208: UM *resberle(int mod,UM f,UM *fp)
1.1 noro 1209: {
1210: UM w,wg,ws,wf,f0,gcd,q,res;
1211: int n;
1212: register int i;
1213:
1214: n = DEG(f); wg = W_UMALLOC(n); mini(mod,f,wg);
1215: if ( DEG(wg) <= 0 ) {
1216: f0 = UMALLOC(n); cpyum(f,f0); *fp++ = f0;
1217: return ( fp );
1218: }
1219: f0 = W_UMALLOC(n); cpyum(f,f0);
1220: ws = W_UMALLOC(n); wf = W_UMALLOC(n);
1221: q = W_UMALLOC(n); gcd = W_UMALLOC(n);
1222: res = W_UMALLOC(2*n);
1223: srchum(mod,f,wg,res);
1224: for ( i = 0; i < mod; i++ ) {
1225: if ( substum(mod,res,i) )
1226: continue;
1227: cpyum(f0,wf); cpyum(wg,ws);
1228: COEF(ws)[0] = ( COEF(ws)[0] + mod - i ) % mod;
1229: Gcdum(mod,wf,ws,gcd);
1230: if ( DEG(gcd) > 0 ) {
1231: if ( DEG(gcd) < n ) {
1232: Divum(mod,f0,gcd,q); f0 = q; fp = resberle(mod,gcd,fp);
1233: }
1234: break;
1235: }
1236: }
1237: fp = resberle(mod,f0,fp);
1238: return ( fp );
1239: }
1240:
1.6 noro 1241: int substum(int mod,UM p,int a)
1.1 noro 1242: {
1243: int i,j,s;
1244: int *c;
1245:
1246: if ( DEG(p) < 0 )
1247: return 0;
1248: if ( DEG(p) == 0 )
1249: return COEF(p)[0];
1250: for ( i = DEG(p), c = COEF(p), s = c[i]; i >= 0; ) {
1251: for ( j = i--; (i>=0) && !c[i]; i-- );
1252: if ( i >= 0 )
1253: s = (s*pwrm(mod,a,j-i)%mod+c[i])%mod;
1254: else
1255: s = s*pwrm(mod,a,j)%mod;
1256: }
1257: return s;
1258: }
1259: #endif
1260:
1.6 noro 1261: void ddd(int mod,UM f,UM *r)
1.1 noro 1262: {
1263: register int i,j;
1264: int d,n;
1265: UM q,s,t,u,v,w,g,x,m;
1266: UM *base;
1267:
1268: n = DEG(f);
1269: if ( n == 1 ) {
1270: r[0] = UMALLOC(1); cpyum(f,r[0]); r[1] = 0; return;
1271: }
1272: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1273: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1274: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1275: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1276: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1277: for ( i = 2; i < n; i++ ) {
1278: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1279: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1280: }
1281: v = W_UMALLOC(n); cpyum(f,v);
1282: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1283: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1284: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1285: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1286: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1287: if ( COEF(w)[i] ) {
1288: Mulsum(mod,base[i],COEF(w)[i],s);
1289: addum(mod,s,t,u); cpyum(u,t);
1290: }
1291: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1292: if ( DEG(g) >= 1 ) {
1293: canzas(mod,g,d,base,r+j); j += DEG(g)/d;
1294: Divum(mod,v,g,q); cpyum(q,v);
1295: DEG(w) = Divum(mod,w,v,q);
1296: for ( i = 0; i < DEG(v); i++ )
1297: DEG(base[i]) = Divum(mod,base[i],v,q);
1298: }
1299: }
1300: if ( DEG(v) ) {
1301: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
1302: }
1303: r[j] = 0;
1304: }
1305:
1306: #if 0
1.6 noro 1307: void canzas(int mod,UM f,int d,UM *base,UM *r)
1.1 noro 1308: {
1309: UM t,s,u,w,g,o,q;
1310: N n1,n2,n3,n4,n5;
1311: UM *b;
1312: int n,m,i;
1313:
1314: if ( DEG(f) == d ) {
1315: r[0] = UMALLOC(d); cpyum(f,r[0]);
1316: return;
1317: } else {
1318: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
1319: for ( i = 0, m = 0; i < n; i++ )
1320: m = MAX(m,DEG(base[i]));
1321: q = W_UMALLOC(m);
1322: for ( i = 0; i < n; i++ ) {
1323: b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
1324: DEG(b[i]) = Divum(mod,b[i],f,q);
1325: }
1326: t = W_UMALLOC(2*d);
1327: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
1328: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
1329: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
1330: STON(mod,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
1331: STON(2,n4); divsn(n3,n4,&n5);
1332: while ( 1 ) {
1333: randum(mod,2*d,t); spwrum(mod,f,b,t,n5,s);
1334: subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
1335: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
1336: canzas(mod,g,d,b,r);
1337: cpyum(f,w); Divum(mod,w,g,s);
1338: canzas(mod,s,d,b,r+DEG(g)/d);
1339: return;
1340: }
1341: }
1342: }
1343: }
1344: #else
1.6 noro 1345: void canzas(int mod,UM f,int d,UM *base,UM *r)
1.1 noro 1346: {
1347: UM t,s,u,w,g,o,q;
1348: N n1,n2,n3,n4,n5;
1349: UM *b;
1350: int n,m,i;
1351:
1352: if ( DEG(f) == d ) {
1353: r[0] = UMALLOC(d); cpyum(f,r[0]);
1354: return;
1355: } else {
1356: n = DEG(f); b = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)b,n*sizeof(UM));
1357: for ( i = 0, m = 0; i < n; i++ )
1358: m = MAX(m,DEG(base[i]));
1359: q = W_UMALLOC(m);
1360: for ( i = 0; i < n; i++ ) {
1361: b[i] = W_UMALLOC(DEG(base[i])); cpyum(base[i],b[i]);
1362: DEG(b[i]) = Divum(mod,b[i],f,q);
1363: }
1364: t = W_UMALLOC(2*d);
1365: s = W_UMALLOC(DEG(f)); u = W_UMALLOC(DEG(f));
1366: w = W_UMALLOC(DEG(f)); g = W_UMALLOC(DEG(f));
1367: o = W_UMALLOC(0); DEG(o) = 0; COEF(o)[0] = 1;
1368: STON(mod,n1); pwrn(n1,d,&n2); subn(n2,ONEN,&n3);
1369: STON(2,n4); divsn(n3,n4,&n5);
1370: while ( 1 ) {
1371: randum(mod,2*d,t); spwrum0(mod,f,t,n5,s);
1372: subum(mod,s,o,u); cpyum(f,w); Gcdum(mod,w,u,g);
1373: if ( (DEG(g) >= 1) && (DEG(g) < DEG(f)) ) {
1374: canzas(mod,g,d,b,r);
1375: cpyum(f,w); Divum(mod,w,g,s);
1376: canzas(mod,s,d,b,r+DEG(g)/d);
1377: return;
1378: }
1379: }
1380: }
1381: }
1382: #endif
1383:
1.6 noro 1384: void randum(int mod,int d,UM p)
1.1 noro 1385: {
1386: unsigned int n;
1387: int i;
1388:
1389: n = ((unsigned int)random()) % d; DEG(p) = n; COEF(p)[n] = 1;
1390: for ( i = 0; i < (int)n; i++ )
1391: COEF(p)[i] = ((unsigned int)random()) % mod;
1392: }
1393:
1.6 noro 1394: void pwrmodum(int mod,UM p,int e,UM f,UM pr)
1.1 noro 1395: {
1396: UM wt,ws,q;
1397:
1398: if ( e == 0 ) {
1399: DEG(pr) = 0; COEF(pr)[0] = 1;
1400: } else if ( DEG(p) < 0 )
1401: DEG(pr) = -1;
1402: else if ( e == 1 ) {
1403: q = W_UMALLOC(DEG(p)); cpyum(p,pr);
1404: DEG(pr) = divum(mod,pr,f,q);
1405: } else if ( DEG(p) == 0 ) {
1406: DEG(pr) = 0; COEF(pr)[0] = pwrm(mod,COEF(p)[0],e);
1407: } else {
1408: wt = W_UMALLOC(2*DEG(f)); ws = W_UMALLOC(2*DEG(f));
1409: q = W_UMALLOC(2*DEG(f));
1410: pwrmodum(mod,p,e/2,f,wt);
1411: if ( !(e%2) ) {
1412: mulum(mod,wt,wt,pr); DEG(pr) = divum(mod,pr,f,q);
1413: } else {
1414: mulum(mod,wt,wt,ws); DEG(ws) = divum(mod,ws,f,q);
1415: mulum(mod,ws,p,pr); DEG(pr) = divum(mod,pr,f,q);
1416: }
1417: }
1418: }
1419:
1.6 noro 1420: void spwrum(int mod,UM m,UM *base,UM f,N e,UM r)
1.1 noro 1421: {
1422: int a,n,i;
1423: N e1,an;
1424: UM t,s,u,q,r1,r2;
1425:
1426: if ( !e ) {
1427: DEG(r) = 0; COEF(r)[0] = 1;
1428: } else if ( UNIN(e) )
1429: cpyum(f,r);
1430: else if ( (PL(e) == 1) && (BD(e)[0] < (unsigned int)mod) )
1431: spwrum0(mod,m,f,e,r);
1432: else {
1433: a = divin(e,mod,&e1); STON(a,an);
1434: n = DEG(m); t = W_UMALLOC(n); s = W_UMALLOC(n);
1435: u = W_UMALLOC(2*n); q = W_UMALLOC(2*n);
1436: for ( DEG(t) = -1, i = 0; i <= DEG(f); i++ )
1437: if ( COEF(f)[i] ) {
1438: Mulsum(mod,base[i],COEF(f)[i],s);
1439: addum(mod,s,t,u); cpyum(u,t);
1440: }
1441: r1 = W_UMALLOC(n); spwrum0(mod,m,f,an,r1);
1442: r2 = W_UMALLOC(n); spwrum(mod,m,base,t,e1,r2);
1443: Mulum(mod,r1,r2,u); DEG(u) = Divum(mod,u,m,q);
1444: cpyum(u,r);
1445: }
1446: }
1447:
1.6 noro 1448: void spwrum0(int mod,UM m,UM f,N e,UM r)
1.1 noro 1449: {
1450: UM t,s,q;
1451: N e1;
1452: int a;
1453:
1454: if ( !e ) {
1455: DEG(r) = 0; COEF(r)[0] = 1;
1456: } else if ( UNIN(e) )
1457: cpyum(f,r);
1458: else {
1459: a = divin(e,2,&e1);
1460: t = W_UMALLOC(2*DEG(m)); spwrum0(mod,m,f,e1,t);
1461: s = W_UMALLOC(2*DEG(m)); q = W_UMALLOC(2*DEG(m));
1462: Mulum(mod,t,t,s); DEG(s) = Divum(mod,s,m,q);
1463: if ( a ) {
1464: Mulum(mod,s,f,t); DEG(t) = Divum(mod,t,m,q); cpyum(t,r);
1465: } else
1466: cpyum(s,r);
1467: }
1468: }
1469:
1470: #if 0
1.6 noro 1471: void Mulum(int mod,UM p1,UM p2,UM pr)
1.1 noro 1472: {
1473: register int *pc1,*pcr;
1474: register int mul,i,j,d1,d2;
1475: int *c1,*c2,*cr;
1476:
1477: if ( ( (d1 = DEG(p1)) < 0) || ( (d2 = DEG(p2)) < 0 ) ) {
1478: DEG(pr) = -1;
1479: return;
1480: }
1481: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
1482: bzero((char *)cr,(d1+d2+1)*sizeof(int));
1483: for ( i = 0; i <= d2; i++, cr++ )
1484: if ( mul = *c2++ )
1485: for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ )
1486: *pcr = (*pc1 * mul + *pcr) % mod;
1487: DEG(pr) = d1 + d2;
1488: }
1489:
1.6 noro 1490: void Mulsum(int mod,UM p,int n,UM pr)
1.1 noro 1491: {
1492: register int *sp,*dp;
1493: register int i;
1494:
1495: for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i;
1496: i >= 0; i--, dp--, sp-- )
1497: *dp = (*sp * n) % mod;
1498: }
1499:
1.6 noro 1500: int Divum(int mod,UM p1,UM p2,UM pq)
1.1 noro 1501: {
1502: register int *pc1,*pct;
1503: register int tmp,i,j,inv;
1504: int *c1,*c2,*ct;
1505: int d1,d2,dd,hd;
1506:
1507: if ( (d1 = DEG(p1)) < (d2 = DEG(p2)) ) {
1508: DEG(pq) = -1;
1509: return( d1 );
1510: }
1511: c1 = COEF(p1); c2 = COEF(p2); dd = d1-d2;
1512: if ( ( hd = c2[d2] ) != 1 ) {
1513: inv = invm(hd,mod);
1514: for ( pc1 = c2 + d2; pc1 >= c2; pc1-- )
1515: *pc1 = (*pc1 * inv) % mod;
1516: } else
1517: inv = 1;
1518: for ( i = dd, ct = c1+d1; i >= 0; i-- )
1519: if ( tmp = *ct-- ) {
1520: tmp = mod - tmp;
1521: for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- )
1522: *pct = (*pc1 * tmp + *pct) % mod;
1523: }
1524: if ( inv != 1 ) {
1525: for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ )
1526: *pc1 = (*pc1 * inv) % mod;
1527: for ( pc1 = c2, pct = c2+d2, inv = hd; pc1 <= pct; pc1++ )
1528: *pc1 = (*pc1 * inv) % mod;
1529: }
1530: for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- );
1531: for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- )
1532: *pct-- = *pc1--;
1533: return( i );
1534: }
1535:
1.6 noro 1536: void Gcdum(int mod,UM p1,UM p2,UM pr)
1.1 noro 1537: {
1538: register int *sp,*dp;
1539: register int i,inv;
1540: UM t1,t2,q,tum;
1541: int drem;
1542:
1543: if ( DEG(p1) < 0 )
1544: cpyum(p2,pr);
1545: else if ( DEG(p2) < 0 )
1546: cpyum(p1,pr);
1547: else {
1548: if ( DEG(p1) >= DEG(p2) ) {
1549: t1 = p1; t2 = p2;
1550: } else {
1551: t1 = p2; t2 = p1;
1552: }
1553: q = W_UMALLOC(DEG(t1));
1554: while ( ( drem = Divum(mod,t1,t2,q) ) >= 0 ) {
1555: tum = t1; t1 = t2; t2 = tum; DEG(t2) = drem;
1556: }
1557: inv = invm(COEF(t2)[DEG(t2)],mod);
1558: Mulsum(mod,t2,inv,pr);
1559: }
1560: }
1561: #endif
1562:
1.6 noro 1563: void mult_mod_tab(UM p,int mod,UM *tab,UM r,int d)
1.1 noro 1564: {
1565: UM w,w1,c;
1566: int n,i;
1567: int *pc;
1568:
1569: w = W_UMALLOC(d); w1 = W_UMALLOC(d);
1570: c = W_UMALLOC(1); DEG(c) = 0;
1571: n = DEG(p); DEG(r) = -1;
1572: for ( i = 0, pc = COEF(p); i <= n; i++ )
1573: if ( pc[i] ) {
1574: COEF(c)[0] = pc[i];
1575: mulum(mod,tab[i],c,w);
1576: addum(mod,r,w,w1);
1577: cpyum(w1,r);
1578: }
1579: }
1580:
1.6 noro 1581: void make_qmat(UM p,int mod,UM *tab,int ***mp)
1.1 noro 1582: {
1583: int n,i,j;
1584: int *c;
1585: UM q,r;
1586: int **mat;
1587:
1588: n = DEG(p);
1589: *mp = mat = almat(n,n);
1590: for ( j = 0; j < n; j++ ) {
1591: r = W_UMALLOC(DEG(tab[j])); q = W_UMALLOC(DEG(tab[j]));
1592: cpyum(tab[j],r); DEG(r) = divum(mod,r,p,q);
1593: for ( i = 0, c = COEF(r); i <= DEG(r); i++ )
1594: mat[i][j] = c[i];
1595: }
1596: for ( i = 0; i < n; i++ )
1597: mat[i][i] = (mat[i][i]+mod-1) % mod;
1598: }
1599:
1.6 noro 1600: void null_mod(int **mat,int mod,int n,int *ind)
1.1 noro 1601: {
1602: int i,j,l,s,h,inv;
1603: int *t,*u;
1604:
1605: bzero((char *)ind,n*sizeof(int));
1606: ind[0] = 0;
1607: for ( i = j = 0; j < n; i++, j++ ) {
1608: for ( ; j < n; j++ ) {
1609: for ( l = i; l < n; l++ )
1610: if ( mat[l][j] )
1611: break;
1612: if ( l < n ) {
1613: t = mat[i]; mat[i] = mat[l]; mat[l] = t; break;
1614: } else
1615: ind[j] = 1;
1616: }
1617: if ( j == n )
1618: break;
1619: inv = invm(mat[i][j],mod);
1620: for ( s = j, t = mat[i]; s < n; s++ )
1621: t[s] = dmar(t[s],inv,0,mod);
1622: for ( l = 0; l < n; l++ ) {
1623: if ( l == i )
1624: continue;
1625: for ( s = j, u = mat[l], h = (mod-u[j])%mod; s < n; s++ )
1626: u[s] = dmar(h,t[s],u[s],mod);
1627: }
1628: }
1629: }
1630:
1.6 noro 1631: void null_to_sol(int **mat,int *ind,int mod,int n,UM *r)
1.1 noro 1632: {
1633: int i,j,k,l;
1634: int *c;
1635: UM w;
1636:
1637: for ( i = 0, l = 0; i < n; i++ ) {
1638: if ( !ind[i] )
1639: continue;
1640: w = UMALLOC(n);
1641: for ( j = k = 0, c = COEF(w); j < n; j++ )
1642: if ( ind[j] )
1643: c[j] = 0;
1644: else
1645: c[j] = mat[k++][i];
1646: c[i] = mod-1;
1647: for ( j = n; j >= 0; j-- )
1648: if ( c[j] )
1649: break;
1650: DEG(w) = j;
1651: r[l++] = w;
1652: }
1653: }
1654: /*
1655: make_qmat(p,mod,tab,mp)
1656: null_mod(mat,mod,n,ind)
1657: null_to_sol(mat,ind,mod,n,r)
1658: */
1659:
1.6 noro 1660: void newddd(int mod,UM f,UM *r)
1.1 noro 1661: {
1662: register int i,j;
1663: int d,n;
1664: UM q,s,t,u,v,w,g,x,m;
1665: UM *base;
1666:
1667: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1668: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1669: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1670: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1671: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1672: for ( i = 2; i < n; i++ ) {
1673: /* fprintf(stderr,"i=%d\n",i); */
1674: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1675: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1676: }
1677: v = W_UMALLOC(n); cpyum(f,v);
1678: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1679: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1680: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1681: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1682: /* fprintf(stderr,"d=%d\n",d); */
1683: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1684: if ( COEF(w)[i] ) {
1685: Mulsum(mod,base[i],COEF(w)[i],s);
1686: addum(mod,s,t,u); cpyum(u,t);
1687: }
1688: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1689: if ( DEG(g) >= 1 ) {
1690: berlekamp(g,mod,d,base,r+j); j += DEG(g)/d;
1691: Divum(mod,v,g,q); cpyum(q,v);
1692: DEG(w) = Divum(mod,w,v,q);
1693: for ( i = 0; i < DEG(v); i++ )
1694: DEG(base[i]) = Divum(mod,base[i],v,q);
1695: }
1696: }
1697: if ( DEG(v) ) {
1698: r[j] = UMALLOC(DEG(v)); cpyum(v,r[j]); j++;
1699: }
1700: r[j] = 0;
1701: }
1702:
1.6 noro 1703: int nfctr_mod(UM f,int mod)
1.1 noro 1704: {
1705: register int i,j;
1706: int d,n;
1707: UM q,s,t,u,v,w,g,x,m;
1708: UM *base;
1709:
1710: n = DEG(f); base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1711: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1712: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1713: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1714: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1715: for ( i = 2; i < n; i++ ) {
1716: /* fprintf(stderr,"i=%d\n",i); */
1717: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1718: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1719: }
1720: v = W_UMALLOC(n); cpyum(f,v);
1721: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1722: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1723: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1724: for ( j = 0, d = 1; 2*d <= DEG(v); d++ ) {
1725: /* fprintf(stderr,"d=%d\n",d); */
1726: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1727: if ( COEF(w)[i] ) {
1728: Mulsum(mod,base[i],COEF(w)[i],s);
1729: addum(mod,s,t,u); cpyum(u,t);
1730: }
1731: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1732: if ( DEG(g) >= 1 ) {
1733: j += DEG(g)/d;
1734: Divum(mod,v,g,q); cpyum(q,v);
1735: DEG(w) = Divum(mod,w,v,q);
1736: for ( i = 0; i < DEG(v); i++ )
1737: DEG(base[i]) = Divum(mod,base[i],v,q);
1738: }
1739: }
1740: if ( DEG(v) ) j++;
1741: return j;
1742: }
1743:
1.6 noro 1744: int irred_check(UM f,int mod)
1.1 noro 1745: {
1746: register int i,j;
1747: int d,n;
1748: UM q,s,t,u,v,w,g,x,m,f1,b;
1749: UM *base;
1750:
1751: if ( (n = DEG(f)) == 1 )
1752: return 1;
1753: t = W_UMALLOC(n); s = W_UMALLOC(n); g = W_UMALLOC(n);
1754: f1 = W_UMALLOC(n); b = W_UMALLOC(n);
1755: diffum(mod,f,t); cpyum(f,s); Gcdum(mod,t,s,g);
1756: if ( DEG(g) )
1757: return 0;
1758:
1759: base = (UM *)ALLOCA(n*sizeof(UM)); bzero((char *)base,n*sizeof(UM));
1760: w = W_UMALLOC(2*n); q = W_UMALLOC(2*n); m = W_UMALLOC(2*n);
1761: base[0] = W_UMALLOC(0); DEG(base[0]) = 0; COEF(base[0])[0] = 1;
1762: t = W_UMALLOC(1); DEG(t) = 1; COEF(t)[0] = 0; COEF(t)[1] = 1;
1763: pwrmodum(mod,t,mod,f,w); base[1] = W_UMALLOC(DEG(w)); cpyum(w,base[1]);
1764: for ( i = 2; i < n; i++ ) {
1765: /* fprintf(stderr,"i=%d\n",i); */
1766: mulum(mod,base[i-1],base[1],m); DEG(m) = divum(mod,m,f,q);
1767: base[i] = W_UMALLOC(DEG(m)); cpyum(m,base[i]);
1768: }
1769: v = W_UMALLOC(n); cpyum(f,v);
1770: DEG(w) = 1; COEF(w)[0] = 0; COEF(w)[1] = 1;
1771: x = W_UMALLOC(1); DEG(x) = 1; COEF(x)[0] = 0; COEF(x)[1] = 1;
1772: t = W_UMALLOC(n); s = W_UMALLOC(n); u = W_UMALLOC(n); g = W_UMALLOC(n);
1773: for ( j = 0, d = 1; 2*d <= n; d++ ) {
1774: /* fprintf(stderr,"d=%d\n",d); */
1775: for ( DEG(t) = -1, i = 0; i <= DEG(w); i++ )
1776: if ( COEF(w)[i] ) {
1777: Mulsum(mod,base[i],COEF(w)[i],s);
1778: addum(mod,s,t,u); cpyum(u,t);
1779: }
1780: cpyum(t,w); cpyum(v,s); subum(mod,w,x,t); Gcdum(mod,s,t,g);
1781: if ( DEG(g) >= 1 )
1782: return 0;
1783: }
1784: return 1;
1785: }
1786:
1.6 noro 1787: int berlekamp(UM p,int mod,int df,UM *tab,UM *r)
1.1 noro 1788: {
1789: int n,i,j,k,nf,d,nr;
1790: int **mat;
1791: int *ind;
1792: UM mp,w,q,gcd,w1,w2;
1793: UM *u;
1794: int *root;
1795:
1796: n = DEG(p);
1797: ind = ALLOCA(n*sizeof(int));
1798: make_qmat(p,mod,tab,&mat);
1799: null_mod(mat,mod,n,ind);
1800: for ( i = 0, d = 0; i < n; i++ )
1801: if ( ind[i] )
1802: d++;
1803: if ( d == 1 ) {
1804: r[0] = UMALLOC(n); cpyum(p,r[0]); return 1;
1805: }
1806: u = ALLOCA(d*sizeof(UM *));
1807: r[0] = UMALLOC(n); cpyum(p,r[0]);
1808: null_to_sol(mat,ind,mod,n,u);
1809: root = ALLOCA(d*sizeof(int));
1810: w = W_UMALLOC(n); mp = W_UMALLOC(d);
1811: w1 = W_UMALLOC(n); w2 = W_UMALLOC(n);
1812: for ( i = 1, nf = 1; i < d; i++ ) {
1813: minipoly_mod(mod,u[i],p,mp);
1814: nr = find_root(mod,mp,root);
1815: for ( j = 0; j < nf; j++ ) {
1816: if ( DEG(r[j]) == df )
1817: continue;
1818: for ( k = 0; k < nr; k++ ) {
1819: cpyum(u[i],w1); cpyum(r[j],w2);
1820: COEF(w1)[0] = (mod-root[k]) % mod;
1821: gcdum(mod,w1,w2,w);
1822: if ( DEG(w) > 0 && DEG(w) < DEG(r[j]) ) {
1823: gcd = UMALLOC(DEG(w));
1824: q = UMALLOC(DEG(r[j])-DEG(w));
1825: cpyum(w,gcd); divum(mod,r[j],w,q);
1826: r[j] = q; r[nf++] = gcd;
1827: }
1828: if ( nf == d )
1829: return d;
1830: }
1831: }
1832: }
1.6 noro 1833: /* NOTREACHED */
1834: error("berlekamp : cannot happen");
1835: return -1;
1.1 noro 1836: }
1837:
1.6 noro 1838: void minipoly_mod(int mod,UM f,UM p,UM mp)
1.1 noro 1839: {
1840: struct p_pair *list,*l,*l1,*lprev;
1841: int n,d;
1842: UM u,p0,p1,np0,np1,q,w;
1843:
1844: list = (struct p_pair *)MALLOC(sizeof(struct p_pair));
1845: list->p0 = u = W_UMALLOC(0); DEG(u) = 0; COEF(u)[0] = 1;
1846: list->p1 = W_UMALLOC(0); cpyum(list->p0,list->p1);
1847: list->next = 0;
1848: n = DEG(p); w = UMALLOC(2*n);
1849: p0 = UMALLOC(2*n); cpyum(list->p0,p0);
1850: p1 = UMALLOC(2*n); cpyum(list->p1,p1);
1851: q = W_UMALLOC(2*n);
1852: while ( 1 ) {
1853: COEF(p0)[DEG(p0)] = 0; DEG(p0)++; COEF(p0)[DEG(p0)] = 1;
1854: mulum(mod,f,p1,w); DEG(w) = divum(mod,w,p,q); cpyum(w,p1);
1855: np0 = UMALLOC(n); np1 = UMALLOC(n);
1856: lnf_mod(mod,n,p0,p1,list,np0,np1);
1857: if ( DEG(np1) < 0 ) {
1858: cpyum(np0,mp); return;
1859: } else {
1860: l1 = (struct p_pair *)MALLOC(sizeof(struct p_pair));
1861: l1->p0 = np0; l1->p1 = np1;
1862: for ( l = list, lprev = 0, d = DEG(np1);
1863: l && (DEG(l->p1) > d); lprev = l, l = l->next );
1864: if ( lprev ) {
1865: lprev->next = l1; l1->next = l;
1866: } else {
1867: l1->next = list; list = l1;
1868: }
1869: }
1870: }
1871: }
1872:
1.6 noro 1873: void lnf_mod(int mod,int n,UM p0,UM p1,struct p_pair *list,UM np0,UM np1)
1.1 noro 1874: {
1875: int inv,h,d1;
1876: UM t0,t1,s0,s1;
1877: struct p_pair *l;
1878:
1879: cpyum(p0,np0); cpyum(p1,np1);
1880: t0 = W_UMALLOC(n); t1 = W_UMALLOC(n);
1881: s0 = W_UMALLOC(n); s1 = W_UMALLOC(n);
1882: for ( l = list; l; l = l->next ) {
1883: d1 = DEG(np1);
1884: if ( d1 == DEG(l->p1) ) {
1885: inv = invm((mod-COEF(l->p1)[d1])%mod,mod);
1886: h = dmar(COEF(np1)[d1],inv,0,mod);
1887: mulsum(mod,l->p0,h,t0); addum(mod,np0,t0,s0); cpyum(s0,np0);
1888: mulsum(mod,l->p1,h,t1); addum(mod,np1,t1,s1); cpyum(s1,np1);
1889: }
1890: }
1891: }
1892:
1.6 noro 1893: int find_root(int mod,UM p,int *root)
1.1 noro 1894: {
1895: UM *r;
1896: int i,j;
1897:
1898: r = ALLOCA((DEG(p)+1)*sizeof(UM));
1899: ddd(mod,p,r);
1900: for ( i = 0, j = 0; r[i]; i++ )
1901: if ( DEG(r[i]) == 1 )
1902: root[j++] = (mod - COEF(r[i])[0]) % mod;
1903: return j;
1904: }
1905:
1.6 noro 1906: void showum(UM p)
1.1 noro 1907: {
1908: int i;
1909: int *c;
1910:
1911: for ( i = DEG(p), c = COEF(p); i >= 0; i-- )
1912: if ( c[i] )
1913: printf("+%dx^%d",c[i],i);
1914: printf("\n");
1915: }
1916:
1.6 noro 1917: void showumat(int **mat,int n)
1.1 noro 1918: {
1919: int i,j;
1920:
1921: for ( i = 0; i < n; i++ ) {
1922: for ( j = 0; j < n; j++ )
1923: printf("%d ",mat[i][j]);
1924: printf("\n");
1925: }
1926: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>