예제 #1
0
        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();
                }
            }
        }
예제 #2
0
        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();
                }
            }
        }
예제 #3
0
        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();
                }
            }
        }
예제 #4
0
        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();
                }
            }
        }
예제 #5
0
        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();
                }
            }
        }
예제 #6
0
        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();
                }
            }
        }
예제 #7
0
        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();
                    }
                }
            }
        }