Skip to content

Commit

Permalink
fix factor test and cado-nfs output
Browse files Browse the repository at this point in the history
  • Loading branch information
gilcu3 committed May 12, 2024
1 parent 36d5109 commit f57383c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
16 changes: 11 additions & 5 deletions discretelog/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ def factor_cado_nfs(n, DEBUG=False):
ds = mexec('cado-nfs %d' % n, DEBUG)
ds = ds.split('\n')[-1]
ps = list(map(int, ds.split()))
return ps
nn = n
pss = []
for p in ps:
while nn % p == 0:
nn //= p
pss += [p]
return pss


def factor_yafu(n):
f = mexec('yafu %d' % n)
def factor_yafu(n, DEBUG=False):
f = mexec('yafu %d' % n, DEBUG=DEBUG)
op = False
ps = []
for line in f.split('\n'):
Expand All @@ -94,9 +100,9 @@ def factor(n, DEBUG=False):
if n <= 10 ** 20:
ps = list(primefac(n, verbose=DEBUG))
elif n <= 10 ** 100:
ps = factor_yafu(n)
ps = factor_yafu(n, DEBUG=DEBUG)
else:
ps = factor_cado_nfs(n)
ps = factor_cado_nfs(n, DEBUG=DEBUG)
for p in ps:
if p in f:
f[p] += 1
Expand Down
25 changes: 18 additions & 7 deletions tests/test_nt_utils.py → tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
from primefac import isprime
from colorama import Fore

from discretelog.common import factor
from discretelog.common import factor, random_prime
from discretelog.utils import mrange


def single_test_factor(d, random):
n = random.randint(2 ** d, 2 ** (d + 1))
def single_test_factor(n):
print(Fore.GREEN + f'testing n={n}' + Fore.RESET)
fn = factor(n, DEBUG=True)
nn = 1
Expand All @@ -19,16 +18,28 @@ def single_test_factor(d, random):
assert nn == n


def single_test_factor_random(d, random):
n = random.randint(10 ** d, 10 ** (d + 1))
single_test_factor(n)


def single_test_factor_rsa(d, random):
n = random_prime(d, random) * random_prime(d, random)
single_test_factor(n)


def test_factor_small(frandom):
print('\n' + Fore.RED + 'testing factoring algorithms small'
+ Fore.RESET + '\n')
for d in mrange(20, 50, 1, True):
single_test_factor(d, frandom)
for d in mrange(10, 30, 1, True):
single_test_factor_random(d, frandom)


@pytest.mark.slow
def test_factor_large(frandom):
print('\n' + Fore.RED + 'testing factoring algorithms large'
+ Fore.RESET + '\n')
for d in mrange(50, 350, 10, True):
single_test_factor(d, frandom)
for d in mrange(30, 100, 10, True):
single_test_factor_random(d, frandom)
for d in mrange(30, 60, 10, True):
single_test_factor_rsa(d, frandom)

0 comments on commit f57383c

Please sign in to comment.