int main() { FILE *fp; big e,n,g,a; brick binst; int window,nb,bits; miracl *mip=mirsys(100,0); n=mirvar(0); e=mirvar(0); a=mirvar(0); g=mirvar(0); fp=fopen("common.dss","rt"); fscanf(fp,"%d\n",&bits); mip->IOBASE=16; cinnum(n,fp); cinnum(g,fp); cinnum(g,fp); mip->IOBASE=10; printf("modulus is %d bits in length\n",logb2(n)); printf("Enter size of exponent in bits = "); scanf("%d",&nb); getchar(); printf("Enter window size in bits (1-10)= "); scanf("%d",&window); getchar(); if (!brick_init(&binst,g,n,window,nb)) { printf("Failed to initialize\n"); return 0; } printf("%d big numbers have been precomputed and stored\n",(1<<window)); bigbits(nb,e); /* random exponent */ printf("naive method\n"); powmod(g,e,n,a); cotnum(a,stdout); printf("Comb method\n"); pow_brick(&binst,e,a); brick_end(&binst); cotnum(a,stdout); return 0; }
int main() { FILE *fp; int ep,m,a,b,c; miracl *mip; epoint *g,*public; char ifname[50],ofname[50]; big a2,a6,q,x,y,v,u1,u2,r,s,hash; /* get public data */ fp=fopen("common2.ecs","r"); if (fp==NULL) { printf("file common2.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&m); mip=mirsys(3+abs(m)/MIRACL,0); a2=mirvar(0); a6=mirvar(0); q=mirvar(0); x=mirvar(0); y=mirvar(0); v=mirvar(0); u1=mirvar(0); u2=mirvar(0); s=mirvar(0); r=mirvar(0); hash=mirvar(0); mip->IOBASE=16; cinnum(a2,fp); cinnum(a6,fp); cinnum(q,fp); cinnum(x,fp); cinnum(y,fp); mip->IOBASE=10; fscanf(fp,"%d\n",&a); fscanf(fp,"%d\n",&b); fscanf(fp,"%d\n",&c); fclose(fp); ecurve2_init(m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE); /* initialise curve */ g=epoint2_init(); epoint2_set(x,y,0,g); /* initialise point of order q */ /* get public key of signer */ fp=fopen("public.ecs","r"); if (fp==NULL) { printf("file public.ecs does not exist\n"); return 0; } fscanf(fp,"%d",&ep); cinnum(x,fp); fclose(fp); public=epoint2_init();
int main() { FILE *fp; big e,n,a,b,x,y,r; epoint *g; ebrick binst; int i,d,ndig,nb,best,time,store,base,bits; miracl *mip=mirsys(50,0); n=mirvar(0); e=mirvar(0); a=mirvar(0); b=mirvar(0); x=mirvar(0); y=mirvar(0); r=mirvar(0); fp=fopen("common.ecs","r"); fscanf(fp,"%d\n",&bits); mip->IOBASE=16; cinnum(n,fp); cinnum(a,fp); cinnum(b,fp); cinnum(r,fp); cinnum(x,fp); cinnum(y,fp); mip->IOBASE=10; printf("modulus is %d bits in length\n",logb2(n)); printf("Enter size of exponent in bits = "); scanf("%d",&nb); getchar(); ebrick_init(&binst,x,y,a,b,n,nb); printf("%d big numbers have been precomputed and stored\n",binst.store); bigdig(nb,2,e); /* random exponent */ printf("naive method\n"); ecurve_init(a,b,n,MR_PROJECTIVE); g=epoint_init(); epoint_set(x,y,0,g); ecurve_mult(e,g,g); epoint_get(g,x,y); cotnum(x,stdout); cotnum(y,stdout); printf("Brickel et al method\n"); mul_brick(&binst,e,x,y); ebrick_end(&binst); cotnum(x,stdout); cotnum(y,stdout); return 0; }
void getprime(char *fname) { /* get prime details from file */ FILE *fp; int i; fp=fopen(fname,"r"); fscanf(fp,"%d\n",&np); for (i=0;i<np;i++) cinnum(pp[i],fp); fclose(fp); convert(1,p1); for (i=0;i<np;i++) multiply(p1,pp[i],p1); incr(p1,1,p); if (!isprime(p)) { printf("Huh - modulus is not prime!"); exit(0); } subdiv(p,3,lim1); premult(lim1,2,lim2); }
int main() { /* factoring program using Brents method */ long k,r,i,m,iter; big x,y,z,n,q,ys,c3; miracl *mip=mirsys(50,0); x=mirvar(mip,0); y=mirvar(mip,0); ys=mirvar(mip,0); z=mirvar(mip,0); n=mirvar(mip,0); q=mirvar(mip,0); c3=mirvar(mip,3); printf("input number to be factored\n"); cinnum(mip,n,stdin); if (isprime(mip,n)) { printf("this number is prime!\n"); return 0; } m=10L; r=1L; iter=0L; do { printf("iterations=%5ld",iter); convert(mip,1,q); do { copy(y,x); for (i=1L;i<=r;i++) mad(mip,y,y,c3,n,n,y); k=0; do { iter++; if (iter%10==0) printf("\b\b\b\b\b%5ld",iter); fflush(stdout); copy(y,ys); for (i=1L;i<=mr_min(m,r-k);i++) { mad(mip,y,y,c3,n,n,y); subtract(mip,y,x,z); mad(mip,z,q,q,n,n,q); } egcd(mip,q,n,z); k+=m; } while (k<r && size(z)==1); r*=2; } while (size(z)==1); if (compare(z,n)==0) do { /* back-track */ mad(mip,ys,ys,c3,n,n,ys); subtract(mip,ys,x,z); } while (egcd(mip,z,n,z)==1); if (!isprime(mip,z)) printf("\ncomposite factor "); else printf("\nprime factor "); cotnum(mip,z,stdout); if (compare(z,n)==0) return 0; divide(mip,n,z,n); divide(mip,y,n,n); } while (!isprime(mip,n)); printf("prime factor "); cotnum(mip,n,stdout); return 0; }
int main() { /* encode using public key */ big e,m,y,ke,mn,mx; FILE *ifile; FILE *ofile; static char line[500]; static char buff[256]; char ifname[13],ofname[13]; BOOL fli,last; int i,ipt,klen; mip=mirsys(100,0); e=mirvar(0); m=mirvar(0); y=mirvar(0); ke=mirvar(0); mn=mirvar(0); mx=mirvar(0); if ((ifile=fopen("public.key","rt"))==NULL) { printf("Unable to open file public.key\n"); return 0; } mip->IOBASE=16; cinnum(ke,ifile); fclose(ifile); nroot(ke,3,mn); multiply(mn,mn,m); multiply(mn,m,mx); subtract(mx,m,mx); klen=0; copy(mx,m); while (size(m)>0) { /* find key length in characters */ klen++; subdiv(m,128,m); } klen--; printf("file to be encoded = "); gets(ifname); fli=FALSE; if (strlen(ifname)>0) fli=TRUE; if (fli) { /* set up input file */ strcpy(ofname,ifname); strip(ofname); strcat(ofname,".rsa"); if ((ifile=fopen(ifname,"rt"))==NULL) { printf("Unable to open file %s\n",ifname); return 0; } printf("encoding message\n"); } else { /* accept input from keyboard */ ifile=stdin; do { printf("output filename = "); gets(ofname); } while (strlen(ofname)==0); strip(ofname); strcat(ofname,".rsa"); printf("input message - finish with cntrl z\n"); } ofile=fopen(ofname,"wt"); ipt=0; last=FALSE; while (!last) { /* encode line by line */ if (fgets(&line[ipt],132,ifile)==NULL) last=TRUE; if (line[ipt]==EOF) last=TRUE; ipt=strlen(line); if (ipt<klen && !last) continue; while (ipt>=klen) { /* chop up into klen-sized chunks and encode */ for (i=0;i<klen;i++) buff[i]=line[i]; buff[klen]='\0'; for (i=klen;i<=ipt;i++) line[i-klen]=line[i]; ipt-=klen; mip->IOBASE=128; cinstr(m,buff); power(m,3,ke,e); mip->IOBASE=16; cotnum(e,ofile); } if (last && ipt>0) { /* now deal with left overs */ mip->IOBASE=128; cinstr(m,line); if (compare(m,mn)<0) { /* pad out with random number if necessary */ bigrand(mn,y); multiply(mn,mn,e); subtract(e,y,e); multiply(mn,e,y); add(m,y,m); } power(m,3,ke,e); mip->IOBASE=16; cotnum(e,ofile); } } fclose(ofile); if (fli) fclose(ifile); return 0; }
int main() { FILE *fp; int m,a,b,c; big e,a2,a6,x,y,r; epoint *g; ebrick2 binst; int i,d,ndig,nb,best,time,store,base; miracl *mip=mirsys(50,0); e=mirvar(0); a2=mirvar(0); a6=mirvar(0); x=mirvar(0); y=mirvar(0); r=mirvar(0); fp=fopen("common2.ecs","r"); fscanf(fp,"%d\n",&m); mip->IOBASE=16; cinnum(a2,fp); cinnum(a6,fp); cinnum(r,fp); cinnum(x,fp); cinnum(y,fp); mip->IOBASE=10; fscanf(fp,"%d\n",&a); fscanf(fp,"%d\n",&b); fscanf(fp,"%d\n",&c); printf("modulus is %d bits in length\n",m); printf("Enter size of exponent in bits = "); scanf("%d",&nb); getchar(); ebrick2_init(&binst,x,y,a2,a6,m,a,b,c,nb); printf("%d big numbers have been precomputed and stored\n",binst.store); bigdig(nb,2,e); /* random exponent */ printf("naive method\n"); ecurve2_init(m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE); g=epoint2_init(); epoint2_set(x,y,0,g); ecurve2_mult(e,g,g); epoint2_get(g,x,y); cotnum(x,stdout); cotnum(y,stdout); zero(x); zero(y); printf("Brickel et al method\n"); mul2_brick(&binst,e,x,y); ebrick2_end(&binst); cotnum(x,stdout); cotnum(y,stdout); return 0; }
int main() { FILE *fp; char ifname[13],ofname[13]; big p,q,g,x,r,s,k,hash; long seed; int bits; miracl *mip; /* get public data */ fp=fopen("common.dss","r"); if (fp==NULL) { printf("file common.dss does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); mip=mirsys(3+bits/MIRACL,0); p=mirvar(0); q=mirvar(0); g=mirvar(0); x=mirvar(0); r=mirvar(0); s=mirvar(0); k=mirvar(0); hash=mirvar(0); mip->IOBASE=16; cinnum(p,fp); cinnum(q,fp); cinnum(g,fp); mip->IOBASE=10; fclose(fp); /* randomise */ printf("Enter 9 digit random number seed = "); scanf("%ld",&seed); getchar(); irand(seed); /* calculate r - this can be done offline, and hence amortized to almost nothing */ bigrand(q,k); powmod(g,k,p,r); /* see brick.c for method to speed this up */ divide(r,q,q); /* get private key of signer */ fp=fopen("private.dss","r"); if (fp==NULL) { printf("file private.dss does not exist\n"); return 0; } cinnum(x,fp); fclose(fp); /* calculate message digest */ printf("file to be signed = "); gets(ifname); strcpy(ofname,ifname); strip(ofname); strcat(ofname,".dss"); if ((fp=fopen(ifname,"rb"))==NULL) { printf("Unable to open file %s\n",ifname); return 0; } hashing(fp,hash); fclose(fp); /* calculate s */ xgcd(k,q,k,k,k); mad(x,r,hash,q,q,s); mad(s,k,k,q,q,s); fp=fopen(ofname,"w"); cotnum(r,fp); cotnum(s,fp); fclose(fp); mirexit(); return 0; }
int main() { /* factoring program using Lenstras Elliptic Curve method */ int phase,m,k,nc,iv,pos,btch,u,v; long i,p,pa,interval; big q,x,z,a,x1,z1,x2,z2,xt,zt,n,fvw; static big fu[1+MULT/2]; static BOOL cp[1+MULT/2]; mip=mirsys(30,0); q=mirvar(0); x=mirvar(0); z=mirvar(0); a=mirvar(0); x1=mirvar(0); z1=mirvar(0); x2=mirvar(0); z2=mirvar(0); n=mirvar(0); t=mirvar(0); s1=mirvar(0); d1=mirvar(0); s2=mirvar(0); d2=mirvar(0); ak=mirvar(0); xt=mirvar(0); zt=mirvar(0); fvw=mirvar(0); w=mirvar(0); gprime(LIMIT1); for (m=1;m<=MULT/2;m+=2) if (igcd(MULT,m)==1) { fu[m]=mirvar(0); cp[m]=TRUE; } else cp[m]=FALSE; printf("input number to be factored\n"); cinnum(n,stdin); if (isprime(n)) { printf("this number is prime!\n"); return 0; } prepare_monty(n); for (nc=1,k=6;k<100;k++) { /* try a new curve */ /* generating an elliptic curve */ u=k*k-5; v=4*k; convert(u,x); nres(x,x); convert(v,z); nres(z,z); nres_modsub(z,x,a); /* a=v-u */ copy(x,t); nres_modmult(x,x,x); nres_modmult(x,t,x); /* x=u^3 */ copy(z,t); nres_modmult(z,z,z); nres_modmult(z,t,z); /* z=v^3 */ copy(a,t); nres_modmult(t,t,t); nres_modmult(t,a,t); /* t=(v-u)^3 */ convert(3*u,a); nres(a,a); convert(v,ak); nres(ak,ak); nres_modadd(a,ak,a); nres_modmult(t,a,t); /* t=(v-u)^3.(3u+v) */ convert(u,a); nres(a,a); copy(a,ak); nres_modmult(a,a,a); nres_modmult(a,ak,a); /* a=u^3 */ convert(v,ak); nres(ak,ak); nres_modmult(a,ak,a); /* a=u^3.v */ nres_premult(a,16,a); nres_moddiv(t,a,ak); /* ak=(v-u)^3.(3u+v)/16u^3v */ nc++; phase=1; p=0; i=0; btch=50; printf("phase 1 - trying all primes less than %d\n",LIMIT1); printf("prime= %8ld",p); forever { /* main loop */ if (phase==1) { p=mip->PRIMES[i]; if (mip->PRIMES[i+1]==0) { /* now change gear */ phase=2; printf("\nphase 2 - trying last prime less than %ld\n", LIMIT2); printf("prime= %8ld",p); copy(x,xt); copy(z,zt); nres_modadd(x,z,s2); nres_modsub(x,z,d2); /* P = (s2,d2) */ duplication(s2,d2,x,z); nres_modadd(x,z,s1); nres_modsub(x,z,d1); /* 2.P = (s1,d1) */ nres_moddiv(x1,z1,fu[1]); /* fu[1] = x1/z1 */ addition(x1,z1,s1,d1,s2,d2,x2,z2); /* 3.P = (x2,z2) */ for (m=5;m<=MULT/2;m+=2) { /* calculate m.P = (x,z) and store fu[m] = x/z */ nres_modadd(x2,z2,s2); nres_modsub(x2,z2,d2); addition(x1,z1,s2,d2,s1,d1,x,z); copy(x2,x1); copy(z2,z1); copy(x,x2); copy(z,z2); if (!cp[m]) continue; copy(z2,fu[m]); nres_moddiv(x2,fu[m],fu[m]); } ellipse(xt,zt,MULT,x,z,x2,z2); nres_modadd(x,z,xt); nres_modsub(x,z,zt); /* MULT.P = (xt,zt) */ iv=(int)(p/MULT); if (p%MULT>MULT/2) iv++; interval=(long)iv*MULT; p=interval+1; ellipse(x,z,iv,x1,z1,x2,z2); /* (x1,z1) = iv.MULT.P */ nres_moddiv(x1,z1,fvw); /* fvw = x1/z1 */ nres_modsub(fvw,fu[p%MULT],q); marks(interval); btch*=100; i++; continue; } pa=p; while ((LIMIT1/p) > pa) pa*=p; ellipse(x,z,(int)pa,x1,z1,x2,z2); copy(x1,x); copy(z1,z); copy(z,q); } else { /* phase 2 - looking for last large prime factor of (p+1+d) */ p+=2; pos=(int)(p%MULT); if (pos>MULT/2) { /* increment giant step */ iv++; interval=(long)iv*MULT; p=interval+1; marks(interval); pos=1; nres_moddiv(x2,z2,fvw); nres_modadd(x2,z2,s2); nres_modsub(x2,z2,d2); addition(x1,z1,s2,d2,xt,zt,x,z); copy(x2,x1); copy(z2,z1); copy(x,x2); copy(z,z2); } if (!cp[pos]) continue; /* if neither interval +/- pos is prime, don't bother */ if (!plus[pos] && !minus[pos]) continue; nres_modsub(fvw,fu[pos],t); nres_modmult(q,t,q); } if (i++%btch==0) { /* try for a solution */ printf("\b\b\b\b\b\b\b\b%8ld",p); fflush(stdout); egcd(q,n,t); if (size(t)==1) { if (p>LIMIT2) break; else continue; } if (compare(t,n)==0) { printf("\ndegenerate case"); break; } printf("\nfactors are\n"); if (isprime(t)) printf("prime factor "); else printf("composite factor "); cotnum(t,stdout); divide(n,t,n); if (isprime(n)) printf("prime factor "); else printf("composite factor "); cotnum(n,stdout); return 0; } } if (nc>NCURVES) break; printf("\ntrying a different curve %d\n",nc); } printf("\nfailed to factor\n"); return 0; }
int main() { /* factoring program using Brents method */ long k,r,i,m,iter; big x,y,z,n,q,ys,c3; #ifndef MR_STATIC miracl *mip=mirsys(16,0); /* if allocating from the heap, specify size of bigs here */ char *mem=memalloc(7); /* allocate and clear memory from the heap for 7 bigs */ #else miracl *mip=mirsys(MR_STATIC,0); /* If allocating from the stack, size of bigs is pre-defined */ char mem[MR_BIG_RESERVE(7)]; /* reserve space on the stack for 7 bigs ... */ memset(mem,0,MR_BIG_RESERVE(7)); /* ... and clear this memory */ #endif x=mirvar_mem(mem,0); /* 7 bigs have index from 0-6 */ y=mirvar_mem(mem,1); ys=mirvar_mem(mem,2); z=mirvar_mem(mem,3); n=mirvar_mem(mem,4); q=mirvar_mem(mem,5); c3=mirvar_mem(mem,6); convert(3,c3); printf("input number to be factored\n"); cinnum(n,stdin); if (isprime(n)) { printf("this number is prime!\n"); return 0; } m=10L; r=1L; iter=0L; do { printf("iterations=%5ld",iter); convert(1,q); do { copy(y,x); for (i=1L;i<=r;i++) mad(y,y,c3,n,n,y); k=0; do { iter++; if (iter%10==0) printf("\b\b\b\b\b%5ld",iter); fflush(stdout); copy(y,ys); for (i=1L;i<=mr_min(m,r-k);i++) { mad(y,y,c3,n,n,y); subtract(y,x,z); mad(z,q,q,n,n,q); } egcd(q,n,z); k+=m; } while (k<r && size(z)==1); r*=2; } while (size(z)==1); if (compare(z,n)==0) do { /* back-track */ mad(ys,ys,c3,n,n,ys); subtract(ys,x,z); } while (egcd(z,n,z)==1); if (!isprime(z)) printf("\ncomposite factor "); else printf("\nprime factor "); cotnum(z,stdout); if (compare(z,n)==0) return 0; divide(n,z,n); divide(y,n,n); } while (!isprime(n)); printf("prime factor "); cotnum(n,stdout); #ifndef MR_STATIC memkill(mem,7); /* delete all 7 bigs */ #else memset(mem,0,MR_BIG_RESERVE(7)); /* clear memory used for bigs */ #endif return 0; }
int main() { FILE *fp; big e,n,a,b,x,y,r; epoint *g; ebrick binst; int nb,bits,window,len,bptr,m,i,j; miracl *mip=mirsys(50,0); n=mirvar(0); e=mirvar(0); a=mirvar(0); b=mirvar(0); x=mirvar(0); y=mirvar(0); r=mirvar(0); #ifndef MR_EDWARDS fp=fopen("common.ecs","rt"); #else fp=fopen("edwards.ecs","rt"); #endif fscanf(fp,"%d\n",&bits); mip->IOBASE=16; cinnum(n,fp); cinnum(a,fp); cinnum(b,fp); cinnum(r,fp); cinnum(x,fp); cinnum(y,fp); mip->IOBASE=10; printf("modulus is %d bits in length\n",logb2(n)); printf("Enter max. size of exponent in bits = "); scanf("%d",&nb); getchar(); printf("Enter window size in bits (1-10)= "); scanf("%d",&window); getchar(); ebrick_init(&binst,x,y,a,b,n,window,nb); /* Print out the precomputed table (for use in ecdhp.c ?) In which case make sure that MR_SPECIAL is defined and active in the build of this program, so MR_COMBA must also be defined as the number of words in the modulus * len=MR_ROUNDUP(bits,MIRACL); bptr=0; for (i=0;i<2*(1<<window);i++) { for (j=0;j<len;j++) { printf("0x%x,",binst.table[bptr++]); } printf("\n"); } */ printf("%d elliptic curve points have been precomputed and stored\n",(1<< window)); bigbits(nb,e); /* random exponent */ printf("naive method\n"); ecurve_init(a,b,n,MR_PROJECTIVE); g=epoint_init(); epoint_set(x,y,0,g); ecurve_mult(e,g,g); epoint_get(g,x,y); cotnum(x,stdout); cotnum(y,stdout); printf("Comb method\n"); mul_brick(&binst,e,x,y); ebrick_end(&binst); cotnum(x,stdout); cotnum(y,stdout); return 0; }
int main() { FILE *fp; int m,a,b,c; big e,a2,a6,x,y,r,t; epoint *g; ebrick2 binst; char fname[100]; BOOL last; int i,j,len,bptr,nb,window,wsize,words,winsize; miracl *mip=mirsys(50,0); e=mirvar(0); a2=mirvar(0); a6=mirvar(0); x=mirvar(0); y=mirvar(0); r=mirvar(0); t=mirvar(0); printf("Enter name of .ecs file= "); gets(fname); strip(fname); strcat(fname,".ecs"); if ((fp=fopen(fname,"rt"))==NULL) { printf("Unable to open file %s\n",fname); return 0; } fscanf(fp,"%d\n",&m); mip->IOBASE=16; cinnum(a2,fp); cinnum(a6,fp); cinnum(r,fp); cinnum(x,fp); cinnum(y,fp); mip->IOBASE=10; fscanf(fp,"%d\n",&a); fscanf(fp,"%d\n",&b); fscanf(fp,"%d\n",&c); printf("modulus is %d bits in length\n",m); nb=m; printf("Enter window size in bits (1-10)= "); scanf("%d",&window); getchar(); printf("Enter word size of application processor (8, 16, 32 or 64 bit)= "); scanf("%d",&wsize); getchar(); if (wsize!=8 && wsize!=16 && wsize!=32 && wsize!=64 || wsize>MIRACL) { printf("Error - Unsupported word size\n"); exit(0); } if (!ebrick2_init(&binst,x,y,a2,a6,m,a,b,c,window,nb)) { printf("Failed to Initialize\n"); return 0; } len=MR_ROUNDUP(m,MIRACL); words=MR_ROUNDUP(m,wsize); printf("\n--------------------CUT HERE----------------------\n\n"); printf("#define CURVE_M %d\n",m); printf("#define CURVE_A %d\n",a); printf("#define CURVE_B %d\n",b); printf("#define CURVE_C %d\n",c); printf("#define WINDOW %d\n",window); printf("#define WORDS %d\n",words); printf("\nstatic const mr_small rom[]={\n"); bprint(a6->w,len,words,wsize,FALSE); bprint(r->w,len,words,wsize,FALSE); bprint(x->w,len,words,wsize,FALSE); bprint(y->w,len,words,wsize,TRUE); printf("\nstatic const mr_small prom[]={\n"); bptr=0; last=FALSE; winsize=2*(1<<window); for (i=0;i<winsize;i++) { zero(t); t->len=len; for (j=0;j<len;j++) t->w[j]=binst.table[bptr++]; if (i==winsize-1) last=TRUE; bprint(t->w,len,words,wsize,last); } ebrick2_end(&binst); return 0; }
int main() { /* factoring program using Williams (p+1) method */ int k,phase,m,nt,iv,pos,btch; long i,p,pa,interval; big b,q,n,fp,fvw,fd,fn,t; static big fu[1+MULT/2]; static BOOL cp[1+MULT/2]; mip=mirsys(30,0); b=mirvar(0); q=mirvar(0); n=mirvar(0); t=mirvar(0); fp=mirvar(0); fvw=mirvar(0); fd=mirvar(0); fn=mirvar(0); gprime(LIMIT1); for (m=1;m<=MULT/2;m+=2) if (igcd(MULT,m)==1) { fu[m]=mirvar(0); cp[m]=TRUE; } else cp[m]=FALSE; printf("input number to be factored\n"); cinnum(n,stdin); if (isprime(n)) { printf("this number is prime!\n"); return 0; } for (nt=0,k=3;k<10;k++) { /* try more than once for p+1 condition (may be p-1) */ convert(k,b); /* try b=3,4,5.. */ convert((k*k-4),t); if (egcd(t,n,t)!=1) continue; /* check (b*b-4,n)!=0 */ nt++; phase=1; p=0; btch=50; i=0; printf("phase 1 - trying all primes less than %d\n",LIMIT1); printf("prime= %8ld",p); forever { /* main loop */ if (phase==1) { /* looking for all factors of p+1 < LIMIT1 */ p=mip->PRIMES[i]; if (mip->PRIMES[i+1]==0) { /* now change gear */ phase=2; printf("\nphase 2 - trying last prime less than %ld\n" ,LIMIT2); printf("prime= %8ld",p); copy(b,fu[1]); copy(b,fp); mad(b,b,b,n,n,fd); decr(fd,2,fd); negify(b,t); mad(fd,b,t,n,n,fn); for (m=5;m<=MULT/2;m+=2) { /* store fu[m] = Vm(b) */ negify(fp,t); mad(fn,fd,t,n,n,t); copy(fn,fp); copy(t,fn); if (!cp[m]) continue; copy(t,fu[m]); } convert(MULT,t); lucas(b,t,n,fp,fd); iv=(int)(p/MULT); if (p%MULT>MULT/2) iv++; interval=(long)iv*MULT; p=interval+1; convert(iv,t); lucas(fd,t,n,fp,fvw); negify(fp,fp); subtract(fvw,fu[p%MULT],q); marks(interval); btch*=100; i++; continue; } pa=p; while ((LIMIT1/p) > pa) pa*=p; convert((int)pa,t); lucas(b,t,n,fp,q); copy(q,b); decr(q,2,q); } else { /* phase 2 - looking for last large prime factor of (p+1) */ p+=2; pos=(int)(p%MULT); if (pos>MULT/2) { /* increment giant step */ iv++; interval=(long)iv*MULT; p=interval+1; marks(interval); pos=1; copy(fvw,t); mad(fvw,fd,fp,n,n,fvw); negify(t,fp); } if (!cp[pos]) continue; /* if neither interval+/-pos is prime, don't bother */ if (!plus[pos] && !minus[pos]) continue; subtract(fvw,fu[pos],t); mad(q,t,t,n,n,q); /* batching gcds */ } if (i++%btch==0) { /* try for a solution */ printf("\b\b\b\b\b\b\b\b%8ld",p); fflush(stdout); egcd(q,n,t); if (size(t)==1) { if (p>LIMIT2) break; else continue; } if (compare(t,n)==0) { printf("\ndegenerate case"); break; } printf("\nfactors are\n"); if (isprime(t)) printf("prime factor "); else printf("composite factor "); cotnum(t,stdout); divide(n,t,n); if (isprime(n)) printf("prime factor "); else printf("composite factor "); cotnum(n,stdout); return 0; } } if (nt>=NTRYS) break; printf("\ntrying again\n"); } printf("\nfailed to factor\n"); return 0; }
int main() { /* Pollard's lambda algorithm for finding discrete logs * * which are known to be less than a certain limit LIMIT */ big x,n,t,trap,table[32]; int i,j,m; long dm,dn,s,distance[32]; miracl *mip=mirsys(50,0); x=mirvar(0); n=mirvar(0); t=mirvar(0); trap=mirvar(0); for (s=1L,m=1;;m++) { /* find table size */ distance[m-1]=s; s*=2; if ((2*s/m)>(LEAPS/4)) break; } mip->IOBASE=60; /* get large modulus */ cinstr(n,modulus); mip->IOBASE=10; printf("solve discrete logarithm problem - using Pollard's kangaroos\n"); printf("finds x in y=%d^x mod n, given y, for fixed n and small x\n",ALPHA); printf("known to be less than %ld\n",LIMIT); printf("n= "); cotnum(n,stdout); for (i=0;i<m;i++) { /* create table */ lgconv(distance[i],t); table[i]=mirvar(0); powltr(ALPHA,t,n,table[i]); } lgconv(LIMIT,t); powltr(ALPHA,t,n,x); printf("setting trap .... \n"); for (dn=0L,j=0;j<LEAPS;j++) { /* set traps beyond LIMIT using tame kangaroo */ i=subdiv(x,m,t); /* random function */ mad(x,table[i],x,n,n,x); dn+=distance[i]; } printf("trap set!\n"); copy(x,trap); forever { /* ready to solve */ printf("Enter x= "); cinnum(x,stdin); if (size(x)<=0) break; powltr(ALPHA,x,n,t); printf("y= "); cotnum(t,stdout); copy(t,x); for (dm=0L;;) { /* unlease wild kangaroo - boing - boing ... */ i=subdiv(x,m,t); mad(x,table[i],x,n,n,x); dm+=distance[i]; if (compare(x,trap)==0 || dm>LIMIT+dn) break; } if (dm>LIMIT+dn) { /* trap stepped over */ printf("trap failed\n"); continue; } printf("Gotcha!\n"); printf("Discrete log of y= %ld\n",LIMIT+dn-dm); } return 0; }
int main() { /* decode using private key */ int i; big e,ep[NP],m,ke,kd,p[NP],kp[NP],mn,mx; FILE *ifile; FILE *ofile; char ifname[13],ofname[13]; BOOL flo; big_chinese ch; mip=mirsys(100,0); for (i=0;i<NP;i++) { p[i]=mirvar(0); ep[i]=mirvar(0); kp[i]=mirvar(0); } e=mirvar(0); m=mirvar(0); kd=mirvar(0); ke=mirvar(0); mn=mirvar(0); mx=mirvar(0); mip->IOBASE=60; if ((ifile=fopen("private.key","r"))==NULL) { printf("Unable to open file private.key\n"); return 0; } for (i=0;i<NP;i++) { cinnum(p[i],ifile); } fclose(ifile); /* generate public and private keys */ convert(1,ke); for (i=0;i<NP;i++) { multiply(ke,p[i],ke); } for (i=0;i<NP;i++) { /* kp[i]=(2*(p[i]-1)+1)/3 = 1/3 mod p[i]-1 */ decr(p[i],1,kd); premult(kd,2,kd); incr(kd,1,kd); subdiv(kd,3,kp[i]); } crt_init(&ch,NP,p); nroot(ke,3,mn); multiply(mn,mn,m); multiply(mn,m,mx); subtract(mx,m,mx); do { /* get input file */ printf("file to be decoded = "); gets(ifname); } while (strlen(ifname)==0); strip(ifname); strcat(ifname,".rsa"); printf("output filename = "); gets(ofname); flo=FALSE; if (strlen(ofname)>0) { /* set up output file */ flo=TRUE; ofile=fopen(ofname,"w"); } printf("decoding message\n"); if ((ifile=fopen(ifname,"r"))==NULL) { printf("Unable to open file %s\n",ifname); return 0; } forever { /* decode line by line */ mip->IOBASE=60; cinnum(m,ifile); if (size(m)==0) break; for (i=0;i<NP;i++) powmod(m,kp[i],p[i],ep[i]); crt(&ch,ep,e); /* Chinese remainder thereom */ if (compare(e,mx)>=0) divide(e,mn,mn); mip->IOBASE=128; if (flo) cotnum(e,ofile); cotnum(e,stdout); } crt_end(&ch); fclose(ifile); if (flo) fclose(ofile); printf("message ends\n"); return 0; }
int main() { FILE *fp; int m,a,b,c,cf; miracl *mip; char ifname[13],ofname[13]; big a2,a6,q,x,y,d,r,s,k,hash; epoint *g; long seed; /* get public data */ fp=fopen("common2.ecs","r"); if (fp==NULL) { printf("file common2.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&m); mip=mirsys(3+m/MIRACL,0); a2=mirvar(0); a6=mirvar(0); q=mirvar(0); x=mirvar(0); y=mirvar(0); d=mirvar(0); r=mirvar(0); s=mirvar(0); k=mirvar(0); hash=mirvar(0); mip->IOBASE=16; cinnum(a2,fp); /* curve parameters */ cinnum(a6,fp); /* curve parameters */ cinnum(q,fp); /* order of (x,y) */ cinnum(x,fp); /* (x,y) point on curve of order q */ cinnum(y,fp); mip->IOBASE=10; fscanf(fp,"%d\n",&a); fscanf(fp,"%d\n",&b); fscanf(fp,"%d\n",&c); fclose(fp); /* randomise */ printf("Enter 9 digit random number seed = "); scanf("%ld",&seed); getchar(); irand(seed); ecurve2_init(m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE); /* initialise curve */ g=epoint2_init(); epoint2_set(x,y,0,g); /* set point of order q */ /* calculate r - this can be done offline, and hence amortized to almost nothing */ bigrand(q,k); ecurve2_mult(k,g,g); /* see ebrick2.c for method to speed this up */ epoint2_get(g,r,r); divide(r,q,q); /* get private key of signer */ fp=fopen("private.ecs","r"); if (fp==NULL) { printf("file private.ecs does not exist\n"); return 0; } cinnum(d,fp); fclose(fp); /* calculate message digest */ printf("file to be signed = "); gets(ifname); strcpy(ofname,ifname); strip(ofname); strcat(ofname,".ecs"); if ((fp=fopen(ifname,"rb"))==NULL) { printf("Unable to open file %s\n",ifname); return 0; } hashing(fp,hash); fclose(fp); /* calculate s */ xgcd(k,q,k,k,k); mad(d,r,hash,q,q,s); mad(s,k,k,q,q,s); fp=fopen(ofname,"w"); cotnum(r,fp); cotnum(s,fp); fclose(fp); return 0; }