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 4 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
50 changes: 36 additions & 14 deletions src/Neo/Cryptography/Crypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.IO.Caching;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

Expand Down Expand Up @@ -94,24 +96,44 @@ 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);
}
}

//private static readonly Dictionary<ECC.ECPoint, ECDsa> CacheECDsa = new();
private static readonly ECDsaCache CacheECDsa = new ECDsaCache();

/// <summary>
/// Create and cache ECDsa objects
/// </summary>
/// <param name="pubkey"></param>
/// <returns></returns>
/// <exception cref="NotSupportedException"></exception>
public static ECDsa CreateECDsa(ECC.ECPoint pubkey)
{
if (CacheECDsa.TryGet(pubkey, out var cache))
{
return cache.value;
}
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);
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
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;
}
}
}
Loading