Example #1
0
/* Galbraith & Scott Method */
static void gs(BIG u[4],BIG e)
{
	int i,j;
	BIG v[4],t,q;
	DBIG d;
	BIG_rcopy(q,CURVE_Order);
	for (i=0;i<4;i++)
	{
		BIG_rcopy(t,CURVE_WB[i]);
		BIG_mul(d,t,e);
		BIG_ddiv(v[i],d,q);
		BIG_zero(u[i]);
	}

	BIG_copy(u[0],e);
	for (i=0;i<4;i++)
		for (j=0;j<4;j++)
		{
			BIG_rcopy(t,CURVE_BB[j][i]);
			BIG_modmul(t,v[j],t,q);
			BIG_add(u[i],u[i],q);
			BIG_sub(u[i],u[i],t);
			BIG_mod(u[i],q);
		}
	return;
}
/* SU= 16 */
void BIG_fromBytes(BIG a,char *b)
{
	int i;
	BIG_zero(a);
	for (i=0;i<MODBYTES;i++)
	{
		BIG_fshl(a,8); a[0]+=(int)(unsigned char)b[i];
		//BIG_inc(a,(int)(unsigned char)b[i]); BIG_norm(a);
	}
#ifdef DEBUG_NORM
	a[NLEN]=0;
#endif
}
Example #3
0
/* Galbraith & Scott Method */
static void gs(BIG u[4],BIG e)
{
    int i;
#if CHOICE<BLS_CURVES
    int j;
    BIG v[4],t,q;
    DBIG d;
    BIG_rcopy(q,CURVE_Order);
    for (i=0; i<4; i++)
    {
        BIG_rcopy(t,CURVE_WB[i]);
//BIG_norm(t); BIG_norm(e);
        BIG_mul(d,t,e);
        BIG_ddiv(v[i],d,q);
        BIG_zero(u[i]);
    }

    BIG_copy(u[0],e);
    for (i=0; i<4; i++)
        for (j=0; j<4; j++)
        {
            BIG_rcopy(t,CURVE_BB[j][i]);
            BIG_modmul(t,v[j],t,q);
            BIG_add(u[i],u[i],q);
            BIG_sub(u[i],u[i],t);
            BIG_mod(u[i],q);
        }

#else

    BIG x,w;
    BIG_rcopy(x,CURVE_Bnx);
    BIG_copy(w,e);

    for (i=0; i<4; i++)
    {
        BIG_copy(u[i],w);
        BIG_mod(u[i],x);
        BIG_sdiv(w,x);
    }

#endif
    return;
}
Example #4
0
/* GLV method */
static void glv(BIG u[2],BIG e)
{
#if CHOICE<BLS_CURVES
    int i,j;
    BIG v[2],t,q;
    DBIG d;
    BIG_rcopy(q,CURVE_Order);
    for (i=0; i<2; i++)
    {
        BIG_rcopy(t,CURVE_W[i]);
//BIG_norm(t); BIG_norm(e);
        BIG_mul(d,t,e);
        BIG_ddiv(v[i],d,q);
        BIG_zero(u[i]);
    }
    BIG_copy(u[0],e);
    for (i=0; i<2; i++)
        for (j=0; j<2; j++)
        {
            BIG_rcopy(t,CURVE_SB[j][i]);
            BIG_modmul(t,v[j],t,q);
            BIG_add(u[i],u[i],q);
            BIG_sub(u[i],u[i],t);
            BIG_mod(u[i],q);
        }

#else
// -(x^2).P = (Beta.x,y)

    BIG x,x2,q;
    BIG_rcopy(x,CURVE_Bnx);
    BIG_smul(x2,x,x);
    BIG_copy(u[0],e);
    BIG_mod(u[0],x2);
    BIG_copy(u[1],e);
    BIG_sdiv(u[1],x2);

    BIG_rcopy(q,CURVE_Order);
    BIG_sub(u[1],q,u[1]);

#endif

    return;
}
/* SU= 40 */
void BIG_smul(BIG c,BIG a,BIG b)
{
	int i,j;
	chunk carry;
	BIG_norm(a);  
	BIG_norm(b);

	BIG_zero(c);
	for (i=0;i<NLEN;i++)
	{
		carry=0;
		for (j=0;j<NLEN;j++)
			if (i+j<NLEN) carry=muladd(a[i],b[j],carry,&c[i+j]);
	}
#ifdef DEBUG_NORM
	c[NLEN]=0;
#endif

}
/* r=x^n using XTR method on traces of FP12s */
void FP4_xtr_pow(FP4 *r,FP4 *x,BIG n)
{
	int i,par,nb;
	BIG v;
	FP2 w;
	FP4 t,a,b,c;

	BIG_zero(v); BIG_inc(v,3); 
	FP2_from_BIG(&w,v);
	FP4_from_FP2(&a,&w);
	FP4_copy(&b,x);
	FP4_xtr_D(&c,x);

	BIG_norm(n); par=BIG_parity(n); BIG_copy(v,n); BIG_shr(v,1);
	if (par==0) {BIG_dec(v,1); BIG_norm(v);}

	nb=BIG_nbits(v);

	for (i=nb-1;i>=0;i--)
	{
		if (!BIG_bit(v,i))
		{
			FP4_copy(&t,&b);
			FP4_conj(x,x);
			FP4_conj(&c,&c);
			FP4_xtr_A(&b,&a,&b,x,&c);
			FP4_conj(x,x);
			FP4_xtr_D(&c,&t);
			FP4_xtr_D(&a,&a);
		}
		else
		{
			FP4_conj(&t,&a);
			FP4_xtr_D(&a,&b);
			FP4_xtr_A(&b,&c,&b,x,&t);
			FP4_xtr_D(&c,&c);
		}
	}
	if (par==0) FP4_copy(r,&c);
	else FP4_copy(r,&b);
	FP4_reduce(r);
}
/* SU= 240 */
void FP4_pow(FP4 *r,FP4* a,BIG b)
{
	FP4 w;
	BIG z,zilch;
	int bt;

	BIG_zero(zilch);
	BIG_norm(b);
	BIG_copy(z,b);
	FP4_copy(&w,a);
	FP4_one(r);

	while(1)
	{
		bt=BIG_parity(z);
		BIG_shr(z,1);
		if (bt) FP4_mul(r,r,&w);
		if (BIG_comp(z,zilch)==0) break;
		FP4_sqr(&w,&w);
	}
	FP4_reduce(r);
}
Example #8
0
void FP12_pow4(FP12 *p,FP12 *q,BIG u[4])
{
	int i,j,a[4],nb,m;
	FP12 g[8],c,s[2];
	BIG t[4],mt;
	sign8 w[NLEN*BASEBITS+1];

	for (i=0;i<4;i++)
		BIG_copy(t[i],u[i]);

	FP12_copy(&g[0],&q[0]); FP12_conj(&s[0],&q[1]); FP12_mul(&g[0],&s[0]);  /* P/Q */	
	FP12_copy(&g[1],&g[0]);
	FP12_copy(&g[2],&g[0]);
	FP12_copy(&g[3],&g[0]);
	FP12_copy(&g[4],&q[0]); FP12_mul(&g[4],&q[1]);  /* P*Q */
	FP12_copy(&g[5],&g[4]);
	FP12_copy(&g[6],&g[4]);
	FP12_copy(&g[7],&g[4]);

	FP12_copy(&s[1],&q[2]); FP12_conj(&s[0],&q[3]); FP12_mul(&s[1],&s[0]);       /* R/S */
	FP12_conj(&s[0],&s[1]); FP12_mul(&g[1],&s[0]); 
	FP12_mul(&g[2],&s[1]); 
	FP12_mul(&g[5],&s[0]);
	FP12_mul(&g[6],&s[1]);
	FP12_copy(&s[1],&q[2]); FP12_mul(&s[1],&q[3]);      /* R*S */
	FP12_conj(&s[0],&s[1]); FP12_mul(&g[0],&s[0]);
	FP12_mul(&g[3],&s[1]);
	FP12_mul(&g[4],&s[0]);
	FP12_mul(&g[7],&s[1]);

/* if power is even add 1 to power, and add q to correction */
	FP12_one(&c);

	BIG_zero(mt);
	for (i=0;i<4;i++)
	{
		if (BIG_parity(t[i])==0)
		{
			BIG_inc(t[i],1); BIG_norm(t[i]);
			FP12_mul(&c,&q[i]);
		}
		BIG_add(mt,mt,t[i]); BIG_norm(mt);
	}

	FP12_conj(&c,&c);
	nb=1+BIG_nbits(mt);

/* convert exponent to signed 1-bit window */
	for (j=0;j<nb;j++)
	{
		for (i=0;i<4;i++)
		{
			a[i]=BIG_lastbits(t[i],2)-2;
			BIG_dec(t[i],a[i]); BIG_norm(t[i]); 
			BIG_fshr(t[i],1);
		}
		w[j]=8*a[0]+4*a[1]+2*a[2]+a[3];
	}
	w[nb]=8*BIG_lastbits(t[0],2)+4*BIG_lastbits(t[1],2)+2*BIG_lastbits(t[2],2)+BIG_lastbits(t[3],2);
	FP12_copy(p,&g[(w[nb]-1)/2]);  

	for (i=nb-1;i>=0;i--)
	{
		m=w[i]>>7;
		j=(w[i]^m)-m;  /* j=abs(w[i]) */
		j=(j-1)/2;
		FP12_copy(&s[0],&g[j]);
		FP12_conj(&s[1],&g[j]);
		FP12_usqr(p,p);
		FP12_mul(p,&s[m&1]);
	}
	FP12_mul(p,&c); /* apply correction */
	FP12_reduce(p);
}