public Span <byte> CreatePublicKey(Span <byte> privateKey, DisposableContext?context = null) { Secp256k1Net.Secp256k1?dsa = context?.Context as Secp256k1Net.Secp256k1; if (dsa == null) { dsa = new Secp256k1Net.Secp256k1(); } try { Span <byte> publicKey = new byte[PublicKeyLength]; if (!dsa.PublicKeyCreate(publicKey, privateKey)) { throw new CryptoException("Can't create public key from private key"); } return(publicKey); } finally { if (context == null) { dsa?.Dispose(); } } }
public Span <byte> GetSECUncompressedPublicKey(Span <byte> uncompressed, DisposableContext?context = null) { Secp256k1Net.Secp256k1?dsa = context?.Context as Secp256k1Net.Secp256k1; if (dsa == null) { dsa = new Secp256k1Net.Secp256k1(); } try { Span <byte> uncompressedBtc = new byte[PublicKeyUncompressedLength]; if (!dsa.PublicKeySerialize(uncompressedBtc, uncompressed, Secp256k1Net.Flags.SECP256K1_EC_UNCOMPRESSED)) { throw new CryptoException("Can't create public uncompressed bitcoin key from uncompressed key"); } return(uncompressedBtc); } finally { if (context == null) { dsa?.Dispose(); } } }
public Signature SignHash(Hash256 hash, Key key, DisposableContext?context = null) { Secp256k1Net.Secp256k1?dsa = context?.Context as Secp256k1Net.Secp256k1; if (dsa == null) { dsa = new Secp256k1Net.Secp256k1(); } try { Span <byte> signature = new byte[SignatureLength]; if (!dsa.Sign(signature, hash.Bytes, key.PrivateKey)) { throw new CryptoException("Secp256k1 sign failure"); } return(new Signature { Bytes = signature }); } finally { if (context == null) { dsa?.Dispose(); } } }
public Key CreateKey(DisposableContext?context = null) { Secp256k1Net.Secp256k1?dsa = context?.Context as Secp256k1Net.Secp256k1; if (dsa == null) { dsa = new Secp256k1Net.Secp256k1(); } try { Key key = this.CreatePrivateKey(); key.PublicKey = new byte[PublicKeyLength]; if (!dsa.PublicKeyCreate(key.PublicKey, key.PrivateKey)) { throw new CryptoException("Secp256k1 can't create public key from private key"); } return(key); } finally { if (context == null) { dsa?.Dispose(); } } }
public bool VerifyHash(Hash256 hash, Signature signature, Key key, DisposableContext?context = null) { Secp256k1Net.Secp256k1?dsa = context?.Context as Secp256k1Net.Secp256k1; if (dsa == null) { dsa = new Secp256k1Net.Secp256k1(); } try { return(dsa.Verify(signature.Bytes, hash.Bytes, key.PublicKey)); } finally { if (context == null) { dsa?.Dispose(); } } }
public bool IsPrivateKeyValid(Key key, DisposableContext?context = null) { Secp256k1Net.Secp256k1?dsa = context?.Context as Secp256k1Net.Secp256k1; if (dsa == null) { dsa = new Secp256k1Net.Secp256k1(); } try { return(dsa.SecretKeyVerify(key.PrivateKey)); } finally { if (context == null) { dsa?.Dispose(); } } }
public void TestSecp256k1UseSharedContextWithSameKey() { using var secp256k1 = new Secp256k1Net.Secp256k1(); using (var context = new Secp256k1DotNetContext(secp256k1)) { IECDsa signer = new Secp256k1DotNet(); var key = signer.CreateKey(); for (var loop = 0; loop < 1000; loop++) { using var rnd = RandomNumberGenerator.Create(); var data = new byte[32]; rnd.GetBytes(data); var hash = data.Hash(); var signature = signer.SignHash(hash, key, context); if (!signer.VerifyHash(hash, signature, key, context)) { throw new Exception(); } } } }