Exemplo n.º 1
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);
            }));
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
 public static byte[] Ecdh(byte[] privateKey, byte[] publicKey)
 {
     try
     {
         Lock.AcquireWriterLock(Timeout.Infinite);
         var usablePublicKey = new byte[Secp256k1.SERIALIZED_UNCOMPRESSED_PUBKEY_LENGTH];
         Secp256K1.PublicKeyParse(usablePublicKey, publicKey);
         var ecdhKey = new byte[Secp256k1.SERIALIZED_COMPRESSED_PUBKEY_LENGTH];
         Secp256K1.Ecdh(ecdhKey, usablePublicKey, privateKey);
         return(ecdhKey);
     }
     finally
     {
         Lock.ReleaseWriterLock();
     }
 }
Exemplo n.º 4
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));
            }
        }
Exemplo n.º 5
0
 public static byte[] Ecdh(byte[] privateKey, byte[] publicKey)
 {
     try
     {
         Lock.AcquireWriterLock(Timeout.Infinite);
         var usablePublicKey = new byte[Secp256k1.SERIALIZED_UNCOMPRESSED_PUBKEY_LENGTH];
         if (!Secp256K1.PublicKeyParse(usablePublicKey, publicKey))
         {
             throw new PublicKeyOperationException("Parse public key failed.");
         }
         var ecdhKey = new byte[Secp256k1.SERIALIZED_COMPRESSED_PUBKEY_LENGTH];
         if (!Secp256K1.Ecdh(ecdhKey, usablePublicKey, privateKey))
         {
             throw new EcdhOperationException("Compute EC Diffie- secret failed.");
         }
         return(ecdhKey);
     }
     finally
     {
         Lock.ReleaseWriterLock();
     }
 }
Exemplo n.º 6
0
 public static PublicKey Decode(byte[] encode)
 {
     return(new PublicKey(Secp256k1.PublicKeyParse(encode)));
 }