public ECPoint GenerateKey(ECPoint publicKey, out byte[] key, BigInteger? k) { for (int i = 0; i < 100; i++) { if (k == null) { byte[] kBytes = new byte[33]; rngCsp.GetBytes(kBytes); kBytes[32] = 0; k = new BigInteger(kBytes); } if (k.Value.IsZero || k.Value >= Secp256k1.N) continue; var tag = Secp256k1.G.Multiply(k.Value); var keyPoint = publicKey.Multiply(k.Value); if (keyPoint.IsInfinity || tag.IsInfinity) continue; key = SHA256.DoubleHash(keyPoint.EncodePoint(false)); return tag; } throw new Exception("Unable to generate key"); }
public byte[] DecipherKey(BigInteger privateKey, ECPoint tag) { var keyPoint = tag.Multiply(privateKey); var key = SHA256.DoubleHash(keyPoint.EncodePoint(false)); return key; }
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); }
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; }