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(); }
public static ECPrivateKey Create(ECCurve curve) { var priv = BigIntegerExt.ModRandom(curve.Order); //todo: check not zero return(new ECPrivateKey(priv, curve)); }
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)); }
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)); }
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); }
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)); }
public ECPrivateKey CreatePrivateKey(byte[] data) { return(new ECPrivateKey(BigIntegerExt.FromBigEndianBytes(data), this)); }
public BigInteger TruncateHash(byte[] hash) { return(TruncateHash(hash, BigIntegerExt.FromBigEndianBytes(hash))); }
public bool VerifySignature(byte[] hash, ECSignature signature) { return(VerifySignature(BigIntegerExt.FromBigEndianBytes(hash), signature)); }
public static ECPrivateKey ParseHex(string hex, ECCurve curve) { return(new ECPrivateKey(BigIntegerExt.ParseHexUnsigned(hex), curve)); }
public ECSignature Sign(byte[] hash, BigInteger random) { var num = BigIntegerExt.FromBigEndianBytes(hash); return(Sign(num, random)); }