diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-29 07:29:17 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-29 07:29:17 +0400 |
commit | 8259ceab74961cb6ca7c657c527db670896d6334 (patch) | |
tree | d1e60bea242d1291d5572225f1a4e4fda01bba8f /core/src/main/java/org/bouncycastle/math | |
parent | ce071054cf89e6890e06dff462da93ec035ab502 (diff) |
Improvements to equals/hashCode
Diffstat (limited to 'core/src/main/java/org/bouncycastle/math')
-rw-r--r-- | core/src/main/java/org/bouncycastle/math/ec/ECPoint.java | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java b/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java index 74ef362c..3a6faf30 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java +++ b/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java @@ -223,28 +223,51 @@ public abstract class ECPoint public boolean equals(ECPoint other) { - ECCurve c = getCurve(); - if (!c.equals(other.getCurve())) + if (null == other) { return false; } + ECCurve c1 = getCurve(), c2 = other.getCurve(); + boolean n1 = (null == c1), n2 = (null == c2); boolean i1 = isInfinity(), i2 = other.isInfinity(); + if (i1 || i2) { - return i1 && i2; + return (i1 && i2) && (n1 || n2 || c1.equals(c2)); } - // TODO Consider just requiring already normalized, to avoid silent performance degradation + ECPoint p1 = this, p2 = other; + if (n1 && n2) + { + // Points with null curve are in affine form, so already normalized + } + else if (n1) + { + p2 = p2.normalize(); + } + else if (n2) + { + p1 = p1.normalize(); + } + else if (!c1.equals(c2)) + { + return false; + } + else + { + // TODO Consider just requiring already normalized, to avoid silent performance degradation - ECPoint[] ns = new ECPoint[]{ this, c.importPoint(other) }; + ECPoint[] points = new ECPoint[]{ this, c1.importPoint(p2) }; - // TODO This is a little strong, really only requires coZNormalizeAll to get Zs equal - c.normalizeAll(ns); + // TODO This is a little strong, really only requires coZNormalizeAll to get Zs equal + c1.normalizeAll(points); - ECPoint p1 = ns[0], p2 = ns[1]; + p1 = points[0]; + p2 = points[1]; + } - return p1.getRawXCoord().equals(p2.getRawXCoord()) && p1.getRawYCoord().equals(p2.getRawYCoord()); + return p1.getXCoord().equals(p2.getXCoord()) && p1.getYCoord().equals(p2.getYCoord()); } public boolean equals(Object other) @@ -264,16 +287,20 @@ public abstract class ECPoint public int hashCode() { - if (isInfinity()) + ECCurve c = getCurve(); + int hc = (null == c) ? 0 : ~c.hashCode(); + + if (!isInfinity()) { - return 0; - } + // TODO Consider just requiring already normalized, to avoid silent performance degradation - // TODO Consider just requiring already normalized, to avoid silent performance degradation + ECPoint p = normalize(); - ECPoint p = normalize(); + hc ^= p.getXCoord().hashCode() * 17; + hc ^= p.getYCoord().hashCode() * 257; + } - return p.getXCoord().hashCode() ^ p.getRawYCoord().hashCode(); + return hc; } public String toString() |