Пример #1
0
        public static void DoVerify(object data)
        {
            _rwLock.AcquireWriterLock(Int32.MaxValue);
            var signature = data as Dictionary <string, byte[]>;
            var s         = signature?["signature"].ToArray();
            var m         = signature?["messageHash"].ToArray();
            var k         = new byte[64];

            Debug.Assert(secp256K1.Recover(k, s, m));
            Debug.Assert(secp256K1.Verify(s, m, k));
            _rwLock.ReleaseWriterLock();
        }
Пример #2
0
        static void TestToPublicKey()
        {
            using (var secp256k1 = new Secp256k1())
                using (var pedersen = new Pedersen())
                {
                    var blinding  = secp256k1.CreatePrivateKey();
                    var commitPos = pedersen.Commit(0, blinding);
                    var commitNeg = pedersen.Commit(0, blinding);

                    var blindSum = pedersen.BlindSum(new List <byte[]> {
                        blinding, blinding
                    }, new List <byte[]> {
                    });

                    var commitSum = pedersen.CommitSum(new List <byte[]> {
                        commitPos
                    }, new List <byte[]> {
                        commitNeg
                    });

                    var msg      = "Message for signing";
                    var msgBytes = Encoding.UTF8.GetBytes(msg);
                    var msgHash  = System.Security.Cryptography.SHA256.Create().ComputeHash(msgBytes);

                    var sig = secp256k1.Sign(msgHash, blinding);

                    var pubKey = pedersen.ToPublicKey(commitSum);

                    var verified1 = secp256k1.Verify(sig, msgHash, pubKey);
                    var pub       = secp256k1.CreatePublicKey(blinding);
                }
        }
Пример #3
0
        public void Sign_With_PubKey_From_Commitment()
        {
            using (var secp256k1 = new Secp256k1())
                using (var pedersen = new Pedersen())
                {
                    string ToHex(byte[] data)
                    {
                        return(BitConverter.ToString(data).Replace("-", string.Empty));
                    }

                    var blinding = secp256k1.CreatePrivateKey();
                    var commit   = pedersen.Commit(0, blinding);

                    var msg      = "Message for signing";
                    var msgBytes = Encoding.UTF8.GetBytes(msg);
                    var msgHash  = System.Security.Cryptography.SHA256.Create().ComputeHash(msgBytes);

                    var sig = secp256k1.Sign(msgHash, blinding);

                    var pubKey = pedersen.ToPublicKey(commit);

                    Assert.True(secp256k1.Verify(sig, msgHash, pubKey));

                    var actualPubKey = secp256k1.CreatePublicKey(blinding);

                    Assert.Equal(ToHex(pubKey), ToHex(actualPubKey));
                }
        }
Пример #4
0
        // mimic of VerifySignatureHashed of DefaultCrypto.cs but using different chain id
        public bool VerifySignatureHashed(byte[] messageHash, byte[] signature, byte[] publicKey, bool useNewChainId)
        {
            if (messageHash.Length != 32 || signature.Length != SignatureSize(useNewChainId))
            {
                return(false);
            }
            return(EcVerify.Benchmark(() =>
            {
                var pk = new byte[64];
                if (!Secp256K1.PublicKeyParse(pk, publicKey))
                {
                    return false;
                }

                var publicKeySerialized = new byte[33];
                if (!Secp256K1.PublicKeySerialize(publicKeySerialized, pk, Flags.SECP256K1_EC_COMPRESSED))
                {
                    throw new Exception("Cannot serialize parsed key: how did it happen?");
                }

                var parsedSig = new byte[65];
                var recId = (RestoreEncodedRecIdFromSignatureBuffer(signature) - 36) / 2 / ChainId(useNewChainId);
                if (recId < 0 || recId > 3)
                {
                    throw new Exception($"Invalid recId={recId}: : recId >= 0 && recId <= 3 ");
                }
                if (!Secp256K1.RecoverableSignatureParseCompact(parsedSig, signature.Take(64).ToArray(), recId))
                {
                    return false;
                }

                return Secp256K1.Verify(parsedSig.Take(64).ToArray(), messageHash, pk);
            }));
        }
Пример #5
0
        static void Main(string[] args)
        {
            // Create a secp256k1 context (ensure disposal to prevent unmanaged memory leaks).
            using (var secp256k1 = new Secp256k1())
            {
                // Generate a private key.
                var privateKey = new byte[32];
                var rnd        = System.Security.Cryptography.RandomNumberGenerator.Create();
                do
                {
                    rnd.GetBytes(privateKey);
                }while (!secp256k1.SecretKeyVerify(privateKey));


                // Create public key from private key.
                var publicKey = new byte[64];
                Debug.Assert(secp256k1.PublicKeyCreate(publicKey, privateKey));


                // Sign a message hash.
                var messageBytes = Encoding.UTF8.GetBytes("Hello world.");
                var messageHash  = System.Security.Cryptography.SHA256.Create().ComputeHash(messageBytes);
                var signature    = new byte[64];
                Debug.Assert(secp256k1.Sign(signature, messageHash, privateKey));

                // Serialize a DER signature from ECDSA signature
                byte[] signatureDer = new byte[Secp256k1.SERIALIZED_DER_SIGNATURE_MAX_SIZE];
                int    outL         = 0;
                Debug.Assert(secp256k1.SignatureSerializeDer(signatureDer, signature, out outL));
                Array.Resize(ref signatureDer, outL);

                // Verify message hash.
                Debug.Assert(secp256k1.Verify(signature, messageHash, publicKey));
            }
        }
 public static bool Secp256k1Verify(byte[] publicKey, byte[] signature, byte[] data)
 {
     lock (ecdsaLock)
     {
         return(secp256k1.Verify(publicKey, signature, data));
     }
 }
Пример #7
0
        public void should_verify_signature()
        {
            var hexReader = new HexReader();
            var secp256K1 = new Secp256k1();

            // https://www.blockchain.com/btc/tx/9f3eca62d13c2e7bf104a8539f5768674d551ecc140f3bb9bdaf3f07d6d3f6de
            // 15RpkAiu17FiYJmMiTJBvEwNQQdgEr1KvK
            // 1HvRorjpF5wyY7To5QhgJDzB3C9nEPcMo1
            var data = "9f3eca62d13c2e7bf104a8539f5768674d551ecc140f3bb9bdaf3f07d6d3f6de";

            var signature =
                "3046022100a7a1bfc118676e8dc1779a6c6dfaece6435ca20e8e2fcf4399300cc930c791e00221009a88d6a51b763fa748254fc487710998c9572bc2f01be06d1a7532c8e38a4c1901";

            var pubkey = "03a5cc256133f721c66201b54f18f08053b6fc62f3dfc84c43a3a2d1c87afbe30c";

            var bData      = hexReader.ToByteArray(data);
            var bSignature = hexReader.ToByteArray(signature);
            var bPubKey    = hexReader.ToByteArray(pubkey);

            var output = new Span <byte>(new byte[66]);

            var normalize = secp256K1.PublicKeyParse(output, bPubKey);

            var res = secp256K1.Verify(bSignature.AsSpan(), bData.AsSpan(), output);

            Console.WriteLine(res);
        }
Пример #8
0
        public void should_verify_pizza_transaction_ec()
        {
            var hexReader = new HexReader();
            var secp256K1 = new Secp256k1();


            // https://bitcoin.stackexchange.com/questions/32305/how-does-the-ecdsa-verification-algorithm-work-during-transaction
            var digest = "692678553d1b85ccf87d4d4443095f276cdf600f2bb7dd44f6effbd7458fd4c2";

            var signature =
                "30450221009908144ca6539e09512b9295c8a27050d478fbb96f8addbc3d075544dc41328702201aa528be2b907d316d2da068dd9eb1e23243d97e444d59290d2fddf25269ee0e01";

            var pubkey =
                "042e930f39ba62c6534ee98ed20ca98959d34aa9e057cda01cfd422c6bab3667b76426529382c23f42b9b08d7832d4fee1d6b437a8526e59667ce9c4e9dcebcabb";



            var bData      = hexReader.ToByteArray(digest);
            var bSignature = hexReader.ToByteArray(signature);
            var bPubKey    = hexReader.ToByteArray(pubkey);



            var res = secp256K1.Verify(bSignature.AsSpan(), bData.AsSpan(), bPubKey);
        }
Пример #9
0
        static void Main(string[] args)
        {
            // Create a secp256k1 context (ensure disposal to prevent unmanaged memory leaks).
            using (var secp256k1 = new Secp256k1())
            {
                // Generate a private key.
                var privateKey = new byte[32];
                var rnd        = System.Security.Cryptography.RandomNumberGenerator.Create();
                do
                {
                    rnd.GetBytes(privateKey);
                }while (!secp256k1.SecretKeyVerify(privateKey));


                // Create public key from private key.
                var publicKey = new byte[64];
                Debug.Assert(secp256k1.PublicKeyCreate(publicKey, privateKey));


                // Sign a message hash.
                var messageBytes = Encoding.UTF8.GetBytes("Hello world.");
                var messageHash  = System.Security.Cryptography.SHA256.Create().ComputeHash(messageBytes);
                var signature    = new byte[64];
                Debug.Assert(secp256k1.Sign(signature, messageHash, privateKey));


                // Verify message hash.
                Debug.Assert(secp256k1.Verify(signature, messageHash, publicKey));
            }
        }
Пример #10
0
        /// <summary>
        /// Verify that authenticity of the <paramref name="digest"/>'s <paramref name="signature"/> using <paramref name="publicKey"/>.
        /// </summary>
        /// <param name="digest">The digest.</param>
        /// <param name="signature">The signature.</param>
        /// <param name="publicKey">The public key.</param>
        /// <returns>true if the signature is authentic, false otherwise.</returns>
        public bool Verify(byte[] digest, byte[] signature, byte[] publicKey)
        {
            if (digest == null)
            {
                throw new ArgumentNullException(nameof(digest));
            }
            if (signature == null)
            {
                throw new ArgumentNullException(nameof(signature));
            }
            if (publicKey == null)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            return(_algorithm.Verify(publicKey, signature, digest));
        }
Пример #11
0
        public bool IsValid()
        {
            if (FromAddress.Equals("System"))
            {
                return(true);
            }
            if (Signature == null || Signature.Count().Equals(0))
            {
                throw new Exception("Transaction not signed..!");
            }
            var secp256k1 = new Secp256k1();

            return(secp256k1.Verify(Signature, CalculateHash(), FromAddress.ToBytes()));
        }
        public bool Verify()
        {
            Span <byte> sig = new byte[64];

            sig = signature;

            Span <byte> m = new byte[32];

            m = message;

            Span <byte> pk = new byte[64];

            pk = publicKey;

            return(secp256k1.Verify(sig, m, pk));
        }
Пример #13
0
        public bool Verify(
            HashDigest <T> messageHash,
            byte[] signature,
            PublicKey publicKey)
        {
            lock (_instanceLock)
            {
                var secp256K1Signature = new byte[64];
                _instance.SignatureParseDer(secp256K1Signature, signature);

                byte[] secp256K1PublicKey  = new byte[64];
                byte[] serializedPublicKey = publicKey.Format(false);
                _instance.PublicKeyParse(secp256K1PublicKey, serializedPublicKey);

                return(_instance.Verify(secp256K1Signature, messageHash.ToByteArray(), secp256K1PublicKey));
            }
        }
Пример #14
0
        public bool IsValid()
        {
            if (string.IsNullOrEmpty(Sender))
            {
                return(false);
            }
            if (string.IsNullOrEmpty(Signature))
            {
                throw new Exception("No Signature is found is this transaction");
            }

            using (var sep = new Secp256k1())
            {
                var signature = Convert.FromBase64String(Signature);
                var hash      = Convert.FromBase64String(ComputeHash());
                var pk        = Convert.FromBase64String(Sender);
                return(sep.Verify(signature, hash, pk));
            }
        }
Пример #15
0
        //Ignore this function as this is just to create Public Private key pairs for testing
        public static void GenerateKeys()
        {
            using (var secp256k1 = new Secp256k1())
            {
                // privatekey= "105c301c92f5d956ad577105e71aba4d29cf7af04cd47c648244dd8ad677381f"
                // publickey = "7a89dec4cc7e0964ed4c5e517f1cfee7e4f145e8500f55fe0317f97e71b7ba5219a4215b1885ac547da87bd0155d02c9bbe0501d0670a4f481df2b42f2130c02"

                // Generate a private key.
                var privateKey = new byte[32];
                var rnd        = System.Security.Cryptography.RandomNumberGenerator.Create();
                do
                {
                    rnd.GetBytes(privateKey);
                }while (!secp256k1.SecretKeyVerify(privateKey));

                var prk = ToHex(privateKey);

                // Create public key from private key.
                var publicKey = new byte[64];
                Debug.Assert(secp256k1.PublicKeyCreate(publicKey, privateKey));

                var pbk = ToHex(publicKey);


                // Sign a message hash.
                var messageBytes = Encoding.UTF8.GetBytes("Hello world.");
                var messageHash  = System.Security.Cryptography.SHA256.Create().ComputeHash(messageBytes);
                var signature    = new byte[64];
                Debug.Assert(secp256k1.Sign(signature, messageHash, privateKey));

                // Serialize a DER signature from ECDSA signature
                byte[] signatureDer = new byte[Secp256k1.SERIALIZED_DER_SIGNATURE_MAX_SIZE];
                int    outL         = 0;
                Debug.Assert(secp256k1.SignatureSerializeDer(signatureDer, signature, out outL));
                Array.Resize(ref signatureDer, outL);

                // Verify message hash.
                Debug.Assert(secp256k1.Verify(signature, messageHash, publicKey));
            }
        }
Пример #16
0
 public bool Verify(byte[] signature, byte[] messageHash)
 {
     return(Secp256k1.Verify(signature, messageHash, mBytes));
 }