Skip to content

Commit f64a1a3

Browse files
committed
In BigIntegers it is faster to use the absolute value of the difference
1 parent 23351f0 commit f64a1a3

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoBrent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public BigInteger findSingleFactor(BigInteger N) {
7171
final int iMax = Math.min(m, r-k);
7272
for (int i=1; i<=iMax; i++) {
7373
y = squareAddModN(y, c);
74+
// In BigInteger variants it is slightly faster to use the absolute value of the difference
7475
final BigInteger diff = x.compareTo(y) < 0 ? y.subtract(x) : x.subtract(y);
7576
q = diff.multiply(q).mod(N);
7677
}

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoBrentModBlock.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public BigInteger findSingleFactor(BigInteger N) {
8181
for (int i = 1; i <= iMax; i = i + jMax) {
8282
for (int j = 0; j < jMax; j++) {
8383
y = squareAddModN(y, c);
84-
q = q.multiply(x.subtract(y));
84+
// In BigInteger variants it is slightly faster to use the absolute value of the difference
85+
final BigInteger diff = x.compareTo(y) < 0 ? y.subtract(x) : x.subtract(y);
86+
q = diff.multiply(q).mod(N);
8587
}
8688
q = q.mod(N); // one mod for the block
8789
}
@@ -97,7 +99,8 @@ public BigInteger findSingleFactor(BigInteger N) {
9799
if (G.equals(N)) {
98100
do {
99101
ys = squareAddModN(ys, c);
100-
G = N.gcd(x.subtract(ys));
102+
final BigInteger diff = x.compareTo(ys) < 0 ? ys.subtract(x) : x.subtract(ys);
103+
G = diff.gcd(N);
101104
} while (G.equals(I_1));
102105
if (DEBUG) LOG.debug("G = " + G);
103106
}

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoTwoLoops.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public BigInteger findSingleFactor(BigInteger N) {
6767
x = squareAddModN(x, c);
6868
xx = squareAddModN(xx, c);
6969
xx = squareAddModN(xx, c);
70-
prod = prod.multiply(x.subtract(xx)).mod(N);
70+
// In BigInteger variants it is slightly faster to use the absolute value of the difference
71+
final BigInteger diff = x.compareTo(xx) < 0 ? xx.subtract(x) : x.subtract(xx);
72+
prod = diff.multiply(prod).mod(N);
7173
}
7274
gcd = prod.gcd(N);
7375
} while (gcd.equals(I_1));
@@ -76,7 +78,8 @@ public BigInteger findSingleFactor(BigInteger N) {
7678
xs = squareAddModN(xs, c);
7779
xxs = squareAddModN(xxs, c);
7880
xxs = squareAddModN(xxs, c);
79-
gcd = N.gcd(xs.subtract(xxs));
81+
final BigInteger diff = xs.compareTo(xxs) < 0 ? xxs.subtract(xs) : xs.subtract(xxs);
82+
gcd = diff.gcd(N);
8083
} while (gcd.equals(I_1));
8184
}
8285
// leave loop if factor found; otherwise continue with a new random c

src/main/java/de/tilman_neumann/jml/factor/pollardRho/PollardRhoTwoLoopsModBlock.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,14 @@ public BigInteger findSingleFactor(BigInteger N) {
8282
x = squareAddModN(x, c);
8383
xx = squareAddModN(xx, c);
8484
xx = squareAddModN(xx, c);
85-
prod = prod.multiply(x.subtract(xx));
85+
// In BigInteger variants it is slightly faster to use the absolute value of the difference
86+
final BigInteger diff = x.compareTo(xx) < 0 ? xx.subtract(x) : x.subtract(xx);
87+
prod = diff.multiply(prod);
8688
}
8789
prod = prod.mod(N);
8890
}
8991
gcd = prod.gcd(N);
90-
// grow the gdBlock like R in Brent
92+
// grow the gdBlock like R in Brent; this improves performance for "larger" N only, like 70 bit or more
9193
if (gcdBlock<gcdBlockMax) {
9294
gcdBlock <<= 1;
9395
if (gcdBlock > gcdBlockMax) gcdBlock = gcdBlockMax;
@@ -99,7 +101,8 @@ public BigInteger findSingleFactor(BigInteger N) {
99101
xs = squareAddModN(xs, c);
100102
xxs = squareAddModN(xxs, c);
101103
xxs = squareAddModN(xxs, c);
102-
gcd = xs.subtract(xxs).gcd(N);
104+
final BigInteger diff = xs.compareTo(xxs) < 0 ? xxs.subtract(xs) : xs.subtract(xxs);
105+
gcd = diff.gcd(N);
103106
} while (gcd.equals(I_1));
104107
}
105108
// leave loop if factor found; otherwise continue with a new random c

0 commit comments

Comments
 (0)