Search in sources :

Example 61 with ECFieldElement

use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.

the class SecT113R2Point method negate.

public ECPoint negate() {
    if (this.isInfinity()) {
        return this;
    }
    ECFieldElement X = this.x;
    if (X.isZero()) {
        return this;
    }
    // L is actually Lambda (X + Y/X) here
    ECFieldElement L = this.y, Z = this.zs[0];
    return new SecT113R2Point(curve, X, L.add(Z), new ECFieldElement[] { Z });
}
Also used : ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement)

Example 62 with ECFieldElement

use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.

the class SecT131R1Point method getYCoord.

public ECFieldElement getYCoord() {
    ECFieldElement X = x, L = y;
    if (this.isInfinity() || X.isZero()) {
        return L;
    }
    // Y is actually Lambda (X + Y/X) here; convert to affine value on the fly
    ECFieldElement Y = L.add(X).multiply(X);
    ECFieldElement Z = zs[0];
    if (!Z.isOne()) {
        Y = Y.divide(Z);
    }
    return Y;
}
Also used : ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement)

Example 63 with ECFieldElement

use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.

the class SecT131R1Point method twicePlus.

public ECPoint twicePlus(ECPoint b) {
    if (this.isInfinity()) {
        return b;
    }
    if (b.isInfinity()) {
        return twice();
    }
    ECCurve curve = this.getCurve();
    ECFieldElement X1 = this.x;
    if (X1.isZero()) {
        // A point with X == 0 is its own additive inverse
        return b;
    }
    ECFieldElement X2 = b.getRawXCoord(), Z2 = b.getZCoord(0);
    if (X2.isZero() || !Z2.isOne()) {
        return twice().add(b);
    }
    ECFieldElement L1 = this.y, Z1 = this.zs[0];
    ECFieldElement L2 = b.getRawYCoord();
    ECFieldElement X1Sq = X1.square();
    ECFieldElement L1Sq = L1.square();
    ECFieldElement Z1Sq = Z1.square();
    ECFieldElement L1Z1 = L1.multiply(Z1);
    ECFieldElement T = curve.getA().multiply(Z1Sq).add(L1Sq).add(L1Z1);
    ECFieldElement L2plus1 = L2.addOne();
    ECFieldElement A = curve.getA().add(L2plus1).multiply(Z1Sq).add(L1Sq).multiplyPlusProduct(T, X1Sq, Z1Sq);
    ECFieldElement X2Z1Sq = X2.multiply(Z1Sq);
    ECFieldElement B = X2Z1Sq.add(T).square();
    if (B.isZero()) {
        if (A.isZero()) {
            return b.twice();
        }
        return curve.getInfinity();
    }
    if (A.isZero()) {
        return new SecT131R1Point(curve, A, curve.getB().sqrt());
    }
    ECFieldElement X3 = A.square().multiply(X2Z1Sq);
    ECFieldElement Z3 = A.multiply(B).multiply(Z1Sq);
    ECFieldElement L3 = A.add(B).square().multiplyPlusProduct(T, L2plus1, Z3);
    return new SecT131R1Point(curve, X3, L3, new ECFieldElement[] { Z3 });
}
Also used : ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement)

Example 64 with ECFieldElement

use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.

the class SecT131R1Point method add.

public ECPoint add(ECPoint b) {
    if (this.isInfinity()) {
        return b;
    }
    if (b.isInfinity()) {
        return this;
    }
    ECCurve curve = this.getCurve();
    ECFieldElement X1 = this.x;
    ECFieldElement X2 = b.getRawXCoord();
    if (X1.isZero()) {
        if (X2.isZero()) {
            return curve.getInfinity();
        }
        return b.add(this);
    }
    ECFieldElement L1 = this.y, Z1 = this.zs[0];
    ECFieldElement L2 = b.getRawYCoord(), Z2 = b.getZCoord(0);
    boolean Z1IsOne = Z1.isOne();
    ECFieldElement U2 = X2, S2 = L2;
    if (!Z1IsOne) {
        U2 = U2.multiply(Z1);
        S2 = S2.multiply(Z1);
    }
    boolean Z2IsOne = Z2.isOne();
    ECFieldElement U1 = X1, S1 = L1;
    if (!Z2IsOne) {
        U1 = U1.multiply(Z2);
        S1 = S1.multiply(Z2);
    }
    ECFieldElement A = S1.add(S2);
    ECFieldElement B = U1.add(U2);
    if (B.isZero()) {
        if (A.isZero()) {
            return twice();
        }
        return curve.getInfinity();
    }
    ECFieldElement X3, L3, Z3;
    if (X2.isZero()) {
        // TODO This can probably be optimized quite a bit
        ECPoint p = this.normalize();
        X1 = p.getXCoord();
        ECFieldElement Y1 = p.getYCoord();
        ECFieldElement Y2 = L2;
        ECFieldElement L = Y1.add(Y2).divide(X1);
        X3 = L.square().add(L).add(X1).add(curve.getA());
        if (X3.isZero()) {
            return new SecT131R1Point(curve, X3, curve.getB().sqrt());
        }
        ECFieldElement Y3 = L.multiply(X1.add(X3)).add(X3).add(Y1);
        L3 = Y3.divide(X3).add(X3);
        Z3 = curve.fromBigInteger(ECConstants.ONE);
    } else {
        B = B.square();
        ECFieldElement AU1 = A.multiply(U1);
        ECFieldElement AU2 = A.multiply(U2);
        X3 = AU1.multiply(AU2);
        if (X3.isZero()) {
            return new SecT131R1Point(curve, X3, curve.getB().sqrt());
        }
        ECFieldElement ABZ2 = A.multiply(B);
        if (!Z2IsOne) {
            ABZ2 = ABZ2.multiply(Z2);
        }
        L3 = AU2.add(B).squarePlusProduct(ABZ2, L1.add(Z1));
        Z3 = ABZ2;
        if (!Z1IsOne) {
            Z3 = Z3.multiply(Z1);
        }
    }
    return new SecT131R1Point(curve, X3, L3, new ECFieldElement[] { Z3 });
}
Also used : ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Example 65 with ECFieldElement

use of com.github.zhenwei.core.math.ec.ECFieldElement in project LinLong-Java by zhenwei1108.

the class SecP224R1Point method add.

public ECPoint add(ECPoint b) {
    if (this.isInfinity()) {
        return b;
    }
    if (b.isInfinity()) {
        return this;
    }
    if (this == b) {
        return twice();
    }
    ECCurve curve = this.getCurve();
    SecP224R1FieldElement X1 = (SecP224R1FieldElement) this.x, Y1 = (SecP224R1FieldElement) this.y;
    SecP224R1FieldElement X2 = (SecP224R1FieldElement) b.getXCoord(), Y2 = (SecP224R1FieldElement) b.getYCoord();
    SecP224R1FieldElement Z1 = (SecP224R1FieldElement) this.zs[0];
    SecP224R1FieldElement Z2 = (SecP224R1FieldElement) b.getZCoord(0);
    int c;
    int[] tt1 = Nat224.createExt();
    int[] t2 = Nat224.create();
    int[] t3 = Nat224.create();
    int[] t4 = Nat224.create();
    boolean Z1IsOne = Z1.isOne();
    int[] U2, S2;
    if (Z1IsOne) {
        U2 = X2.x;
        S2 = Y2.x;
    } else {
        S2 = t3;
        SecP224R1Field.square(Z1.x, S2);
        U2 = t2;
        SecP224R1Field.multiply(S2, X2.x, U2);
        SecP224R1Field.multiply(S2, Z1.x, S2);
        SecP224R1Field.multiply(S2, Y2.x, S2);
    }
    boolean Z2IsOne = Z2.isOne();
    int[] U1, S1;
    if (Z2IsOne) {
        U1 = X1.x;
        S1 = Y1.x;
    } else {
        S1 = t4;
        SecP224R1Field.square(Z2.x, S1);
        U1 = tt1;
        SecP224R1Field.multiply(S1, X1.x, U1);
        SecP224R1Field.multiply(S1, Z2.x, S1);
        SecP224R1Field.multiply(S1, Y1.x, S1);
    }
    int[] H = Nat224.create();
    SecP224R1Field.subtract(U1, U2, H);
    int[] R = t2;
    SecP224R1Field.subtract(S1, S2, R);
    // Check if b == this or b == -this
    if (Nat224.isZero(H)) {
        if (Nat224.isZero(R)) {
            // this == b, i.e. this must be doubled
            return this.twice();
        }
        // this == -b, i.e. the result is the point at infinity
        return curve.getInfinity();
    }
    int[] HSquared = t3;
    SecP224R1Field.square(H, HSquared);
    int[] G = Nat224.create();
    SecP224R1Field.multiply(HSquared, H, G);
    int[] V = t3;
    SecP224R1Field.multiply(HSquared, U1, V);
    SecP224R1Field.negate(G, G);
    Nat224.mul(S1, G, tt1);
    c = Nat224.addBothTo(V, V, G);
    SecP224R1Field.reduce32(c, G);
    SecP224R1FieldElement X3 = new SecP224R1FieldElement(t4);
    SecP224R1Field.square(R, X3.x);
    SecP224R1Field.subtract(X3.x, G, X3.x);
    SecP224R1FieldElement Y3 = new SecP224R1FieldElement(G);
    SecP224R1Field.subtract(V, X3.x, Y3.x);
    SecP224R1Field.multiplyAddToExt(Y3.x, R, tt1);
    SecP224R1Field.reduce(tt1, Y3.x);
    SecP224R1FieldElement Z3 = new SecP224R1FieldElement(H);
    if (!Z1IsOne) {
        SecP224R1Field.multiply(Z3.x, Z1.x, Z3.x);
    }
    if (!Z2IsOne) {
        SecP224R1Field.multiply(Z3.x, Z2.x, Z3.x);
    }
    ECFieldElement[] zs = new ECFieldElement[] { Z3 };
    return new SecP224R1Point(curve, X3, Y3, zs);
}
Also used : ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement) ECPoint(com.github.zhenwei.core.math.ec.ECPoint)

Aggregations

ECFieldElement (com.github.zhenwei.core.math.ec.ECFieldElement)133 ECCurve (com.github.zhenwei.core.math.ec.ECCurve)71 ECPoint (com.github.zhenwei.core.math.ec.ECPoint)36 BigInteger (java.math.BigInteger)7 ECDomainParameters (com.github.zhenwei.core.crypto.params.ECDomainParameters)3 ECPublicKeyParameters (com.github.zhenwei.core.crypto.params.ECPublicKeyParameters)2 ECPrivateKeyParameters (com.github.zhenwei.core.crypto.params.ECPrivateKeyParameters)1 ECMultiplier (com.github.zhenwei.core.math.ec.ECMultiplier)1 SecureRandom (java.security.SecureRandom)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1