From fb5125ee26b69802c6315582898c2203ffa9f8da Mon Sep 17 00:00:00 2001 From: Carti Date: Sun, 6 Sep 2026 23:23:00 +0200 Subject: [PATCH 1/2] HexEncoder: Optimize `EncodeData` to avoid `byte[]` allocations --- NBitcoin/DataEncoders/HexEncoder.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/NBitcoin/DataEncoders/HexEncoder.cs b/NBitcoin/DataEncoders/HexEncoder.cs index ac1ebacfb..9ecef835c 100644 --- a/NBitcoin/DataEncoders/HexEncoder.cs +++ b/NBitcoin/DataEncoders/HexEncoder.cs @@ -52,6 +52,14 @@ public class HexEncoder : DataEncoder private static readonly string[] HexTbl = Enumerable.Range(0, 256).Select(v => v.ToString("x2")).ToArray(); #endif +#if NET8_0_OR_GREATER + public override string EncodeData(ReadOnlySpan data) + { + // TODO: Convert.ToHexStringLower (.NET 9+) once available. + return Convert.ToHexString(data).ToLowerInvariant(); + } +#endif + public override string EncodeData(byte[] data, int offset, int count) { if (data == null) From f3fc78f2bef639ef0b04f4fca68e3440c04ab133 Mon Sep 17 00:00:00 2001 From: Carti Date: Sun, 6 Sep 2026 23:23:05 +0200 Subject: [PATCH 2/2] Simplify --- NBitcoin/Secp256k1/ECPrivKey.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/NBitcoin/Secp256k1/ECPrivKey.cs b/NBitcoin/Secp256k1/ECPrivKey.cs index 4afd37864..32d0cad8a 100644 --- a/NBitcoin/Secp256k1/ECPrivKey.cs +++ b/NBitcoin/Secp256k1/ECPrivKey.cs @@ -298,7 +298,7 @@ public ECPrivKey TweakAdd(ReadOnlySpan tweak) /// /// 32 bytes tweak /// False If the tweak is not 32 bytes or if the tweak was out of range (chance of around 1 in 2^128 for uniformly random 32-byte arrays, or if the resulting private key would be invalid(only when the tweak is the complement of the private key) - /// This instance has been disposed + /// This instance has been disposed /// A tweaked private key public bool TryTweakAdd(ReadOnlySpan tweak, out ECPrivKey? tweakedPrivKey) { @@ -877,9 +877,8 @@ public bool TryTweakMul(ReadOnlySpan tweak, out ECPrivKey? tweakedPrivkey) tweakedPrivkey = null; if (tweak.Length != 32) return false; - Scalar factor; - int overflow; - factor = new Scalar(tweak, out overflow); + + var factor = new Scalar(tweak, out int overflow); var sec = this.sec; bool ret = overflow == 0 && secp256k1_eckey_privkey_tweak_mul(ref sec, factor); if (ret)