1 #ifndef lint 2 static char sccsid[] = "@(#)pow.c 5.2 (Berkeley) 03/02/87"; 3 #endif not lint 4 5 #include <mp.h> 6 pow(a,b,c,d) MINT *a,*b,*c,*d; 7 { int i,j,n; 8 MINT x,y; 9 x.len=y.len=0; 10 xfree(d); 11 d->len=1; 12 d->val=xalloc(1,"pow"); 13 *d->val=1; 14 for(j=0;j<b->len;j++) 15 { n=b->val[b->len-j-1]; 16 for(i=0;i<15;i++) 17 { mult(d,d,&x); 18 mdiv(&x,c,&y,d); 19 if((n=n<<1)&0100000) 20 { mult(a,d,&x); 21 mdiv(&x,c,&y,d); 22 } 23 } 24 } 25 xfree(&x); 26 xfree(&y); 27 return; 28 } 29 rpow(a,n,b) MINT *a,*b; 30 { MINT x,y; 31 int i; 32 x.len=1; 33 x.val=xalloc(1,"rpow"); 34 *x.val=n; 35 y.len=n*a->len+4; 36 y.val=xalloc(y.len,"rpow2"); 37 for(i=0;i<y.len;i++) y.val[i]=0; 38 y.val[y.len-1]=010000; 39 xfree(b); 40 pow(a,&x,&y,b); 41 xfree(&x); 42 xfree(&y); 43 return; 44 } 45