コード例 #1
0
ファイル: EllipticCurve_EDS.cs プロジェクト: Tgjmjgj/edsa
        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);
            }
        }
コード例 #2
0
ファイル: EllipticCurve.cs プロジェクト: Tgjmjgj/edsa
        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);
        }
コード例 #3
0
ファイル: PolynomialMethods.cs プロジェクト: Tgjmjgj/edsa
        private void Recurse(BigInteger p, List <BigInteger> A, ref List <BigInteger> root)
        {
            int               count = 0, degreeA = A.Count - 1, degreeB = 0;
            BigInteger        exp = 0, p1 = p - 1, D = 0, a = 0, b = 0, c = 0, e = 0;
            List <BigInteger> B = null, d = null;
            List <BigInteger> q = null, r = null, u = null;

            exp = p1 / 2;
            if (degreeA != 0)
            {
                if (degreeA == 1)
                {
                    if (A[0] != 0)
                    {
                        a = Maths.GetInverse(A[1], p);
                        b = A[0];
                        b = -b;
                        b = Maths.MulMod(b, a, p);
                        root.Add(b);
                    }
                }
                else if (degreeA == 2)
                {
                    a = Maths.MulMod(A[1], A[1], p);
                    b = Maths.MulMod(A[0], A[2], p);
                    c = Maths.MulMod(b, 4, p);
                    D = Maths.SubMod(a, c, p);
                    e = hcr.SquareRootModPrime(D, p);
                    BigInteger test = (e * e) % p;
                    if (e == 1)
                    {
                        return;
                    }
                    a = Maths.MulMod(A[2], 2, p);
                    D = Maths.GetInverse(a, p);
                    if (D == 0)
                    {
                        a = -a;
                        a = Maths.AddMod(a, p, p);
                        D = Maths.GetInverse(a, p);
                    }
                    a = Maths.SubMod(e, A[1], p);
                    root.Add(Maths.MulMod(a, D, p));
                    A[1] = -A[1];
                    e    = -e;
                    a    = Maths.AddMod(A[1], e, p);
                    root.Add(Maths.MulMod(a, D, p));
                }
                else
                {
                    do
                    {
                        count++;
                        a = hcr.RandomRange(0, p1);
                        u = new List <BigInteger>();
                        u.Add(a);
                        u.Add(1);
                        PolyPowMod(p, exp, u, A, ref d);
                        if (d.Count - 1 != 0)
                        {
                            d[0] = Maths.SubMod(d[0], 1, p);
                            B    = PolyGCDMod(p, d, A);
                            if (B.Count == 1 && B[0] == 1)
                            {
                                return;
                            }
                            degreeB = B.Count - 1;
                        }
                    }while (count < 16 && degreeB == 0 || degreeB == degreeA);
                    if (count == 16)
                    {
                        return;
                    }
                    Recurse(p, B, ref root);
                    PolyDivMod(p, A, B, ref q, ref r);
                    Recurse(p, q, ref root);
                }
            }
        }