Example usage for org.bouncycastle.math.ec ECPoint isInfinity

List of usage examples for org.bouncycastle.math.ec ECPoint isInfinity

Introduction

In this page you can find the example usage for org.bouncycastle.math.ec ECPoint isInfinity.

Prototype

public boolean isInfinity() 

Source Link

Usage

From source file:com.bitsofproof.supernode.api.ExtendedKey.java

License:Apache License

private ExtendedKey generateKey(int sequence) throws ValidationException {
    try {/*from   ww  w  .  j  ava  2s .  c o m*/
        if ((sequence & 0x80000000) != 0 && master.getPrivate() == null) {
            throw new ValidationException("need private key for private generation");
        }
        Mac mac = Mac.getInstance("HmacSHA512", "BC");
        SecretKey key = new SecretKeySpec(chainCode, "HmacSHA512");
        mac.init(key);

        byte[] extended;
        byte[] pub = master.getPublic();
        if ((sequence & 0x80000000) == 0) {
            extended = new byte[pub.length + 4];
            System.arraycopy(pub, 0, extended, 0, pub.length);
            extended[pub.length] = (byte) ((sequence >>> 24) & 0xff);
            extended[pub.length + 1] = (byte) ((sequence >>> 16) & 0xff);
            extended[pub.length + 2] = (byte) ((sequence >>> 8) & 0xff);
            extended[pub.length + 3] = (byte) (sequence & 0xff);
        } else {
            byte[] priv = master.getPrivate();
            extended = new byte[priv.length + 5];
            System.arraycopy(priv, 0, extended, 1, priv.length);
            extended[priv.length + 1] = (byte) ((sequence >>> 24) & 0xff);
            extended[priv.length + 2] = (byte) ((sequence >>> 16) & 0xff);
            extended[priv.length + 3] = (byte) ((sequence >>> 8) & 0xff);
            extended[priv.length + 4] = (byte) (sequence & 0xff);
        }
        byte[] lr = mac.doFinal(extended);
        byte[] l = Arrays.copyOfRange(lr, 0, 32);
        byte[] r = Arrays.copyOfRange(lr, 32, 64);

        BigInteger m = new BigInteger(1, l);
        if (m.compareTo(curve.getN()) >= 0) {
            throw new ValidationException("This is rather unlikely, but it did just happen");
        }
        if (master.getPrivate() != null) {
            BigInteger k = m.add(new BigInteger(1, master.getPrivate())).mod(curve.getN());
            if (k.equals(BigInteger.ZERO)) {
                throw new ValidationException("This is rather unlikely, but it did just happen");
            }
            return new ExtendedKey(new ECKeyPair(k, true), r, depth, parent, sequence);
        } else {
            ECPoint q = curve.getG().multiply(m).add(curve.getCurve().decodePoint(pub));
            if (q.isInfinity()) {
                throw new ValidationException("This is rather unlikely, but it did just happen");
            }
            pub = new ECPoint.Fp(curve.getCurve(), q.getX(), q.getY(), true).getEncoded();
            return new ExtendedKey(new ECPublicKey(pub, true), r, depth, parent, sequence);
        }
    } catch (NoSuchAlgorithmException e) {
        throw new ValidationException(e);
    } catch (NoSuchProviderException e) {
        throw new ValidationException(e);
    } catch (InvalidKeyException e) {
        throw new ValidationException(e);
    }
}

From source file:com.bitsofproof.supernode.common.ExtendedKey.java

License:Apache License

private ExtendedKey generateKey(int sequence) throws ValidationException {
    try {//w  ww.j a  v a 2  s.  c  om
        if ((sequence & 0x80000000) != 0 && master.getPrivate() == null) {
            throw new ValidationException("need private key for private generation");
        }
        Mac mac = Mac.getInstance("HmacSHA512", "BC");
        SecretKey key = new SecretKeySpec(chainCode, "HmacSHA512");
        mac.init(key);

        byte[] extended;
        byte[] pub = master.getPublic();
        if ((sequence & 0x80000000) == 0) {
            extended = new byte[pub.length + 4];
            System.arraycopy(pub, 0, extended, 0, pub.length);
            extended[pub.length] = (byte) ((sequence >>> 24) & 0xff);
            extended[pub.length + 1] = (byte) ((sequence >>> 16) & 0xff);
            extended[pub.length + 2] = (byte) ((sequence >>> 8) & 0xff);
            extended[pub.length + 3] = (byte) (sequence & 0xff);
        } else {
            byte[] priv = master.getPrivate();
            extended = new byte[priv.length + 5];
            System.arraycopy(priv, 0, extended, 1, priv.length);
            extended[priv.length + 1] = (byte) ((sequence >>> 24) & 0xff);
            extended[priv.length + 2] = (byte) ((sequence >>> 16) & 0xff);
            extended[priv.length + 3] = (byte) ((sequence >>> 8) & 0xff);
            extended[priv.length + 4] = (byte) (sequence & 0xff);
        }
        byte[] lr = mac.doFinal(extended);
        byte[] l = Arrays.copyOfRange(lr, 0, 32);
        byte[] r = Arrays.copyOfRange(lr, 32, 64);

        BigInteger m = new BigInteger(1, l);
        if (m.compareTo(curve.getN()) >= 0 || m.compareTo(BigInteger.ZERO) == 0) {
            throw new ValidationException("This is rather unlikely, but it did just happen");
        }
        if (master.getPrivate() != null) {
            BigInteger k = m.add(new BigInteger(1, master.getPrivate())).mod(curve.getN());
            if (k.compareTo(BigInteger.ZERO) == 0) {
                throw new ValidationException("This is rather unlikely, but it did just happen");
            }
            return new ExtendedKey(new ECKeyPair(k, true), r, depth, parent, sequence);
        } else {
            ECPoint q = curve.getG().multiply(m).add(curve.getCurve().decodePoint(pub));
            if (q.isInfinity()) {
                throw new ValidationException("This is rather unlikely, but it did just happen");
            }
            pub = q.getEncoded(true);
            return new ExtendedKey(new ECPublicKey(pub, true), r, depth, parent, sequence);
        }
    } catch (NoSuchAlgorithmException e) {
        throw new ValidationException(e);
    } catch (NoSuchProviderException e) {
        throw new ValidationException(e);
    } catch (InvalidKeyException e) {
        throw new ValidationException(e);
    }
}

From source file:com.github.horrorho.inflatabledonkey.crypto.ec.ECCurvePoint.java

License:Open Source License

public byte[] agreement(BigInteger d) {
    // TODO thread safety of ECPoint unclear.
    synchronized (lock) {
        ECPoint P = Q.multiply(d).normalize();
        if (P.isInfinity()) {
            throw new IllegalStateException("invalid EDCH: infinity");
        }//ww  w .  j  a v  a2  s . co  m

        return P.getAffineXCoord().getEncoded();
    }
}

From source file:dorkbox.util.serialization.EccPrivateKeySerializer.java

License:Apache License

static void serializeECPoint(ECPoint point, Output output) throws KryoException {
    if (point.isInfinity()) {
        return;//from   w  w w .ja v a2 s. c  o m
    }

    ECPoint normed = point.normalize();

    byte[] X = normed.getXCoord().getEncoded();
    byte[] Y = normed.getYCoord().getEncoded();

    int length = 1 + X.length + Y.length;
    output.writeInt(length, true);

    output.write(0x04);
    output.write(X);
    output.write(Y);
}

From source file:org.hyperledger.common.MasterPublicKey.java

License:Apache License

private MasterPublicKey generateKey(int sequence) throws HyperLedgerException {
    try {/*from  ww w . j  av a2s.  co m*/
        if ((sequence & 0x80000000) != 0) {
            throw new HyperLedgerException("need private key for hardened generation");
        }
        Mac mac = Mac.getInstance("HmacSHA512", "BC");
        SecretKey key = new SecretKeySpec(chainCode, "HmacSHA512");
        mac.init(key);

        byte[] extended;
        byte[] pub = master.toByteArray();
        extended = new byte[pub.length + 4];
        System.arraycopy(pub, 0, extended, 0, pub.length);
        extended[pub.length] = (byte) ((sequence >>> 24) & 0xff);
        extended[pub.length + 1] = (byte) ((sequence >>> 16) & 0xff);
        extended[pub.length + 2] = (byte) ((sequence >>> 8) & 0xff);
        extended[pub.length + 3] = (byte) (sequence & 0xff);
        byte[] lr = mac.doFinal(extended);
        byte[] l = Arrays.copyOfRange(lr, 0, 32);
        byte[] r = Arrays.copyOfRange(lr, 32, 64);

        BigInteger m = new BigInteger(1, l);
        if (m.compareTo(curve.getN()) >= 0 || m.compareTo(BigInteger.ZERO) == 0) {
            throw new HyperLedgerException("This is rather unlikely, but it did just happen");
        }
        ECPoint q = curve.getG().multiply(m).add(curve.getCurve().decodePoint(pub));
        if (q.isInfinity()) {
            throw new HyperLedgerException("This is rather unlikely, but it did just happen");
        }
        pub = q.getEncoded(true);
        return new MasterPublicKey(new PublicKey(pub, true), r, depth, parent, sequence);
    } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException e) {
        throw new HyperLedgerException(e);
    }
}

From source file:org.hyperledger.common.PublicKey.java

License:Apache License

public PublicKey offsetKey(BigInteger offset) throws HyperLedgerException {
    boolean invert = false;
    if (offset.compareTo(BigInteger.ZERO) < 0) {
        invert = true;//from   ww w . java 2s  .  co  m
        offset = offset.abs();
    }
    ECPoint oG = curve.getG().multiply(offset);
    if (invert) {
        oG = oG.negate();
    }
    ECPoint q = oG.add(curve.getCurve().decodePoint(pub));
    if (q.isInfinity()) {
        throw new HyperLedgerException("This is rather unlikely, but it did just happen");
    }
    return new PublicKey(q.getEncoded(compressed), compressed);
}

From source file:us.eharning.atomun.keygen.internal.spi.bip0032.BouncyCastleBIP0032NodeProcessor.java

License:Apache License

/**
 * Derives a BIP0032 node given the singular sequence value.
 *
 * @param node/*  www.  j a  va  2 s  . c o m*/
 *         base node to derive from.
 * @param sequence
 *         value to use for derivation.
 *
 * @return BIP0032 node derived using the necessary algorithms per BIP0032 specification.
 *
 * @throws ValidationException
 *         it is impossible to derive a key due to missing private bits,
 *         or the resultant key is an invalid EC key (unlikely).
 */
@SuppressWarnings("checkstyle:localvariablename")
@Override
public BIP0032Node deriveNode(BIP0032Node node, int sequence) throws ValidationException {
    NodeSequence nodeSequence = new NodeSequence(node, sequence);
    BIP0032Node cached = NODE_CACHE.getIfPresent(nodeSequence);
    if (null != cached) {
        return cached;
    }
    final ECKey master = node.getMaster();
    try {
        if ((sequence & 0x80000000) != 0 && master.exportPrivate() == null) {
            throw new ValidationException("Need private key for private generation");
        }
        byte[] lr = deriveI(node, sequence);

        byte[] l = java.util.Arrays.copyOfRange(lr, 0, 32);
        byte[] r = java.util.Arrays.copyOfRange(lr, 32, 64);
        BigInteger m = new BigInteger(1, l);
        if (m.compareTo(curve.getN()) >= 0 || m.compareTo(BigInteger.ZERO) == 0) {
            throw new ValidationException("Invalid chain value generated");
        }
        if (master.hasPrivate()) {
            byte[] priv = master.exportPrivate();
            assert (null != priv);
            BigInteger k = m.add(new BigInteger(1, priv)).mod(curve.getN());
            if (k.compareTo(BigInteger.ZERO) == 0) {
                throw new ValidationException("Invalid private node generated");
            }
            cached = new BIP0032Node(ECKeyFactory.getInstance().fromSecretExponent(k, true), r,
                    node.getDepth() + 1, node.getFingerPrint(), sequence);
            NODE_CACHE.put(nodeSequence, cached);
            return cached;
        } else {
            byte[] pub = master.exportPublic();
            ECPoint q = curve.getG().multiply(m).add(curve.getCurve().decodePoint(pub));
            if (q.isInfinity()) {
                throw new ValidationException("Invalid public node generated");
            }
            pub = q.getEncoded(true);
            cached = new BIP0032Node(ECKeyFactory.getInstance().fromEncodedPublicKey(pub, true), r,
                    node.getDepth() + 1, node.getFingerPrint(), sequence);
            NODE_CACHE.put(nodeSequence, cached);
            return cached;
        }
    } catch (NoSuchAlgorithmException | InvalidKeyException e) {
        throw new ValidationException(e);
    }
}