Skip to content

Commit 3327801

Browse files
authored
Merge branch 'master' into mem-pool-improve
2 parents 474499e + 0f6aeca commit 3327801

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

Diff for: src/Neo/Cryptography/Crypto.cs

+9-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// modifications are permitted.
1111

1212
using Neo.IO.Caching;
13-
using Org.BouncyCastle.Asn1.Sec;
1413
using Org.BouncyCastle.Asn1.X9;
1514
using Org.BouncyCastle.Crypto.Parameters;
1615
using Org.BouncyCastle.Crypto.Signers;
@@ -32,11 +31,9 @@ public static class Crypto
3231
private static readonly BigInteger s_prime = new(1,
3332
Hex.Decode("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"));
3433

35-
private static readonly ECDsaCache CacheECDsa = new();
36-
private static readonly bool IsOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
37-
private static readonly ECCurve secP256k1 = ECCurve.CreateFromFriendlyName("secP256k1");
38-
private static readonly X9ECParameters bouncySecp256k1 = SecNamedCurves.GetByName("secp256k1");
39-
private static readonly X9ECParameters bouncySecp256r1 = SecNamedCurves.GetByName("secp256r1");
34+
private static readonly ECDsaCache s_cacheECDsa = [];
35+
private static readonly bool s_isOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
36+
private static readonly ECCurve s_secP256k1 = ECCurve.CreateFromFriendlyName("secP256k1");
4037

4138
/// <summary>
4239
/// Calculates the 160-bit hash value of the specified message.
@@ -82,7 +79,7 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve, Ha
8279
/// <returns>The ECDSA signature for the specified message.</returns>
8380
public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = null, HashAlgorithm hashAlgorithm = HashAlgorithm.SHA256)
8481
{
85-
if (hashAlgorithm == HashAlgorithm.Keccak256 || (IsOSX && ecCurve == ECC.ECCurve.Secp256k1))
82+
if (hashAlgorithm == HashAlgorithm.Keccak256 || (s_isOSX && ecCurve == ECC.ECCurve.Secp256k1))
8683
{
8784
var signer = new ECDsaSigner();
8885
var privateKey = new BigInteger(1, priKey);
@@ -103,7 +100,7 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n
103100

104101
var curve =
105102
ecCurve == null || ecCurve == ECC.ECCurve.Secp256r1 ? ECCurve.NamedCurves.nistP256 :
106-
ecCurve == ECC.ECCurve.Secp256k1 ? secP256k1 :
103+
ecCurve == ECC.ECCurve.Secp256k1 ? s_secP256k1 :
107104
throw new NotSupportedException();
108105

109106
using var ecdsa = ECDsa.Create(new ECParameters
@@ -143,7 +140,7 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte
143140
{
144141
if (signature.Length != 64) return false;
145142

146-
if (hashAlgorithm == HashAlgorithm.Keccak256 || (IsOSX && pubkey.Curve == ECC.ECCurve.Secp256k1))
143+
if (hashAlgorithm == HashAlgorithm.Keccak256 || (s_isOSX && pubkey.Curve == ECC.ECCurve.Secp256k1))
147144
{
148145
var point = pubkey.Curve.BouncyCastleCurve.Curve.CreatePoint(
149146
new BigInteger(pubkey.X.Value.ToString()),
@@ -174,13 +171,13 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte
174171
/// <exception cref="NotSupportedException"></exception>
175172
public static ECDsa CreateECDsa(ECPoint pubkey)
176173
{
177-
if (CacheECDsa.TryGet(pubkey, out var cache))
174+
if (s_cacheECDsa.TryGet(pubkey, out var cache))
178175
{
179176
return cache.Value;
180177
}
181178
var curve =
182179
pubkey.Curve == ECC.ECCurve.Secp256r1 ? ECCurve.NamedCurves.nistP256 :
183-
pubkey.Curve == ECC.ECCurve.Secp256k1 ? secP256k1 :
180+
pubkey.Curve == ECC.ECCurve.Secp256k1 ? s_secP256k1 :
184181
throw new NotSupportedException();
185182
var buffer = pubkey.EncodePoint(false);
186183
var ecdsa = ECDsa.Create(new ECParameters
@@ -192,7 +189,7 @@ public static ECDsa CreateECDsa(ECPoint pubkey)
192189
Y = buffer[33..]
193190
}
194191
});
195-
CacheECDsa.Add(new ECDsaCacheItem(pubkey, ecdsa));
192+
s_cacheECDsa.Add(new ECDsaCacheItem(pubkey, ecdsa));
196193
return ecdsa;
197194
}
198195

0 commit comments

Comments
 (0)