int main() { FILE *fp; big p,q,g,x,y; long seed; int bits; miracl *mip; /* get common data */ fp=fopen("common.dss","rt"); if (fp==NULL) { printf("file common.dss does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); mip=mirsys(bits/4,16); /* use Hex internally */ p=mirvar(0); q=mirvar(0); g=mirvar(0); x=mirvar(0); y=mirvar(0); innum(p,fp); innum(q,fp); innum(g,fp); fclose(fp); /* randomise */ printf("Enter 9 digit random number seed = "); scanf("%ld",&seed); getchar(); irand(seed); powmod(g,q,p,y); if (size(y)!=1) { printf("Problem - generator g is not of order q\n"); return 0; } /* generate public/private keys */ bigrand(q,x); powmod(g,x,p,y); printf("public key = "); otnum(y,stdout); fp=fopen("public.dss","wt"); otnum(y,fp); fclose(fp); fp=fopen("private.dss","wt"); otnum(x,fp); fclose(fp); mirexit(); return 0; }
int main () { /* hailstone numbers */ int iter,r; big x,y,mx; mirsys(400,10); x=mirvar(0); y=mirvar(0); mx=mirvar(0); iter=0; printf("number = \n"); innum(x,stdin); do { /* main loop */ if (compare(x,mx)>0) copy(x,mx); r=subdiv(x,2,y); if (r!=0) { /* what goes up ... */ premult(x,3,x); incr(x,1,x); } /* ... must come down */ else copy(y,x); otnum(x,stdout); iter++; } while (size(x)!=1); printf("path length = %d \n",iter); printf("maximum = \n"); otnum(mx,stdout); return 0; }
void mcl_ecpbs_import_pk(mcl_ecpbs_pk *pk) { FILE *fp; int compressed_y; big x; fp = fopen("keys/ec160.public", "rt"); if (fp == NULL) { printf("file ec.public does not exist\n"); exit(0); } x = mirvar(0); /* import the compressed y value */ fscanf(fp, "%d", &compressed_y); /* import the x coordinate on the curve */ innum(x, fp); fclose(fp); /* check if x is valid on the curve */ if (!epoint_x(x)) { printf( "Problem - imported x value of the public key is not on the active curve\n"); exit(0); } /* decompress point */ if (!epoint_set(x, x, compressed_y, pk->key)) { printf("Problem - public key point (x,y) is not on the curve\n"); exit(0); } mirkill(x); }
void mcl_ecpbs_import_sk(mcl_ecpbs_sk *sk) { FILE *fp; fp = fopen("keys/ec160.private", "rt"); if (fp == NULL) { printf("file ec.private does not exist\n"); exit(0); } innum(sk->key, fp); fclose(fp); }
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","rt"); if (fp==NULL) { printf("file common2.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&m); mip=mirsys(MR_ROUNDUP(abs(m),4),16); 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); innum(a2,fp); innum(a6,fp); innum(q,fp); innum(x,fp); innum(y,fp); 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_BEST); /* initialise curve */ g=epoint_init(); epoint2_set(x,y,0,g); /* initialise point of order q */ /* get public key of signer */ fp=fopen("public.ecs","rt"); if (fp==NULL) { printf("file public.ecs does not exist\n"); return 0; } fscanf(fp,"%d",&ep); innum(x,fp); fclose(fp); public=epoint_init();
int main() { FILE *fp; int ep,bits; epoint *g,*w; big a,b,p,q,x,y,d; long seed; miracl instance; miracl *mip=&instance; char mem[MR_BIG_RESERVE(7)]; /* reserve space on the stack for 7 bigs */ char mem1[MR_ECP_RESERVE(2)]; /* and two elliptic curve points */ memset(mem,0,MR_BIG_RESERVE(7)); memset(mem1,0,MR_ECP_RESERVE(2)); #ifndef MR_EDWARDS fp=fopen("common.ecs","rt"); if (fp==NULL) { printf("file common.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); #else fp=fopen("edwards.ecs","rt"); if (fp==NULL) { printf("file edwards.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); #endif mirsys(mip,bits/4,16); /* Use Hex internally */ a=mirvar_mem(mip,mem,0); b=mirvar_mem(mip,mem,1); p=mirvar_mem(mip,mem,2); q=mirvar_mem(mip,mem,3); x=mirvar_mem(mip,mem,4); y=mirvar_mem(mip,mem,5); d=mirvar_mem(mip,mem,6); innum(mip,p,fp); innum(mip,a,fp); innum(mip,b,fp); innum(mip,q,fp); innum(mip,x,fp); innum(mip,y,fp); fclose(fp); /* randomise */ printf("Enter 9 digit random number seed = "); scanf("%ld",&seed); getchar(); irand(mip,seed); ecurve_init(mip,a,b,p,MR_PROJECTIVE); /* initialise curve */ g=epoint_init_mem(mip,mem1,0); w=epoint_init_mem(mip,mem1,1); if (!epoint_set(mip,x,y,0,g)) /* initialise point of order q */ { printf("Problem - point (x,y) is not on the curve\n"); exit(0); } ecurve_mult(mip,q,g,w); if (!point_at_infinity(w)) { printf("Problem - point (x,y) is not of order q\n"); exit(0); } /* generate public/private keys */ bigrand(mip,q,d); ecurve_mult(mip,d,g,g); ep=epoint_get(mip,g,x,x); /* compress point */ printf("public key = %d ",ep); otnum(mip,x,stdout); fp=fopen("public.ecs","wt"); fprintf(fp,"%d ",ep); otnum(mip,x,fp); fclose(fp); fp=fopen("private.ecs","wt"); otnum(mip,d,fp); fclose(fp); /* clear all memory used */ memset(mem,0,MR_BIG_RESERVE(7)); memset(mem1,0,MR_ECP_RESERVE(2)); return 0; }
int main() { FILE *fp; char ifname[50],ofname[50]; big a,b,p,q,x,y,d,r,s,k,hash; epoint *g; long seed; int bits; miracl instance; miracl *mip=&instance; char mem[MR_BIG_RESERVE(11)]; /* reserve space on the stack for 11 bigs */ char mem1[MR_ECP_RESERVE(1)]; /* and one elliptic curve points */ memset(mem,0,MR_BIG_RESERVE(11)); memset(mem1,0,MR_ECP_RESERVE(1)); /* get public data */ #ifndef MR_EDWARDS fp=fopen("common.ecs","rt"); if (fp==NULL) { printf("file common.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); #else fp=fopen("edwards.ecs","rt"); if (fp==NULL) { printf("file edwards.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); #endif mirsys(mip,bits/4,16); /* Use Hex internally */ a=mirvar_mem(mip,mem,0); b=mirvar_mem(mip,mem,1); p=mirvar_mem(mip,mem,2); q=mirvar_mem(mip,mem,3); x=mirvar_mem(mip,mem,4); y=mirvar_mem(mip,mem,5); d=mirvar_mem(mip,mem,6); r=mirvar_mem(mip,mem,7); s=mirvar_mem(mip,mem,8); k=mirvar_mem(mip,mem,9); hash=mirvar_mem(mip,mem,10); innum(mip,p,fp); /* modulus */ innum(mip,a,fp); /* curve parameters */ innum(mip,b,fp); innum(mip,q,fp); /* order of (x,y) */ innum(mip,x,fp); /* (x,y) point on curve of order q */ innum(mip,y,fp); fclose(fp); /* randomise */ printf("Enter 9 digit random number seed = "); scanf("%ld",&seed); getchar(); irand(mip,seed); ecurve_init(mip,a,b,p,MR_PROJECTIVE); /* initialise curve */ g=epoint_init_mem(mip,mem1,0); epoint_set(mip,x,y,0,g); /* initialise point of order q */ /* calculate r - this can be done offline, and hence amortized to almost nothing */ bigrand(mip,q,k); ecurve_mult(mip,k,g,g); /* see ebrick.c for method to speed this up */ epoint_get(mip,g,r,r); divide(mip,r,q,q); /* get private key of signer */ fp=fopen("private.ecs","rt"); if (fp==NULL) { printf("file private.ecs does not exist\n"); return 0; } innum(mip,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(mip,fp,hash); fclose(fp); /* calculate s */ xgcd(mip,k,q,k,k,k); mad(mip,d,r,hash,q,q,s); mad(mip,s,k,k,q,q,s); fp=fopen(ofname,"wt"); otnum(mip,r,fp); otnum(mip,s,fp); fclose(fp); memset(mem,0,MR_BIG_RESERVE(11)); memset(mem1,0,MR_ECP_RESERVE(1)); return 0; }
int main() { int i; FILE *fp; big K,rid,id,w,a,b,n,q1; miracl *mip=mirsys(200,256); for (i=0;i<NPRIMES;i++) { pp[i]=mirvar(0); rem[i]=mirvar(0); } w=mirvar(0); n=mirvar(0); a=mirvar(0); b=mirvar(0); p=mirvar(0); p1=mirvar(0); q1=mirvar(0); K=mirvar(0); lim1=mirvar(0); lim2=mirvar(0); id=mirvar(0); rid=mirvar(0); order=mirvar(0); printf("Enter ID= "); innum(rid,stdin); getprime("trap1.dat"); copy(p,n); getprime("trap2.dat"); multiply(n,p,n); printf("\ncomposite =\n"); cotnum(n,stdout); premult(rid,256,id); while (jack(id,n)!=1) { /* bad identity - id=256*rid+i */ printf("No Discrete Log. for this ID -- incrementing\n"); incr(id,1,id); } getprime("trap1.dat"); copy(p1,q1); pollard(id,b); getprime("trap2.dat"); pollard(id,a); xgcd(p1,q1,K,K,K); subtract(b,a,w); mad(w,K,w,q1,q1,w); if(size(w)<0) add_r(w,q1,w); subdiv(w,2,w); multiply(w,p1,w); add_r(w,a,w); fp=fopen("secret.dat","w"); otnum(rid,fp); cotnum(w,fp); cotnum(n,fp); fclose(fp); printf("\nDiscrete log (secret key) \n"); cotnum(w,stdout); powltr(PROOT,w,n,id); subdiv(id,256,id); otstr(id,mip->IOBUFF); printf("Check Identity= %s\n",mip->IOBUFF); return 0; }
void mcl_ecpbs_import_parameters(mcl_ecpbs_parameters *parameters) { FILE *fp; int bits; big x, y; miracl *mip; /* get public curve parameters */ fp = fopen("keys/ec160.parameters", "rt"); if (fp == NULL) { printf("file ec.parameters does not exist\n"); exit(0); } /* read in big number bitlength */ fscanf(fp, "%d\n", &bits); /* * Initialize the system, using HEX (base16) internally. * The internal storage for each big number uses bits/4. */ mip = mirsys(bits / 4, 16); /* initialize memory for parameters */ parameters->p = mirvar(0); parameters->A = mirvar(0); parameters->B = mirvar(0); parameters->q = mirvar(0); x = mirvar(0); y = mirvar(0); parameters->g = epoint_init(); /* import parameters */ /* p is the modulus */ innum(parameters->p, fp); /* A and B are curve parameters */ innum(parameters->A, fp); innum(parameters->B, fp); /* q is the order of (x,y) */ innum(parameters->q, fp); /* (x,y) point on curve of order q */ innum(x, fp); innum(y, fp); fclose(fp); /* FIXME randomize */ irand(675822498); /* initialize curve - can use MR_PROJECTIVE, or MR_AFFINE */ ecurve_init(parameters->A, parameters->B, parameters->p, MR_PROJECTIVE); /* check if x is valid on the curve */ if (!epoint_x(x)) { printf( "Problem - imported x value of the generator is not on the active curve\n"); exit(0); } /* initialize generator to the point of order q */ if (!epoint_set(x, y, 0, parameters->g)) { printf("Problem - generator point (x,y) is not on the curve\n"); exit(0); } mirkill(x); mirkill(y); }
int main() { FILE *fp; int ep,m,a,b,c; epoint *g,*public; char ifname[50],ofname[50]; big a2,a6,q,x,y,v,u1,u2,r,s,hash; miracl instance; miracl *mip=&instance; char mem[MR_BIG_RESERVE(11)]; /* reserve space on the stack for 11 bigs */ char mem1[MR_ECP_RESERVE(2)]; /* and two elliptic curve points */ memset(mem,0,MR_BIG_RESERVE(11)); memset(mem1,0,MR_ECP_RESERVE(2)); /* get public data */ fp=fopen("common2.ecs","rt"); if (fp==NULL) { printf("file common2.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&m); mip=mirsys(mip,MR_ROUNDUP(abs(m),4),16); a2=mirvar_mem(mip,mem,0); a6=mirvar_mem(mip,mem,1); q=mirvar_mem(mip,mem,2); x=mirvar_mem(mip,mem,3); y=mirvar_mem(mip,mem,4); v=mirvar_mem(mip,mem,5); u1=mirvar_mem(mip,mem,6); u2=mirvar_mem(mip,mem,7); s=mirvar_mem(mip,mem,8); r=mirvar_mem(mip,mem,9); hash=mirvar_mem(mip,mem,10); innum(mip,a2,fp); innum(mip,a6,fp); innum(mip,q,fp); innum(mip,x,fp); innum(mip,y,fp); fscanf(fp,"%d\n",&a); fscanf(fp,"%d\n",&b); fscanf(fp,"%d\n",&c); fclose(fp); ecurve2_init(mip,m,a,b,c,a2,a6,FALSE,MR_PROJECTIVE); /* initialise curve */ g=epoint_init_mem(mip,mem1,0); epoint2_set(mip,x,y,0,g); /* initialise point of order q */ /* get public key of signer */ fp=fopen("public.ecs","rt"); if (fp==NULL) { printf("file public.ecs does not exist\n"); return 0; } fscanf(fp,"%d",&ep); innum(mip,x,fp); fclose(fp); public=epoint_init_mem(mip,mem1,1);
int main() { FILE *fp; int bits,ep; epoint *g,*public; char ifname[50],ofname[50]; big a,b,p,q,x,y,v,u1,u2,r,s,hash; miracl instance; miracl *mip=&instance; char mem[MR_BIG_RESERVE(12)]; /* reserve space on the stack for 12 bigs */ char mem1[MR_ECP_RESERVE(2)]; /* and two elliptic curve points */ memset(mem,0,MR_BIG_RESERVE(12)); memset(mem1,0,MR_ECP_RESERVE(2)); /* get public data */ #ifndef MR_EDWARDS fp=fopen("common.ecs","rt"); if (fp==NULL) { printf("file common.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); #else fp=fopen("edwards.ecs","rt"); if (fp==NULL) { printf("file edwards.ecs does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); #endif mirsys(mip,bits/4,16); /* Use Hex Internally */ a=mirvar_mem(mip,mem,0); b=mirvar_mem(mip,mem,1); p=mirvar_mem(mip,mem,2); q=mirvar_mem(mip,mem,3); x=mirvar_mem(mip,mem,4); y=mirvar_mem(mip,mem,5); v=mirvar_mem(mip,mem,6); u1=mirvar_mem(mip,mem,7); u2=mirvar_mem(mip,mem,8); s=mirvar_mem(mip,mem,9); r=mirvar_mem(mip,mem,10); hash=mirvar_mem(mip,mem,11); innum(mip,p,fp); innum(mip,a,fp); innum(mip,b,fp); innum(mip,q,fp); innum(mip,x,fp); innum(mip,y,fp); fclose(fp); ecurve_init(mip,a,b,p,MR_PROJECTIVE); /* initialise curve */ g=epoint_init_mem(mip,mem1,0); epoint_set(mip,x,y,0,g); /* initialise point of order q */ /* get public key of signer */ fp=fopen("public.ecs","rt"); if (fp==NULL) { printf("file public.ecs does not exist\n"); return 0; } fscanf(fp,"%d",&ep); innum(mip,x,fp); fclose(fp); public=epoint_init_mem(mip,mem1,1);
int main() { FILE *fp; char ifname[50],ofname[50]; big p,q,g,x,r,s,k,hash; long seed; int bits; miracl *mip; /* get public data */ fp=fopen("common.dss","rt"); if (fp==NULL) { printf("file common.dss does not exist\n"); return 0; } fscanf(fp,"%d\n",&bits); mip=mirsys(bits/4,16); /* use Hex internally */ p=mirvar(0); q=mirvar(0); g=mirvar(0); x=mirvar(0); r=mirvar(0); s=mirvar(0); k=mirvar(0); hash=mirvar(0); innum(p,fp); innum(q,fp); innum(g,fp); 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","rt"); if (fp==NULL) { printf("file private.dss does not exist\n"); return 0; } innum(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,"wt"); otnum(r,fp); otnum(s,fp); fclose(fp); mirexit(); return 0; }