Coverage for src\pqlattice\lattice\_cvp.py: 54%

24 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-01-07 03:12 +0100

1import numpy as np 

2 

3from .._utils import as_integer, as_rational 

4from ..typing import SquareMatrix, Vector, validate_aliases 

5from ._gso import gso, project_coeffs 

6from ._lll import lll 

7 

8 

9@validate_aliases 

10def schnorr_euchner_cvp(mu: SquareMatrix, B: Vector, target_coeffs: Vector) -> Vector: 

11 raise NotImplementedError() 

12 

13 

14@validate_aliases 

15def closest_vector(lattice_basis: SquareMatrix, target_vector: Vector) -> Vector: 

16 raise NotImplementedError() 

17 

18 

19@validate_aliases 

20def babai_nearest_plane(lattice_basis: SquareMatrix, target_vector: Vector) -> Vector: 

21 """_summary_ 

22 

23 Parameters 

24 ---------- 

25 lattice_basis : SquareMatrix 

26 _description_ 

27 target_vector : Vector 

28 _description_ 

29 

30 Returns 

31 ------- 

32 Vector 

33 _description_ 

34 """ 

35 n, _ = lattice_basis.shape 

36 B = lll(lattice_basis) 

37 b = as_rational(target_vector) 

38 for j in range(n - 1, -1, -1): 

39 B_star, _ = gso(B) 

40 cj = round(project_coeffs(B_star[j], b)) 

41 b -= cj * B[j] 

42 

43 return as_integer(as_rational(target_vector) - b) 

44 

45 

46@validate_aliases 

47def babai_closest_vector(lattice_basis: SquareMatrix, target_vector: Vector) -> Vector: 

48 """_summary_ 

49 

50 Parameters 

51 ---------- 

52 lattice_basis : SquareMatrix 

53 _description_ 

54 target_vector : Vector 

55 _description_ 

56 

57 Returns 

58 ------- 

59 Vector 

60 _description_ 

61 """ 

62 return as_integer(np.rint(target_vector @ np.linalg.inv(lattice_basis.astype(float))))