Skip to content

Commit

Permalink
Related to #3177 (comment) (#3179)
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Mar 22, 2024
1 parent a432e4b commit 127f74b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Neo/Cryptography/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

using Neo.IO.Caching;
using Org.BouncyCastle.Asn1.X9;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using System;
Expand All @@ -25,6 +26,8 @@ public static class Crypto
{
private static readonly ECDsaCache CacheECDsa = new();
private static readonly bool IsOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
private static readonly ECCurve secP256k1 = ECCurve.CreateFromFriendlyName("secP256k1");
private static readonly X9ECParameters bouncySecp256k1 = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");

/// <summary>
/// Calculates the 160-bit hash value of the specified message.
Expand Down Expand Up @@ -57,8 +60,7 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n
{
if (IsOSX && ecCurve == ECC.ECCurve.Secp256k1)
{
var curveParameters = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
var domain = new ECDomainParameters(curveParameters.Curve, curveParameters.G, curveParameters.N, curveParameters.H);
var domain = new ECDomainParameters(bouncySecp256k1.Curve, bouncySecp256k1.G, bouncySecp256k1.N, bouncySecp256k1.H);
var signer = new Org.BouncyCastle.Crypto.Signers.ECDsaSigner();
var privateKey = new BigInteger(1, priKey);
var priKeyParameters = new ECPrivateKeyParameters(privateKey, domain);
Expand All @@ -77,7 +79,7 @@ public static byte[] Sign(byte[] message, byte[] priKey, ECC.ECCurve ecCurve = n

var curve =
ecCurve == null || ecCurve == ECC.ECCurve.Secp256r1 ? ECCurve.NamedCurves.nistP256 :
ecCurve == ECC.ECCurve.Secp256k1 ? ECCurve.CreateFromFriendlyName("secP256k1") :
ecCurve == ECC.ECCurve.Secp256k1 ? secP256k1 :
throw new NotSupportedException();

using var ecdsa = ECDsa.Create(new ECParameters
Expand All @@ -101,9 +103,8 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte

if (IsOSX && pubkey.Curve == ECC.ECCurve.Secp256k1)
{
var curve = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
var point = curve.Curve.CreatePoint(
var domain = new ECDomainParameters(bouncySecp256k1.Curve, bouncySecp256k1.G, bouncySecp256k1.N, bouncySecp256k1.H);
var point = bouncySecp256k1.Curve.CreatePoint(
new BigInteger(pubkey.X.Value.ToString()),
new BigInteger(pubkey.Y.Value.ToString()));
var pubKey = new ECPublicKeyParameters("ECDSA", point, domain);
Expand Down Expand Up @@ -135,7 +136,7 @@ public static ECDsa CreateECDsa(ECC.ECPoint pubkey)
}
var curve =
pubkey.Curve == ECC.ECCurve.Secp256r1 ? ECCurve.NamedCurves.nistP256 :
pubkey.Curve == ECC.ECCurve.Secp256k1 ? ECCurve.CreateFromFriendlyName("secP256k1") :
pubkey.Curve == ECC.ECCurve.Secp256k1 ? secP256k1 :
throw new NotSupportedException();
var buffer = pubkey.EncodePoint(false);
var ecdsa = ECDsa.Create(new ECParameters
Expand Down

0 comments on commit 127f74b

Please sign in to comment.