10
10
// modifications are permitted.
11
11
12
12
using Neo . IO . Caching ;
13
- using Org . BouncyCastle . Asn1 . Sec ;
14
13
using Org . BouncyCastle . Asn1 . X9 ;
15
14
using Org . BouncyCastle . Crypto . Parameters ;
16
15
using Org . BouncyCastle . Crypto . Signers ;
@@ -32,11 +31,9 @@ public static class Crypto
32
31
private static readonly BigInteger s_prime = new ( 1 ,
33
32
Hex . Decode ( "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F" ) ) ;
34
33
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" ) ;
40
37
41
38
/// <summary>
42
39
/// 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
82
79
/// <returns>The ECDSA signature for the specified message.</returns>
83
80
public static byte [ ] Sign ( byte [ ] message , byte [ ] priKey , ECC . ECCurve ecCurve = null , HashAlgorithm hashAlgorithm = HashAlgorithm . SHA256 )
84
81
{
85
- if ( hashAlgorithm == HashAlgorithm . Keccak256 || ( IsOSX && ecCurve == ECC . ECCurve . Secp256k1 ) )
82
+ if ( hashAlgorithm == HashAlgorithm . Keccak256 || ( s_isOSX && ecCurve == ECC . ECCurve . Secp256k1 ) )
86
83
{
87
84
var signer = new ECDsaSigner ( ) ;
88
85
var privateKey = new BigInteger ( 1 , priKey ) ;
@@ -103,7 +100,7 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n
103
100
104
101
var curve =
105
102
ecCurve == null || ecCurve == ECC . ECCurve . Secp256r1 ? ECCurve . NamedCurves . nistP256 :
106
- ecCurve == ECC . ECCurve . Secp256k1 ? secP256k1 :
103
+ ecCurve == ECC . ECCurve . Secp256k1 ? s_secP256k1 :
107
104
throw new NotSupportedException ( ) ;
108
105
109
106
using var ecdsa = ECDsa . Create ( new ECParameters
@@ -143,7 +140,7 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte
143
140
{
144
141
if ( signature . Length != 64 ) return false ;
145
142
146
- if ( hashAlgorithm == HashAlgorithm . Keccak256 || ( IsOSX && pubkey . Curve == ECC . ECCurve . Secp256k1 ) )
143
+ if ( hashAlgorithm == HashAlgorithm . Keccak256 || ( s_isOSX && pubkey . Curve == ECC . ECCurve . Secp256k1 ) )
147
144
{
148
145
var point = pubkey . Curve . BouncyCastleCurve . Curve . CreatePoint (
149
146
new BigInteger ( pubkey . X . Value . ToString ( ) ) ,
@@ -174,13 +171,13 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte
174
171
/// <exception cref="NotSupportedException"></exception>
175
172
public static ECDsa CreateECDsa ( ECPoint pubkey )
176
173
{
177
- if ( CacheECDsa . TryGet ( pubkey , out var cache ) )
174
+ if ( s_cacheECDsa . TryGet ( pubkey , out var cache ) )
178
175
{
179
176
return cache . Value ;
180
177
}
181
178
var curve =
182
179
pubkey . Curve == ECC . ECCurve . Secp256r1 ? ECCurve . NamedCurves . nistP256 :
183
- pubkey . Curve == ECC . ECCurve . Secp256k1 ? secP256k1 :
180
+ pubkey . Curve == ECC . ECCurve . Secp256k1 ? s_secP256k1 :
184
181
throw new NotSupportedException ( ) ;
185
182
var buffer = pubkey . EncodePoint ( false ) ;
186
183
var ecdsa = ECDsa . Create ( new ECParameters
@@ -192,7 +189,7 @@ public static ECDsa CreateECDsa(ECPoint pubkey)
192
189
Y = buffer [ 33 ..]
193
190
}
194
191
} ) ;
195
- CacheECDsa . Add ( new ECDsaCacheItem ( pubkey , ecdsa ) ) ;
192
+ s_cacheECDsa . Add ( new ECDsaCacheItem ( pubkey , ecdsa ) ) ;
196
193
return ecdsa ;
197
194
}
198
195
0 commit comments