double mult_precomp(int eb,big x,big y,big a,big b,big p) { big e,c,d; int iterations=0; ebrick binst; clock_t start; double elapsed; char *mem; mem=(char *)memalloc(3); e=mirvar_mem(mem,0); c=mirvar_mem(mem,1); d=mirvar_mem(mem,2); ebrick_init(&binst,x,y,a,b,p,WINDOW,eb); bigbits(eb,e); start=clock(); do { mul_brick(&binst,e,c,d); iterations++; elapsed=(clock()-start)/(double)CLOCKS_PER_SEC; } while (elapsed<MIN_TIME || iterations<MIN_ITERS); elapsed=1000.0*elapsed/iterations; printf("EP - %8d iterations ",iterations); printf(" %8.2lf ms per iteration\n",elapsed); ebrick_end(&binst); memkill(mem,3); return elapsed; }
/* * Class: edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp * Method: computeFpExponentiateWithPrecomputedValue * Signature: (JJ[B)J * * This function wraps the actual computation of the exponentation with precomputed values for the requested base for Dlog groups over Fp. It gets as a parameter * a pointer to the ebrick structure created by a previous call to initFpExponentiateWithPrecomputedValues. This implies that initFpExponentiateWithPrecomputedValues * MUST have been called prior to this function for the same base. */ JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECFp_computeFpExponentiateWithPrecomputedValues (JNIEnv * env, jobject, jlong m, jlong ebrickPointer, jbyteArray exponent){ //translate parameters to miracl notation miracl* mip = (miracl*)m; big exponentB = byteArrayToMiraclBig(env, mip, exponent); //(x,y) are the coordinates of the point which is the result of the exponentiation big x, y; x = mirvar(mip, 0); y = mirvar(mip, 0); //calculates the required exponent mul_brick(mip, (ebrick*)ebrickPointer, exponentB, x, y); //printf("The result of mul_brick(mip, exponentiations, exponent, x, y) is x=%d, y=%d\n", (*x).w,(*y).w); epoint* p = new epoint(); p = epoint_init(mip); epoint_set(mip, x, y, 0, p); mirkill(x); mirkill(y); return (jlong)p; }
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; }
int BaseOT::Miracl_mulbrick(ebrick* bg, big x, big y, big z) { return mul_brick(bg, x, y, z); }
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; }
void Miraclmulbrick(ebrick* bg, ECn& result, big e) { Big xtmp, ytmp; mul_brick(bg, e, xtmp.getbig(), ytmp.getbig()); MiraclInitPoint(result, xtmp, ytmp); }
int main() { int promptr; epoint *PB; big A,B,p,a,b,pa,pb,key; ebrick binst; miracl instance; /* create miracl workspace on the stack */ /* Specify base 16 here so that HEX can be read in directly without a base-change */ miracl *mip=mirsys(&instance,WORDS*HEXDIGS,16); /* size of bigs is fixed */ char mem_big[MR_BIG_RESERVE(8)]; /* we need 8 bigs... */ char mem_ecp[MR_ECP_RESERVE(1)]; /* ..and 1 elliptic curve points */ memset(mem_big, 0, MR_BIG_RESERVE(8)); /* clear the memory */ memset(mem_ecp, 0, MR_ECP_RESERVE(1)); A=mirvar_mem(mip, mem_big, 0); /* Initialise big numbers */ B=mirvar_mem(mip, mem_big, 1); pa=mirvar_mem(mip, mem_big, 2); pb=mirvar_mem(mip, mem_big, 3); key=mirvar_mem(mip, mem_big, 4); a=mirvar_mem(mip, mem_big, 5); b=mirvar_mem(mip, mem_big, 6); p=mirvar_mem(mip, mem_big, 7); PB=epoint_init_mem(mip, mem_ecp, 0); /* initialise Elliptic Curve points */ irand(mip, 3L); /* change parameter for different random numbers */ promptr=0; init_big_from_rom(p,WORDS,rom,WORDS*5,&promptr); /* Read in prime modulus p from ROM */ init_big_from_rom(B,WORDS,rom,WORDS*5,&promptr); /* Read in curve parameter B from ROM */ /* don't need q or G(x,y) (we have precomputed table from it) */ convert(mip,-3,A); /* set A=-3 */ /* Create precomputation instance from precomputed table in ROM */ ebrick_init(&binst,prom,A,B,p,WINDOW,CURVE_BITS); /* offline calculations */ bigbits(mip,CURVE_BITS,a); /* A's random number */ mul_brick(mip,&binst,a,pa,pa); /* a*G =(pa,ya) */ bigbits(mip,CURVE_BITS,b); /* B's random number */ mul_brick(mip,&binst,b,pb,pb); /* b*G =(pb,yb) */ /* swap X values of point */ /* online calculations */ ecurve_init(mip,A,B,p,MR_PROJECTIVE); epoint_set(mip,pb,pb,0,PB); /* decompress PB */ ecurve_mult(mip,a,PB,PB); epoint_get(mip,PB,key,key); /* since internal base is HEX, can use otnum instead of cotnum - avoiding a base change */ #ifndef MR_NO_STANDARD_IO printf("Alice's Key= "); otnum(mip,key,stdout); #endif epoint_set(mip,pa,pa,0,PB); /* decompress PA */ ecurve_mult(mip,b,PB,PB); epoint_get(mip,PB,key,key); #ifndef MR_NO_STANDARD_IO printf("Bob's Key= "); otnum(mip,key,stdout); #endif /* clear the memory */ memset(mem_big, 0, MR_BIG_RESERVE(8)); memset(mem_ecp, 0, MR_ECP_RESERVE(1)); return 0; }