Skip to content

Commit

Permalink
lint recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
teschlg committed Nov 7, 2024
1 parent 8bb1aeb commit c744c40
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion doc/kryptools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@
{
"data": {
"text/plain": [
"268"
"271"
]
},
"execution_count": 27,
Expand Down
2 changes: 1 addition & 1 deletion kryptools/Zmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, n: int, short: bool = True):
def __call__(self, x: int | list | tuple):
if isinstance(x, list):
return [ZmodPoint(xx, self) for xx in x]
elif isinstance(x, tuple):
if isinstance(x, tuple):
return (ZmodPoint(xx, self) for xx in x)
return ZmodPoint(x, self)

Expand Down
14 changes: 7 additions & 7 deletions kryptools/factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ def divisors(n:int, proper:bool = False) -> int:
raise ValueError("Number must be an integer!")
n = abs(n)
if proper:
divisors = []
divisor_list = []
else:
divisors = [1]
divisor_list = [1]
if n <= 1:
return divisors
return divisor_list
facctordict= factorint(n)
primes = [p for p in facctordict]
nprimes = len(primes)
Expand All @@ -200,7 +200,7 @@ def divisors(n:int, proper:bool = False) -> int:
d = prod(primes[i] ** exponents[i] for i in range(nprimes))
if d == n:
if proper:
return divisors
divisors.append(n)
return divisors
divisors.append(d)
return divisor_list
divisor_list.append(n)
return divisor_list
divisor_list.append(d)
6 changes: 3 additions & 3 deletions kryptools/la.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ def __eq__(self, other):
return False
if self.rows != other.rows or self.cols != other.cols:
return False
return any([self[i] == other[i] for i in range(self.rows * self.cols)])
return any(self[i] == other[i] for i in range(self.rows * self.cols))

def __bool__(self):
return any([bool(self[i]) for i in range(self.rows * self.cols)])
return any(bool(self[i]) for i in range(self.rows * self.cols))

def map(self, func):
"Apply a function to all elements in place."
Expand Down Expand Up @@ -277,7 +277,7 @@ def is_integer(i):
if isinstance(i, int) or (isinstance(i, Fraction) and i.denominator == 1):
return True
return False
return all([is_integer(i) for i in self]) and self.det()**2 == 1
return all(is_integer(i) for i in self) and self.det()**2 == 1

def zeros(self, m: int = None, n: int = None):
"Returns a zero matrix of the same dimension."
Expand Down
8 changes: 4 additions & 4 deletions kryptools/lat.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def babai_round_cvp(x: Matrix, U: Matrix) -> Matrix:

def babai_round_bnd(U: Matrix) -> float:
"Bound for Babai's rounding algorithm for solving the CVPwith respect to the sup norm."
return floor(1 / (2 * max([U.inv()[i, :].norm(1) for i in range(U.rows)])))
return floor(1 / (2 * max(U.inv()[i, :].norm(1) for i in range(U.rows))))


def babai_plane_cvp(x: Matrix, U: Matrix) -> Matrix:
Expand All @@ -104,7 +104,7 @@ def babai_plane_cvp(x: Matrix, U: Matrix) -> Matrix:
def babai_plane_bnd(U: Matrix, p=2) -> float:
"Bound for Babai's closest plane algorithm for solving the CVP with respect to the Euclidean norm (p=2) or sup norm (p=inf)."
Us = gram_schmidt(U)[0]
return float(0.5 * min([Us[:, i].norm(p) for i in range(Us.rows)]))
return float(0.5 * min(Us[:, i].norm(p) for i in range(Us.rows)))


def lagrange_lr(V: Matrix) -> Matrix:
Expand Down Expand Up @@ -196,10 +196,10 @@ def random_unimodular_matrix(n: int, iterations: int = 50, max_val: int = None)
for _ in range(iterations):
i, j = sample(range(n), 2)
tmp = W[i, :] + choice([-1, 1]) * W[j, :]
if not max_val or max([abs(x) for x in tmp]) <= max_val:
if not max_val or max(abs(x) for x in tmp) <= max_val:
W[i, :] = tmp
i, j = sample(range(n), 2)
tmp = W[:, i] + choice([-1, 1]) * W[:, j]
if not max_val or max([abs(x) for x in tmp]) <= max_val:
if not max_val or max(abs(x) for x in tmp) <= max_val:
W[:, i] = tmp
return W
4 changes: 2 additions & 2 deletions kryptools/nt.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def jacobi_symbol(a: int, n: int) -> int:
while a % 2 == 0:
a //= 2
tmp = n % 8
if tmp == 3 or tmp == 5:
if tmp in (3, 5):
t *= -1
a, n = n, a
if a % 4 == 3 and n % 4 == 3:
Expand All @@ -111,7 +111,7 @@ def jacobi_symbol(a: int, n: int) -> int:
def sqrt_mod(a: int, p: int) -> int:
"""Compute a square root of `a` modulo `p` unsing Cipolla's algorithm."""
a %= p
if a == 0 or a == 1:
if a in (0, 1):
return a
if pow(a, (p - 1) // 2, p) != 1: # Legendre symbol must be equal one
return None
Expand Down
10 changes: 3 additions & 7 deletions kryptools/poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,7 @@ def __mul__(self, other: "Poly") -> "Poly":
ls, lo = len(self.coeff), len(other.coeff)
coeff = [None] * (ls + lo - 1)
for k in range(ls + lo - 1):
coeff[k] = sum(
[
self.coeff[j] * other.coeff[k - j]
for j in range(max(0, k - lo + 1), min(ls, k + 1))
], start=zero)
coeff[k] = sum((self.coeff[j] * other.coeff[k - j] for j in range(max(0, k - lo + 1), min(ls, k + 1))), start=zero)
modulus = self.modulus
if not modulus and other.modulus:
modulus = other.modulus
Expand Down Expand Up @@ -218,11 +214,11 @@ def __pow__(self, j: int) -> "Poly":

def max(self) -> int:
"Maximum of the absolute value of all coefficients."
return max([abs(c) for c in self.coeff])
return max(abs(c) for c in self.coeff)

def sum(self) -> int:
"Sum of the absolute value of all coefficients."
return sum([abs(c) for c in self.coeff])
return sum(abs(c) for c in self.coeff)

def __floordiv__(self, other: "Poly") -> "Poly":
return self.divmod(other)[0]
Expand Down

0 comments on commit c744c40

Please sign in to comment.