Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
teschlg committed Jul 19, 2024
1 parent 4b21bdf commit 2154fa7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 11 additions & 1 deletion kryptools/la.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from math import inf, sqrt, prod
from numbers import Number

from fractions import Fraction

class Matrix:
"""
Expand Down Expand Up @@ -246,6 +246,16 @@ def inv(self) -> "Matrix":
raise ValueError("Matrix is not invertible!")
return MM[:,n:]

def is_unimodular(self) -> bool:
"Test if the matrix is unimodular."
if self.rows != self.rows:
return False
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

def zeros(self, m: int = None, n: int = None):
"Returns a zero matrix of the same dimension"
if not m and not n:
Expand Down
6 changes: 3 additions & 3 deletions kryptools/lat.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ def hadamard_ratio(M: Matrix) -> float:
return (gram_det(M) / prod([M[:, i].norm() for i in range(m)])) ** (1 / m)

def babai_round_cvp(x: Matrix, U: Matrix) -> Matrix:
"Babai's rounding algorithm for solving the CVP."
"Babai's rounding algorithm for approximately solving the CVP."
s = U.inv() * x
k = s.applyfunc(round)
return U * k

def babai_plane_cvp(x: Matrix, U: Matrix) -> Matrix:
"Babai's closest plane algorithm for solving the CVP."
"Babai's closest plane algorithm for approximately solving the CVP."
Us = gram_schmidt(U)[0]
y = x
for k in range(U.cols - 1, -1, -1):
Expand All @@ -99,7 +99,7 @@ def lagrange_lr(V: Matrix) -> Matrix:
return Matrix([list(v1), list(v3)]).transpose()

def lll(V: Matrix, delta: float = 0.75, sort: bool = True) -> Matrix:
"lll algorithm for lattice reduction"
"LLL algorithm for lattice reduction."

assert 0 < delta <= 1, f"LLL reqires 0 < delta={delta} <= 1"
j = 1
Expand Down

0 comments on commit 2154fa7

Please sign in to comment.