Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize crypto #3174

Merged
merged 7 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions src/Neo/Cryptography/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO.Caching;
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
Expand All @@ -20,6 +21,7 @@ namespace Neo.Cryptography
/// </summary>
public static class Crypto
{
private static readonly ECDsaCache CacheECDsa = new();
private static readonly bool IsOSX = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

/// <summary>
Expand Down Expand Up @@ -94,24 +96,41 @@ public static bool VerifySignature(ReadOnlySpan<byte> message, ReadOnlySpan<byte
}
else
{
ECCurve curve =
pubkey.Curve == ECC.ECCurve.Secp256r1 ? ECCurve.NamedCurves.nistP256 :
pubkey.Curve == ECC.ECCurve.Secp256k1 ? ECCurve.CreateFromFriendlyName("secP256k1") :
throw new NotSupportedException();
byte[] buffer = pubkey.EncodePoint(false);
using var ecdsa = ECDsa.Create(new ECParameters
{
Curve = curve,
Q = new ECPoint
{
X = buffer[1..33],
Y = buffer[33..]
}
});
var ecdsa = CreateECDsa(pubkey);
return ecdsa.VerifyData(message, signature, HashAlgorithmName.SHA256);
}
}

/// <summary>
/// Create and cache ECDsa objects
/// </summary>
/// <param name="pubkey"></param>
/// <returns>Cached ECDsa</returns>
/// <exception cref="NotSupportedException"></exception>
public static ECDsa CreateECDsa(ECC.ECPoint pubkey)
{
if (CacheECDsa.TryGet(pubkey, out var cache))
{
return cache.value;
}
var curve =
pubkey.Curve == ECC.ECCurve.Secp256r1 ? ECCurve.NamedCurves.nistP256 :
pubkey.Curve == ECC.ECCurve.Secp256k1 ? ECCurve.CreateFromFriendlyName("secP256k1") :
throw new NotSupportedException();
var buffer = pubkey.EncodePoint(false);
var ecdsa = ECDsa.Create(new ECParameters
{
Curve = curve,
Q = new ECPoint
{
X = buffer[1..33],
Y = buffer[33..]
}
});
CacheECDsa.Add(new ECDsaCacheItem(pubkey, ecdsa));
return ecdsa;
}

/// <summary>
/// Verifies that a digital signature is appropriate for the provided key and message.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Cryptography/ECC/ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ public bool Equals(ECPoint other)
{
if (ReferenceEquals(this, other)) return true;
if (other is null) return false;
if (!Curve.Equals(other.Curve)) return false;
if (IsInfinity && other.IsInfinity) return true;
if (IsInfinity || other.IsInfinity) return false;
return X.Equals(other.X) && Y.Equals(other.Y);
Expand Down
32 changes: 32 additions & 0 deletions src/Neo/IO/Caching/ECDsaCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ECDsaCache.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.

using Neo.Cryptography.ECC;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace Neo.IO.Caching
{
record ECDsaCacheItem(Cryptography.ECC.ECPoint key, ECDsa value);
internal class ECDsaCache : FIFOCache<Cryptography.ECC.ECPoint, ECDsaCacheItem>
{
public ECDsaCache(int max_capacity = 20000) : base(max_capacity, EqualityComparer<Cryptography.ECC.ECPoint>.Default)
{
}

protected override Cryptography.ECC.ECPoint GetKeyForItem(ECDsaCacheItem item)
{
return item.key;
}
}
}
4 changes: 2 additions & 2 deletions tests/Neo.UnitTests/Cryptography/ECC/UT_ECPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void TestEquals()
point.Equals(null).Should().BeFalse();

point = new ECPoint(null, null, ECCurve.Secp256k1);
point.Equals(new ECPoint(null, null, ECCurve.Secp256r1)).Should().BeTrue();
point.Equals(new ECPoint(null, null, ECCurve.Secp256r1)).Should().BeFalse();
point.Equals(ECCurve.Secp256r1.G).Should().BeFalse();
ECCurve.Secp256r1.G.Equals(point).Should().BeFalse();

Expand All @@ -199,7 +199,7 @@ public void TestEqualsObject()
point.Equals(1u).Should().BeFalse();

point = new ECPoint(null, null, ECCurve.Secp256k1);
point.Equals(new ECPoint(null, null, ECCurve.Secp256r1)).Should().BeTrue();
point.Equals(new ECPoint(null, null, ECCurve.Secp256r1)).Should().BeFalse();
point.Equals(ECCurve.Secp256r1.G).Should().BeFalse();
ECCurve.Secp256r1.G.Equals(point).Should().BeFalse();

Expand Down
Loading