diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-28 12:43:26 +0400 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2014-01-28 12:43:26 +0400 |
commit | f4a09abf9da1875c273b82abb1e4e368c7d7ffda (patch) | |
tree | a72b0faf45fa2454e74336e1da6e8d800c1804e3 /core/src/main/java/org/bouncycastle/asn1 | |
parent | 646925daf65dc68f84a337bf1bda7ee0a116465e (diff) |
Refactor to work more clearly wit field elements
Use createPoint to support e.g. lambda-projective coordinates
Diffstat (limited to 'core/src/main/java/org/bouncycastle/asn1')
-rw-r--r-- | core/src/main/java/org/bouncycastle/asn1/ua/DSTU4145PointEncoder.java | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/core/src/main/java/org/bouncycastle/asn1/ua/DSTU4145PointEncoder.java b/core/src/main/java/org/bouncycastle/asn1/ua/DSTU4145PointEncoder.java index 8c16620f..08321494 100644 --- a/core/src/main/java/org/bouncycastle/asn1/ua/DSTU4145PointEncoder.java +++ b/core/src/main/java/org/bouncycastle/asn1/ua/DSTU4145PointEncoder.java @@ -7,7 +7,6 @@ import org.bouncycastle.math.ec.ECConstants; import org.bouncycastle.math.ec.ECCurve; import org.bouncycastle.math.ec.ECFieldElement; import org.bouncycastle.math.ec.ECPoint; -import org.bouncycastle.util.Arrays; /** * DSTU4145 encodes points somewhat differently than X9.62 @@ -15,21 +14,21 @@ import org.bouncycastle.util.Arrays; */ public abstract class DSTU4145PointEncoder { - private static BigInteger trace(ECFieldElement fe) + private static ECFieldElement trace(ECFieldElement fe) { ECFieldElement t = fe; - for (int i = 0; i < fe.getFieldSize() - 1; i++) + for (int i = 1; i < fe.getFieldSize(); ++i) { t = t.square().add(fe); } - return t.toBigInteger(); + return t; } /** * Solves a quadratic equation <code>z<sup>2</sup> + z = beta</code>(X9.62 * D.1.6) The other solution is <code>z + 1</code>. * - * @param beta The value to solve the qradratic equation for. + * @param beta The value to solve the quadratic equation for. * @return the solution for <code>z<sup>2</sup> + z = beta</code> or * <code>null</code> if no solution exists. */ @@ -91,8 +90,8 @@ public abstract class DSTU4145PointEncoder if (!x.isZero()) { - ECFieldElement y = Q.getAffineYCoord().divide(x); - if (trace(y).equals(ECConstants.ONE)) + ECFieldElement z = Q.getAffineYCoord().divide(x); + if (trace(z).isOne()) { bytes[bytes.length - 1] |= 0x01; } @@ -118,26 +117,22 @@ public abstract class DSTU4145PointEncoder return curve.decodePoint(bp_enc);*/ - BigInteger k = BigInteger.valueOf(bytes[bytes.length - 1] & 0x1); - if (!trace(curve.fromBigInteger(new BigInteger(1, bytes))).equals(curve.getA().toBigInteger())) + ECFieldElement k = curve.fromBigInteger(BigInteger.valueOf(bytes[bytes.length - 1] & 0x1)); + + ECFieldElement xp = curve.fromBigInteger(new BigInteger(1, bytes)); + if (!trace(xp).equals(curve.getA())) { - bytes = Arrays.clone(bytes); - bytes[bytes.length - 1] ^= 0x01; + xp = xp.addOne(); } - ECFieldElement xp = curve.fromBigInteger(new BigInteger(1, bytes)); - ECFieldElement yp = null; + + ECFieldElement yp; if (xp.isZero()) { - yp = (ECFieldElement.F2m)curve.getB(); - for (int i = 0; i < curve.getFieldSize() - 1; i++) - { - yp = yp.square(); - } + yp = curve.getB().sqrt(); } else { - ECFieldElement beta = xp.add(curve.getA()).add( - curve.getB().multiply(xp.square().invert())); + ECFieldElement beta = xp.square().invert().multiply(curve.getB()).add(curve.getA()).add(xp); ECFieldElement z = solveQuadraticEquation(curve, beta); if (z == null) { @@ -150,7 +145,6 @@ public abstract class DSTU4145PointEncoder yp = xp.multiply(z); } - return new ECPoint.F2m(curve, xp, yp); + return curve.createPoint(xp.toBigInteger(), yp.toBigInteger()); } - } |