📄 ecpoint.java
字号:
{ return this; } // Add -b return add(b.negate()); } public ECPoint negate() { return new ECPoint.Fp(curve, this.x, this.y.negate(), this.withCompression); } // TODO Uncomment this to enable WNAF algorithm for Fp point multiplication// /**// * Sets the default <code>ECMultiplier</code>, unless already set. // */// synchronized void assertECMultiplier()// {// if (this.multiplier == null)// {// this.multiplier = new WNafMultiplier();// }// } } /** * Elliptic curve points over F2m */ public static class F2m extends ECPoint { /** * @param curve base curve * @param x x point * @param y y point */ public F2m(ECCurve curve, ECFieldElement x, ECFieldElement y) { this(curve, x, y, false); } /** * @param curve base curve * @param x x point * @param y y point * @param withCompression true if encode with point compression. */ public F2m(ECCurve curve, ECFieldElement x, ECFieldElement y, boolean withCompression) { super(curve, x, y); if ((x != null && y == null) || (x == null && y != null)) { throw new IllegalArgumentException("Exactly one of the field elements is null"); } if (x != null) { // Check if x and y are elements of the same field ECFieldElement.F2m.checkFieldElements(this.x, this.y); // Check if x and a are elements of the same field if (curve != null) { ECFieldElement.F2m.checkFieldElements(this.x, this.curve.getA()); } } this.withCompression = withCompression; } /** * @deprecated use ECCurve.getInfinity() * Constructor for point at infinity */ public F2m(ECCurve curve) { super(curve, null, null); } /* (non-Javadoc) * @see org.bouncycastle.math.ec.ECPoint#getEncoded() */ public byte[] getEncoded() { if (this.isInfinity()) { return new byte[1]; } int byteCount = converter.getByteLength(this.x); byte[] X = converter.integerToBytes(this.getX().toBigInteger(), byteCount); byte[] PO; if (withCompression) { // See X9.62 4.3.6 and 4.2.2 PO = new byte[byteCount + 1]; PO[0] = 0x02; // X9.62 4.2.2 and 4.3.6: // if x = 0 then ypTilde := 0, else ypTilde is the rightmost // bit of y * x^(-1) // if ypTilde = 0, then PC := 02, else PC := 03 // Note: PC === PO[0] if (!(this.getX().toBigInteger().equals(ECConstants.ZERO))) { if (this.getY().multiply(this.getX().invert()) .toBigInteger().testBit(0)) { // ypTilde = 1, hence PC = 03 PO[0] = 0x03; } } System.arraycopy(X, 0, PO, 1, byteCount); } else { byte[] Y = converter.integerToBytes(this.getY().toBigInteger(), byteCount); PO = new byte[byteCount + byteCount + 1]; PO[0] = 0x04; System.arraycopy(X, 0, PO, 1, byteCount); System.arraycopy(Y, 0, PO, byteCount + 1, byteCount); } return PO; } /** * Check, if two <code>ECPoint</code>s can be added or subtracted. * @param a The first <code>ECPoint</code> to check. * @param b The second <code>ECPoint</code> to check. * @throws IllegalArgumentException if <code>a</code> and <code>b</code> * cannot be added. */ private static void checkPoints(ECPoint a, ECPoint b) { // Check, if points are on the same curve if (!(a.curve.equals(b.curve))) { throw new IllegalArgumentException("Only points on the same " + "curve can be added or subtracted"); }// ECFieldElement.F2m.checkFieldElements(a.x, b.x); } /* (non-Javadoc) * @see org.bouncycastle.math.ec.ECPoint#add(org.bouncycastle.math.ec.ECPoint) */ public ECPoint add(ECPoint b) { checkPoints(this, b); return addSimple((ECPoint.F2m)b); } /** * Adds another <code>ECPoints.F2m</code> to <code>this</code> without * checking if both points are on the same curve. Used by multiplication * algorithms, because there all points are a multiple of the same point * and hence the checks can be omitted. * @param b The other <code>ECPoints.F2m</code> to add to * <code>this</code>. * @return <code>this + b</code> */ public ECPoint.F2m addSimple(ECPoint.F2m b) { ECPoint.F2m other = b; if (this.isInfinity()) { return other; } if (other.isInfinity()) { return this; } ECFieldElement.F2m x2 = (ECFieldElement.F2m)other.getX(); ECFieldElement.F2m y2 = (ECFieldElement.F2m)other.getY(); // Check if other = this or other = -this if (this.x.equals(x2)) { if (this.y.equals(y2)) { // this = other, i.e. this must be doubled return (ECPoint.F2m)this.twice(); } // this = -other, i.e. the result is the point at infinity return (ECPoint.F2m)this.curve.getInfinity(); } ECFieldElement.F2m lambda = (ECFieldElement.F2m)(this.y.add(y2)).divide(this.x.add(x2)); ECFieldElement.F2m x3 = (ECFieldElement.F2m)lambda.square().add(lambda).add(this.x).add(x2).add(this.curve.getA()); ECFieldElement.F2m y3 = (ECFieldElement.F2m)lambda.multiply(this.x.add(x3)).add(x3).add(this.y); return new ECPoint.F2m(curve, x3, y3, withCompression); } /* (non-Javadoc) * @see org.bouncycastle.math.ec.ECPoint#subtract(org.bouncycastle.math.ec.ECPoint) */ public ECPoint subtract(ECPoint b) { checkPoints(this, b); return subtractSimple((ECPoint.F2m)b); } /** * Subtracts another <code>ECPoints.F2m</code> from <code>this</code> * without checking if both points are on the same curve. Used by * multiplication algorithms, because there all points are a multiple * of the same point and hence the checks can be omitted. * @param b The other <code>ECPoints.F2m</code> to subtract from * <code>this</code>. * @return <code>this - b</code> */ public ECPoint.F2m subtractSimple(ECPoint.F2m b) { if (b.isInfinity()) { return this; } // Add -b return addSimple((ECPoint.F2m)b.negate()); } /* (non-Javadoc) * @see org.bouncycastle.math.ec.ECPoint#twice() */ public ECPoint twice() { if (this.isInfinity()) { // Twice identity element (point at infinity) is identity return this; } if (this.x.toBigInteger().signum() == 0) { // if x1 == 0, then (x1, y1) == (x1, x1 + y1) // and hence this = -this and thus 2(x1, y1) == infinity return this.curve.getInfinity(); } ECFieldElement.F2m lambda = (ECFieldElement.F2m)this.x.add(this.y.divide(this.x)); ECFieldElement.F2m x3 = (ECFieldElement.F2m)lambda.square().add(lambda). add(this.curve.getA()); ECFieldElement ONE = this.curve.fromBigInteger(ECConstants.ONE); ECFieldElement.F2m y3 = (ECFieldElement.F2m)this.x.square().add( x3.multiply(lambda.add(ONE))); return new ECPoint.F2m(this.curve, x3, y3, withCompression); } public ECPoint negate() { return new ECPoint.F2m(curve, this.getX(), this.getY().add(this.getX()), withCompression); } // TODO Uncomment this to enable WNAF/WTNAF F2m point multiplication// /**// * Sets the appropriate <code>ECMultiplier</code>, unless already set. // */// synchronized void assertECMultiplier()// {// if (this.multiplier == null)// {// if (((ECCurve.F2m)(this.curve)).isKoblitz())// {// this.multiplier = new WTauNafMultiplier();// }// else// {// this.multiplier = new WNafMultiplier();// }// }// } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -