Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes in fpylll.algorithms.babai #274

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/fpylll/algorithms/babai.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
"""

from fpylll import IntegerMatrix, LLL
from math import ceil
from math import isqrt


def babai(B, t):
def babai(B, t, *args, **kwargs):
"""
Run Babai's Nearest Plane algorithm by running LLL.

:param B: Input lattice basis.
:param target: Target point (∈ ZZ^n)
:param args: Passed onto LLL.reduction
:param kwargs: Passed onto LLL.reduction
:returns coordinates of the solution vector:

This implementation is more numerically stable compared to the one offered by `MatGSO.babai()`.
Expand Down Expand Up @@ -46,15 +48,16 @@ def babai(B, t):
A[i, j] = B[i, j]

# make sure the input is LLL reduced before reading the norm of the last vector
LLL.reduction(A)
LLL.reduction(A, *args, **kwargs)
# zero vector at the end
A.swap_rows(0, B.nrows)

for j in range(B.ncols):
A[-1, j] = t[j]
A[-1, -1] = ceil(A[-2].norm())
# precise norm
A[-1, -1] = isqrt(sum(x**2 for x in A[-2])) + 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be wrong if the sqrt is an integer, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes, but it doesn't matter if it's too big only too small

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should at least be documented lest future readers stumble over this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, does this look better?


LLL.reduction(A) # now call LLL to run Babai
LLL.reduction(A, *args, **kwargs) # now call LLL to run Babai

v = [0] * len(t)
if A[-1, -1] > 0:
Expand Down
Loading