Skip to content

Commit

Permalink
refinement on GCD sieve, added bounds test
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Apr 4, 2019
1 parent 522f6de commit a34ea0e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions core/src/main/java/org/bouncycastle/util/BigIntegers.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static BigInteger createRandomBigInteger(int bitLength, SecureRandom rand
+ "ce86165a978d719ebf647f362d33fca29cd179fb42401cbaf3df0c614056f9c8"
+ "f3cfd51e474afb6bc6974f78db8aba8e9e517fded658591ab7502bd41849462f",
16);
private static final int SQR_MAX_SMALL = 20; // bitlength of 743 * 743
private static final int MAX_SMALL = BigInteger.valueOf(743).bitLength(); // bitlength of 743 * 743

/**
* Return a prime number candidate of the specified bit length.
Expand Down Expand Up @@ -183,7 +183,7 @@ public static BigInteger createRandomPrime(int bitLength, int certainty, SecureR
base[base.length - 1] |= 0x01;

rv = new BigInteger(1, base);
if (bitLength > SQR_MAX_SMALL)
if (bitLength > MAX_SMALL)
{
while (!rv.gcd(SMALL_PRIMES_PRODUCT).equals(ONE))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.bouncycastle.crypto.test;

import java.math.BigInteger;

import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.test.SimpleTest;
import org.bouncycastle.util.test.TestRandomData;

public class BigIntegersTest
extends SimpleTest
{
public String getName()
{
return "BigIntegers";
}

public void performTest()
throws Exception
{
BigInteger min = BigInteger.valueOf(5);
isTrue(min.equals(BigIntegers.createRandomPrime(min.bitLength(), 1,
new TestRandomData(BigIntegers.asUnsignedByteArray(min)))));

BigInteger max = BigInteger.valueOf(743);
isTrue(max.equals(BigIntegers.createRandomPrime(max.bitLength(), 1,
new TestRandomData(BigIntegers.asUnsignedByteArray(max)))));
}

public static void main(String[] args)
throws Exception
{
runTest(new BigIntegersTest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public class RegressionTest
new CSHAKETest(),
new Argon2Test(),
new OpenSSHKeyParsingTests(),
new EthereumIESTest()
new EthereumIESTest(),
new BigIntegersTest()
};

public static void main(
Expand Down

0 comments on commit a34ea0e

Please sign in to comment.