Example #1
0
/* Line function */
static void PAIR_line(FP12 *v,ECP2 *A,ECP2 *B,BIG Qx,BIG Qy)
{
	ECP2 P;
	FP2 Z3,X,Y,ZZ,T,NY;
	FP4 a,b,c;
	int D;
	ECP2_copy(&P,A);
	if (A==B)
		D=ECP2_dbl(A);  // check these return numbers...
	else
		D=ECP2_add(A,B);
	if (D<0)
	{ /* Infinity */
		FP12_one(v);
		return;
	}

	FP2_copy(&Z3,&(A->z));
	FP4_zero(&c);
	FP2_sqr(&ZZ,&(P.z));    /* ZZ=Z^2 */
	if (D==0)
	{ /* addition */
		ECP2_get(&X,&Y,B);
		FP2_mul(&T,&(P.z),&Y);  /* T=Z*Y2 */

		FP2_mul(&ZZ,&ZZ,&T);

		FP2_neg(&NY,&(P.y));
		FP2_add(&ZZ,&ZZ,&NY); /* ZZ=Z^3*Y2-Y (slope numerator) */
		FP2_pmul(&Z3,&Z3,Qy);    /* Z3*Qy */
		FP2_mul(&T,&T,&(P.x));
		FP2_mul(&X,&X,&NY);
		FP2_add(&T,&T,&X);       /* Z*Y2*X-X2*Y */
		FP4_from_FP2s(&a,&Z3,&T); /* a=[Z3*Qy,Z*Y2*X-X2*Y] */
		FP2_neg(&ZZ,&ZZ);
		FP2_pmul(&ZZ,&ZZ,Qx);
		FP4_from_FP2(&b,&ZZ);    /* b=-slope*Qx */
	}
	else
	{ /* doubling */
		FP2_sqr(&T,&(P.x));
		FP2_imul(&T,&T,3);   /* T=3X^2 (slope numerator) */
		FP2_sqr(&Y,&(P.y));

		FP2_add(&Y,&Y,&Y);   /* Y=2Y^2 */
		FP2_mul(&Z3,&Z3,&ZZ);   /* Z3=Z3*ZZ */
		FP2_pmul(&Z3,&Z3,Qy);   /* Z3=Z3*ZZ*Qy */

		FP2_mul(&X,&(P.x),&T);
		FP2_sub(&X,&X,&Y);      /* X=X*slope-2Y^2 */
		FP4_from_FP2s(&a,&Z3,&X); /* a=[Z3*ZZ*Qy , X*slope-2Y^2] */
		FP2_neg(&T,&T);
		FP2_mul(&ZZ,&ZZ,&T);
		FP2_pmul(&ZZ,&ZZ,Qx);
		FP4_from_FP2(&b,&ZZ);    /* b=-slope*ZZ*Qx */
	}

	FP12_from_FP4s(v,&a,&b,&c);
}
Example #2
0
int FP6_neg(const PAIRING_GROUP *group, FP6 *r, const FP6 *a) {
	if (!FP2_neg(group, &r->f[0], &a->f[0])) {
		return 0;
	}
	if (!FP2_neg(group, &r->f[1], &a->f[1])) {
		return 0;
	}
	if (!FP2_neg(group, &r->f[2], &a->f[2])) {
		return 0;
	}
	return 1;
}
/* SU= 160 */
void FP4_neg(FP4 *w,FP4 *x)
{ /* Just one field neg */
	FP2 m,t;
	FP2_add(&m,&(x->a),&(x->b));
	FP2_neg(&m,&m);
	FP2_norm(&m);
	FP2_add(&t,&m,&(x->b));
	FP2_add(&(w->b),&m,&(x->a));
	FP2_copy(&(w->a),&t);
}
/* SU= 160 */
void FP4_inv(FP4 *w,FP4 *x)
{
	FP2 t1,t2;
	FP2_sqr(&t1,&(x->a));
	FP2_sqr(&t2,&(x->b));
	FP2_mul_ip(&t2);
	FP2_sub(&t1,&t1,&t2);
	FP2_inv(&t1,&t1);
	FP2_mul(&(w->a),&t1,&(x->a));
	FP2_neg(&t1,&t1);
	FP2_mul(&(w->b),&t1,&(x->b));
}
/* SU= 232 */
void FP4_sqr(FP4 *w,FP4 *x)
{
	FP2 t1,t2,t3;
 
	FP2_mul(&t3,&(x->a),&(x->b)); /* norms x */
	FP2_copy(&t2,&(x->b));
	FP2_add(&t1,&(x->a),&(x->b)); 
	FP2_mul_ip(&t2); 

	FP2_add(&t2,&(x->a),&t2); 

	FP2_mul(&(w->a),&t1,&t2);

	FP2_copy(&t2,&t3);   
	FP2_mul_ip(&t2);         

	FP2_add(&t2,&t2,&t3); 

	FP2_neg(&t2,&t2);         
	FP2_add(&(w->a),&(w->a),&t2);  /* a=(a+b)(a+i^2.b)-i^2.ab-ab = a*a+ib*ib */
	FP2_add(&(w->b),&t3,&t3);  /* b=2ab */

	FP4_norm(w);
}
/* SU= 16 */
void FP4_nconj(FP4 *w,FP4 *x)
{
	FP2_copy(&(w->b),&(x->b));
	FP2_neg(&(w->a), &(x->a));
	FP2_norm(&(w->a));
}