Skip to content

Commit ce115ea

Browse files
authored
feat(secp): make keys IEqutable (#1313)
1 parent c29ab25 commit ce115ea

3 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎NBitcoin/Secp256k1/ECPrivKey.cs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public bool TryGetNonce(Span<byte> nonce32, ReadOnlySpan<byte> msg32, ReadOnlySp
116116
#if SECP256K1_LIB
117117
public
118118
#endif
119-
partial class ECPrivKey : IDisposable
119+
partial class ECPrivKey : IDisposable, IEquatable<ECPrivKey>
120120
{
121121
internal bool cleared = false;
122122
#if SECP256K1_LIB
@@ -811,6 +811,15 @@ internal static bool secp256k1_ecdsa_sig_sign(ECMultGenContext ctx, out Scalar s
811811
return true;
812812
}
813813

814+
public bool Equals(ECPrivKey? other)
815+
{
816+
if (other is ECPrivKey item)
817+
{
818+
return this == item;
819+
}
820+
return false;
821+
}
822+
814823
public override bool Equals(object? obj)
815824
{
816825
if (obj is ECPrivKey item)
@@ -894,7 +903,7 @@ protected virtual void Dispose(bool disposing)
894903
{
895904
if (this.cleared)
896905
return;
897-
906+
898907
Clear();
899908
}
900909

‎NBitcoin/Secp256k1/ECPubKey.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace NBitcoin.Secp256k1
1212
#if SECP256K1_LIB
1313
public
1414
#endif
15-
partial class ECPubKey : IComparable<ECPubKey>
15+
partial class ECPubKey : IComparable<ECPubKey>, IEquatable<ECPubKey>
1616
{
1717

1818
#if SECP256K1_LIB
@@ -242,6 +242,12 @@ public ECPubKey Negate()
242242
return new ECPubKey(Q.Negate(), ctx);
243243
}
244244

245+
public bool Equals(ECPubKey? other)
246+
{
247+
if (other is ECPubKey item)
248+
return this == item;
249+
return false;
250+
}
245251

246252
public override bool Equals(object? obj)
247253
{

‎NBitcoin/Secp256k1/ECXOnlyPubKey.cs‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace NBitcoin.Secp256k1
1515
#if SECP256K1_LIB
1616
public
1717
#endif
18-
partial class ECXOnlyPubKey : IComparable<ECXOnlyPubKey>
18+
partial class ECXOnlyPubKey : IComparable<ECXOnlyPubKey>, IEquatable<ECXOnlyPubKey>
1919
{
2020
internal static byte[] TAG_BIP0340Challenge = ASCIIEncoding.ASCII.GetBytes("BIP0340/challenge");
2121
#if SECP256K1_LIB
@@ -179,7 +179,35 @@ public int CompareTo(ECXOnlyPubKey? other)
179179

180180
public override int GetHashCode()
181181
{
182-
return this.Q.x.GetHashCode();
182+
return Q.x.GetHashCode();
183+
}
184+
185+
public bool Equals(ECXOnlyPubKey? other)
186+
{
187+
if (other is ECXOnlyPubKey)
188+
return this == other;
189+
return false;
190+
}
191+
192+
public override bool Equals(object? obj)
193+
{
194+
if (obj is ECXOnlyPubKey item)
195+
return this == item;
196+
return false;
197+
}
198+
199+
public static bool operator ==(ECXOnlyPubKey? a, ECXOnlyPubKey? b)
200+
{
201+
if (a is ECXOnlyPubKey aa && b is ECXOnlyPubKey bb)
202+
{
203+
return aa.Q.x == bb.Q.x;
204+
}
205+
return a is null && b is null;
206+
}
207+
208+
public static bool operator !=(ECXOnlyPubKey? a, ECXOnlyPubKey? b)
209+
{
210+
return !(a == b);
183211
}
184212
}
185213
}

0 commit comments

Comments
 (0)