Beispiel #1
0
 public ECPoint CreatePoint(string hex)
 {
     if (hex == "00")
     {
         return(null);
     }
     if (hex.StartsWith("02"))
     {
         var x = BigIntegerExt.ParseHexUnsigned(hex.Substring(2));
         return(CreatePoint(x, false));
     }
     else if (hex.StartsWith("03"))
     {
         var x = BigIntegerExt.ParseHexUnsigned(hex.Substring(2));
         return(CreatePoint(x, true));
     }
     else if (hex.StartsWith("04"))
     {
         var keySize = (int)KeySize8 * 2;
         var x       = BigIntegerExt.ParseHexUnsigned(hex.Substring(2, keySize));
         var y       = BigIntegerExt.ParseHexUnsigned(hex.Substring(keySize + 2));
         return(CreatePoint(x, y));
     }
     throw new System.FormatException();
 }
Beispiel #2
0
        public static ECPrivateKey Create(ECCurve curve)
        {
            var priv = BigIntegerExt.ModRandom(curve.Order);

            //todo: check not zero
            return(new ECPrivateKey(priv, curve));
        }
Beispiel #3
0
        public bool VerifySignature(BigInteger hash, ECSignature signature)
        {
            hash = Curve.TruncateHash(hash);
            var w  = signature.S.ModInverse(Curve.Order);
            var u1 = (hash * w) % Curve.Order;
            var u2 = (signature.R * w) % Curve.Order;
            var p  = Curve.G * u1 + Point * u2;

            return(BigIntegerExt.ModEqual(signature.R, p.X, Curve.Order));
        }
Beispiel #4
0
        public bool Has(ECPoint p)
        {
            if (p == ECPoint.Infinity)
            {
                return(true);
            }

            var left  = p.Y * p.Y;
            var right = p.X * p.X * p.X + A * p.X + B;

            return(BigIntegerExt.ModEqual(left, right, Modulus));
        }
Beispiel #5
0
        public ECSignature Sign(BigInteger message)
        {
            message = Curve.TruncateHash(message);
            ECSignature signature = null;

            do
            {
                var random = BigIntegerExt.ModRandom(Curve.Order);
                signature = SignTruncated(message, random);
            } while (signature == null);
            return(signature);
        }
Beispiel #6
0
        private ECSignature SignTruncated(BigInteger message, BigInteger random)
        {
            var p = Curve.G * random;
            var r = p.X % Curve.Order;

            if (r == 0)
            {
                return(null);
            }
            var s = ((message + r * D) * BigIntegerExt.ModInverse(random, Curve.Order)) % Curve.Order;

            if (s == 0)
            {
                return(null);
            }
            return(new ECSignature(r, s, Curve));
        }
Beispiel #7
0
 public ECPrivateKey CreatePrivateKey(byte[] data)
 {
     return(new ECPrivateKey(BigIntegerExt.FromBigEndianBytes(data), this));
 }
Beispiel #8
0
 public BigInteger TruncateHash(byte[] hash)
 {
     return(TruncateHash(hash, BigIntegerExt.FromBigEndianBytes(hash)));
 }
Beispiel #9
0
 public bool VerifySignature(byte[] hash, ECSignature signature)
 {
     return(VerifySignature(BigIntegerExt.FromBigEndianBytes(hash), signature));
 }
Beispiel #10
0
 public static ECPrivateKey ParseHex(string hex, ECCurve curve)
 {
     return(new ECPrivateKey(BigIntegerExt.ParseHexUnsigned(hex), curve));
 }
Beispiel #11
0
        public ECSignature Sign(byte[] hash, BigInteger random)
        {
            var num = BigIntegerExt.FromBigEndianBytes(hash);

            return(Sign(num, random));
        }