Skip to content

Commit a2d92ee

Browse files
committed
replace BPSWTest by PrPTest, which is faster for 32 to 63 bit arguments
1 parent e9cb170 commit a2d92ee

16 files changed

Lines changed: 66 additions & 66 deletions

src/main/java/de/tilman_neumann/jml/factor/CombinedFactorAlgorithm.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -43,7 +43,7 @@
4343
import de.tilman_neumann.jml.factor.siqs.tdiv.TDiv_QS_Small;
4444
import de.tilman_neumann.jml.factor.tdiv.TDiv;
4545
import de.tilman_neumann.jml.factor.tdiv.TDiv31Barrett;
46-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
46+
import de.tilman_neumann.jml.primes.probable.PrPTest;
4747
import de.tilman_neumann.util.Ensure;
4848

4949
/**
@@ -72,7 +72,7 @@ public class CombinedFactorAlgorithm extends FactorAlgorithm {
7272
// The SIQS chosen for big arguments depends on constructor parameters
7373
private FactorAlgorithm siqsForBigArgs;
7474

75-
private BPSWTest bpsw = new BPSWTest();
75+
private PrPTest prpTest = new PrPTest();
7676

7777
// profiling
7878
private long t0;
@@ -169,7 +169,7 @@ public void searchFactors(FactorArguments args, FactorResult result) {
169169
int exp = result.untestedFactors.removeAll(N);
170170
if (DEBUG) Ensure.ensureEquals(1, exp); // looks safe, otherwise we'ld have to consider exp below
171171

172-
if (bpsw.isProbablePrime(N)) { // TODO exploit tdiv done so far
172+
if (prpTest.isProbablePrime(N)) { // TODO exploit tdiv done so far
173173
result.primeFactors.add(N);
174174
return;
175175
}

src/main/java/de/tilman_neumann/jml/factor/FactorAlgorithm.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -22,7 +22,7 @@
2222

2323
import de.tilman_neumann.jml.factor.base.FactorArguments;
2424
import de.tilman_neumann.jml.factor.base.FactorResult;
25-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
25+
import de.tilman_neumann.jml.primes.probable.PrPTest;
2626
import de.tilman_neumann.util.SortedMultiset;
2727
import de.tilman_neumann.util.SortedMultiset_BottomUp;
2828

@@ -41,7 +41,7 @@ abstract public class FactorAlgorithm {
4141
/** the number of primes needed to factor any int <= 2^31 - 1 using trial division */
4242
protected static final int NUM_PRIMES_FOR_31_BIT_TDIV = 4793;
4343

44-
private BPSWTest bpsw = new BPSWTest();
44+
private PrPTest prpTest = new PrPTest();
4545

4646
protected Integer tdivLimit;
4747

@@ -123,7 +123,7 @@ public void factor(BigInteger N, SortedMultiset<BigInteger> primeFactors) {
123123
while (untestedFactors.size()>0) {
124124
BigInteger untestedFactor = untestedFactors.firstKey();
125125
int exp = untestedFactors.removeAll(untestedFactor);
126-
if (bpsw.isProbablePrime(untestedFactor)) {
126+
if (prpTest.isProbablePrime(untestedFactor)) {
127127
// The untestedFactor is probable prime. In exceptional cases this prediction may be wrong and untestedFactor composite
128128
// -> then we would falsely predict untestedFactor to be prime. BPSW is known to be exact for arguments <= 64 bit.
129129
//LOG.debug(untestedFactor + " is probable prime.");

src/main/java/de/tilman_neumann/jml/factor/TestsetGenerator.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -20,7 +20,7 @@
2020
import org.apache.logging.log4j.Logger;
2121
import org.apache.logging.log4j.LogManager;
2222

23-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
23+
import de.tilman_neumann.jml.primes.probable.PrPTest;
2424
import de.tilman_neumann.jml.random.Rng;
2525

2626
import static de.tilman_neumann.jml.base.BigIntConstants.*;
@@ -38,7 +38,7 @@ public class TestsetGenerator {
3838
private static final boolean DEBUG = false;
3939
private static final boolean DUMP_DATA_TO_FILE = false;
4040

41-
private static final BPSWTest bpsw = new BPSWTest();
41+
private static final PrPTest prpTest = new PrPTest();
4242
private static final Rng RNG = new Rng();
4343

4444
/**
@@ -61,7 +61,7 @@ public static BigInteger[] generate(int N_count, int bits, TestNumberNature mode
6161
if (bits<3) throw new IllegalArgumentException("There are no composites with " + bits + " bits.");
6262
for (int i=0; i<N_count; ) {
6363
BigInteger N = new BigInteger(bits, RNG);
64-
if(N.bitLength()==bits && !bpsw.isProbablePrime(N)) {
64+
if(N.bitLength()==bits && !prpTest.isProbablePrime(N)) {
6565
NArray[i++] = N;
6666
// TODO write to data file
6767
}
@@ -72,7 +72,7 @@ public static BigInteger[] generate(int N_count, int bits, TestNumberNature mode
7272
if (bits<4) throw new IllegalArgumentException("There are no odd composites with " + bits + " bits.");
7373
for (int i=0; i<N_count; ) {
7474
BigInteger N = new BigInteger(bits, RNG).or(I_1); // odd random number
75-
if(N.bitLength()==bits && !bpsw.isProbablePrime(N)) {
75+
if(N.bitLength()==bits && !prpTest.isProbablePrime(N)) {
7676
NArray[i++] = N;
7777
// TODO write to data file
7878
}
@@ -88,11 +88,11 @@ public static BigInteger[] generate(int N_count, int bits, TestNumberNature mode
8888
// of randomness while still being reasonably fast for large bit sizes.
8989
int n1bits = RNG.nextInt(minBits, maxBits);
9090
BigInteger n1 = new BigInteger(n1bits, RNG);
91-
n1 = bpsw.nextProbablePrime(n1);
91+
n1 = prpTest.nextProbablePrime(n1);
9292
if (n1.bitLength()<minBits) continue;
9393

9494
BigInteger N = new BigInteger(bits, RNG);
95-
BigInteger n2 = bpsw.nextProbablePrime(N.divide(n1));
95+
BigInteger n2 = prpTest.nextProbablePrime(N.divide(n1));
9696
N = n1.multiply(n2);
9797
if (N.bitLength() != bits) continue;
9898
if (n1.pow(3).compareTo(N) < 0) continue;
@@ -109,11 +109,11 @@ public static BigInteger[] generate(int N_count, int bits, TestNumberNature mode
109109
// generate random N with 2 prime factors
110110
BigInteger n1 = new BigInteger(minBits, RNG);
111111
n1 = n1.setBit(minBits-1);
112-
n1 = bpsw.nextProbablePrime(n1);
112+
n1 = prpTest.nextProbablePrime(n1);
113113
int n2bits = bits-n1.bitLength();
114114
BigInteger n2 = new BigInteger(n2bits, RNG);
115115
n2 = n2.setBit(n2bits-1);
116-
n2 = bpsw.nextProbablePrime(n2);
116+
n2 = prpTest.nextProbablePrime(n2);
117117
BigInteger N = n1.multiply(n2);
118118
if (DEBUG) LOG.debug("bits=" + bits + ", N1Bits=" + n1.bitLength() + ", N2Bits=" + n2.bitLength());
119119

src/main/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import de.tilman_neumann.jml.factor.base.FactorResult;
4646
import de.tilman_neumann.jml.factor.tdiv.TDiv63Inverse;
4747
import de.tilman_neumann.jml.gcd.Gcd63;
48-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
48+
import de.tilman_neumann.jml.primes.probable.PrPTest;
4949
import de.tilman_neumann.jml.random.SpRand32;
5050
import de.tilman_neumann.util.Ensure;
5151

@@ -161,7 +161,7 @@ public ecm_work() {
161161

162162
private TDiv63Inverse tdiv = new TDiv63Inverse(1<<21);
163163

164-
private BPSWTest bpsw = new BPSWTest();
164+
private PrPTest prpTest = new PrPTest();
165165

166166
private Gcd63 gcd63 = new Gcd63();
167167

@@ -1167,7 +1167,7 @@ public void searchFactors(FactorArguments args, FactorResult result) {
11671167
N = result.untestedFactors.firstKey();
11681168
exp = result.untestedFactors.removeAll(N); // can be > 1
11691169

1170-
if (bpsw.isProbablePrime(N)) {
1170+
if (prpTest.isProbablePrime(N)) {
11711171
result.primeFactors.add(N, exp);
11721172
return;
11731173
}

src/main/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64MH.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import de.tilman_neumann.jml.factor.base.FactorResult;
4646
import de.tilman_neumann.jml.factor.tdiv.TDiv63Inverse;
4747
import de.tilman_neumann.jml.gcd.Gcd63;
48-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
48+
import de.tilman_neumann.jml.primes.probable.PrPTest;
4949
import de.tilman_neumann.jml.random.SpRand32;
5050
import de.tilman_neumann.util.Ensure;
5151

@@ -164,7 +164,7 @@ public ecm_work() {
164164

165165
private TDiv63Inverse tdiv = new TDiv63Inverse(1<<21);
166166

167-
private BPSWTest bpsw = new BPSWTest();
167+
private PrPTest prpTest = new PrPTest();
168168

169169
private Gcd63 gcd63 = new Gcd63();
170170

@@ -1170,7 +1170,7 @@ public void searchFactors(FactorArguments args, FactorResult result) {
11701170
N = result.untestedFactors.firstKey();
11711171
exp = result.untestedFactors.removeAll(N); // can be > 1
11721172

1173-
if (bpsw.isProbablePrime(N)) {
1173+
if (prpTest.isProbablePrime(N)) {
11741174
result.primeFactors.add(N, exp);
11751175
return;
11761176
}

src/main/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64MHInlined.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import de.tilman_neumann.jml.factor.base.FactorResult;
4646
import de.tilman_neumann.jml.factor.tdiv.TDiv63Inverse;
4747
import de.tilman_neumann.jml.gcd.Gcd63;
48-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
48+
import de.tilman_neumann.jml.primes.probable.PrPTest;
4949
import de.tilman_neumann.jml.random.SpRand32;
5050
import de.tilman_neumann.util.Ensure;
5151

@@ -164,7 +164,7 @@ public ecm_work() {
164164

165165
private TDiv63Inverse tdiv = new TDiv63Inverse(1<<21);
166166

167-
private BPSWTest bpsw = new BPSWTest();
167+
private PrPTest prpTest = new PrPTest();
168168

169169
private Gcd63 gcd63 = new Gcd63();
170170

@@ -1179,7 +1179,7 @@ public void searchFactors(FactorArguments args, FactorResult result) {
11791179
N = result.untestedFactors.firstKey();
11801180
exp = result.untestedFactors.removeAll(N); // can be > 1
11811181

1182-
if (bpsw.isProbablePrime(N)) {
1182+
if (prpTest.isProbablePrime(N)) {
11831183
result.primeFactors.add(N, exp);
11841184
return;
11851185
}

src/main/java/de/tilman_neumann/jml/primes/probable/BPSWTest.java

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

src/main/java/de/tilman_neumann/jml/smooth/CANEntry.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -21,7 +21,7 @@
2121
import org.apache.logging.log4j.Logger;
2222
import org.apache.logging.log4j.LogManager;
2323

24-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
24+
import de.tilman_neumann.jml.primes.probable.PrPTest;
2525

2626
/**
2727
* A colossally abundant number (CAN), together with some information that was necessary to compute it.
@@ -38,7 +38,7 @@ public class CANEntry {
3838
/** the sum of prime factor exponents is the sequence element number of CANs */
3939
private int exponentSum;
4040

41-
private static final BPSWTest bpsw = new BPSWTest();
41+
private static final PrPTest prpTest = new PrPTest();
4242

4343
// private, use factory method computeCAN(epsilon)
4444
private CANEntry(double epsilon) {
@@ -86,7 +86,7 @@ public static CANEntry computeCAN(double epsilon) {
8686
if (exponent==0) break;
8787
if (DEBUG) LOG.debug(" epsilon=" + epsilon + ", p=" + p + ", exponent=" + exponent);
8888
result.add(p, exponent);
89-
p = bpsw.nextProbablePrime(p);
89+
p = prpTest.nextProbablePrime(p);
9090
}
9191
return result;
9292
}

src/main/java/de/tilman_neumann/jml/smooth/SHCNEntry.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -21,7 +21,7 @@
2121
import org.apache.logging.log4j.Logger;
2222
import org.apache.logging.log4j.LogManager;
2323

24-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
24+
import de.tilman_neumann.jml.primes.probable.PrPTest;
2525

2626
/**
2727
* A superior highly composite number (SHCN), together with some information that was necessary to compute it.
@@ -38,7 +38,7 @@ public class SHCNEntry {
3838
/** the sum of prime factor exponents is the sequence element number of SHCNs */
3939
private int exponentSum;
4040

41-
private static final BPSWTest bpsw = new BPSWTest();
41+
private static final PrPTest prpTest = new PrPTest();
4242

4343
// private, use factory method computeSHCN(x)
4444
private SHCNEntry(double x) {
@@ -84,7 +84,7 @@ public static SHCNEntry computeSHCN(double x) {
8484
int exponent = computeExponent(x, p);
8585
if (DEBUG) LOG.debug(" x=" + x + ", p=" + p + ", exponent=" + exponent);
8686
result.add(p, exponent);
87-
p = bpsw.nextProbablePrime(p);
87+
p = prpTest.nextProbablePrime(p);
8888
}
8989
return result;
9090
}

src/test/java/de/tilman_neumann/jml/factor/FactorTestBase.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2026 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -27,7 +27,7 @@
2727
import org.apache.logging.log4j.LogManager;
2828
import org.apache.logging.log4j.Logger;
2929

30-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
30+
import de.tilman_neumann.jml.primes.probable.PrPTest;
3131
import de.tilman_neumann.util.SortedMultiset;
3232

3333
/**
@@ -39,7 +39,7 @@ public class FactorTestBase {
3939

4040
private static final Logger LOG = LogManager.getLogger(FactorTestBase.class);
4141

42-
private static final BPSWTest bpsw = new BPSWTest();
42+
private static final PrPTest prpTest = new PrPTest();
4343

4444
private static FactorAlgorithm factorizer;
4545
private static FactorAlgorithm verificationFactorizer;
@@ -76,12 +76,12 @@ protected List<BigInteger> testFullFactorizationOfComposites(long nMax) {
7676
protected List<BigInteger> testFullFactorizationOfComposites(BigInteger nMin, BigInteger nMax) {
7777
ArrayList<BigInteger> fails = new ArrayList<>();
7878
for (BigInteger n=nMin; n.compareTo(nMax)<=0; n=n.add(I_1)) {
79-
if (bpsw.isProbablePrime(n)) continue; // skip primes
79+
if (prpTest.isProbablePrime(n)) continue; // skip primes
8080
SortedMultiset<BigInteger> factors = factorizer.factor(n);
8181
boolean isFail = false;
8282
BigInteger testProd = I_1;
8383
for (BigInteger factor : factors.keySet()) {
84-
if (!bpsw.isProbablePrime(factor)) {
84+
if (!prpTest.isProbablePrime(factor)) {
8585
isFail = true;
8686
break;
8787
}
@@ -121,12 +121,12 @@ protected List<BigInteger> testFindSingleFactorForSemiprimes(BigInteger nMin, Bi
121121
}
122122

123123
private boolean isSemiprime(BigInteger N) {
124-
if (bpsw.isProbablePrime(N)) return false;
124+
if (prpTest.isProbablePrime(N)) return false;
125125

126126
BigInteger factor1 = verificationFactorizer.findSingleFactor(N);
127127
assertTrue(factor1.compareTo(I_1) > 0 && factor1.compareTo(N) < 0); // otherwise the verificationFactorizer failed
128128
BigInteger factor2 = N.divide(factor1);
129-
return bpsw.isProbablePrime(factor1) && bpsw.isProbablePrime(factor2);
129+
return prpTest.isProbablePrime(factor1) && prpTest.isProbablePrime(factor2);
130130
}
131131

132132
/**

0 commit comments

Comments
 (0)