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