Java Code Examples for org.spongycastle.math.ec.ECPoint#getEncoded()

The following examples show how to use org.spongycastle.math.ec.ECPoint#getEncoded() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tx.java    From bitherj with Apache License 2.0 6 votes vote down vote up
private byte[] getSignPubs(byte[] messageHash,
                           ECKey.ECDSASignature sig, List<byte[]> pubs) {

    for (int i = 0; i < 4; i++) {
        ECPoint point = ECKey.recoverECPointFromSignature(i, sig, messageHash);
        ECKey ecKeyCompress = new ECKey(null, point.getEncoded(true));
        ECKey ecKeyUnCompress = new ECKey(null, point.getEncoded(false));
        for (int j = 0; j < pubs.size(); j++) {
            if (Arrays.equals(ecKeyCompress.getPubKey(), pubs.get(j))) {
                return ecKeyCompress.getPubKey();

            }
            if (Arrays.equals(ecKeyUnCompress.getPubKey(), pubs.get(j))) {
                return ecKeyUnCompress.getPubKey();

            }
        }
    }
    return null;
}
 
Example 2
Source File: ECKeyPair.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 6 votes vote down vote up
public static ECKeyPair createNew(boolean compressed) {
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(domain,
            secureRandom);
    generator.init(keygenParams);
    AsymmetricCipherKeyPair keypair = generator.generateKeyPair();
    ECPrivateKeyParameters privParams = (ECPrivateKeyParameters) keypair.getPrivate();
    ECPublicKeyParameters pubParams = (ECPublicKeyParameters) keypair.getPublic();
    ECKeyPair k = new ECKeyPair();
    k.priv = privParams.getD();
    k.compressed = compressed;
    ECPoint multiply = CURVE.getG().multiply(k.priv);
    k.pub = multiply.getEncoded(false);
    k.pubComp = multiply.getEncoded(true);
    return k;
}
 
Example 3
Source File: ECKey.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte array into a BigInteger, use <tt>
 * new BigInteger(1, bytes);</tt>
 */
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
    ECPoint point = CURVE.getG().multiply(privKey);
    if (compressed)
        point = compressPoint(point);
    return point.getEncoded();
}
 
Example 4
Source File: Secp256k1.java    From neb.java with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static byte[] PublicFromPrivateKey(byte[] privateKey) {
    BigInteger privKey = new BigInteger(1, privateKey);
    if (privKey.bitLength() > CURVE.getN().bitLength()) {
        privKey = privKey.mod(CURVE.getN());
    }

    ECPoint point = new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
    return point.getEncoded(false);
}
 
Example 5
Source File: ECKeyPair.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 5 votes vote down vote up
public ECKeyPair(BigInteger priv, boolean compressed) {
    this.priv = priv;
    this.compressed = compressed;

    ECPoint multiply = CURVE.getG().multiply(priv);
    this.pub = multiply.getEncoded(false);
    this.pubComp = multiply.getEncoded(true);
}
 
Example 6
Source File: Secp256r1SC.java    From ontology-java-sdk with GNU Lesser General Public License v3.0 4 votes vote down vote up
static byte[] pointSerP(final ECPoint point) {
    return point.getEncoded(true);
}
 
Example 7
Source File: ECKey.java    From ethereumj with MIT License 4 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte array into a BigInteger, use <tt>
 * new BigInteger(1, bytes);</tt>
 */
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
    ECPoint point = CURVE.getG().multiply(privKey);
    return point.getEncoded(compressed);
}
 
Example 8
Source File: ECKey.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte array into a BigInteger, use <tt>
 * new BigInteger(1, bytes);</tt>
 */
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
    ECPoint point = publicPointFromPrivate(privKey);
    return point.getEncoded(compressed);
}
 
Example 9
Source File: ECKey.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte array into a BigInteger, use <tt>
 * new BigInteger(1, bytes);</tt>
 */
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
    ECPoint point = publicPointFromPrivate(privKey);
    return point.getEncoded(compressed);
}
 
Example 10
Source File: Secp256k1SC.java    From dapp-wallet-demo with Apache License 2.0 4 votes vote down vote up
static byte[] pointSerP(final ECPoint point) {
    return point.getEncoded(true);
}
 
Example 11
Source File: ECKey.java    From bitherj with Apache License 2.0 3 votes vote down vote up
/**
 * <p>Given the components of a signature and a selector value, recover and return the public key
 * that generated the signature according to the algorithm in SEC1v2 section 4.1.6.</p>
 * <p/>
 * <p>The recId is an index from 0 to 3 which indicates which of the 4 possible keys is the correct one. Because
 * the key recovery operation yields multiple potential keys, the correct key must either be stored alongside the
 * signature, or you must be willing to try each recId in turn until you find one that outputs the key you are
 * expecting.</p>
 * <p/>
 * <p>If this method returns null it means recovery was not possible and recId should be iterated.</p>
 * <p/>
 * <p>Given the above two points, a correct usage of this method is inside a for loop from 0 to 3, and if the
 * output is null OR a key that is not the one you expect, you try again with the next recId.</p>
 *
 * @param recId      Which possible key to recover.
 * @param sig        the R and S components of the signature, wrapped.
 * @param message    Hash of the data that was signed.
 * @param compressed Whether or not the original pubkey was compressed.
 * @return An ECKey containing only the public part, or null if recovery wasn't possible.
 */
@Nullable
public static ECKey recoverFromSignature(int recId, ECDSASignature sig, byte[] message, boolean compressed) {
    ECPoint q = recoverECPointFromSignature(recId, sig, message);
    if (q != null) {
        return new ECKey((BigInteger) null, (byte[]) q.getEncoded(compressed));
    } else {
        return null;
    }
}
 
Example 12
Source File: ECKey.java    From wkcwallet-java with Apache License 2.0 2 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte
 * array into a BigInteger, use <tt>
 * new BigInteger(1, bytes);</tt>
 *
 * @param privKey
 *            -
 * @param compressed
 *            -
 * @return -
 */
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
    ECPoint point = CURVE.getG().multiply(privKey);
    return point.getEncoded(compressed);
}
 
Example 13
Source File: ECKey.java    From wkcwallet-java with Apache License 2.0 2 votes vote down vote up
/**
 * Compute the encoded X, Y coordinates of a public point.
 *
 * This is the encoded public key without the leading byte.
 *
 * @param pubPoint
 *            a public point
 * @return 64-byte X,Y point pair
 */
public static byte[] pubBytesWithoutFormat(ECPoint pubPoint) {
    final byte[] pubBytes = pubPoint.getEncoded(/* uncompressed */false);
    return Arrays.copyOfRange(pubBytes, 1, pubBytes.length);
}
 
Example 14
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 2 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte array into a BigInteger,
 * use <tt> new BigInteger(1, bytes);</tt>
 *
 * @param privKey -
 * @param compressed -
 * @return -
 */
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean
    compressed) {
  ECPoint point = CURVE.getG().multiply(privKey);
  return point.getEncoded(compressed);
}
 
Example 15
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 2 votes vote down vote up
/**
 * Compute the encoded X, Y coordinates of a public point. <p> This is the encoded public key
 * without the leading byte.
 *
 * @param pubPoint a public point
 * @return 64-byte X,Y point pair
 */
public static byte[] pubBytesWithoutFormat(ECPoint pubPoint) {
  final byte[] pubBytes = pubPoint.getEncoded(/* uncompressed */ false);
  return Arrays.copyOfRange(pubBytes, 1, pubBytes.length);
}
 
Example 16
Source File: ECKey.java    From asf-sdk with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte array into a
 * BigInteger, use <tt>
 * new BigInteger(1, bytes);</tt>
 *
 * @param privKey -
 * @param compressed -
 *
 * @return -
 */
public static byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
  ECPoint point = CURVE.getG()
      .multiply(privKey);
  return point.getEncoded(compressed);
}
 
Example 17
Source File: ECKey.java    From asf-sdk with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Compute the encoded X, Y coordinates of a public point.
 *
 * This is the encoded public key without the leading byte.
 *
 * @param pubPoint a public point
 *
 * @return 64-byte X,Y point pair
 */
public static byte[] pubBytesWithoutFormat(ECPoint pubPoint) {
  byte[] pubBytes = pubPoint.getEncoded(/* uncompressed */ false);
  return Arrays.copyOfRange(pubBytes, 1, pubBytes.length);
}
 
Example 18
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Compute the encoded X, Y coordinates of a public point. <p> This is the encoded public key
 * without the leading byte.
 *
 * @param pubPoint a public point
 * @return 64-byte X,Y point pair
 */
public static byte[] pubBytesWithoutFormat(ECPoint pubPoint) {
    final byte[] pubBytes = pubPoint.getEncoded(/* uncompressed */ false);
    return Arrays.copyOfRange(pubBytes, 1, pubBytes.length);
}
 
Example 19
Source File: ECKeySecp256k1.java    From aion with MIT License 2 votes vote down vote up
/**
 * Compute the encoded X, Y coordinates of a public point.
 *
 * <p>This is the encoded public key without the leading byte.
 *
 * @param pubPoint a public point
 * @return 64-byte X,Y point pair
 */
public byte[] pubBytesWithoutFormat(ECPoint pubPoint) {
    final byte[] pubBytes = pubPoint.getEncoded(/* uncompressed */ false);
    return Arrays.copyOfRange(pubBytes, 1, pubBytes.length);
}
 
Example 20
Source File: ECKeySecp256k1.java    From aion with MIT License 2 votes vote down vote up
/**
 * Returns public key bytes from the given private key. To convert a byte array into a
 * BigInteger, use <code> new BigInteger(1, bytes);</code>
 *
 * @param privKey -
 * @param compressed -
 * @return -
 */
public byte[] publicKeyFromPrivate(BigInteger privKey, boolean compressed) {
    ECPoint point = CURVE.getG().multiply(privKey);
    return point.getEncoded(compressed);
}