/* this*=this */ public void sqr() { norm(); FP2 t1 = new FP2(a); FP2 t2 = new FP2(b); FP2 t3 = new FP2(a); t3.mul(b); t1.add(b); t2.mul_ip(); t2.add(a); a.copy(t1); a.mul(t2); t2.copy(t3); t2.mul_ip(); t2.add(t3); t2.neg(); a.add(t2); b.copy(t3); b.add(t3); norm(); }
/* this+=this */ public int dbl() { if (INF) { return(-1); } if (y.iszilch()) { inf(); return(-1); } FP2 w1 = new FP2(x); FP2 w2 = new FP2(0); FP2 w3 = new FP2(x); FP2 w8 = new FP2(x); w1.sqr(); w8.copy(w1); w8.imul(3); w2.copy(y); w2.sqr(); w3.copy(x); w3.mul(w2); w3.imul(4); w1.copy(w3); w1.neg(); // w1.norm(); x.copy(w8); x.sqr(); x.add(w1); x.add(w1); x.norm(); z.mul(y); z.add(z); w2.add(w2); w2.sqr(); w2.add(w2); w3.sub(x); y.copy(w8); y.mul(w3); // w2.norm(); y.sub(w2); y.norm(); z.norm(); return(1); }
/* set this=-this */ public void neg() { FP2 m = new FP2(a); FP2 t = new FP2(0); m.add(b); m.neg(); m.norm(); t.copy(m); t.add(b); b.copy(m); b.add(a); a.copy(t); }
/* this*=y */ public void mul(FP4 y) { norm(); FP2 t1 = new FP2(a); FP2 t2 = new FP2(b); FP2 t3 = new FP2(0); FP2 t4 = new FP2(b); t1.mul(y.a); t2.mul(y.b); t3.copy(y.b); t3.add(y.a); t4.add(a); t4.mul(t3); t4.sub(t1); // t4.norm(); b.copy(t4); b.sub(t2); t2.mul_ip(); a.copy(t2); a.add(t1); norm(); }
/* this*=i where i = sqrt(-1+sqrt(-1)) */ public void times_i() { norm(); FP2 s = new FP2(b); FP2 t = new FP2(b); s.times_i(); t.add(s); // t.norm(); b.copy(a); a.copy(t); }
/* Calculate RHS of twisted curve equation x^3+B/i */ public static FP2 RHS(FP2 x) { x.norm(); FP2 r = new FP2(x); r.sqr(); FP2 b = new FP2(new BIG(ROM.CURVE_B)); b.div_ip(); r.mul(x); r.add(b); r.reduce(); return(r); }
/* this+=Q - return 0 for add, 1 for double, -1 for O */ public int add(ECP2 Q) { if (INF) { copy(Q); return(-1); } if (Q.INF) { return(-1); } bool aff = false; if (Q.z.isunity()) { aff = true; } FP2 A, C; FP2 B = new FP2(z); FP2 D = new FP2(z); if (!aff) { A = new FP2(Q.z); C = new FP2(Q.z); A.sqr(); B.sqr(); C.mul(A); D.mul(B); A.mul(x); C.mul(y); } else { A = new FP2(x); C = new FP2(y); B.sqr(); D.mul(B); } B.mul(Q.x); B.sub(A); D.mul(Q.y); D.sub(C); if (B.iszilch()) { if (D.iszilch()) { dbl(); return(1); } else { INF = true; return(-1); } } if (!aff) { z.mul(Q.z); } z.mul(B); FP2 e = new FP2(B); e.sqr(); B.mul(e); A.mul(e); e.copy(A); e.add(A); e.add(B); x.copy(D); x.sqr(); x.sub(e); A.sub(x); y.copy(A); y.mul(D); C.mul(B); y.sub(C); x.norm(); y.norm(); z.norm(); return(0); }
/* this+=x */ public void add(FP4 x) { a.add(x.a); b.add(x.b); }
/* Line function */ public static FP12 line(ECP2 A, ECP2 B, FP Qx, FP Qy) { ECP2 P = new ECP2(); FP4 a, b, c; P.copy(A); FP2 ZZ = new FP2(P.getz()); ZZ.sqr(); int D; if (A == B) { D = A.dbl(); // Check this return value in amcl_ec2.c } else { D = A.add(B); } if (D < 0) { return(new FP12(1)); } FP2 Z3 = new FP2(A.getz()); c = new FP4(0); if (D == 0) { // Addition FP2 X = new FP2(B.getx()); FP2 Y = new FP2(B.gety()); FP2 T = new FP2(P.getz()); T.mul(Y); ZZ.mul(T); FP2 NY = new FP2(P.gety()); NY.neg(); ZZ.add(NY); Z3.pmul(Qy); T.mul(P.getx()); X.mul(NY); T.add(X); a = new FP4(Z3, T); ZZ.neg(); ZZ.pmul(Qx); b = new FP4(ZZ); } else { // Doubling FP2 X = new FP2(P.getx()); FP2 Y = new FP2(P.gety()); FP2 T = new FP2(P.getx()); T.sqr(); T.imul(3); Y.sqr(); Y.add(Y); Z3.mul(ZZ); Z3.pmul(Qy); X.mul(T); X.sub(Y); a = new FP4(Z3, X); T.neg(); ZZ.mul(T); ZZ.pmul(Qx); b = new FP4(ZZ); } return(new FP12(a, b, c)); }