Skip to content

Commit

Permalink
+ Correct key disposing (memory leak)
Browse files Browse the repository at this point in the history
+ Aggressive Inlining
  • Loading branch information
kzorin52 committed Jan 19, 2024
1 parent 9f5339a commit fbd0027
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
6 changes: 2 additions & 4 deletions Algos/Ed25519HdKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ public HdKey GetMasterKeyFromSeed(ReadOnlySpan<byte> seed)

public byte[] GetPublic(ReadOnlySpan<byte> privateKey)
{
return Key.Import(SignatureAlgorithm.Ed25519,
privateKey,
KeyBlobFormat.RawPrivateKey).PublicKey
.Export(KeyBlobFormat.RawPublicKey);
using var key = Key.Import(SignatureAlgorithm.Ed25519, privateKey, KeyBlobFormat.RawPrivateKey);
return key.PublicKey.Export(KeyBlobFormat.RawPublicKey);
}
/* some my benchamrks:
| Method | Mean | Error | StdDev |
Expand Down
5 changes: 4 additions & 1 deletion Interfaces/IHdKeyAlgo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Security.Cryptography;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;

namespace NBip32Fast.Interfaces;

Expand Down Expand Up @@ -33,11 +34,13 @@ public HdKey DeriveFromMasterKey(ReadOnlySpan<KeyPathElement> path, HdKey master

public byte[] GetPublic(ReadOnlySpan<byte> privateKey);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static byte[] Bip32Hash(ReadOnlySpan<byte> chainCode, KeyPathElement index, byte[] data)
{
return HMACSHA512.HashData(chainCode, [.. data, .. index.Serialized.Span]);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected static byte[] Bip32Hash(ReadOnlySpan<byte> chainCode, KeyPathElement index, byte prefix, ReadOnlySpan<byte> data)
{
return HMACSHA512.HashData(chainCode, [prefix, .. data, .. index.Serialized.Span]);
Expand Down
4 changes: 3 additions & 1 deletion KeyPath.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Runtime.CompilerServices;
using System.Text;

namespace NBip32Fast;

Expand Down Expand Up @@ -149,6 +150,7 @@ public override int GetHashCode()

#endregion

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] SerializeUInt32(in uint index)
{
return
Expand Down
2 changes: 1 addition & 1 deletion NBip32Fast.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Supports SecP256k1 and Ed25519</Description>
<PropertyGroup>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.0.1</Version>
<Version>1.0.3</Version>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit fbd0027

Please sign in to comment.