File tree Expand file tree Collapse file tree 4 files changed +17
-7
lines changed
src/main/java/de/tilman_neumann/jml/factor/pollardRho Expand file tree Collapse file tree 4 files changed +17
-7
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ public BigInteger findSingleFactor(BigInteger N) {
71
71
final int iMax = Math .min (m , r -k );
72
72
for (int i =1 ; i <=iMax ; i ++) {
73
73
y = squareAddModN (y , c );
74
+ // In BigInteger variants it is slightly faster to use the absolute value of the difference
74
75
final BigInteger diff = x .compareTo (y ) < 0 ? y .subtract (x ) : x .subtract (y );
75
76
q = diff .multiply (q ).mod (N );
76
77
}
Original file line number Diff line number Diff line change @@ -81,7 +81,9 @@ public BigInteger findSingleFactor(BigInteger N) {
81
81
for (int i = 1 ; i <= iMax ; i = i + jMax ) {
82
82
for (int j = 0 ; j < jMax ; j ++) {
83
83
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 );
85
87
}
86
88
q = q .mod (N ); // one mod for the block
87
89
}
@@ -97,7 +99,8 @@ public BigInteger findSingleFactor(BigInteger N) {
97
99
if (G .equals (N )) {
98
100
do {
99
101
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 );
101
104
} while (G .equals (I_1 ));
102
105
if (DEBUG ) LOG .debug ("G = " + G );
103
106
}
Original file line number Diff line number Diff line change @@ -67,7 +67,9 @@ public BigInteger findSingleFactor(BigInteger N) {
67
67
x = squareAddModN (x , c );
68
68
xx = squareAddModN (xx , c );
69
69
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 );
71
73
}
72
74
gcd = prod .gcd (N );
73
75
} while (gcd .equals (I_1 ));
@@ -76,7 +78,8 @@ public BigInteger findSingleFactor(BigInteger N) {
76
78
xs = squareAddModN (xs , c );
77
79
xxs = squareAddModN (xxs , c );
78
80
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 );
80
83
} while (gcd .equals (I_1 ));
81
84
}
82
85
// leave loop if factor found; otherwise continue with a new random c
Original file line number Diff line number Diff line change @@ -82,12 +82,14 @@ public BigInteger findSingleFactor(BigInteger N) {
82
82
x = squareAddModN (x , c );
83
83
xx = squareAddModN (xx , c );
84
84
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 );
86
88
}
87
89
prod = prod .mod (N );
88
90
}
89
91
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
91
93
if (gcdBlock <gcdBlockMax ) {
92
94
gcdBlock <<= 1 ;
93
95
if (gcdBlock > gcdBlockMax ) gcdBlock = gcdBlockMax ;
@@ -99,7 +101,8 @@ public BigInteger findSingleFactor(BigInteger N) {
99
101
xs = squareAddModN (xs , c );
100
102
xxs = squareAddModN (xxs , c );
101
103
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 );
103
106
} while (gcd .equals (I_1 ));
104
107
}
105
108
// leave loop if factor found; otherwise continue with a new random c
You can’t perform that action at this time.
0 commit comments