org.bouncycastle.math.ec.FixedPointCombMultiplier Java Examples

The following examples show how to use org.bouncycastle.math.ec.FixedPointCombMultiplier. 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: SECP256K1.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
/**
 * Create the public key from a secret key.
 *
 * @param secretKey The secret key.
 * @return The associated public key.
 */
public static PublicKey fromSecretKey(SecretKey secretKey) {
  BigInteger privKey = secretKey.bytes().toUnsignedBigInteger();

  /*
   * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
   * order, but that could change in future versions.
   */
  if (privKey.bitLength() > Parameters.CURVE_ORDER.bitLength()) {
    privKey = privKey.mod(Parameters.CURVE_ORDER);
  }

  ECPoint point = new FixedPointCombMultiplier().multiply(Parameters.CURVE.getG(), privKey);
  return PublicKey.fromBytes(Bytes.wrap(Arrays.copyOfRange(point.getEncoded(false), 1, 65)));
}
 
Example #2
Source File: SECP256K1.java    From besu with Apache License 2.0 5 votes vote down vote up
public static PublicKey create(final PrivateKey privateKey) {
  BigInteger privKey = privateKey.getEncodedBytes().toUnsignedBigInteger();

  /*
   * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
   * order, but that could change in future versions.
   */
  if (privKey.bitLength() > CURVE.getN().bitLength()) {
    privKey = privKey.mod(CURVE.getN());
  }

  final ECPoint point = new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
  return PublicKey.create(Bytes.wrap(Arrays.copyOfRange(point.getEncoded(false), 1, 65)));
}
 
Example #3
Source File: PEMProcessor.java    From eosio-java with MIT License 5 votes vote down vote up
/**
 * Extract EOS public key
 *
 * @param isLegacy - Set to true if the legacy format of the key is desired.  This uses "EOS"
 * to prefix the key data and only applies to keys generated with the secp256k1 algorithm.  The
 * new format prefixes the key data with "PUB_K1_".
 * @return EOS format public key of the current private key
 * @throws PEMProcessorError when the public key extraction fails.
 */
public String extractEOSPublicKeyFromPrivateKey(boolean isLegacy) throws PEMProcessorError {
    if (!this.getType().equals(PRIVATE_KEY_TYPE)) {
        throw new PEMProcessorError(ErrorConstants.PUBLIC_KEY_COULD_NOT_BE_EXTRACTED_FROM_PRIVATE_KEY);
    }

    AlgorithmEmployed keyCurve = this.getAlgorithm();
    BigInteger privateKeyBI = new BigInteger(BIG_INTEGER_POSITIVE, this.getKeyData());
    BigInteger n;
    ECPoint g;

    switch (keyCurve) {
        case SECP256R1:
            n = CURVE_R1.getN();
            g = CURVE_R1.getG();
            break;

        default:
            n = CURVE_K1.getN();
            g = CURVE_K1.getG();
            break;
    }

    if (privateKeyBI.bitLength() > n.bitLength()) {
        privateKeyBI = privateKeyBI.mod(n);
    }

    byte[] publicKeyByteArray = new FixedPointCombMultiplier().multiply(g, privateKeyBI).getEncoded(true);

    try {
        return EOSFormatter.encodePublicKey(publicKeyByteArray, keyCurve, isLegacy);
    } catch (Base58ManipulationError e) {
        throw new PEMProcessorError(e);
    }
}
 
Example #4
Source File: Sign.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Returns public key point from the given private key.
 */
private static ECPoint publicPointFromPrivate(BigInteger privKey) {
    /*
     * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
     * order, but that could change in future versions.
     */
    if (privKey.bitLength() > CURVE.getN().bitLength()) {
        privKey = privKey.mod(CURVE.getN());
    }
    return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
 
Example #5
Source File: Sign.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns public key point from the given private key.
 */
private static ECPoint publicPointFromPrivate(BigInteger privKey) {
    /*
     * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
     * order, but that could change in future versions.
     */
    if (privKey.bitLength() > CURVE.getN().bitLength()) {
        privKey = privKey.mod(CURVE.getN());
    }
    return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
 
Example #6
Source File: SECP256K1.java    From cava with Apache License 2.0 5 votes vote down vote up
/**
 * Create the public key from a secret key.
 *
 * @param secretKey The secret key.
 * @return The associated public key.
 */
public static PublicKey fromSecretKey(SecretKey secretKey) {
  BigInteger privKey = secretKey.bytes().toUnsignedBigInteger();

  /*
   * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
   * order, but that could change in future versions.
   */
  if (privKey.bitLength() > Parameters.CURVE_ORDER.bitLength()) {
    privKey = privKey.mod(Parameters.CURVE_ORDER);
  }

  ECPoint point = new FixedPointCombMultiplier().multiply(Parameters.CURVE.getG(), privKey);
  return PublicKey.fromBytes(Bytes.wrap(Arrays.copyOfRange(point.getEncoded(false), 1, 65)));
}
 
Example #7
Source File: ECKey.java    From nuls-v2 with MIT License 5 votes vote down vote up
/**
 * Returns public key point from the given private key. To convert a byte array into a BigInteger,
 * use {@code new BigInteger(1, bytes);}
 */
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
    /*
     * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group order,
     * but that could change in future versions.
     */
    if (privKey.bitLength() > CURVE.getN().bitLength()) {
        privKey = privKey.mod(CURVE.getN());
    }
    return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
 
Example #8
Source File: Sign.java    From blockchain with Apache License 2.0 5 votes vote down vote up
/**
 * Returns public key point from the given private key.
 */
private static ECPoint publicPointFromPrivate(BigInteger privKey) {
	/*
	 * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
	 * order, but that could change in future versions.
	 */
	if (privKey.bitLength() > CURVE.getN().bitLength()) {
		privKey = privKey.mod(CURVE.getN());
	}
	return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
 
Example #9
Source File: Sign.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Returns public key point from the given private key.
 *
 * @param privKey the private key to derive the public key from
 * @return ECPoint public key
 */
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
    /*
     * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
     * order, but that could change in future versions.
     */
    if (privKey.bitLength() > CURVE.getN().bitLength()) {
        privKey = privKey.mod(CURVE.getN());
    }
    return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
 
Example #10
Source File: Sign.java    From web3j with Apache License 2.0 5 votes vote down vote up
/**
 * Returns public key point from the given private key.
 *
 * @param privKey the private key to derive the public key from
 * @return ECPoint public key
 */
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
    /*
     * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group
     * order, but that could change in future versions.
     */
    if (privKey.bitLength() > CURVE.getN().bitLength()) {
        privKey = privKey.mod(CURVE.getN());
    }
    return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
 
Example #11
Source File: ECCurvePoint.java    From InflatableDonkey with MIT License 5 votes vote down vote up
public static Optional<ECCurvePoint> create(BigInteger d, String curveName) {
    X9ECParameters x9ECParameters = ECAssistant.x9ECParameters(curveName);
    ECPoint Q = new FixedPointCombMultiplier().multiply(x9ECParameters.getG(), d).normalize();

    ECCurvePoint point = new ECCurvePoint(Q, curveName, x9ECParameters);
    return Optional.of(point);
}
 
Example #12
Source File: SM2PreprocessSigner.java    From gmhelper with Apache License 2.0 4 votes vote down vote up
protected ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #13
Source File: Signer.java    From evt4j with MIT License 4 votes vote down vote up
protected static ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #14
Source File: SM2Signer.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
protected static ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #15
Source File: ECDSASigner.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
protected ECMultiplier createBasePointMultiplier() {
    return new FixedPointCombMultiplier();
}
 
Example #16
Source File: SM2Signer.java    From xipki with Apache License 2.0 4 votes vote down vote up
public byte[] generateSignatureForHash(byte[] eHash) throws CryptoException {
  BigInteger n = ecParams.getN();
  BigInteger e = new BigInteger(1, eHash);
  BigInteger d = ((ECPrivateKeyParameters)ecKey).getD();

  BigInteger r;
  BigInteger s;

  ECMultiplier basePointMultiplier = new FixedPointCombMultiplier();

  // 5.2.1 Draft RFC:  SM2 Public Key Algorithms
  do { // generate s
    BigInteger k;
    do { // generate r
      // A3
      k = kCalculator.nextK();

      // A4
      ECPoint p = basePointMultiplier.multiply(ecParams.getG(), k).normalize();

      // A5
      r = e.add(p.getAffineXCoord().toBigInteger()).mod(n);
    } while (r.equals(ECConstants.ZERO) || r.add(k).equals(n));

    // A6
    // CHECKSTYLE:SKIP
    BigInteger dPlus1ModN = d.add(ECConstants.ONE).modInverse(n);

    s = k.subtract(r.multiply(d)).mod(n);
    s = dPlus1ModN.multiply(s).mod(n);
  } while (s.equals(ECConstants.ZERO));

  // A7
  try {
    ASN1EncodableVector v = new ASN1EncodableVector();
    v.add(new ASN1Integer(r));
    v.add(new ASN1Integer(s));
    return new DERSequence(v).getEncoded(ASN1Encoding.DER);
  } catch (IOException ex) {
    throw new CryptoException("unable to encode signature: " + ex.getMessage(), ex);
  }
}