int BigTochar (Big &x, char *c, int s) { int len = 0; int totlen = sizeof (int); // format: 4 bytes length, followed by the big if (s <= sizeof (int)) return -1; // Code assumes epoint contains either nulls or bigs > 0 s -= sizeof (int); c += sizeof (int); if (x.iszero()) { len = 0; } else { len = to_binary (x, s, c, FALSE); } if (len < 0) return -1; memcpy ((char *)(c - sizeof(int)), (void *)&len, sizeof (int)); totlen += len; s -= len; c += len; // cout << "Len1 " << len << " x " << x; return totlen; }
GF2m4x pow(const GF2m4x& a,const Big& k) { int i,j,nb,n,nbw,nzs; GF2m4x u,u2,t[16]; if (k.iszero()) return (GF2m4x)1; u=a; if (k.isone()) return u; // // Prepare table for windowing // u2=(u*u); t[0]=u; for (i=1;i<16;i++) t[i]=u2*t[i-1]; // Left to right method - with windows nb=bits(k); if (nb>1) for (i=nb-2;i>=0;) { n=window(k,i,&nbw,&nzs,5); for (j=0;j<nbw;j++) u*=u; if (n>0) u*=t[n/2]; i-=nbw; if (nzs) { for (j=0;j<nzs;j++) u*=u; i-=nzs; } } return u; }
ostream& operator<<(ostream& s,const Ps_Big& p) { BOOL first=TRUE; Big a; term_ps_big *ptr=p.start; int pw; if (ptr==NULL) { s << "0"; return s; } while (ptr!=NULL) { a=ptr->an; if (a.iszero()) { ptr=ptr->next; continue; } if (a < (Big)0) { a=(-a); s << " - "; } else if (!first) s << " + "; first=FALSE; pw=ptr->n*p.pwr-p.offset; if (pw==0) { s << a; ptr=ptr->next; continue; } if (a==1) s << "x"; else s << a << "*x"; if (pw!=1) s << "^" << pw; ptr=ptr->next; } return s; }
ZZn2 powu(const ZZn2& x,const Big& e) { int i,j,nb,n,nbw,nzs; ZZn2 u,u2,t[11]; Big k,k3; if (e.iszero()) return (ZZn2)one(); k=e; if (e<0) k=-k; u=x; if (k.isone()) { if (e<0) u=conj(u); return u; } // // Prepare table for windowing // k3=3*k; u2=(u*u); t[0]=u; for (i=1;i<=10;i++) t[i]=u2*t[i-1]; nb=bits(k3); for (i=nb-2;i>=1;) { n=naf_window(k,k3,i,&nbw,&nzs,11); for (j=0;j<nbw;j++) u*=u; if (n>0) u*=t[n/2]; if (n<0) u*=conj(t[(-n)/2]); i-=nbw; if (nzs) { for (j=0;j<nzs;j++) u*=u; i-=nzs; } } if (e<0) u=conj(u); return u; }
Ps_Big operator*(Ps_Big& a,Ps_Big& b) { Ps_Big prod; Big t; term_ps_big *aptr,*bptr,*pos; int ka,kb,i,pa,pb,d,deg,g; if (a.IsInt()) { if (a.start==NULL) return prod; else return (a.start->an*b); } if (b.IsInt()) { if (b.start==NULL) return prod; else return (b.start->an*a); } g=igcd(a.pwr,b.pwr); deg=psN/g; #ifdef FFT if (deg>=FFT_BREAK_EVEN) { // use fast methods big *A,*B,*C; A=(big *)mr_alloc(deg,sizeof(big)); B=(big *)mr_alloc(deg,sizeof(big)); C=(big *)mr_alloc(deg,sizeof(big)); char *memc=(char *)memalloc(deg); for (i=0;i<deg;i++) C[i]=mirvar_mem(memc,i); ka=a.pwr/g; a.decompress(ka); aptr=a.start; while (aptr!=NULL) { d=aptr->n; if (d >= deg) break; A[d]=(aptr->an).getbig(); aptr=aptr->next; } kb=b.pwr/g; b.decompress(kb); bptr=b.start; while (bptr!=NULL) { d=bptr->n; if (d >= deg) break; B[d]=(bptr->an).getbig(); bptr=bptr->next; } mr_ps_big_mul(deg,A,B,C); pos=NULL; for (d=0;d<deg;d++) { t=C[d]; if (t.iszero()) continue; pos=prod.addterm(t,d*g,pos); } memkill(memc,deg); a.compress(ka); b.compress(kb); mr_free(C); mr_free(B); mr_free(A); } else { #endif bptr=b.start; while (bptr!=NULL) { aptr=a.start; pb=bptr->n*b.pwr-b.offset; pos=NULL; while (aptr!=NULL) { pa=aptr->n*a.pwr-a.offset; if (pb+pa>=psN) break; pos=prod.addterm(aptr->an*bptr->an,pa+pb,pos); aptr=aptr->next; } bptr=bptr->next; } #ifdef FFT } #endif if (prod.start!=NULL) prod.offset=a.offset+b.offset; return prod; }