Skip to content

Commit

Permalink
Small optimization for default pubkey comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Sep 30, 2024
1 parent 8245b0d commit 0bc86c8
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NBitcoin.Tests/Secp256k1Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4468,6 +4468,8 @@ public void musig_key_sort_vectors()
var root = JObject.Parse(File.ReadAllText("data/musig/key_sort_vectors.json"));
var pubkeys = GetArray<string>(root["pubkeys"]).Select(p => new PubKey(Encoders.Hex.DecodeData(p))).ToArray();
var sorted_pubkeys = GetArray<string>(root["sorted_pubkeys"]).Select(p => new PubKey(Encoders.Hex.DecodeData(p))).ToArray();
Array.Sort(pubkeys);
AssertEx.CollectionEquals(pubkeys, sorted_pubkeys);
Array.Sort(pubkeys, PubKeyComparer.Instance);
AssertEx.CollectionEquals(pubkeys, sorted_pubkeys);

Expand Down
1 change: 1 addition & 0 deletions NBitcoin/BIP174/PSBTUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static void WriteBIP32Derivations(this JsonTextWriter jsonWriter, HDKeyPa
/// <summary>
/// Lexicographical comparison of public keys.
/// Use <see cref="PubKeyComparer.Instance"/> to get an instance of this class."/>
/// <see cref="IComparable{T}"/> implementation for <see cref="PubKey"/> is using this comparer.
/// </summary>
public class PubKeyComparer : IComparer<PubKey>
{
Expand Down
3 changes: 1 addition & 2 deletions NBitcoin/PubKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ internal ECKey ECKey
}
#endif


public int CompareTo(PubKey? other) => other is null ? 1 : BytesComparer.Instance.Compare(this.ToBytes(), other.ToBytes());
public int CompareTo(PubKey? other) => other is null ? 1 : PubKeyComparer.Instance.Compare(this, other);

public PubKey Compress()
{
Expand Down

0 comments on commit 0bc86c8

Please sign in to comment.