double mult2_precomp(int eb,big x,big y,big a2,big a6,int M,int A,int B,int C) { big e,c,d; int iterations=0; ebrick2 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); ebrick2_init(&binst,x,y,a2,a6,M,A,B,C,WINDOW,eb); bigbits(eb,e); start=clock(); do { mul2_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); ebrick2_end(&binst); memkill(mem,3); return elapsed; }
/* * Class: edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m * Method: computeF2mExponentiateWithPrecomputedValues * Signature: (JJ[B)J * This function wraps the actual computation of the exponentation with precomputed values for the requested base for Dlog groups over F2m. 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_MiraclDlogECF2m_computeF2mExponentiateWithPrecomputedValues (JNIEnv * env, jobject, jlong mipp, jlong ebrick2Pointer, jbyteArray exponent){ //private native long computeF2mExponentiateWithPrecomputedValues(long mip, long ebrickPointer, byte[] exponent); //translate parameters to miracl notation miracl* mip = (miracl*)mipp; 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 mul2_brick(mip, (ebrick2*)ebrick2Pointer, exponentB, x, y); epoint* p = new epoint(); p = epoint_init(mip); bool valid = epoint2_set(mip, x, y, 0, p); mirkill(x); mirkill(y); return (jlong)p; }
int main() { int ia,ib,promptr; epoint *PA,*PB; big A,B,a,b,q,pa,pb,key,x,y; ebrick2 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(10)]; /* we need 10 bigs... */ char mem_ecp[MR_ECP_RESERVE(2)]; /* ..and two elliptic curve points */ memset(mem_big, 0, MR_BIG_RESERVE(10)); /* clear the memory */ memset(mem_ecp, 0, MR_ECP_RESERVE(2)); 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); x=mirvar_mem(mip, mem_big, 5); y=mirvar_mem(mip, mem_big, 6); q=mirvar_mem(mip,mem_big,7); a=mirvar_mem(mip, mem_big, 8); b=mirvar_mem(mip, mem_big, 9); PA=epoint_init_mem(mip, mem_ecp, 0); /* initialise Elliptic Curve points */ PB=epoint_init_mem(mip, mem_ecp, 1); irand(mip, 3L); /* change parameter for different random numbers */ promptr=0; init_big_from_rom(B,WORDS,rom,WORDS*4,&promptr); /* Read in curve parameter B from ROM */ /* don't need q or G(x,y) (we have precomputed table from it) */ init_big_from_rom(q,WORDS,rom,WORDS*4,&promptr); init_big_from_rom(x,WORDS,rom,WORDS*4,&promptr); init_big_from_rom(y,WORDS,rom,WORDS*4,&promptr); convert(mip,1,A); /* set A=1 */ /* Create precomputation instance from precomputed table in ROM */ ebrick2_init(&binst,prom,A,B,CURVE_M,CURVE_A,CURVE_B,CURVE_C,WINDOW,CURVE_M); /* offline calculations */ bigbits(mip,CURVE_M,a); /* A's random number */ ia=mul2_brick(mip,&binst,a,pa,pa); /* a*G =(pa,ya), ia is sign of ya */ bigbits(mip,CURVE_M,b); /* B's random number */ ib=mul2_brick(mip,&binst,b,pb,pb); /* b*G =(pb,yb), ib is sign of yb */ /* online calculations */ ecurve2_init(mip,CURVE_M,CURVE_A,CURVE_B,CURVE_C,A,B,FALSE,MR_PROJECTIVE); epoint2_set(mip,pb,pb,ib,PB); /* decompress PB */ ecurve2_mult(mip,a,PB,PB); epoint2_get(mip,PB,key,key); /* since internal base is HEX, can use otnum instead of cotnum - avoiding a base change */ printf("Alice's Key= "); otnum(mip,key,stdout); epoint2_set(mip,pa,pa,ia,PB); /* decompress PA */ ecurve2_mult(mip,b,PB,PB); epoint2_get(mip,PB,key,key); printf("Bob's Key= "); otnum(mip,key,stdout); /* clear the memory */ memset(mem_big, 0, MR_BIG_RESERVE(10)); memset(mem_ecp, 0, MR_ECP_RESERVE(2)); return 0; }
int BaseOT::Miracl_mulbrick(ebrick2* bg, big x, big y, big z) { return mul2_brick(bg, x, y, z); }
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; }
void Miraclmulbrick(ebrick2* bg, EC2& result, big e) { Big xtmp, ytmp; mul2_brick(bg, e, xtmp.getbig(), ytmp.getbig()); MiraclInitPoint(result, xtmp, ytmp); }