From 14179dd21d961e36fac6d5c494fe4857a19732e9 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Wed, 1 Jan 2025 22:27:01 +0800 Subject: [PATCH 1/4] rename --- src/Neo/Cryptography/Crypto.cs | 80 ++++++++++++++----- src/Neo/Cryptography/HashAlgorithmType.cs | 26 ++++++ src/Neo/Cryptography/Hasher.cs | 3 + src/Neo/SmartContract/Native/CryptoLib.cs | 14 ++-- .../SmartContract/Native/UT_CryptoLib.cs | 16 ++-- 5 files changed, 106 insertions(+), 33 deletions(-) create mode 100644 src/Neo/Cryptography/HashAlgorithmType.cs diff --git a/src/Neo/Cryptography/Crypto.cs b/src/Neo/Cryptography/Crypto.cs index a2e89dc787..be197dcdb5 100644 --- a/src/Neo/Cryptography/Crypto.cs +++ b/src/Neo/Cryptography/Crypto.cs @@ -60,17 +60,31 @@ public static byte[] Hash256(ReadOnlySpan message) return message.Sha256().Sha256(); } + /// + /// Signs the specified message using the ECDSA algorithm and specified hash algorithm. + /// + /// The message to be signed. + /// The private key to be used. + /// The curve of the signature. + /// The hash algorithm to hash the message. + /// The ECDSA signature for the specified message. + [Obsolete("Use Sign(byte[], byte[], ECC.ECCurve, HashAlgorithmType) instead")] + public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve, Hasher hasher) + { + return Sign(message, priKey, ecCurve, (HashAlgorithmType)hasher); + } + /// /// Signs the specified message using the ECDSA algorithm and specified hash algorithm. /// /// The message to be signed. /// The private key to be used. /// The curve of the signature, default is . - /// The hash algorithm to hash the message, default is SHA256. + /// The hash algorithm to hash the message, default is SHA256. /// The ECDSA signature for the specified message. - public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = null, Hasher hasher = Hasher.SHA256) + public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = null, HashAlgorithmType hashAlgorithm = HashAlgorithmType.SHA256) { - if (hasher == Hasher.Keccak256 || (IsOSX && ecCurve == ECC.ECCurve.Secp256k1)) + if (hashAlgorithm == HashAlgorithmType.Keccak256 || (IsOSX && ecCurve == ECC.ECCurve.Secp256k1)) { var domain = ecCurve == null || ecCurve == ECC.ECCurve.Secp256r1 ? secp256r1DomainParams : @@ -81,9 +95,9 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n var priKeyParameters = new ECPrivateKeyParameters(privateKey, domain); signer.Init(true, priKeyParameters); var messageHash = - hasher == Hasher.SHA256 ? message.Sha256() : - hasher == Hasher.Keccak256 ? message.Keccak256() : - throw new NotSupportedException(nameof(hasher)); + hashAlgorithm == HashAlgorithmType.SHA256 ? message.Sha256() : + hashAlgorithm == HashAlgorithmType.Keccak256 ? message.Keccak256() : + throw new NotSupportedException(nameof(hashAlgorithm)); var signature = signer.GenerateSignature(messageHash); var signatureBytes = new byte[64]; @@ -107,8 +121,8 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n D = priKey, }); var hashAlg = - hasher == Hasher.SHA256 ? HashAlgorithmName.SHA256 : - throw new NotSupportedException(nameof(hasher)); + hashAlgorithm == HashAlgorithmType.SHA256 ? HashAlgorithmName.SHA256 : + throw new NotSupportedException($"The hash algorithm {nameof(hashAlgorithm)} is not supported."); return ecdsa.SignData(message, hashAlg); } @@ -118,13 +132,28 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n /// The signed message. /// The signature to be verified. /// The public key to be used. - /// The hash algorithm to be used to hash the message, the default is SHA256. + /// The hash algorithm to be used to hash the message. /// if the signature is valid; otherwise, . - public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ECC.ECPoint pubkey, Hasher hasher = Hasher.SHA256) + [Obsolete("Use VerifySignature(ReadOnlySpan, ReadOnlySpan, ECC.ECPoint, HashAlgorithmType) instead")] + public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ECC.ECPoint pubkey, Hasher hasher) + { + return VerifySignature(message, signature, pubkey, (HashAlgorithmType)hasher); + } + + + /// + /// Verifies that a digital signature is appropriate for the provided key, message and hash algorithm. + /// + /// The signed message. + /// The signature to be verified. + /// The public key to be used. + /// The hash algorithm to be used to hash the message, the default is SHA256. + /// if the signature is valid; otherwise, . + public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ECC.ECPoint pubkey, HashAlgorithmType hashAlgorithm = HashAlgorithmType.SHA256) { if (signature.Length != 64) return false; - if (hasher == Hasher.Keccak256 || (IsOSX && pubkey.Curve == ECC.ECCurve.Secp256k1)) + if (hashAlgorithm == HashAlgorithmType.Keccak256 || (IsOSX && pubkey.Curve == ECC.ECCurve.Secp256k1)) { var domain = pubkey.Curve == ECC.ECCurve.Secp256r1 ? secp256r1DomainParams : @@ -146,17 +175,17 @@ public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpanThe curve to be used by the ECDSA algorithm. /// The hash algorithm to be used hash the message, the default is SHA256. /// if the signature is valid; otherwise, . - public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ReadOnlySpan pubkey, ECC.ECCurve curve, Hasher hasher = Hasher.SHA256) + [Obsolete("Use VerifySignature(ReadOnlySpan, ReadOnlySpan, ReadOnlySpan, ECC.ECCurve, HashAlgorithmType) instead")] + public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ReadOnlySpan pubkey, ECC.ECCurve curve, Hasher hasher) + { + return VerifySignature(message, signature, ECC.ECPoint.DecodePoint(pubkey, curve), (HashAlgorithmType)hasher); + } + + /// + /// Verifies that a digital signature is appropriate for the provided key, curve, message and hasher. + /// + /// The signed message. + /// The signature to be verified. + /// The public key to be used. + /// The curve to be used by the ECDSA algorithm. + /// The hash algorithm to be used hash the message, the default is SHA256. + /// if the signature is valid; otherwise, . + public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ReadOnlySpan pubkey, ECC.ECCurve curve, HashAlgorithmType hashAlgorithm = HashAlgorithmType.SHA256) { - return VerifySignature(message, signature, ECC.ECPoint.DecodePoint(pubkey, curve), hasher); + return VerifySignature(message, signature, ECC.ECPoint.DecodePoint(pubkey, curve), hashAlgorithm); } } } diff --git a/src/Neo/Cryptography/HashAlgorithmType.cs b/src/Neo/Cryptography/HashAlgorithmType.cs new file mode 100644 index 0000000000..3de153b4f8 --- /dev/null +++ b/src/Neo/Cryptography/HashAlgorithmType.cs @@ -0,0 +1,26 @@ +// Copyright (C) 2015-2024 The Neo Project. +// +// HashAlgorithmType.cs file belongs to the neo project and is free +// software distributed under the MIT software license, see the +// accompanying file LICENSE in the main directory of the +// repository or http://www.opensource.org/licenses/mit-license.php +// for more details. +// +// Redistribution and use in source and binary forms with or without +// modifications are permitted. + +namespace Neo.Cryptography +{ + public enum HashAlgorithmType : byte + { + /// + /// The SHA256 hash algorithm. + /// + SHA256 = 0x00, + + /// + /// The Keccak256 hash algorithm. + /// + Keccak256 = 0x01, + } +} diff --git a/src/Neo/Cryptography/Hasher.cs b/src/Neo/Cryptography/Hasher.cs index 21c5fef6a8..9a7cc6b8f3 100644 --- a/src/Neo/Cryptography/Hasher.cs +++ b/src/Neo/Cryptography/Hasher.cs @@ -9,11 +9,14 @@ // Redistribution and use in source and binary forms with or without // modifications are permitted. +using System; + namespace Neo.Cryptography { /// /// Represents hash function identifiers supported by ECDSA message signature and verification. /// + [Obsolete("Use HashAlgorithmType instead")] public enum Hasher : byte { /// diff --git a/src/Neo/SmartContract/Native/CryptoLib.cs b/src/Neo/SmartContract/Native/CryptoLib.cs index 9027298752..77bb89f290 100644 --- a/src/Neo/SmartContract/Native/CryptoLib.cs +++ b/src/Neo/SmartContract/Native/CryptoLib.cs @@ -21,12 +21,12 @@ namespace Neo.SmartContract.Native /// public sealed partial class CryptoLib : NativeContract { - private static readonly Dictionary s_curves = new() + private static readonly Dictionary s_curves = new() { - [NamedCurveHash.secp256k1SHA256] = (ECCurve.Secp256k1, Hasher.SHA256), - [NamedCurveHash.secp256r1SHA256] = (ECCurve.Secp256r1, Hasher.SHA256), - [NamedCurveHash.secp256k1Keccak256] = (ECCurve.Secp256k1, Hasher.Keccak256), - [NamedCurveHash.secp256r1Keccak256] = (ECCurve.Secp256r1, Hasher.Keccak256), + [NamedCurveHash.secp256k1SHA256] = (ECCurve.Secp256k1, HashAlgorithmType.SHA256), + [NamedCurveHash.secp256r1SHA256] = (ECCurve.Secp256r1, HashAlgorithmType.SHA256), + [NamedCurveHash.secp256k1Keccak256] = (ECCurve.Secp256k1, HashAlgorithmType.Keccak256), + [NamedCurveHash.secp256r1Keccak256] = (ECCurve.Secp256r1, HashAlgorithmType.Keccak256), }; internal CryptoLib() : base() { } @@ -91,7 +91,7 @@ public static bool VerifyWithECDsa(byte[] message, byte[] pubkey, byte[] signatu try { var ch = s_curves[curveHash]; - return Crypto.VerifySignature(message, signature, pubkey, ch.Curve, ch.Hasher); + return Crypto.VerifySignature(message, signature, pubkey, ch.Curve, ch.HashAlgorithm); } catch (ArgumentException) { @@ -100,7 +100,7 @@ public static bool VerifyWithECDsa(byte[] message, byte[] pubkey, byte[] signatu } // This is for solving the hardfork issue in https://github.com/neo-project/neo/pull/3209 - [ContractMethod(true, Hardfork.HF_Cockatrice, CpuFee = 1 << 15, Name = "verifyWithECDsa")] + [ContractMethod(true, Hardfork.HF_Cockatrice, CpuFee = 1 << 15, Name = nameof(VerifyWithECDsa))] public static bool VerifyWithECDsaV0(byte[] message, byte[] pubkey, byte[] signature, NamedCurveHash curve) { if (curve != NamedCurveHash.secp256k1SHA256 && curve != NamedCurveHash.secp256r1SHA256) diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs index daf1922644..e092d8aba0 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs @@ -494,7 +494,7 @@ public void TestVerifyWithECDsa_CustomTxWitness_SingleSig() Version = 0, Witnesses = [] }; - var tx_signature = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), privkey, ECCurve.Secp256k1, Hasher.Keccak256); + var tx_signature = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), privkey, ECCurve.Secp256k1, HashAlgorithmType.Keccak256); // inv is a builder of witness invocation script corresponding to the public key. using ScriptBuilder inv = new(); @@ -737,7 +737,7 @@ public void TestVerifyWithECDsa_CustomTxWitness_MultiSig() { if (i == 1) // Skip one key since we need only 3 signatures. continue; - var sig = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), keys[i].Item1, ECCurve.Secp256k1, Hasher.Keccak256); + var sig = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), keys[i].Item1, ECCurve.Secp256k1, HashAlgorithmType.Keccak256); inv.EmitPush(sig); } @@ -868,23 +868,23 @@ public void TestVerifyWithECDsa() byte[] message = System.Text.Encoding.Default.GetBytes("HelloWorld"); // secp256r1 + SHA256 - byte[] signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, Hasher.SHA256); + byte[] signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, HashAlgorithmType.SHA256); Crypto.VerifySignature(message, signature, pubR1).Should().BeTrue(); // SHA256 hash is used by default. CallVerifyWithECDsa(message, pubR1, signature, NamedCurveHash.secp256r1SHA256).Should().Be(true); // secp256r1 + Keccak256 - signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, Hasher.Keccak256); - Crypto.VerifySignature(message, signature, pubR1, Hasher.Keccak256).Should().BeTrue(); + signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, HashAlgorithmType.Keccak256); + Crypto.VerifySignature(message, signature, pubR1, HashAlgorithmType.Keccak256).Should().BeTrue(); CallVerifyWithECDsa(message, pubR1, signature, NamedCurveHash.secp256r1Keccak256).Should().Be(true); // secp256k1 + SHA256 - signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, Hasher.SHA256); + signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, HashAlgorithmType.SHA256); Crypto.VerifySignature(message, signature, pubK1).Should().BeTrue(); // SHA256 hash is used by default. CallVerifyWithECDsa(message, pubK1, signature, NamedCurveHash.secp256k1SHA256).Should().Be(true); // secp256k1 + Keccak256 - signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, Hasher.Keccak256); - Crypto.VerifySignature(message, signature, pubK1, Hasher.Keccak256).Should().BeTrue(); + signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, HashAlgorithmType.Keccak256); + Crypto.VerifySignature(message, signature, pubK1, HashAlgorithmType.Keccak256).Should().BeTrue(); CallVerifyWithECDsa(message, pubK1, signature, NamedCurveHash.secp256k1Keccak256).Should().Be(true); } From 621b589f1051e73e7609a6e3433be0e964f9f369 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Wed, 1 Jan 2025 22:31:38 +0800 Subject: [PATCH 2/4] revert unrelated change --- src/Neo/SmartContract/Native/CryptoLib.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Neo/SmartContract/Native/CryptoLib.cs b/src/Neo/SmartContract/Native/CryptoLib.cs index 77bb89f290..6da1a1fffe 100644 --- a/src/Neo/SmartContract/Native/CryptoLib.cs +++ b/src/Neo/SmartContract/Native/CryptoLib.cs @@ -100,7 +100,7 @@ public static bool VerifyWithECDsa(byte[] message, byte[] pubkey, byte[] signatu } // This is for solving the hardfork issue in https://github.com/neo-project/neo/pull/3209 - [ContractMethod(true, Hardfork.HF_Cockatrice, CpuFee = 1 << 15, Name = nameof(VerifyWithECDsa))] + [ContractMethod(true, Hardfork.HF_Cockatrice, CpuFee = 1 << 15, Name = "verifyWithECDsa")] public static bool VerifyWithECDsaV0(byte[] message, byte[] pubkey, byte[] signature, NamedCurveHash curve) { if (curve != NamedCurveHash.secp256k1SHA256 && curve != NamedCurveHash.secp256r1SHA256) From 3b08facc81b2d9b53e30442b6cc870c7a8a3d030 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Thu, 2 Jan 2025 13:15:47 +0800 Subject: [PATCH 3/4] HashAlgorithm instead --- src/Neo/Cryptography/Crypto.cs | 34 +++++++++---------- ...{HashAlgorithmType.cs => HashAlgorithm.cs} | 4 +-- src/Neo/Cryptography/Hasher.cs | 2 +- src/Neo/Cryptography/Murmur128.cs | 2 +- src/Neo/Cryptography/Murmur32.cs | 2 +- src/Neo/Cryptography/RIPEMD160Managed.cs | 2 +- src/Neo/SmartContract/Native/CryptoLib.cs | 10 +++--- .../SmartContract/Native/UT_CryptoLib.cs | 16 ++++----- 8 files changed, 36 insertions(+), 36 deletions(-) rename src/Neo/Cryptography/{HashAlgorithmType.cs => HashAlgorithm.cs} (85%) diff --git a/src/Neo/Cryptography/Crypto.cs b/src/Neo/Cryptography/Crypto.cs index be197dcdb5..11b11bb94d 100644 --- a/src/Neo/Cryptography/Crypto.cs +++ b/src/Neo/Cryptography/Crypto.cs @@ -68,10 +68,10 @@ public static byte[] Hash256(ReadOnlySpan message) /// The curve of the signature. /// The hash algorithm to hash the message. /// The ECDSA signature for the specified message. - [Obsolete("Use Sign(byte[], byte[], ECC.ECCurve, HashAlgorithmType) instead")] + [Obsolete("Use Sign(byte[], byte[], ECC.ECCurve, HashAlgorithm) instead")] public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve, Hasher hasher) { - return Sign(message, priKey, ecCurve, (HashAlgorithmType)hasher); + return Sign(message, priKey, ecCurve, (HashAlgorithm)hasher); } /// @@ -82,9 +82,9 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve, Ha /// The curve of the signature, default is . /// The hash algorithm to hash the message, default is SHA256. /// The ECDSA signature for the specified message. - public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = null, HashAlgorithmType hashAlgorithm = HashAlgorithmType.SHA256) + public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = null, HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256) { - if (hashAlgorithm == HashAlgorithmType.Keccak256 || (IsOSX && ecCurve == ECC.ECCurve.Secp256k1)) + if (hashAlgorithm == HashAlgorithm.Keccak256 || (IsOSX && ecCurve == ECC.ECCurve.Secp256k1)) { var domain = ecCurve == null || ecCurve == ECC.ECCurve.Secp256r1 ? secp256r1DomainParams : @@ -95,8 +95,8 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n var priKeyParameters = new ECPrivateKeyParameters(privateKey, domain); signer.Init(true, priKeyParameters); var messageHash = - hashAlgorithm == HashAlgorithmType.SHA256 ? message.Sha256() : - hashAlgorithm == HashAlgorithmType.Keccak256 ? message.Keccak256() : + hashAlgorithm == HashAlgorithm.SHA256 ? message.Sha256() : + hashAlgorithm == HashAlgorithm.Keccak256 ? message.Keccak256() : throw new NotSupportedException(nameof(hashAlgorithm)); var signature = signer.GenerateSignature(messageHash); @@ -121,7 +121,7 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n D = priKey, }); var hashAlg = - hashAlgorithm == HashAlgorithmType.SHA256 ? HashAlgorithmName.SHA256 : + hashAlgorithm == HashAlgorithm.SHA256 ? HashAlgorithmName.SHA256 : throw new NotSupportedException($"The hash algorithm {nameof(hashAlgorithm)} is not supported."); return ecdsa.SignData(message, hashAlg); } @@ -134,10 +134,10 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n /// The public key to be used. /// The hash algorithm to be used to hash the message. /// if the signature is valid; otherwise, . - [Obsolete("Use VerifySignature(ReadOnlySpan, ReadOnlySpan, ECC.ECPoint, HashAlgorithmType) instead")] + [Obsolete("Use VerifySignature(ReadOnlySpan, ReadOnlySpan, ECC.ECPoint, HashAlgorithm) instead")] public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ECC.ECPoint pubkey, Hasher hasher) { - return VerifySignature(message, signature, pubkey, (HashAlgorithmType)hasher); + return VerifySignature(message, signature, pubkey, (HashAlgorithm)hasher); } @@ -149,11 +149,11 @@ public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpanThe public key to be used. /// The hash algorithm to be used to hash the message, the default is SHA256. /// if the signature is valid; otherwise, . - public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ECC.ECPoint pubkey, HashAlgorithmType hashAlgorithm = HashAlgorithmType.SHA256) + public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ECC.ECPoint pubkey, HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256) { if (signature.Length != 64) return false; - if (hashAlgorithm == HashAlgorithmType.Keccak256 || (IsOSX && pubkey.Curve == ECC.ECCurve.Secp256k1)) + if (hashAlgorithm == HashAlgorithm.Keccak256 || (IsOSX && pubkey.Curve == ECC.ECCurve.Secp256k1)) { var domain = pubkey.Curve == ECC.ECCurve.Secp256r1 ? secp256r1DomainParams : @@ -175,8 +175,8 @@ public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan message, ReadOnlySpanThe curve to be used by the ECDSA algorithm. /// The hash algorithm to be used hash the message, the default is SHA256. /// if the signature is valid; otherwise, . - [Obsolete("Use VerifySignature(ReadOnlySpan, ReadOnlySpan, ReadOnlySpan, ECC.ECCurve, HashAlgorithmType) instead")] + [Obsolete("Use VerifySignature(ReadOnlySpan, ReadOnlySpan, ReadOnlySpan, ECC.ECCurve, HashAlgorithm) instead")] public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ReadOnlySpan pubkey, ECC.ECCurve curve, Hasher hasher) { - return VerifySignature(message, signature, ECC.ECPoint.DecodePoint(pubkey, curve), (HashAlgorithmType)hasher); + return VerifySignature(message, signature, ECC.ECPoint.DecodePoint(pubkey, curve), (HashAlgorithm)hasher); } /// @@ -243,7 +243,7 @@ public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpanThe curve to be used by the ECDSA algorithm. /// The hash algorithm to be used hash the message, the default is SHA256. /// if the signature is valid; otherwise, . - public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ReadOnlySpan pubkey, ECC.ECCurve curve, HashAlgorithmType hashAlgorithm = HashAlgorithmType.SHA256) + public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan signature, ReadOnlySpan pubkey, ECC.ECCurve curve, HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256) { return VerifySignature(message, signature, ECC.ECPoint.DecodePoint(pubkey, curve), hashAlgorithm); } diff --git a/src/Neo/Cryptography/HashAlgorithmType.cs b/src/Neo/Cryptography/HashAlgorithm.cs similarity index 85% rename from src/Neo/Cryptography/HashAlgorithmType.cs rename to src/Neo/Cryptography/HashAlgorithm.cs index 3de153b4f8..dfd8b2280d 100644 --- a/src/Neo/Cryptography/HashAlgorithmType.cs +++ b/src/Neo/Cryptography/HashAlgorithm.cs @@ -1,6 +1,6 @@ // Copyright (C) 2015-2024 The Neo Project. // -// HashAlgorithmType.cs file belongs to the neo project and is free +// HashAlgorithm.cs file belongs to the neo project and is free // software distributed under the MIT software license, see the // accompanying file LICENSE in the main directory of the // repository or http://www.opensource.org/licenses/mit-license.php @@ -11,7 +11,7 @@ namespace Neo.Cryptography { - public enum HashAlgorithmType : byte + public enum HashAlgorithm : byte { /// /// The SHA256 hash algorithm. diff --git a/src/Neo/Cryptography/Hasher.cs b/src/Neo/Cryptography/Hasher.cs index 9a7cc6b8f3..9b085aa587 100644 --- a/src/Neo/Cryptography/Hasher.cs +++ b/src/Neo/Cryptography/Hasher.cs @@ -16,7 +16,7 @@ namespace Neo.Cryptography /// /// Represents hash function identifiers supported by ECDSA message signature and verification. /// - [Obsolete("Use HashAlgorithmType instead")] + [Obsolete("Use HashAlgorithm instead")] public enum Hasher : byte { /// diff --git a/src/Neo/Cryptography/Murmur128.cs b/src/Neo/Cryptography/Murmur128.cs index 15eca775cd..cd8e54f664 100644 --- a/src/Neo/Cryptography/Murmur128.cs +++ b/src/Neo/Cryptography/Murmur128.cs @@ -19,7 +19,7 @@ namespace Neo.Cryptography /// /// Computes the 128 bits murmur hash for the input data. /// - public sealed class Murmur128 : HashAlgorithm + public sealed class Murmur128 : System.Security.Cryptography.HashAlgorithm { private const ulong c1 = 0x87c37b91114253d5; private const ulong c2 = 0x4cf5ad432745937f; diff --git a/src/Neo/Cryptography/Murmur32.cs b/src/Neo/Cryptography/Murmur32.cs index 0f2c1b828d..cccff68d76 100644 --- a/src/Neo/Cryptography/Murmur32.cs +++ b/src/Neo/Cryptography/Murmur32.cs @@ -18,7 +18,7 @@ namespace Neo.Cryptography /// /// Computes the murmur hash for the input data. /// - public sealed class Murmur32 : HashAlgorithm + public sealed class Murmur32 : System.Security.Cryptography.HashAlgorithm { private const uint c1 = 0xcc9e2d51; private const uint c2 = 0x1b873593; diff --git a/src/Neo/Cryptography/RIPEMD160Managed.cs b/src/Neo/Cryptography/RIPEMD160Managed.cs index 932c1cfea6..19a406d20f 100644 --- a/src/Neo/Cryptography/RIPEMD160Managed.cs +++ b/src/Neo/Cryptography/RIPEMD160Managed.cs @@ -20,7 +20,7 @@ namespace Neo.Cryptography /// Computes the ripemd160 hash for the input data. /// [ComVisible(true)] - public class RIPEMD160Managed : HashAlgorithm + public class RIPEMD160Managed : System.Security.Cryptography.HashAlgorithm { private readonly byte[] _buffer; private long _count; // Number of bytes in the hashed message diff --git a/src/Neo/SmartContract/Native/CryptoLib.cs b/src/Neo/SmartContract/Native/CryptoLib.cs index 6da1a1fffe..23c76aa158 100644 --- a/src/Neo/SmartContract/Native/CryptoLib.cs +++ b/src/Neo/SmartContract/Native/CryptoLib.cs @@ -21,12 +21,12 @@ namespace Neo.SmartContract.Native /// public sealed partial class CryptoLib : NativeContract { - private static readonly Dictionary s_curves = new() + private static readonly Dictionary s_curves = new() { - [NamedCurveHash.secp256k1SHA256] = (ECCurve.Secp256k1, HashAlgorithmType.SHA256), - [NamedCurveHash.secp256r1SHA256] = (ECCurve.Secp256r1, HashAlgorithmType.SHA256), - [NamedCurveHash.secp256k1Keccak256] = (ECCurve.Secp256k1, HashAlgorithmType.Keccak256), - [NamedCurveHash.secp256r1Keccak256] = (ECCurve.Secp256r1, HashAlgorithmType.Keccak256), + [NamedCurveHash.secp256k1SHA256] = (ECCurve.Secp256k1, HashAlgorithm.SHA256), + [NamedCurveHash.secp256r1SHA256] = (ECCurve.Secp256r1, HashAlgorithm.SHA256), + [NamedCurveHash.secp256k1Keccak256] = (ECCurve.Secp256k1, HashAlgorithm.Keccak256), + [NamedCurveHash.secp256r1Keccak256] = (ECCurve.Secp256r1, HashAlgorithm.Keccak256), }; internal CryptoLib() : base() { } diff --git a/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs b/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs index e092d8aba0..457bd56ca0 100644 --- a/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs +++ b/tests/Neo.UnitTests/SmartContract/Native/UT_CryptoLib.cs @@ -494,7 +494,7 @@ public void TestVerifyWithECDsa_CustomTxWitness_SingleSig() Version = 0, Witnesses = [] }; - var tx_signature = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), privkey, ECCurve.Secp256k1, HashAlgorithmType.Keccak256); + var tx_signature = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), privkey, ECCurve.Secp256k1, HashAlgorithm.Keccak256); // inv is a builder of witness invocation script corresponding to the public key. using ScriptBuilder inv = new(); @@ -737,7 +737,7 @@ public void TestVerifyWithECDsa_CustomTxWitness_MultiSig() { if (i == 1) // Skip one key since we need only 3 signatures. continue; - var sig = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), keys[i].Item1, ECCurve.Secp256k1, HashAlgorithmType.Keccak256); + var sig = Crypto.Sign(tx.GetSignData(TestBlockchain.TheNeoSystem.Settings.Network), keys[i].Item1, ECCurve.Secp256k1, HashAlgorithm.Keccak256); inv.EmitPush(sig); } @@ -868,23 +868,23 @@ public void TestVerifyWithECDsa() byte[] message = System.Text.Encoding.Default.GetBytes("HelloWorld"); // secp256r1 + SHA256 - byte[] signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, HashAlgorithmType.SHA256); + byte[] signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, HashAlgorithm.SHA256); Crypto.VerifySignature(message, signature, pubR1).Should().BeTrue(); // SHA256 hash is used by default. CallVerifyWithECDsa(message, pubR1, signature, NamedCurveHash.secp256r1SHA256).Should().Be(true); // secp256r1 + Keccak256 - signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, HashAlgorithmType.Keccak256); - Crypto.VerifySignature(message, signature, pubR1, HashAlgorithmType.Keccak256).Should().BeTrue(); + signature = Crypto.Sign(message, privR1, ECCurve.Secp256r1, HashAlgorithm.Keccak256); + Crypto.VerifySignature(message, signature, pubR1, HashAlgorithm.Keccak256).Should().BeTrue(); CallVerifyWithECDsa(message, pubR1, signature, NamedCurveHash.secp256r1Keccak256).Should().Be(true); // secp256k1 + SHA256 - signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, HashAlgorithmType.SHA256); + signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, HashAlgorithm.SHA256); Crypto.VerifySignature(message, signature, pubK1).Should().BeTrue(); // SHA256 hash is used by default. CallVerifyWithECDsa(message, pubK1, signature, NamedCurveHash.secp256k1SHA256).Should().Be(true); // secp256k1 + Keccak256 - signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, HashAlgorithmType.Keccak256); - Crypto.VerifySignature(message, signature, pubK1, HashAlgorithmType.Keccak256).Should().BeTrue(); + signature = Crypto.Sign(message, privK1, ECCurve.Secp256k1, HashAlgorithm.Keccak256); + Crypto.VerifySignature(message, signature, pubK1, HashAlgorithm.Keccak256).Should().BeTrue(); CallVerifyWithECDsa(message, pubK1, signature, NamedCurveHash.secp256k1Keccak256).Should().Be(true); } From dbaa0d378e5cf315b3cec7caf7a7acd6b41fc60d Mon Sep 17 00:00:00 2001 From: Shargon Date: Fri, 3 Jan 2025 02:34:45 -0800 Subject: [PATCH 4/4] Update src/Neo/Cryptography/Crypto.cs --- src/Neo/Cryptography/Crypto.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Neo/Cryptography/Crypto.cs b/src/Neo/Cryptography/Crypto.cs index 11b11bb94d..99da988042 100644 --- a/src/Neo/Cryptography/Crypto.cs +++ b/src/Neo/Cryptography/Crypto.cs @@ -140,7 +140,6 @@ public static bool VerifySignature(ReadOnlySpan message, ReadOnlySpan /// Verifies that a digital signature is appropriate for the provided key, message and hash algorithm. ///