diff options
Diffstat (limited to 'crypto/src/math')
-rw-r--r-- | crypto/src/math/BigInteger.cs | 10 | ||||
-rw-r--r-- | crypto/src/math/ec/ECFieldElement.cs | 55 | ||||
-rw-r--r-- | crypto/src/math/ec/LongArray.cs | 15 |
3 files changed, 33 insertions, 47 deletions
diff --git a/crypto/src/math/BigInteger.cs b/crypto/src/math/BigInteger.cs index e5ab22e92..588adaf77 100644 --- a/crypto/src/math/BigInteger.cs +++ b/crypto/src/math/BigInteger.cs @@ -5,6 +5,9 @@ using System.Globalization; #if NETCOREAPP3_0_OR_GREATER using System.Numerics; #endif +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER +using System.Runtime.InteropServices; +#endif using System.Runtime.Serialization; using System.Text; @@ -1464,6 +1467,12 @@ namespace Org.BouncyCastle.Math public override int GetHashCode() { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + HashCode hc = default; + hc.AddBytes(MemoryMarshal.AsBytes(magnitude.AsSpan())); + hc.Add(sign); + return hc.ToHashCode(); +#else int hc = magnitude.Length; if (magnitude.Length > 0) { @@ -1476,6 +1485,7 @@ namespace Org.BouncyCastle.Math } return sign < 0 ? ~hc : hc; +#endif } // TODO Make public? diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs index 3afc843cd..e2306a7a9 100644 --- a/crypto/src/math/ec/ECFieldElement.cs +++ b/crypto/src/math/ec/ECFieldElement.cs @@ -516,29 +516,24 @@ namespace Org.BouncyCastle.Math.EC return x3; } - public override bool Equals( - object obj) + public override bool Equals(object obj) => obj is FpFieldElement that && Equals(that); + + public virtual bool Equals(FpFieldElement other) { - if (obj == this) + if (this == other) return true; - FpFieldElement other = obj as FpFieldElement; - - if (other == null) - return false; - - return Equals(other); - } - - public virtual bool Equals( - FpFieldElement other) - { - return q.Equals(other.q) && base.Equals(other); + return q.Equals(other.q) + && x.Equals(other.x); } public override int GetHashCode() { - return q.GetHashCode() ^ base.GetHashCode(); +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + return HashCode.Combine(q, x); +#else + return q.GetHashCode() ^ x.GetHashCode(); +#endif } } @@ -910,32 +905,26 @@ namespace Org.BouncyCastle.Math.EC get { return this.ks.Length >= 3 ? this.ks[2] : 0; } } - public override bool Equals( - object obj) + public override bool Equals(object obj) => obj is F2mFieldElement that && Equals(that); + + public virtual bool Equals(F2mFieldElement other) { - if (obj == this) + if (this == other) return true; - F2mFieldElement other = obj as F2mFieldElement; - - if (other == null) - return false; - - return Equals(other); - } - - public virtual bool Equals( - F2mFieldElement other) - { - return ((this.m == other.m) - && (this.representation == other.representation) + return this.m == other.m + && this.representation == other.representation && Arrays.AreEqual(this.ks, other.ks) - && (this.x.Equals(other.x))); + && this.x.Equals(other.x); } public override int GetHashCode() { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + return HashCode.Combine(x, m, Arrays.GetHashCode(ks)); +#else return x.GetHashCode() ^ m ^ Arrays.GetHashCode(ks); +#endif } } } diff --git a/crypto/src/math/ec/LongArray.cs b/crypto/src/math/ec/LongArray.cs index 3475e9ecc..e492f2913 100644 --- a/crypto/src/math/ec/LongArray.cs +++ b/crypto/src/math/ec/LongArray.cs @@ -1245,20 +1245,7 @@ namespace Org.BouncyCastle.Math.EC return true; } - public override int GetHashCode() - { - int usedLen = GetUsedLength(); - int hash = 1; - for (int i = 0; i < usedLen; i++) - { - ulong mi = m_data[i]; - hash *= 31; - hash ^= (int)mi; - hash *= 31; - hash ^= (int)(mi >> 32); - } - return hash; - } + public override int GetHashCode() => Arrays.GetHashCode(m_data, 0, GetUsedLength()); public LongArray Copy() { |