double mult2_double(int eb,epoint *g) { big e1,e2; int iterations=0; clock_t start; double elapsed; char *mem1; char *mem2; epoint *w; epoint *r1; epoint *r2; mem1=(char *)memalloc(2); mem2=(char *)ecp_memalloc(3); e1=mirvar_mem(mem1,0); e2=mirvar_mem(mem1,1); w=epoint_init_mem(mem2,0); r1=epoint_init_mem(mem2,1); r2=epoint_init_mem(mem2,2); bigbits(eb,e1); ecurve2_mult(e1,g,r1); /* generate a random point on the curve */ bigbits(eb,e2); ecurve2_mult(e2,g,r2); /* generate a random point on the curve */ bigbits(eb,e1); bigbits(eb,e2); start=clock(); do { ecurve2_mult2(e1,r1,e2,r2,w); iterations++; elapsed=(clock()-start)/(double)CLOCKS_PER_SEC; } while (elapsed<MIN_TIME || iterations<MIN_ITERS); elapsed=1000.0*elapsed/iterations; printf("ED - %8d iterations ",iterations); printf(" %8.2lf ms per iteration\n",elapsed); ecp_memkill(mem2,3); memkill(mem1,2); return elapsed; }
double mults2(int eb,epoint *g) { big e; int iterations=0; clock_t start; double elapsed; epoint *w; epoint *r; char *mem1; char *mem2; mem1=(char *)memalloc(1); mem2=(char *)ecp_memalloc(2); e=mirvar_mem(mem1,0); w=epoint_init_mem(mem2,0); r=epoint_init_mem(mem2,1); bigbits(eb,e); ecurve2_mult(e,g,r); /* generate a random point on the curve */ bigbits(eb,e); start=clock(); do { ecurve2_mult(e,r,w); iterations++; elapsed=(clock()-start)/(double)CLOCKS_PER_SEC; } while (elapsed<MIN_TIME || iterations<MIN_ITERS); elapsed=1000.0*elapsed/iterations; printf("ER - %8d iterations ",iterations); printf(" %8.2lf ms per iteration\n",elapsed); memkill(mem1,1); ecp_memkill(mem2,2); return elapsed; }
int main() { #ifdef MR_GENERIC_MT miracl instance; #endif big p,A,B,fr,q,delta,t,T; zzn2 res,Qx,Qy; epoint *P; int i,romptr; #ifndef MR_STATIC #ifdef MR_GENERIC_MT miracl *mr_mip=mirsys(&instance,WORDS*NPW,16); #else miracl *mr_mip=mirsys(WORDS*NPW,16); #endif char *mem=memalloc(_MIPP_ 14); char *mem1=ecp_memalloc(_MIPP_ 1); #else #ifdef MR_GENERIC_MT miracl *mr_mip=mirsys(&instance,MR_STATIC*NPW,16); #else miracl *mr_mip=mirsys(MR_STATIC*NPW,16); #endif char mem[MR_BIG_RESERVE(14)]; /* reserve space on the stack for 14 bigs */ char mem1[MR_ECP_RESERVE(1)]; /* reserve space on stack for 1 curve points */ memset(mem,0,MR_BIG_RESERVE(14)); /* clear this memory */ memset(mem1,0,MR_ECP_RESERVE(1)); #endif p=mirvar_mem(_MIPP_ mem,0); A=mirvar_mem(_MIPP_ mem,1); B=mirvar_mem(_MIPP_ mem,2); T=mirvar_mem(_MIPP_ mem,3); q=mirvar_mem(_MIPP_ mem,4); fr=mirvar_mem(_MIPP_ mem,5); delta=mirvar_mem(_MIPP_ mem,6); res.a=mirvar_mem(_MIPP_ mem,7); res.b=mirvar_mem(_MIPP_ mem,8); t=mirvar_mem(_MIPP_ mem,9); Qx.a=mirvar_mem(_MIPP_ mem,10); Qx.b=mirvar_mem(_MIPP_ mem,11); Qy.a=mirvar_mem(_MIPP_ mem,12); Qy.b=mirvar_mem(_MIPP_ mem,13); P=epoint_init_mem(_MIPP_ mem1,0); convert(_MIPP_ -3,A); romptr=0; init_big_from_rom(p,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(B,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(q,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(delta,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(fr,WORDS,romp,ROMSZ,&romptr); init_big_from_rom(T,WORDS,romp,ROMSZ,&romptr); #ifndef MR_NO_STANDARD_IO printf("ROM size= %d\n",sizeof(romp)+sizeof(Prom)); printf("sizeof(miracl)= %d\n",sizeof(miracl)); #endif ecurve_init(_MIPP_ A,B,p,MR_PROJECTIVE); romptr=0; init_point_from_rom(P,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qx.a,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qx.b,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qy.a,WORDS,Prom,PROMSZ,&romptr); init_big_from_rom(Qy.b,WORDS,Prom,PROMSZ,&romptr); #ifdef MR_COUNT_OPS fpa=fpc=fpx=0; #endif ecap(_MIPP_ &Qx,&Qy,P,T,fr,delta,&res); /* #ifdef MR_COUNT_OPS printf("fpc= %d\n",fpc); printf("fpa= %d\n",fpa); printf("fpx= %d\n",fpx); fpa=fpc=fpx=0; #endif */ bigbits(_MIPP_ 160,t); zzn2_powl(_MIPP_ &res,t,&res); #ifndef MR_NO_STANDARD_IO zzn2_out(_MIPP_ "res= ",&res); #endif ecurve_mult(_MIPP_ t,P,P); ecap(_MIPP_ &Qx,&Qy,P,T,fr,delta,&res); #ifndef MR_NO_STANDARD_IO zzn2_out(_MIPP_ "res= ",&res); #endif #ifndef MR_STATIC memkill(_MIPP_ mem,14); ecp_memkill(_MIPP_ mem1,1); #else memset(mem,0,MR_BIG_RESERVE(14)); /* clear this stack memory */ memset(mem1,0,MR_ECP_RESERVE(1)); #endif return 0; }
int main() { big a2,a6,bx,r; big res[4]; epoint *P,*Q; int i,romptr; miracl instance; /* sizeof(miracl)= 2000 bytes from the stack */ #ifndef MR_STATIC #ifdef MR_GENERIC_MT miracl *mr_mip=mirsys(WORDS*NPW,16); #else miracl *mr_mip=mirsys(WORDS*NPW,16); #endif char *mem=(char *)memalloc(_MIPP_ 8); char *mem1=(char *)ecp_memalloc(_MIPP_ 2); #else #ifdef MR_GENERIC_MT miracl *mr_mip=mirsys(&instance,MR_STATIC*NPW,16); /* size of bigs is fixed */ #else miracl *mr_mip=mirsys(&instance,MR_STATIC*NPW,16); #endif char mem[MR_BIG_RESERVE(8)]; /* reserve space on the stack for 8 bigs */ char mem1[MR_ECP_RESERVE(2)]; /* reserve space on stack for 2 curve points */ memset(mem,0,MR_BIG_RESERVE(8)); /* clear this memory */ memset(mem1,0,MR_ECP_RESERVE(2)); /* ~668 bytes in all */ #endif /* Initialise bigs */ a2=mirvar_mem(_MIPP_ mem,0); a6=mirvar_mem(_MIPP_ mem,1); bx=mirvar_mem(_MIPP_ mem,2); for (i=0;i<4;i++) res[i]=mirvar_mem(_MIPP_ mem,3+i); r=mirvar_mem(_MIPP_ mem,7); /* printf("ROM size= %d\n",sizeof(rom)+sizeof(prom)); */ #ifndef MR_NO_STANDARD_IO #ifdef MR_STATIC printf("n Bigs require n*%d+%d bytes\n",MR_SIZE,MR_SL); printf("n Points require n*%d+%d bytes\n",MR_ESIZE,MR_SL); printf("sizeof(miracl)= %d\n",sizeof(miracl)); #endif #endif /* Initialise Elliptic curve points */ P=epoint_init_mem(_MIPP_ mem1,0); Q=epoint_init_mem(_MIPP_ mem1,1); /* Initialise supersingular curve */ convert(_MIPP_ 1,a2); convert(_MIPP_ B,a6); /* The -M tells MIRACL that this is a supersingular curve */ if (!ecurve2_init(_MIPP_ -M,T,U,V,a2,a6,FALSE,MR_PROJECTIVE)) { #ifndef MR_NO_STANDARD_IO printf("Problem with the curve\n"); #endif return 0; } /* Get P and Q from ROM */ /* These should have been multiplied by the cofactor 487805 = 5*97561 */ /* 487805 is a cofactor of the group order 2^271+2^136+1 */ romptr=0; init_point_from_rom(P,WORDS,rom,ROMSZ,&romptr); init_point_from_rom(Q,WORDS,rom,ROMSZ,&romptr); #ifndef MR_NO_STANDARD_IO printf( "P= \n"); otnum(_MIPP_ P->X,stdout); otnum(_MIPP_ P->Y,stdout); printf( "Q= \n"); otnum(_MIPP_ Q->X,stdout); otnum(_MIPP_ Q->Y,stdout); #endif bigbits(_MIPP_ 160,r); /* Simple bilinearity test */ tate(_MIPP_ P,Q,res); /* this could break the 4k stack, 2060+668+2996 >4K */ /* so we cannot afford much precomputation in power4 */ power4(_MIPP_ res,r,res); /* res=res^{sr} */ #ifndef MR_NO_STANDARD_IO printf( "\ne(P,Q)^r= \n"); for (i=0;i<4;i++) { otnum(_MIPP_ res[i],stdout); zero(res[i]); } #endif ecurve2_mult(_MIPP_ r,Q,Q); /* Q=rQ */ epoint2_norm(_MIPP_ Q); tate(_MIPP_ P,Q,res); /* Now invert is taken out of Tate, and the stack should be OK */ #ifndef MR_NO_STANDARD_IO printf( "\ne(P,rQ)= \n"); for (i=0;i<4;i++) otnum(_MIPP_ res[i],stdout); #endif /* all done */ #ifndef MR_STATIC memkill(_MIPP_ mem,8); ecp_memkill(_MIPP_ mem1,2); #else memset(mem,0,MR_BIG_RESERVE(8)); /* clear this stack memory */ memset(mem1,0,MR_ECP_RESERVE(2)); #endif mirexit(_MIPPO_ ); /* clears workspace memory */ return 0; }
void fast_tate_pairing(_MIPD_ epoint *P,zzn3 *Qx,zzn3 *Qy,big q,big cf,zzn6 *w,zzn6* res) { int i,j,n,nb,nbw,nzs; epoint *t[4],*A,*P2; zzn6 zn[4]; big work[4]; #ifndef MR_STATIC char *mem=memalloc(_MIPP_ 28); char *mem1=ecp_memalloc(_MIPP_ 6); #else char mem[MR_BIG_RESERVE(28)]; char mem1[MR_ECP_RESERVE(6)]; memset(mem,0,MR_BIG_RESERVE(28)); memset(mem1,0,MR_ECP_RESERVE(6)); #endif for (i=0;i<4;i++) t[i]=epoint_init_mem(_MIPP_ mem1,i); A=epoint_init_mem(_MIPP_ mem1,4); P2=epoint_init_mem(_MIPP_ mem1,5); for (j=i=0;i<4;i++) { work[i]=mirvar_mem(_MIPP_ mem,j++); zn[i].x.a=mirvar_mem(_MIPP_ mem,j++); zn[i].x.b=mirvar_mem(_MIPP_ mem,j++); zn[i].x.c=mirvar_mem(_MIPP_ mem,j++); zn[i].y.a=mirvar_mem(_MIPP_ mem,j++); zn[i].y.b=mirvar_mem(_MIPP_ mem,j++); zn[i].y.c=mirvar_mem(_MIPP_ mem,j++); zn[i].unitary=FALSE; } zzn6_from_int(_MIPP_ 1,&zn[0]); epoint_copy(P,A); epoint_copy(P,P2); epoint_copy(P,t[0]); g(_MIPP_ P2,P2,Qx,Qy,res); epoint_norm(_MIPP_ P2); for (i=1;i<4;i++) { g(_MIPP_ A,P2,Qx,Qy,w); epoint_copy(A,t[i]); zzn6_mul(_MIPP_ &zn[i-1],w,&zn[i]); zzn6_mul(_MIPP_ &zn[i],res,&zn[i]); } epoint_multi_norm(_MIPP_ 4,work,t); epoint_copy(P,A); zzn6_from_int(_MIPP_ 1,res); nb=logb2(_MIPP_ q); for (i=nb-2;i>=0;i-=(nbw+nzs)) { n=mr_window(_MIPP_ q,i,&nbw,&nzs,3); for (j=0;j<nbw;j++) { zzn6_mul(_MIPP_ res,res,res); g(_MIPP_ A,A,Qx,Qy,w); zzn6_mul(_MIPP_ res,w,res); } if (n>0) { zzn6_mul(_MIPP_ res,&zn[n/2],res); g(_MIPP_ A,t[n/2],Qx,Qy,w); zzn6_mul(_MIPP_ res,w,res); } for (j=0;j<nzs;j++) { zzn6_mul(_MIPP_ res,res,res); g(_MIPP_ A,A,Qx,Qy,w); zzn6_mul(_MIPP_ res,w,res); } } zzn6_copy(res,w); zzn6_powq(_MIPP_ w); zzn6_mul(_MIPP_ res,w,res); zzn6_copy(res,w); zzn6_powq(_MIPP_ w); zzn6_powq(_MIPP_ w); zzn6_powq(_MIPP_ w); zzn6_inv(_MIPP_ res); zzn6_mul(_MIPP_ res,w,res); res->unitary=TRUE; #ifndef MR_STATIC memkill(_MIPP_ mem,28); ecp_memkill(_MIPP_ mem1,6); #else memset(mem,0,MR_BIG_RESERVE(28)); memset(mem1,0,MR_ECP_RESERVE(6)); #endif }