use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class ECUtil method addPoint.
private static ECPoint addPoint(ECPoint r, ECPoint s, EllipticCurve curve) {
if (r.equals(s))
return doublePoint(r, curve);
else if (r.equals(ECPoint.POINT_INFINITY))
return s;
else if (s.equals(ECPoint.POINT_INFINITY))
return r;
BigInteger prime = ((ECFieldFp) curve.getField()).getP();
// use NBI modInverse();
BigInteger tmp = r.getAffineX().subtract(s.getAffineX());
tmp = new NativeBigInteger(tmp);
BigInteger slope = (r.getAffineY().subtract(s.getAffineY())).multiply(tmp.modInverse(prime)).mod(prime);
slope = new NativeBigInteger(slope);
BigInteger xOut = (slope.modPow(TWO, prime).subtract(r.getAffineX())).subtract(s.getAffineX()).mod(prime);
BigInteger yOut = s.getAffineY().negate().mod(prime);
yOut = yOut.add(slope.multiply(s.getAffineX().subtract(xOut))).mod(prime);
ECPoint out = new ECPoint(xOut, yOut);
return out;
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class ECUtil method doublePoint.
private static ECPoint doublePoint(ECPoint r, EllipticCurve curve) {
if (r.equals(ECPoint.POINT_INFINITY))
return r;
BigInteger slope = (r.getAffineX().pow(2)).multiply(THREE);
slope = slope.add(curve.getA());
BigInteger prime = ((ECFieldFp) curve.getField()).getP();
// use NBI modInverse();
BigInteger tmp = r.getAffineY().multiply(TWO);
tmp = new NativeBigInteger(tmp);
slope = slope.multiply(tmp.modInverse(prime));
BigInteger xOut = slope.pow(2).subtract(r.getAffineX().multiply(TWO)).mod(prime);
BigInteger yOut = (r.getAffineY().negate()).add(slope.multiply(r.getAffineX().subtract(xOut))).mod(prime);
ECPoint out = new ECPoint(xOut, yOut);
return out;
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class KeyGenerator method generatePKIKeys.
/**
* Same as above but different return type
* @since 0.8.7
*/
public SimpleDataStructure[] generatePKIKeys() {
BigInteger a = new NativeBigInteger(getElGamalExponentSize(), _context.random());
BigInteger aalpha = CryptoConstants.elgg.modPow(a, CryptoConstants.elgp);
SimpleDataStructure[] keys = new SimpleDataStructure[2];
keys[0] = new PublicKey();
keys[1] = new PrivateKey();
try {
keys[0].setData(SigUtil.rectify(aalpha, PublicKey.KEYSIZE_BYTES));
keys[1].setData(SigUtil.rectify(a, PrivateKey.KEYSIZE_BYTES));
} catch (InvalidKeyException ike) {
throw new IllegalArgumentException(ike);
}
return keys;
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class KeyGenerator method generateSigningKeys.
/**
* DSA-SHA1 only.
*
* Same as above but different return type
* @since 0.8.7
*/
public SimpleDataStructure[] generateSigningKeys() {
SimpleDataStructure[] keys = new SimpleDataStructure[2];
BigInteger x = null;
// make sure the random key is less than the DSA q and greater than zero
do {
x = new NativeBigInteger(160, _context.random());
} while (x.compareTo(CryptoConstants.dsaq) >= 0 || x.equals(BigInteger.ZERO));
BigInteger y = CryptoConstants.dsag.modPow(x, CryptoConstants.dsap);
keys[0] = new SigningPublicKey();
keys[1] = new SigningPrivateKey();
try {
keys[0].setData(SigUtil.rectify(y, SigningPublicKey.KEYSIZE_BYTES));
keys[1].setData(SigUtil.rectify(x, SigningPrivateKey.KEYSIZE_BYTES));
} catch (InvalidKeyException ike) {
throw new IllegalStateException(ike);
}
return keys;
}
use of net.i2p.util.NativeBigInteger in project i2p.i2p by i2p.
the class SigUtil method toJavaRSAKey.
/**
* @deprecated unused
*/
public static RSAPublicKey toJavaRSAKey(SigningPublicKey pk) throws GeneralSecurityException {
SigType type = pk.getType();
KeyFactory kf = KeyFactory.getInstance("RSA");
BigInteger n = new NativeBigInteger(1, pk.getData());
BigInteger e = ((RSAKeyGenParameterSpec) type.getParams()).getPublicExponent();
// modulus exponent
KeySpec ks = new RSAPublicKeySpec(n, e);
return (RSAPublicKey) kf.generatePublic(ks);
}
Aggregations