diff options
author | Elliott Hughes <enh@google.com> | 2013-04-08 18:19:51 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2013-04-10 13:47:55 -0700 |
commit | b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2 (patch) | |
tree | d5e7403979d3c04008c2e721d2dabc64d196046a /luni | |
parent | c5f088304766357ba8efae909bef6e5abc09c1f0 (diff) | |
download | libcore-b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2.zip libcore-b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2.tar.gz libcore-b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2.tar.bz2 |
Fix BigInteger test failures.
We were hitting a couple of OpenSSL bugs. I've reported them upstream, and
work around them in the meantime.
Bug: 2943474
Change-Id: Ia06bcf3025f525a954d9b918669d09531631a266
Diffstat (limited to 'luni')
-rw-r--r-- | luni/src/main/java/java/math/BigInteger.java | 40 | ||||
-rw-r--r-- | luni/src/test/java/tests/api/java/math/OldBigIntegerTest.java | 6 |
2 files changed, 24 insertions, 22 deletions
diff --git a/luni/src/main/java/java/math/BigInteger.java b/luni/src/main/java/java/math/BigInteger.java index e58bfd5..e040a64 100644 --- a/luni/src/main/java/java/math/BigInteger.java +++ b/luni/src/main/java/java/math/BigInteger.java @@ -164,7 +164,10 @@ public class BigInteger extends Number if (bitLength < 2) { throw new ArithmeticException("bitLength < 2: " + bitLength); } - setBigInt(BigInt.generatePrimeDefault(bitLength)); + bitLength = Math.max(bitLength, 16); // OpenSSL won't generate shorter primes. + do { + setBigInt(BigInt.generatePrimeDefault(bitLength)); + } while (bitLength() != bitLength); // Work around an OpenSSL bug; http://b/8588028. } /** @@ -940,26 +943,27 @@ public class BigInteger extends Number /** * Returns a {@code BigInteger} whose value is {@code - * pow(this, exponent) mod m}. The modulus {@code m} must be positive. The - * result is guaranteed to be in the interval {@code [0, m)} (0 inclusive, - * m exclusive). If the exponent is negative, then {@code - * pow(this.modInverse(m), -exponent) mod m} is computed. The inverse of - * this only exists if {@code this} is relatively prime to m, otherwise an - * exception is thrown. + * pow(this, exponent) mod modulus}. The modulus must be positive. The + * result is guaranteed to be in the interval {@code [0, modulus)}. + * If the exponent is negative, then + * {@code pow(this.modInverse(modulus), -exponent) mod modulus} is computed. + * The inverse of this only exists if {@code this} is relatively prime to the modulus, + * otherwise an exception is thrown. * - * @param exponent the exponent. - * @param m the modulus. - * @throws NullPointerException if {@code m == null} or {@code exponent == - * null}. - * @throws ArithmeticException if {@code m < 0} or if {@code exponent<0} and - * this is not relatively prime to {@code m}. + * @throws NullPointerException if {@code modulus == null} or {@code exponent == null}. + * @throws ArithmeticException if {@code modulus < 0} or if {@code exponent < 0} and + * not relatively prime to {@code modulus}. */ - public BigInteger modPow(BigInteger exponent, BigInteger m) { - if (m.signum() <= 0) { - throw new ArithmeticException("m.signum() <= 0"); + public BigInteger modPow(BigInteger exponent, BigInteger modulus) { + if (modulus.signum() <= 0) { + throw new ArithmeticException("modulus.signum() <= 0"); + } + int exponentSignum = exponent.signum(); + if (exponentSignum == 0) { // OpenSSL gets this case wrong; http://b/8574367. + return ONE.mod(modulus); } - BigInteger base = exponent.signum() < 0 ? modInverse(m) : this; - return new BigInteger(BigInt.modExp(base.getBigInt(), exponent.getBigInt(), m.getBigInt())); + BigInteger base = exponentSignum < 0 ? modInverse(modulus) : this; + return new BigInteger(BigInt.modExp(base.getBigInt(), exponent.getBigInt(), modulus.getBigInt())); } /** diff --git a/luni/src/test/java/tests/api/java/math/OldBigIntegerTest.java b/luni/src/test/java/tests/api/java/math/OldBigIntegerTest.java index 77bcabf..7fd5415 100644 --- a/luni/src/test/java/tests/api/java/math/OldBigIntegerTest.java +++ b/luni/src/test/java/tests/api/java/math/OldBigIntegerTest.java @@ -135,15 +135,13 @@ public class OldBigIntegerTest extends junit.framework.TestCase { try { new BigInteger(1, 80, (Random)null); fail("ArithmeticException expected"); - } catch (ArithmeticException e) { - // PASSED + } catch (ArithmeticException expected) { } try { new BigInteger(-1, (Random)null); fail("IllegalArgumentException expected"); - } catch (IllegalArgumentException e) { - // PASSED + } catch (IllegalArgumentException expected) { } } |