Search in sources :

Example 21 with ECFieldElement

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

the class DiscoverEndomorphisms method findNonTrivialOrder3FieldElements.

private static ECFieldElement[] findNonTrivialOrder3FieldElements(ECCurve c) {
    BigInteger q = c.getField().getCharacteristic();
    BigInteger e = q.divide(ECConstants.THREE);
    // Search for a random value that generates a non-trival cube root of 1
    SecureRandom random = new SecureRandom();
    BigInteger b;
    do {
        BigInteger r = BigIntegers.createRandomInRange(ECConstants.TWO, q.subtract(ECConstants.TWO), random);
        b = r.modPow(e, q);
    } while (b.equals(ECConstants.ONE));
    ECFieldElement beta = c.fromBigInteger(b);
    return new ECFieldElement[] { beta, beta.square() };
}
Also used : BigInteger(java.math.BigInteger) SecureRandom(java.security.SecureRandom) ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement)

Example 22 with ECFieldElement

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

the class TraceOptimizer method implPrintNonZeroTraceBits.

public static void implPrintNonZeroTraceBits(X9ECParameters x9) {
    ECCurve c = x9.getCurve();
    int m = c.getFieldSize();
    ArrayList nonZeroTraceBits = new ArrayList();
    /*
     * Determine which of the bits contribute to the trace.
     *
     * See section 3.6.2 of "Guide to Elliptic Curve Cryptography" (Hankerson, Menezes, Vanstone)
     */
    {
        for (int i = 0; i < m; ++i) {
            if (0 == (i & 1) && 0 != i) {
                if (nonZeroTraceBits.contains(Integers.valueOf(i >>> 1))) {
                    nonZeroTraceBits.add(Integers.valueOf(i));
                    System.out.print(" " + i);
                }
            } else {
                BigInteger zi = ONE.shiftLeft(i);
                ECFieldElement fe = c.fromBigInteger(zi);
                int tr = calculateTrace(fe);
                if (tr != 0) {
                    nonZeroTraceBits.add(Integers.valueOf(i));
                    System.out.print(" " + i);
                }
            }
        }
        System.out.println();
    }
    /*
     *  Check calculation based on the non-zero-trace bits against explicit calculation, for random field elements
     */
    {
        for (int i = 0; i < 1000; ++i) {
            BigInteger x = new BigInteger(m, R);
            ECFieldElement fe = c.fromBigInteger(x);
            int check = calculateTrace(fe);
            int tr = 0;
            for (int j = 0; j < nonZeroTraceBits.size(); ++j) {
                int bit = ((Integer) nonZeroTraceBits.get(j)).intValue();
                if (x.testBit(bit)) {
                    tr ^= 1;
                }
            }
            if (check != tr) {
                throw new IllegalStateException("Optimized-trace sanity check failed");
            }
        }
    }
}
Also used : ECCurve(com.github.zhenwei.core.math.ec.ECCurve) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement)

Example 23 with ECFieldElement

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

the class SecT131R2Point 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 24 with ECFieldElement

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

the class SecT131R2Point 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 SecT131R2Point(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 SecT131R2Point(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 SecT131R2Point(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 25 with ECFieldElement

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

the class SecT131R2Point 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 SecT131R2Point(curve, X, L.add(Z), new ECFieldElement[] { Z });
}
Also used : ECFieldElement(com.github.zhenwei.core.math.ec.ECFieldElement)

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