From f57383c28429b7f36f29f137586efbd294f86bc3 Mon Sep 17 00:00:00 2001 From: gilcu3 <828241+gilcu3@users.noreply.github.com> Date: Sun, 12 May 2024 16:46:36 +0200 Subject: [PATCH] fix factor test and cado-nfs output --- discretelog/common.py | 16 +++++++++----- tests/{test_nt_utils.py => test_common.py} | 25 ++++++++++++++++------ 2 files changed, 29 insertions(+), 12 deletions(-) rename tests/{test_nt_utils.py => test_common.py} (51%) diff --git a/discretelog/common.py b/discretelog/common.py index 829e4b4..60dbcd5 100644 --- a/discretelog/common.py +++ b/discretelog/common.py @@ -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'): @@ -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 diff --git a/tests/test_nt_utils.py b/tests/test_common.py similarity index 51% rename from tests/test_nt_utils.py rename to tests/test_common.py index d7676e4..67bf8c4 100644 --- a/tests/test_nt_utils.py +++ b/tests/test_common.py @@ -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 @@ -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)