diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-28 12:54:07 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2013-09-28 12:54:07 +0400 |
commit | c0538d910cb8c46556de4875bd6791cb1fac6d95 (patch) | |
tree | 1fe2d21c34c8d324bcadf2d1ebbb8e4d7199317d /core/src/main/java/org/bouncycastle/math | |
parent | ffd37a44acabae3466c5c3685658b1e1c0abf3d4 (diff) |
Add ECPoint.toString() method
Add checkCurveEquation to validate lambda-projective points
Fix negate() for lambda-projective points
Diffstat (limited to 'core/src/main/java/org/bouncycastle/math')
-rw-r--r-- | core/src/main/java/org/bouncycastle/math/ec/ECPoint.java | 61 |
1 files changed, 59 insertions, 2 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 1dcf41e3..98c2e3f1 100644 --- a/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java +++ b/core/src/main/java/org/bouncycastle/math/ec/ECPoint.java @@ -276,6 +276,27 @@ public abstract class ECPoint return p.getXCoord().hashCode() ^ p.getRawYCoord().hashCode(); } + public String toString() + { + if (isInfinity()) + { + return "INF"; + } + + StringBuffer sb = new StringBuffer(); + sb.append('('); + sb.append(getRawXCoord()); + sb.append(','); + sb.append(getRawYCoord()); + for (int i = 0; i < zs.length; ++i) + { + sb.append(','); + sb.append(zs[i]); + } + sb.append(')'); + return sb.toString(); + } + public byte[] getEncoded() { return getEncoded(withCompression); @@ -1054,6 +1075,8 @@ public abstract class ECPoint } this.withCompression = withCompression; + +// checkCurveEquation(); } F2m(ECCurve curve, ECFieldElement x, ECFieldElement y, ECFieldElement[] zs, boolean withCompression) @@ -1061,6 +1084,8 @@ public abstract class ECPoint super(curve, x, y, zs); this.withCompression = withCompression; + +// checkCurveEquation(); } public ECFieldElement getYCoord() @@ -1330,6 +1355,26 @@ public abstract class ECPoint } } + protected void checkCurveEquation() + { + if (getCurveCoordinateSystem() != ECCurve.COORD_LAMBDA_PROJECTIVE) + { + return; + } + + ECFieldElement X = this.x, L = this.y, Z = this.zs[0]; + ECFieldElement XSq = X.square(); + ECFieldElement ZSq = Z.square(); + + ECFieldElement lhs = L.square().add(L.multiply(Z)).add(getCurve().getA().multiply(ZSq)).multiply(XSq); + ECFieldElement rhs = ZSq.square().multiply(getCurve().getB()).add(XSq.square()); + + if (!lhs.equals(rhs)) + { + throw new IllegalStateException("F2m Lambda-Projective invariant broken"); + } + } + public ECPoint negate() { if (this.isInfinity()) @@ -1341,12 +1386,24 @@ public abstract class ECPoint switch (getCurveCoordinateSystem()) { + case ECCurve.COORD_AFFINE: + { + return new ECPoint.F2m(curve, X, Y.add(X), withCompression); + } case ECCurve.COORD_LAMBDA_AFFINE: + { + return new ECPoint.F2m(curve, X, Y.addOne(), withCompression); + } case ECCurve.COORD_LAMBDA_PROJECTIVE: + { // Y is actually Lambda (X + Y/X) here - return new ECPoint.F2m(curve, X, Y.addOne(), withCompression); + ECFieldElement L = Y, Z = this.zs[0]; + return new ECPoint.F2m(curve, X, L.add(Z), new ECFieldElement[]{ Z }, withCompression); + } default: - return new ECPoint.F2m(curve, X, Y.add(X), withCompression); + { + throw new UnsupportedOperationException("unsupported coordinate system"); + } } } } |