Skip to content

Commit

Permalink
added linting support
Browse files Browse the repository at this point in the history
  • Loading branch information
gilcu3 committed May 18, 2024
1 parent 31d458b commit fefe19a
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 20 deletions.
12 changes: 12 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions discretelog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion discretelog/block_lanczos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions discretelog/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions discretelog/linear_sieve_index_calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)}',
Expand Down Expand Up @@ -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]]
Expand All @@ -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}')
Expand Down
8 changes: 4 additions & 4 deletions discretelog/naive_index_calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand Down
88 changes: 87 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"]
Expand Down

0 comments on commit fefe19a

Please sign in to comment.