Skip to content

Commit f0a4abd

Browse files
committed
Detect 2^64 overflow while computing 5-smooth factorization for very-
high-value numbers. The numbers for which overflow would occur are those >= 0.2*2^64.
1 parent 2b09821 commit f0a4abd

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/util/integerfactoring.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,12 +995,22 @@ static int gaIFactorize5Smooth(uint64_t n, ga_factor_list* fl){
995995
}
996996

997997
for(i3=0, p3=1;i3<=40;i3++, p3*=3){
998+
/**
999+
* Detect when the product p3*p5 would overflow 2^64.
1000+
*/
1001+
1002+
if(i3){
1003+
nCurr = (p3/3)*p5;
1004+
if(nCurr+nCurr < nCurr || nCurr+nCurr+nCurr < nCurr+nCurr){
1005+
break;
1006+
}
1007+
}
9981008
nCurr = p3*p5;
9991009

10001010
/**
10011011
* If the current product of powers of 3 and 5 is >= n, then this
1002-
* must be the last iteration, but perhaps a pure power of 3 is the
1003-
* best choice, so check for this.
1012+
* must be the last iteration, but perhaps a pure product of powers
1013+
* of 3 and 5 is the best choice, so check for this.
10041014
*/
10051015

10061016
if(nCurr >= n){

0 commit comments

Comments
 (0)