Exemplo n.º 1
0
 public bool VerifySignature(byte[] message, BigInteger r, BigInteger s)
 {
     if (r.Sign < 1 || s.Sign < 1 || r.CompareTo(curve.N) >= 0 || s.CompareTo(curve.N) >= 0)
         return false;
     BigInteger e = CalculateE(curve.N, message);
     BigInteger c = s.ModInverse(curve.N);
     BigInteger u1 = (e * c).Mod(curve.N);
     BigInteger u2 = (r * c).Mod(curve.N);
     ECPoint point = SumOfTwoMultiplies(curve.G, u1, publicKey, u2);
     BigInteger v = point.X.Value.Mod(curve.N);
     return v.Equals(r);
 }
Exemplo n.º 2
0
        public ECPoint RecoverFromSignature(byte[] hash, BigInteger r, BigInteger s, int recId)
        {
            var x = r;
            if (recId > 1 && recId < 4)
            {
                x += Secp256k1.N;
                x = x % Secp256k1.P;
            }

            if (x >= Secp256k1.P)
            {
                return null;
            }

            byte[] xBytes = x.ToByteArrayUnsigned(true);
            byte[] compressedPoint = new Byte[33];
            compressedPoint[0] = (byte)(0x02 + (recId % 2));
            Buffer.BlockCopy(xBytes, 0, compressedPoint, 33 - xBytes.Length, xBytes.Length);

            ECPoint publicKey = ECPoint.DecodePoint(compressedPoint);

            if (!publicKey.Multiply(Secp256k1.N).IsInfinity) return null;

            var z = -hash.ToBigIntegerUnsigned(true) % Secp256k1.N;
            if (z < 0)
            {
                z += Secp256k1.N;
            }

            var rr = r.ModInverse(Secp256k1.N);
            var u1 = (z * rr) % Secp256k1.N;
            var u2 = (s * rr) % Secp256k1.N;

            var Q = Secp256k1.G.Multiply(u1).Add(publicKey.Multiply(u2));

            return Q;
        }
Exemplo n.º 3
0
        public static bool VerifySignature(byte[] message, BigInteger r, BigInteger s, ECPoint publicPoint)
        {
            var n = Secp256k1.N;

            if (r.Sign < 1 || s.Sign < 1 || r >= n || s >= n)
                return false;

            var z = CalculateZ(message);
            var w = s.ModInverse(n);

            var u1 = (z * w) % n;
            var u2 = (r * w) % n;

            var G = Secp256k1.G;
            var Q = publicPoint;

            var C = u1 * G + u2 * Q;

            if(C.IsInfinity) return false;

            var Cmodn = C.X % n;

            return Cmodn == r;
        }
Exemplo n.º 4
0
        public bool VerifySignature(ECPoint publicKey, byte[] hash, BigInteger r, BigInteger s)
        {
            if (r >= Secp256k1.N || r.IsZero || s >= Secp256k1.N || s.IsZero)
            {
                return false;
            }

            var z = hash.ToBigIntegerUnsigned(true);
            var w = s.ModInverse(Secp256k1.N);
            var u1 = (z * w) % Secp256k1.N;
            var u2 = (r * w) % Secp256k1.N;
            var pt = Secp256k1.G.Multiply(u1).Add(publicKey.Multiply(u2));
            var pmod = pt.X % Secp256k1.N;

            return pmod == r;
        }