Skip to content

Commit

Permalink
maxstep option for factor_fermat
Browse files Browse the repository at this point in the history
  • Loading branch information
teschlg committed Oct 23, 2024
1 parent 46ef893 commit a0a695e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 7 additions & 4 deletions kryptools/factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# Factoring


def _factor_fermat(n: int, steps: int = 10, verbose: int = 0) -> list:
def _factor_fermat(n: int, maxsteps: int = 10, verbose: int = 0) -> list:
"Fermat method"
if verbose:
print(f"Factoring (Fermat, steps={steps}): {n} ({floor(log10(n)) + 1} digits)")
print(f"Factoring (Fermat, steps={maxsteps}): {n} ({floor(log10(n)) + 1} digits)")
parameters = {11: (12, 6), 23: (12, 0),
5: (6, 3), 17: (6, 3),
19: (4, 2), 7: (4, 0),
Expand All @@ -27,7 +27,10 @@ def _factor_fermat(n: int, steps: int = 10, verbose: int = 0) -> list:
start += (mod - start) % step
if verbose > 1:
print("Working ", end= "")
for a in range(start, min(start + steps * step,(n + 9) // 6) + 1, step):
maxa = (n + 9) // 6 + 1
if maxsteps:
maxa = min(maxa, start + maxsteps * step + 1)
for a in range(start, maxa, step):
if verbose > 1:
print(".", end= "")
b = isqrt(a * a - n)
Expand Down Expand Up @@ -121,7 +124,7 @@ def add_factors(m: int, mm: tuple) -> None:
factors = list(remaining_factors)
for m in factors:
if method == _factor_fermat:
tmp = _factor_fermat(m, steps = fermat_steps, verbose = max(0, verbose - 1))
tmp = _factor_fermat(m, maxsteps = fermat_steps, verbose = max(0, verbose - 1))
elif method == factor_pm1:
tmp = factor_pm1(m, pm1_parameters = pm1_parameters, verbose = max(0, verbose - 1))
elif method == factor_ecm:
Expand Down
8 changes: 6 additions & 2 deletions kryptools/factor_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from math import isqrt, log10, floor, log10


def factor_fermat(n: int, verbose: int = 0) -> list:
def factor_fermat(n: int, maxsteps: int|None = None, verbose: int = 0) -> list:
"""Find factors of n using the method of Fermat."""
if verbose:
print(f"Factoring (Fermat): {n} ({floor(log10(n)) + 1} digits)")
Expand All @@ -24,7 +24,11 @@ def factor_fermat(n: int, verbose: int = 0) -> list:
start += (mod - start) % step
if verbose > 1:
print("Working ", end= "")
for a in range(start, (n + 9) // 6 + 1, step):
maxa = (n + 9) // 6 + 1
if maxsteps:
maxsteps = max(1, maxsteps)
maxa = min(maxa, start + maxsteps * step + 1)
for a in range(start, maxa, step):
if verbose > 1:
print(".", end= "")
b = isqrt(a * a - n)
Expand Down

0 comments on commit a0a695e

Please sign in to comment.