/* copy this=P */ public void copy(ECP2 P) { x.copy(P.x); y.copy(P.y); z.copy(P.z); INF = P.INF; }
/* 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*=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(); }
/* normalises m-array of ECP2 points. Requires work vector of m FP2s */ public static void multiaffine(int m, ECP2[] P) { int i; FP2 t1 = new FP2(0); FP2 t2 = new FP2(0); FP2[] work = new FP2[m]; work[0] = new FP2(1); work[1] = new FP2(P[0].z); for (i = 2; i < m; i++) { work[i] = new FP2(work[i - 1]); work[i].mul(P[i - 1].z); } t1.copy(work[m - 1]); t1.mul(P[m - 1].z); t1.inverse(); t2.copy(P[m - 1].z); work[m - 1].mul(t1); for (i = m - 2;; i--) { if (i == 0) { work[0].copy(t1); work[0].mul(t2); break; } work[i].mul(t2); work[i].mul(t1); t2.mul(P[i].z); } /* now work[] contains inverses of all Z coordinates */ for (i = 0; i < m; i++) { P[i].z.one(); t1.copy(work[i]); t1.sqr(); P[i].x.mul(t1); t1.mul(work[i]); P[i].y.mul(t1); } }
/* 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); }
/* construct this from x - but set to O if not on curve */ public ECP2(FP2 ix) { x = new FP2(ix); y = new FP2(1); z = new FP2(1); FP2 rhs = RHS(x); if (rhs.sqrt()) { y.copy(rhs); INF = false; } else { x.zero(); INF = true; } }
/* 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); }
/* copy this=x */ public void copy(FP4 x) { a.copy(x.a); b.copy(x.b); }