public EllipticCurve_Point GenerateRandomPoint() { EllipticCurve_Point ret = new EllipticCurve_Point(); BigInteger x = new BigInteger(); BigInteger sqr = new BigInteger(); BigInteger pp = BigInteger.Parse(P.ToString()); do { x = Maths.RandInRange(0, P); BigInteger alpha = ((x * x * x) + A * x + B) % P; if (alpha == 0) { ret.X = x; ret.Y = 0; return(ret); } BigInteger bi = BigInteger.Parse(alpha.ToString()); sqr = Maths.ModSqrt(bi, pp); if ((sqr * sqr) % pp != bi) { sqr = 0; } }while (sqr == 0); ret.X = x; ret.Y = sqr; return(ret); }
public string SingGen(byte[] h, BigInteger d) { BigInteger alpha = BigInteger.Parse(SupportEDS.DecStringFromByteArray(h)); BigInteger e = alpha % this.curv.N; if (e == 0) { e = 1; } BigInteger k = new BigInteger(); EllipticCurve_Point C = new EllipticCurve_Point(); BigInteger r = new BigInteger(); BigInteger s = new BigInteger(); do { Random rnd = new Random(); do { k = Maths.RandBigInteger(Maths.Length(this.curv.N), rnd); }while ((k < 0) || (k > this.curv.N)); curv.Mult(k, this.curv.G, ref C); r = C.X % this.curv.N; s = ((r * d) + (k * e)) % this.curv.N; }while ((r == 0) || (s == 0)); int midl = Maths.Length(this.curv.N) / 4; string Rvector = SupportEDS.Add0PaddingToString(r.ToString("X"), midl); string Svector = SupportEDS.Add0PaddingToString(s.ToString("X"), midl); return(Rvector + Svector); }
public bool Mult(BigInteger x, EllipticCurve_Point p1, ref EllipticCurve_Point res) { if (x < 0) { res = EllipticCurve.O; return(false); } BigInteger k = x; EllipticCurve_Point S; res = new EllipticCurve_Point(true); S = new EllipticCurve_Point(p1); while (k != 0) { BigInteger m = k % 2; if (m == 1) { if (!Sum(res, S, ref res)) { return(false); } } k = k / 2; if (k != 0) { if (!Sum(S, S, ref S)) { return(false); } } } return(true); }
private BigInteger TestHelper(BigInteger h0, BigInteger h1, BigInteger p2, BigInteger t, EllipticCurve_Point P) { BigInteger n0 = p2 - t, n1 = p2 + t; EllipticCurve_Point Q = new EllipticCurve_Point(); if (n0 >= h0 && n0 <= h1) { if (ecs.Mult(n0, P, ref Q)) { if (Q.IsNull) { return(n0); } } } if (n1 >= h0 && n1 <= h1) { if (ecs.Mult(n1, P, ref Q)) { if (Q.IsNull) { return(n1); } } } return(-1); }
public EllipticCurve_Point GenPublicKey(BigInteger d) { EllipticCurve_Point Q = new EllipticCurve_Point(); curv.Mult(d, this.curv.G, ref Q); return(Q); }
public EllipticCurve(BigInteger p, BigInteger a, BigInteger b, BigInteger n, EllipticCurve_Point g) { this.P = p; this.A = a; this.B = b; this.N = n; this.G = g; }
public EllipticCurve_Point Negate(EllipticCurve_Point p1) { EllipticCurve_Point Q = new EllipticCurve_Point(); Q.X = p1.X; Q.Y = (p - p1.Y) % p; if (Q.Y < 0) { Q.Y += p; } return(Q); }
private BigInteger Test(BigInteger h0, BigInteger h1, BigInteger p2, BigInteger x, BigInteger y, List <BigInteger> m, List <BigInteger> v, BigInteger N) { BigInteger m2 = 0, n = 0, pr = 0; BigInteger t, tModN = 0; EllipticCurve_Point P = new EllipticCurve_Point(); P.X = x; P.Y = y; t = Maths.ChineseRemainderTheorem(m, v); tModN = (t % N) % q2; n = TestHelper(h0, h1, p2, tModN, P); if (n != -1) { return(n); } return(-1); }
public bool SingVer(byte[] H, string sing, EllipticCurve_Point Q) { int midl = Maths.Length(this.curv.N) / 4; string Rvector = sing.Substring(0, midl); string Svector = sing.Substring(midl, midl); BigInteger r = BigInteger.Parse(SupportEDS.DecStringFromHexString(Rvector)); BigInteger s = BigInteger.Parse(SupportEDS.DecStringFromHexString(Svector)); if ((r < 1) || (r > (this.curv.N - 1)) || (s < 1) || (s > (this.curv.N - 1))) { return(false); } BigInteger alpha = BigInteger.Parse(SupportEDS.DecStringFromByteArray(H)); BigInteger e = alpha % this.curv.N; if (e == 0) { e = 1; } BigInteger v = Maths.GetInverse(e, this.curv.N); BigInteger z1 = (s * v) % this.curv.N; BigInteger z2 = this.curv.N + ((-(r * v)) % this.curv.N); EllipticCurve_Point A = new EllipticCurve_Point(); EllipticCurve_Point B = new EllipticCurve_Point(); EllipticCurve_Point C = new EllipticCurve_Point(); curv.Mult(z1, this.curv.G, ref A); curv.Mult(z2, Q, ref B); curv.Sum(A, B, ref C); BigInteger R = C.X % this.curv.N; if (R == r) { return(true); } else { return(false); } }
public int CompareTo(object p2) { EllipticCurve_Point pp = p2 as EllipticCurve_Point; if (this.X < pp.X) { return(-1); } if (this.X > pp.X) { return(1); } if (this.Y < pp.Y) { return(-1); } if (this.Y > pp.Y) { return(1); } return(0); }
public EllipticCurve_Point PointFinding(BigInteger u, BigInteger h, BigInteger n) { EllipticCurve_Point point; EllipticCurve_Point G = new EllipticCurve_Point(); EllipticCurve_Point Q = new EllipticCurve_Point(); do { do { point = GenerateRandomPoint(); }while (point.IsNull); Mult(h, point, ref G); }while (G.IsNull); Mult(n, G, ref Q); if (Q.IsNull) { return(G); } else { return(EllipticCurve.O); } }
public bool Sum(EllipticCurve_Point p1, EllipticCurve_Point p2, ref EllipticCurve_Point res) { BigInteger lambda = -1; if (p1.IsNull && p2.IsNull) { res = EllipticCurve.O; return(true); } BigInteger ps = (p - p2.Y) % p; if (p1.X == p2.X && p1.Y == ps) { res = EllipticCurve.O; return(true); } if (p1.IsNull) { res = new EllipticCurve_Point(p2); return(true); } if (p2.IsNull) { res = new EllipticCurve_Point(p1); return(true); } if (p1.X != p2.X) { BigInteger i = (p2.X - p1.X) % p; BigInteger y = (p2.Y - p1.Y) % p; if (i < 0) { i += p; } i = Maths.GetInverse(i, p); if (i == 0) { return(false); } lambda = (i * y) % p; } else { BigInteger y2 = (2 * p1.Y) % p; BigInteger i = Maths.GetInverse(y2, p); if (i < 0) { i += p; } if (i == 0) { return(false); } BigInteger x3 = (3 * p1.X) % p; x3 = (x3 * p1.X) % p; x3 = (x3 + a) % p; lambda = (x3 * i) % p; } res = new EllipticCurve_Point(); BigInteger lambda2 = (lambda * lambda) % p; BigInteger delta1 = (p1.X + p2.X) % p; res.X = (lambda2 - delta1) % p; BigInteger delta2 = (p1.X - res.X) % p; delta2 = (lambda * delta2) % p; res.Y = (delta2 - p1.Y) % p; if (res.X < 0) { res.X += p; } if (res.Y < 0) { res.Y += p; } return(true); }
private BigInteger SchoofAlgorithm(BigInteger t2) { BigInteger p2 = ecs.P + 1; BigInteger h0 = p2 - q2; BigInteger h1 = p2 + q2; BigInteger x = 0, y = 0, n = 0, N = 0, t = 0; EllipticCurve_Point P = new EllipticCurve_Point(), Q = new EllipticCurve_Point(); List <BigInteger> m = null, v = null; t = q2; P.X = xl[0]; P.Y = yl[0]; n = TestHelper(h0, h1, p2, t, P); if (n != -1) { return(n); } if (bw.CancellationPending) { return(-1); } x = xl[0]; y = yl[0]; m = new List <BigInteger>(); v = new List <BigInteger>(); m.Add(2); v.Add(t2); for (int i = 1; i < primes.Count; i++) { if (bw.CancellationPending) { return(-1); } if (ecs.P < 64 && i > 200) { return(-1); } if (ecs.P < 1024 && i > 2000) { return(-1); } if (ecs.P == 1301) { return(-1); } BigInteger f = primes[i]; BigInteger u = BabyStepGiantStep(primes[i], t2, x, y); if (i > 10000) { return(-1); } if (u >= 0) { m.Add(f); v.Add(u); if (m.Count >= 3) { N = 2; for (int j = 1; j < m.Count; j++) { N *= m[j]; } if (N > q4 && m.Count >= 3) { n = Test(h0, h1, p2, x, y, m, v, N); if (n > 0) { return(n); } } } } else if (u == -3) { for (int j = 1; ; j++) { if (bw.CancellationPending) { return(-1); } t = j * f; if (t % 2 == t2) { P.X = x; P.Y = y; if (t >= h0 && t <= h1) { if (ecs.Mult(t, P, ref Q)) { if (Q.IsNull) { return(t); } } } } } } } return(-1); }
private BigInteger BabyStepGiantStep(long l, BigInteger t2, BigInteger x, BigInteger y) { long W = (long)(Math.Ceiling(Math.Sqrt(0.5 * l))); BigInteger beta = 0, gamma = 0, k = 0; BigInteger pmodl = ecs.P % l; BigInteger ps = ecs.P * ecs.P; BigInteger Ax = 0, Bx = 0, Ay = 0, By = 0, kmod2 = 0; BigInteger psil = Maths.Psi((int)l, x, y, ecs.A, ecs.B, ecs.P); List <ijPair> S = new List <ijPair>(); if (psil == 0) { return(-3); } EllipticCurve_Point P0 = new EllipticCurve_Point(), P1 = new EllipticCurve_Point(), P2 = new EllipticCurve_Point(), P3 = new EllipticCurve_Point(), P12 = new EllipticCurve_Point(), P = new EllipticCurve_Point(); List <EllipticCurve_Point> A = new List <EllipticCurve_Point>(); List <EllipticCurve_Point> B = new List <EllipticCurve_Point>(); P.X = x; P.Y = y; P0.X = BigInteger.ModPow(x, ecs.P, psil); P0.Y = BigInteger.ModPow(y, ecs.P, psil); P1.X = BigInteger.ModPow(x, ps, psil); P1.Y = BigInteger.ModPow(y, ps, psil); ecs.Mult(pmodl, P, ref P2); if (!ecs.Sum(P1, P2, ref P12)) { return(-1); } if (P12.IsNull) { return(0); } for (beta = 0; beta < W; beta++) { if (!ecs.Mult(beta, P0, ref P3)) { return(-1); } if (!ecs.Sum(P12, P3, ref P3)) { return(-1); } bool found = false; for (int i = 0; !found && i < A.Count; i++) { found = A[i].X == P3.X && A[i].Y == P3.Y; } if (!found && !P3.IsNull) { A.Add(P3); } } for (gamma = 0; gamma <= W; gamma++) { if (!ecs.Mult(gamma * W, P0, ref P3)) { return(-1); } bool found = false; for (int i = 0; !found && i < B.Count; i++) { found = B[i].X == P3.X && B[i].Y == P3.Y; } if (!found && !P3.IsNull) { B.Add(P3); } } if (A.Count != 0 && B.Count != 0) { A.Sort(); B.Sort(); for (int i = 0; i < A.Count; i++) { Ax = A[i].X; for (int j = 0; j < B.Count; j++) { Bx = B[j].X; if (Ax == Bx) { ijPair ij = new ijPair(); ij.i = i; ij.j = j; S.Add(ij); } } } if (S.Count == 1) { beta = S[0].i; gamma = S[0].j; k = beta + gamma * W; kmod2 = k % 2; if (t2 == kmod2) { Ay = A[(int)beta].Y; By = B[(int)gamma].Y; if (Ay == By) { return(k); } else { return(l - k); } } k = beta - gamma * W; kmod2 = k % 2; if (kmod2 < 0) { kmod2 += 2; } if (t2 == kmod2) { Ay = A[(int)beta].Y; By = B[(int)gamma].Y; if (Ay == By) { return(k); } else { return(l - k); } } } } return(-1); }
public EllipticCurve_Point(EllipticCurve_Point p) { this.X = p.X; this.Y = p.Y; this.IsNull = p.IsNull; }
public NameSubmit(BigInteger d, EllipticCurve_Point Q) { this.d = d; this.Q = Q; InitializeComponent(); }