From 6d46044b078fd485ad2a4ab172a8274613120272 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 28 Sep 2013 09:34:39 +0700 Subject: Consolidate null curve checks required by implicitlyCa stuff --- .../java/org/bouncycastle/math/ec/ECPoint.java | 54 ++++++++++++---------- 1 file changed, 29 insertions(+), 25 deletions(-) (limited to 'core/src/main/java/org/bouncycastle/math') 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 8aac49c2..4c703dc5 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java +++ b/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java @@ -11,12 +11,10 @@ public abstract class ECPoint protected static ECFieldElement[] getInitialZCoords(ECCurve curve) { - if (curve == null) - { - return EMPTY_ZS; - } + // Cope with null curve, most commonly used by implicitlyCa + int coord = null == curve ? ECCurve.COORD_AFFINE : curve.getCoordinateSystem(); - switch (curve.getCoordinateSystem()) + switch (coord) { case ECCurve.COORD_AFFINE: case ECCurve.COORD_LAMBDA_AFFINE: @@ -27,7 +25,7 @@ public abstract class ECPoint ECFieldElement one = curve.fromBigInteger(ECConstants.ONE); - switch (curve.getCoordinateSystem()) + switch (coord) { case ECCurve.COORD_HOMOGENEOUS: case ECCurve.COORD_JACOBIAN: @@ -69,6 +67,12 @@ public abstract class ECPoint return curve; } + protected int getCurveCoordinateSystem() + { + // Cope with null curve, most commonly used by implicitlyCa + return null == curve ? ECCurve.COORD_AFFINE : curve.getCoordinateSystem(); + } + /** * @deprecated Use getAffineXCoord instead */ @@ -144,7 +148,12 @@ public abstract class ECPoint public boolean isNormalized() { - return curve.getCoordinateSystem() == ECCurve.COORD_AFFINE || isInfinity() || zs[0].bitLength() == 1; + int coord = getCurveCoordinateSystem(); + + return coord == ECCurve.COORD_AFFINE + || coord == ECCurve.COORD_LAMBDA_AFFINE + || isInfinity() + || zs[0].bitLength() == 1; } public ECPoint normalize() @@ -154,7 +163,7 @@ public abstract class ECPoint return this; } - switch (getCurve().getCoordinateSystem()) + switch (getCurveCoordinateSystem()) { case ECCurve.COORD_AFFINE: case ECCurve.COORD_LAMBDA_AFFINE: @@ -176,7 +185,7 @@ public abstract class ECPoint ECPoint normalize(ECFieldElement zInv) { - switch (getCurve().getCoordinateSystem()) + switch (getCurveCoordinateSystem()) { case ECCurve.COORD_HOMOGENEOUS: case ECCurve.COORD_LAMBDA_PROJECTIVE: @@ -199,7 +208,7 @@ public abstract class ECPoint protected ECPoint createScaledPoint(ECFieldElement sx, ECFieldElement sy) { - return curve.createRawPoint(getRawXCoord().multiply(sx), getRawYCoord().multiply(sy), withCompression); + return getCurve().createRawPoint(getRawXCoord().multiply(sx), getRawYCoord().multiply(sy), withCompression); } public boolean isInfinity() @@ -261,16 +270,8 @@ public abstract class ECPoint } // TODO Consider just requiring already normalized, to avoid silent performance degradation - // watch for degenerate point due to implicitlyCa - ECPoint p; - if (getCurve() != null) - { - p = normalize(); - } - else - { - p = this; - } + + ECPoint p = normalize(); return p.getXCoord().hashCode() ^ p.getRawYCoord().hashCode(); } @@ -411,7 +412,7 @@ public abstract class ECPoint public ECFieldElement getZCoord(int index) { - if (index == 1 && ECCurve.COORD_JACOBIAN_MODIFIED == getCurve().getCoordinateSystem()) + if (index == 1 && ECCurve.COORD_JACOBIAN_MODIFIED == getCurveCoordinateSystem()) { return getJacobianModifiedW(); } @@ -944,7 +945,10 @@ public abstract class ECPoint return this; } - if (getCurve().getCoordinateSystem() != ECCurve.COORD_AFFINE) + ECCurve curve = getCurve(); + int coord = curve.getCoordinateSystem(); + + if (ECCurve.COORD_AFFINE != coord) { return new ECPoint.Fp(curve, this.x, this.y.negate(), this.zs, this.withCompression); } @@ -960,7 +964,7 @@ public abstract class ECPoint } ECFieldElement W = ZSquared.square(); - ECFieldElement a4 = curve.getA(); + ECFieldElement a4 = getCurve().getA(); ECFieldElement a4Neg = a4.negate(); if (a4Neg.bitLength() < a4.bitLength()) { @@ -999,7 +1003,7 @@ public abstract class ECPoint ECFieldElement W3 = calculateW ? two(_8T.multiply(W1)) : null; ECFieldElement Z3 = two(Z1.bitLength() == 1 ? Y1 : Y1.multiply(Z1)); - return new ECPoint.Fp(curve, X3, Y3, new ECFieldElement[]{ Z3, W3 }, this.withCompression); + return new ECPoint.Fp(getCurve(), X3, Y3, new ECFieldElement[]{ Z3, W3 }, this.withCompression); } } @@ -1061,7 +1065,7 @@ public abstract class ECPoint public ECFieldElement getYCoord() { - switch (getCurve().getCoordinateSystem()) + switch (getCurveCoordinateSystem()) { case ECCurve.COORD_LAMBDA_AFFINE: case ECCurve.COORD_LAMBDA_PROJECTIVE: -- cgit v1.2.3