Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
BOOL MiraclInitBrick(ebrick2* brick, EC2 point, fparams* param)
{
	Big x, y;
	point.getxy(x, y);
	return ebrick2_init(brick, x.getbig(), y.getbig(), param->eccparams.BA->getbig(), param->eccparams.BB->getbig(),
			param->eccparams.m, param->eccparams.a, param->eccparams.b, param->eccparams.c, 8, param->secparam);
}
Exemplo n.º 3
0
/*
 * Class:     edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m
 * Method:    initF2mExponentiateWithPrecomputedValues
 * Signature: (JIIII[B[BJ[BII)J
 *
 * This function wraps the creation of an ebrick structure used to precompute exponentiations for a certain base for Dlog groups over Fp. It returns
 * a pointer to the ebrick structure which will be kept by the calling application (edu.biu.scapi...) in some data structure and will
 * be used for further calls to exponentiations with the same base.
 */
JNIEXPORT jlong JNICALL Java_edu_biu_scapi_primitives_dlog_miracl_MiraclDlogECF2m_initF2mExponentiateWithPrecomputedValues
	(JNIEnv * env, jobject, jlong mipp, jint mod, jint k1, jint k2, jint k3, jbyteArray a, jbyteArray b, jlong base, jint window, jint maxBits){

	//translate parameters  to miracl notation
	miracl* mip = (miracl*)mipp;
	big aB = byteArrayToMiraclBig(env, mip, a);
	big bB = byteArrayToMiraclBig(env, mip, b);

	//Get the coordinates (x,y) from the requested base point: 
	big x, y;
	x = mirvar(mip, 0);
	y = mirvar(mip, 0);
	epoint2_get(mip, (epoint*)base, x, y);

	//Create a new structure to hold the precomputed values for given base and exponent
	ebrick2* exponentiations = new ebrick2();
	//Perform precomputation
	ebrick2_init(mip, exponentiations, x, y, aB, bB, mod, k1, k2, k3, window, maxBits);
	//clean up
	mirkill(aB);
	mirkill(bB);
	//May be clan up also x and y
	mirkill(x);
	mirkill(y);
	//Return the pointer to the structure where the precomputed values are held
	return (jlong)exponentiations;
}
Exemplo n.º 4
0
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;
}
Exemplo n.º 5
0
BOOL BaseOT::Miracl_InitBrick(ebrick2* brick, EC2* point)
{
	Big x, y;
	point->getxy(x, y);
	return ebrick2_init(brick, x.getbig(), y.getbig(), m_BA->getbig(), m_BB->getbig(), m_nM, m_nA, m_nB, m_nC, 8, m_SecParam);
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
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;
}