use of java.math.BigInteger in project j2objc by google.
the class DecimalFormatTest method test_formatObject.
public void test_formatObject() {
DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US);
// format maxLong
FieldPosition pos = new FieldPosition(0);
StringBuffer out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L1: " + out, out.toString().equals("9,223,372,036,854,775,807"));
// format minLong
pos = new FieldPosition(0);
out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos);
assertTrue("Wrong result L2: " + out, out.toString().equals("-9,223,372,036,854,775,808"));
// format maxLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI1: " + out, out.toString().equals("9,223,372,036,854,775,807"));
// format minLong of type BigInteger
pos = new FieldPosition(0);
out = format.format(new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)), new StringBuffer(), pos);
assertTrue("Wrong result BI2: " + out, out.toString().equals("-9,223,372,036,854,775,808"));
// format maxLong + 1
java.math.BigInteger big;
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)).add(new java.math.BigInteger("1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI3: " + out, out.toString().equals("9,223,372,036,854,775,808"));
// format minLong - 1
pos = new FieldPosition(0);
big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)).add(new java.math.BigInteger("-1"));
out = format.format(big, new StringBuffer(), pos);
assertTrue("Wrong result BI4: " + out, out.toString().equals("-9,223,372,036,854,775,809"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51.348"), new StringBuffer(), pos);
assertTrue("Wrong result BD1: " + out, out.toString().equals("51.348"));
// format big decimal
pos = new FieldPosition(0);
out = format.format(new java.math.BigDecimal("51"), new StringBuffer(), pos);
assertTrue("Wrong result BD2: " + out, out.toString().equals("51"));
// format big decimal Double.MAX_VALUE * 2
java.math.BigDecimal bigDecimal;
pos = new FieldPosition(0);
final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408," + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077," + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653," + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152," + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," + "354,361,838,599,762,500,808,052,368,249,716,736";
bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
assertTrue("Wrong result BDmax2: " + out, out.toString().equals(doubleMax2));
// format big decimal Double.MIN_VALUE + Double.MIN_VALUE
// and Double.MIN_VALUE - Double.MIN_VALUE
pos = new FieldPosition(0);
bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal(Double.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
bigDecimal = new BigDecimal(Float.MAX_VALUE).add(new BigDecimal(Float.MAX_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
final String BDFloatMax2 = "680,564,693,277,057,719,623,408,366,969,033,850,880";
assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMax2));
// format big decimal Float.MIN_VALUE + Float.MIN_VALUE
// and Float.MIN_VALUE - Float.MIN_VALUE
bigDecimal = new BigDecimal(Float.MIN_VALUE).add(new BigDecimal(Float.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
final String BDFloatMin2 = "0";
bigDecimal = new BigDecimal(Float.MIN_VALUE).subtract(new BigDecimal(Float.MIN_VALUE));
out = format.format(bigDecimal, new StringBuffer(), pos);
assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMin2));
}
use of java.math.BigInteger in project wycheproof by google.
the class EcdsaTest method extractS.
BigInteger extractS(byte[] signature) throws Exception {
int startR = (signature[1] & 0x80) != 0 ? 3 : 2;
int lengthR = signature[startR + 1];
int startS = startR + 2 + lengthR;
int lengthS = signature[startS + 1];
return new BigInteger(Arrays.copyOfRange(signature, startS + 2, startS + 2 + lengthS));
}
use of java.math.BigInteger in project groovy-core by groovy.
the class DefaultGroovyMethods method downto.
/**
* Iterates from this number down to the given number, inclusive,
* decrementing by one each time.
*
* @param self a BigInteger
* @param to the end number
* @param closure the code to execute for each number
* @since 1.0
*/
public static void downto(BigInteger self, Number to, @ClosureParams(FirstParam.class) Closure closure) {
if (to instanceof BigDecimal) {
// That's what you get for "1.0".
final BigDecimal one = BigDecimal.valueOf(10, 1);
final BigDecimal to1 = (BigDecimal) to;
final BigDecimal selfD = new BigDecimal(self);
if (selfD.compareTo(to1) >= 0) {
for (BigDecimal i = selfD; i.compareTo(to1) >= 0; i = i.subtract(one)) {
closure.call(i.toBigInteger());
}
} else
throw new GroovyRuntimeException(MessageFormat.format("The argument ({0}) to downto() cannot be greater than the value ({1}) it''s called on.", to, self));
} else if (to instanceof BigInteger) {
final BigInteger one = BigInteger.valueOf(1);
final BigInteger to1 = (BigInteger) to;
if (self.compareTo(to1) >= 0) {
for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
closure.call(i);
}
} else
throw new GroovyRuntimeException(MessageFormat.format("The argument ({0}) to downto() cannot be greater than the value ({1}) it''s called on.", to, self));
} else {
final BigInteger one = BigInteger.valueOf(1);
final BigInteger to1 = new BigInteger(to.toString());
if (self.compareTo(to1) >= 0) {
for (BigInteger i = self; i.compareTo(to1) >= 0; i = i.subtract(one)) {
closure.call(i);
}
} else
throw new GroovyRuntimeException(MessageFormat.format("The argument ({0}) to downto() cannot be greater than the value ({1}) it''s called on.", to, self));
}
}
use of java.math.BigInteger in project wycheproof by google.
the class EcdsaTest method testBias.
/** Checks whether the one time key k in ECDSA is biased. */
public void testBias(String algorithm, String curve, ECParameterSpec ecParams) throws Exception {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
try {
keyGen.initialize(ecParams);
} catch (InvalidAlgorithmParameterException ex) {
System.out.println("This provider does not support curve:" + curve);
return;
}
KeyPair keyPair = keyGen.generateKeyPair();
ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
// If we throw a fair coin tests times then the probability that
// either heads or tails appears less than mincount is less than 2^{-32}.
// Therefore the test below is not expected to fail unless the generation
// of the one time keys is indeed biased.
final int tests = 1024;
final int mincount = 410;
String hashAlgorithm = getHashAlgorithm(algorithm);
String message = "Hello";
byte[] messageBytes = message.getBytes("UTF-8");
byte[] digest = MessageDigest.getInstance(hashAlgorithm).digest(messageBytes);
// TODO(bleichen): Truncate the digest if the digest size is larger than the
// curve size.
BigInteger h = new BigInteger(1, digest);
BigInteger q = priv.getParams().getOrder();
BigInteger qHalf = q.shiftRight(1);
Signature signer = Signature.getInstance(algorithm);
signer.initSign(priv);
// count the number of k's with msb set
int countLsb = 0;
// count the number of k's with lsb set
int countMsb = 0;
for (int i = 0; i < tests; i++) {
signer.update(messageBytes);
byte[] signature = signer.sign();
BigInteger k = extractK(signature, h, priv);
if (k.testBit(0)) {
countLsb++;
}
if (k.compareTo(qHalf) == 1) {
countMsb++;
}
}
System.out.println(signer.getProvider().getName() + " curve:" + curve + " countLsb:" + countLsb + " countMsb:" + countMsb);
if (countLsb < mincount || countLsb > tests - mincount) {
fail("Bias detected in the least significant bit of k:" + countLsb);
}
if (countMsb < mincount || countMsb > tests - mincount) {
fail("Bias detected in the most significant bit of k:" + countMsb);
}
}
use of java.math.BigInteger in project wycheproof by google.
the class RsaKeyTest method checkPrivateCrtKey.
private void checkPrivateCrtKey(RSAPrivateCrtKey key, int expectedKeySize) throws Exception {
BigInteger p = key.getPrimeP();
BigInteger q = key.getPrimeQ();
BigInteger n = key.getModulus();
BigInteger e = key.getPublicExponent();
BigInteger d = key.getPrivateExponent();
BigInteger dp = key.getPrimeExponentP();
BigInteger dq = key.getPrimeExponentQ();
BigInteger crtCoeff = key.getCrtCoefficient();
// Simple test that (n,d,e) is a valid RSA key.
assertEquals(n, p.multiply(q));
assertEquals(expectedKeySize, n.bitLength());
int certainty = 80;
assertTrue(p.isProbablePrime(certainty));
assertTrue(q.isProbablePrime(certainty));
// Very simple checks for weak random number generators.
RandomUtil.checkPrime(p);
RandomUtil.checkPrime(q);
assertTrue(d.bitLength() > expectedKeySize / 2);
// TODO(bleichen): Keys that are very imbalanced can be broken with elliptic curve factoring.
// Add other checks. E.g. for the size of dp and dq
assertTrue(p.bitLength() > 256);
assertTrue(q.bitLength() > 256);
BigInteger p1 = p.subtract(BigInteger.ONE);
BigInteger q1 = q.subtract(BigInteger.ONE);
BigInteger phi = p1.multiply(q1);
// maximal order of elements
BigInteger order = phi.divide(p1.gcd(q1));
assertEquals(BigInteger.ONE, d.multiply(e).mod(order));
assertEquals(d.mod(p1), dp.mod(p1));
assertEquals(d.mod(q1), dq.mod(q1));
assertEquals(q.multiply(crtCoeff).mod(p), BigInteger.ONE);
}
Aggregations