diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..e3dfea3 --- /dev/null +++ b/.flake8 @@ -0,0 +1,12 @@ +[flake8] +max-line-length = 88 +max-complexity = 10 +select = C,E,F,W,B,B950 +ignore = E203,E501,W503,C901 +exclude = + .git, + __pycache__, + *.egg-info, + .nox, + .pytest_cache, + .mypy_cache diff --git a/discretelog/__init__.py b/discretelog/__init__.py index 6523b3a..05e7915 100644 --- a/discretelog/__init__.py +++ b/discretelog/__init__.py @@ -44,7 +44,7 @@ def pollard_rho_dlg(g, v, q, DEBUG=False): sn = 4 * isqrt(int(pi * n / 8)) x, a, b = 1, 0, 0 X, A, B = x, a, b - for i in mrange(1, sn, DEBUG=DEBUG): + for _ in mrange(1, sn, DEBUG=DEBUG): x, a, b = new_xab[x % 3](x, a, b) X, A, B = new_xab[X % 3](X, A, B) X, A, B = new_xab[X % 3](X, A, B) @@ -103,7 +103,7 @@ def dlog_prime_power_order(g, h, q, p, e, DEBUG=False): x = 0 pk = pe // p gamma = pow(g, pk, q) - for k in range(e): + for _ in range(e): hk = pow(pow(g, -x, q) * h, pk, q) dk = dlog_prime_order(gamma, hk, q, p, DEBUG) x = (x + pe // pk // p * dk) % pe diff --git a/discretelog/block_lanczos.py b/discretelog/block_lanczos.py index 597a1c7..3c26350 100644 --- a/discretelog/block_lanczos.py +++ b/discretelog/block_lanczos.py @@ -38,7 +38,7 @@ def spmax(mat): mx = 0 for i in range(len(mat)): c1, c2 = 0, 0 - for j, v in mat[i]: + for _, v in mat[i]: if v > 0: c1 += v else: diff --git a/discretelog/common.py b/discretelog/common.py index b355569..4e5e530 100644 --- a/discretelog/common.py +++ b/discretelog/common.py @@ -40,13 +40,13 @@ def combine(c1, c2): a2, m2 = c2 a, b, c = m1, a2 - a1, m2 g = gcd(a, b, c) - a, b, c = [i//g for i in [a, b, c]] + a, b, c = [i // g for i in [a, b, c]] if a != 1: inv_a, _, g = egcd(a, c) if g != 1: return None b *= inv_a - a, m = a1 + m1*b, m1*c + a, m = a1 + m1 * b, m1 * c return a, m rv = (0, 1) diff --git a/discretelog/linear_sieve_index_calculus.py b/discretelog/linear_sieve_index_calculus.py index e4f7b84..d8c471d 100644 --- a/discretelog/linear_sieve_index_calculus.py +++ b/discretelog/linear_sieve_index_calculus.py @@ -134,7 +134,7 @@ def reduce_row(c, orig, new): w = len([v for v in rels[i] if v != 0]) rw += [(w, i)] rw.sort() - for w, i in reversed(rw): + for _w, i in reversed(rw): rn -= 1 marked[i] = True if rn <= kp * extrarels: @@ -163,7 +163,7 @@ def rels_filter(rels, relsex, k): well_connected(k, graph, degc) n = len(rels) marked = [False] * n - for i, (cr, crex) in enumerate(zip(rels, relsex)): + for i, (_cr, crex) in enumerate(zip(rels, relsex)): pos = False for c in crex: if degc[c] <= 1: @@ -256,7 +256,7 @@ def linear_sieve(p, A, B, def worker_linear_sieve(queue, rc2, p, H, clog, eps, qlimit, fbq, fbqlogs): for c2 in rc2: - A, B, climit = H + c2, H, c2+1 + A, B, climit = H + c2, H, c2 + 1 res = [] for c1, fn in linear_sieve(p, A, B, climit, clog, eps, qlimit, fbq, fbqlogs): @@ -318,7 +318,7 @@ def individual_logs0(dlogs, Hlogs, y, g, p, op, qlimit, climit, DEBUG=False): if DEBUG: print(f'individual log: y={y} g={g} op={op}') assert pow(y, op, p) == 1 - U = L(p, 2/3, 3 ** (-1/3)) + U = L(p, 2 / 3, 3 ** (-1 / 3)) H = isqrt(p) + 1 yy = y @@ -383,7 +383,7 @@ def __init__(self, H, p, opq, fbq, fbqlogs, ifbq, qlimit, np): def sieve_values(self, n, DEBUG=False): for c2 in mrange(self.c2, self.c2 + n, DEBUG=DEBUG): - for c1, fn in linear_sieve(self.p, self.H + c2, self.H, c2+1, + for c1, fn in linear_sieve(self.p, self.H + c2, self.H, c2 + 1, self.clog, self.eps, self.qlimit, self.fbq, self.fbqlogs): yield c2, c1, fn @@ -403,7 +403,7 @@ def sieve_values_parallel(self, n, DEBUG): def get(self, n, DEBUG=False): if DEBUG: - print(f'Searching congruences in range {self.c2}-{self.c2+n}') + print(f'Searching congruences in range {self.c2}-{self.c2 + n}') rels, relsex = [], [] self.infb += [None] * n self.clog += [log(c) if c > 0 else 0 @@ -514,7 +514,7 @@ def linear_sieve_dlog(p, gy, y, op=None, qlimit=None, climit=None, DEBUG=False): if rounds > 10: if DEBUG: print('solution not converging, trying higher qlimit') - return linear_sieve_dlog(p, gy, y, op, qlimit+50, climit, DEBUG) + return linear_sieve_dlog(p, gy, y, op, qlimit + 50, climit, DEBUG) nclimit = climit if first else max(50, climit // 10) if DEBUG: print(f'\nSolving: p={p} gy={gy} y={y} fbp={len(fbq)}', @@ -575,7 +575,7 @@ def linear_sieve_dlog(p, gy, y, op=None, qlimit=None, climit=None, DEBUG=False): exps[g] = 1 Hexps = [None] * climit solved = True - for i in range(m + np-1): + for i in range(m + np - 1): if x[i] is not None: if i < m: v = iinfb[ikmap[i]] @@ -600,8 +600,8 @@ def linear_sieve_dlog(p, gy, y, op=None, qlimit=None, climit=None, DEBUG=False): other = Hexps[iinfb[c]] v = iinfb[unknown[0]] pf = {fbq[j]: rel[j] for j in range(np) if rel[j] != 0} - Hexps[v] = (compute_dlog(exps, pf, opq) * - (opq + 1) // 2 - other) % opq + Hexps[v] = (compute_dlog(exps, pf, opq) + * (opq + 1) // 2 - other) % opq if DEBUG: if not solved: print(f'more relations required {len(exps)}/{np}') diff --git a/discretelog/naive_index_calculus.py b/discretelog/naive_index_calculus.py index e29832c..9611f91 100644 --- a/discretelog/naive_index_calculus.py +++ b/discretelog/naive_index_calculus.py @@ -20,7 +20,7 @@ def producttree(X): def remaindersusingproducttree(n, T): result = [n] for t in reversed(T): - result = [result[i//2] % t[i] for i in range(len(t))] + result = [result[i // 2] % t[i] for i in range(len(t))] return result @@ -127,7 +127,7 @@ def check_dlogs(g, p, q, exponents, bases): def msolve_prime(M, q, DEBUG=False): if DEBUG: print(f'solving linear system {len(M)}x{len(M[0])}') - n = len(M[0])-1 + n = len(M[0]) - 1 m = modMatrix(M, q) if not row_reduce(m, q): return None @@ -136,12 +136,12 @@ def msolve_prime(M, q, DEBUG=False): def dlog_prime(b, h, p, DEBUG=False): if p <= 10 ** 5: - assert False, f'{p} is too small' + raise AssertionError(f'{p} is too small') q = multiplicative_order(b, p) assert isprime(q) o = phi(p) g = primitive_root(p) - B = int(4 * exp(sqrt(log(p) * log(log(p)))/2)) + 10 + B = int(4 * exp(sqrt(log(p) * log(log(p))) / 2)) + 10 if DEBUG: print("p: {}, b: {}, h: {}, B: {}".format(p, b, h, B)) congs = [] diff --git a/poetry.lock b/poetry.lock index 6124269..b47b775 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,5 +1,24 @@ # This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. +[[package]] +name = "attrs" +version = "23.2.0" +description = "Classes Without Boilerplate" +optional = false +python-versions = ">=3.7" +files = [ + {file = "attrs-23.2.0-py3-none-any.whl", hash = "sha256:99b87a485a5820b23b879f04c2305b44b951b502fd64be915879d77a7e8fc6f1"}, + {file = "attrs-23.2.0.tar.gz", hash = "sha256:935dc3b529c262f6cf76e50877d35a4bd3c1de194fd41f47a2b7ae8f19971f30"}, +] + +[package.extras] +cov = ["attrs[tests]", "coverage[toml] (>=5.3)"] +dev = ["attrs[tests]", "pre-commit"] +docs = ["furo", "myst-parser", "sphinx", "sphinx-notfound-page", "sphinxcontrib-towncrier", "towncrier", "zope-interface"] +tests = ["attrs[tests-no-zope]", "zope-interface"] +tests-mypy = ["mypy (>=1.6)", "pytest-mypy-plugins"] +tests-no-zope = ["attrs[tests-mypy]", "cloudpickle", "hypothesis", "pympler", "pytest (>=4.3.0)", "pytest-xdist[psutil]"] + [[package]] name = "colorama" version = "0.4.6" @@ -92,6 +111,40 @@ files = [ [package.extras] test = ["pytest (>=6)"] +[[package]] +name = "flake8" +version = "7.0.0" +description = "the modular source code checker: pep8 pyflakes and co" +optional = false +python-versions = ">=3.8.1" +files = [ + {file = "flake8-7.0.0-py2.py3-none-any.whl", hash = "sha256:a6dfbb75e03252917f2473ea9653f7cd799c3064e54d4c8140044c5c065f53c3"}, + {file = "flake8-7.0.0.tar.gz", hash = "sha256:33f96621059e65eec474169085dc92bf26e7b2d47366b70be2f67ab80dc25132"}, +] + +[package.dependencies] +mccabe = ">=0.7.0,<0.8.0" +pycodestyle = ">=2.11.0,<2.12.0" +pyflakes = ">=3.2.0,<3.3.0" + +[[package]] +name = "flake8-bugbear" +version = "24.4.26" +description = "A plugin for flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle." +optional = false +python-versions = ">=3.8.1" +files = [ + {file = "flake8_bugbear-24.4.26-py3-none-any.whl", hash = "sha256:cb430dd86bc821d79ccc0b030789a9c87a47a369667f12ba06e80f11305e8258"}, + {file = "flake8_bugbear-24.4.26.tar.gz", hash = "sha256:ff8d4ba5719019ebf98e754624c30c05cef0dadcf18a65d91c7567300e52a130"}, +] + +[package.dependencies] +attrs = ">=19.2.0" +flake8 = ">=6.0.0" + +[package.extras] +dev = ["coverage", "hypothesis", "hypothesmith (>=0.2)", "pre-commit", "pytest", "tox"] + [[package]] name = "iniconfig" version = "2.0.0" @@ -103,6 +156,17 @@ files = [ {file = "iniconfig-2.0.0.tar.gz", hash = "sha256:2d91e135bf72d31a410b17c16da610a82cb55f6b0477d1a902134b24a455b8b3"}, ] +[[package]] +name = "mccabe" +version = "0.7.0" +description = "McCabe checker, plugin for flake8" +optional = false +python-versions = ">=3.6" +files = [ + {file = "mccabe-0.7.0-py2.py3-none-any.whl", hash = "sha256:6c2d30ab6be0e4a46919781807b4f0d834ebdd6c6e3dca0bda5a15f863427b6e"}, + {file = "mccabe-0.7.0.tar.gz", hash = "sha256:348e0240c33b60bbdf4e523192ef919f28cb2c3d7d5c7794f74009290f236325"}, +] + [[package]] name = "packaging" version = "24.0" @@ -151,6 +215,28 @@ files = [ {file = "py_cpuinfo-9.0.0-py3-none-any.whl", hash = "sha256:859625bc251f64e21f077d099d4162689c762b5d6a4c3c97553d56241c9674d5"}, ] +[[package]] +name = "pycodestyle" +version = "2.11.1" +description = "Python style guide checker" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pycodestyle-2.11.1-py2.py3-none-any.whl", hash = "sha256:44fe31000b2d866f2e41841b18528a505fbd7fef9017b04eff4e2648a0fadc67"}, + {file = "pycodestyle-2.11.1.tar.gz", hash = "sha256:41ba0e7afc9752dfb53ced5489e89f8186be00e599e712660695b7a75ff2663f"}, +] + +[[package]] +name = "pyflakes" +version = "3.2.0" +description = "passive checker of Python programs" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pyflakes-3.2.0-py2.py3-none-any.whl", hash = "sha256:84b5be138a2dfbb40689ca07e2152deb896a65c3a3e24c251c5c62489568074a"}, + {file = "pyflakes-3.2.0.tar.gz", hash = "sha256:1c61603ff154621fb2a9172037d84dca3500def8c8b630657d1701f026f8af3f"}, +] + [[package]] name = "pytest" version = "8.2.0" @@ -227,4 +313,4 @@ telegram = ["requests"] [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "2537dbf5f502190101c12e43e24a9ea7c6e2b60da59aef9dcf2d6d45d00a4c56" +content-hash = "9617e3196c643baf4b9d2a52e71d52234c64d4210bbef247547726f17e56341b" diff --git a/pyproject.toml b/pyproject.toml index 23cd388..3b88d62 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,8 @@ pytest-benchmark = "^4.0.0" [tool.poetry.group.dev.dependencies] coverage = {extras = ["toml"], version = "^7.5.1"} +flake8 = "^7.0.0" +flake8-bugbear = "^24.4.26" [tool.coverage.run] omit = [".*", "*/site-packages/*"]