From f9d1a02949b1325b68d0a4d9f9c21cd53de2fb36 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 21 Jan 2014 18:22:20 +0700 Subject: Fix Equals methods --- crypto/src/math/ec/ECFieldElement.cs | 20 +++++++------------- crypto/src/math/ec/ECPoint.cs | 35 +++++++++++++++++++++-------------- 2 files changed, 28 insertions(+), 27 deletions(-) (limited to 'crypto/src/math/ec') diff --git a/crypto/src/math/ec/ECFieldElement.cs b/crypto/src/math/ec/ECFieldElement.cs index cc5050002..fb0e8535b 100644 --- a/crypto/src/math/ec/ECFieldElement.cs +++ b/crypto/src/math/ec/ECFieldElement.cs @@ -39,23 +39,17 @@ namespace Org.BouncyCastle.Math.EC return ToBigInteger().TestBit(0); } - public override bool Equals( - object obj) + public override bool Equals(object obj) { - if (obj == this) - return true; - - ECFieldElement other = obj as ECFieldElement; - - if (other == null) - return false; - - return Equals(other); + return Equals(obj as ECFieldElement); } - public virtual bool Equals( - ECFieldElement other) + public virtual bool Equals(ECFieldElement other) { + if (this == other) + return true; + if (null == other) + return false; return ToBigInteger().Equals(other.ToBigInteger()); } diff --git a/crypto/src/math/ec/ECPoint.cs b/crypto/src/math/ec/ECPoint.cs index 6a06be4d1..e4ce58d2d 100644 --- a/crypto/src/math/ec/ECPoint.cs +++ b/crypto/src/math/ec/ECPoint.cs @@ -59,29 +59,36 @@ namespace Org.BouncyCastle.Math.EC get { return withCompression; } } - public override bool Equals( - object obj) + public override bool Equals(object obj) { - if (obj == this) - return true; - - ECPoint o = obj as ECPoint; + return Equals(obj as ECPoint); + } - if (o == null) + public virtual bool Equals(ECPoint other) + { + if (this == other) + return true; + if (null == other) return false; - if (this.IsInfinity) - return o.IsInfinity; + bool i1 = IsInfinity, i2 = other.IsInfinity; + if (i1 || i2) + { + return i1 && i2; + } - return x.Equals(o.x) && y.Equals(o.y); + return X.Equals(other.X) && Y.Equals(other.Y); } public override int GetHashCode() { - if (this.IsInfinity) - return 0; - - return x.GetHashCode() ^ y.GetHashCode(); + int hc = 0; + if (!IsInfinity) + { + hc ^= X.GetHashCode() * 17; + hc ^= Y.GetHashCode() * 257; + } + return hc; } // /** -- cgit 1.4.1