Skip to content

Commit 7e4c3ac

Browse files
committed
れどめとかを更新
1 parent fda2332 commit 7e4c3ac

File tree

2 files changed

+238
-6
lines changed

2 files changed

+238
-6
lines changed

PythonLatticeLibrary/PythonLatticeLibrary.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ def __init__(self, b: np.ndarray, n: int, m: int, dtype = None):
2323
self.B = np.zeros(n)
2424

2525
def __init__(self, b: np.ndarray, dtype = None):
26-
n, m = b.shape
2726
self.basis = np.copy(np.array(b, dtype = dtype))
27+
n, m = self.basis.shape
2828
self.nrows = n
2929
self.ncols = m
3030
self.mu = np.eye(n)
@@ -46,7 +46,7 @@ def vol(self) -> float:
4646
Returns:
4747
float: lattice
4848
"""
49-
return np.sqrt(np.linalg.det(self.basis * self.basis.T))
49+
return np.sqrt(np.linalg.det(np.matmul(self.basis, self.basis.T)))
5050

5151

5252
def volume(self) -> float:
@@ -55,7 +55,7 @@ def volume(self) -> float:
5555
Returns:
5656
float: lattice
5757
"""
58-
return np.sqrt(np.linalg.det(self.basis * self.basis.T))
58+
return np.sqrt(np.linalg.det(np.matmul(self.basis, self.basis.T)))
5959

6060

6161
def det(self) -> float:
@@ -64,7 +64,7 @@ def det(self) -> float:
6464
Returns:
6565
float: Determinant of lattice.
6666
"""
67-
return np.sqrt(np.linalg.det(self.basis * self.basis.T))
67+
return np.sqrt(np.linalg.det(np.matmul(self.basis, self.basis.T)))
6868

6969

7070
def dual(self):

README.md

Lines changed: 234 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,237 @@ Python Lattice Library<br>
33
This is a python library to use lattices on python.<br>
44
This library has algorithms to lattice reduce, solve SVP, solve CVP, and other many operation for lattices.
55

6-
# What Functions are Available?
7-
Available functions in this library are below:
6+
## How to Use?
7+
### Initialization
8+
If you want to use ```A = numpy.array([[123, 0, 0], [234, 1, 0], [345, 0, 1]])``` as lattice basis matrix, you can do like below:
9+
10+
```Python
11+
import PythonLatticeLibrary as PLL
12+
import numpy as np
13+
14+
A = np.array([[123, 0, 0], [234, 1, 0], [345, 0, 1]])
15+
b = PLL.lattice(A)
16+
b.print()
17+
```
18+
Output is below.
19+
20+
```
21+
Basis =
22+
[[123 0 0]
23+
[234 1 0]
24+
[345 0 1]]
25+
Rank = 3
26+
Volume = 122.99999999999994
27+
```
28+
29+
Not only numpy.array but list is available for argument of ``PLL.lattice()``:
30+
31+
```Python
32+
import PythonLatticeLibrary as PLL
33+
import numpy as np
34+
35+
A = [[123, 0, 0], [234, 1, 0], [345, 0, 1]]
36+
b = PLL.lattice(A)
37+
b.print()
38+
```
39+
Output is below.
40+
41+
```
42+
Basis =
43+
[[123 0 0]
44+
[234 1 0]
45+
[345 0 1]]
46+
Rank = 3
47+
Volume = 122.99999999999994
48+
```
49+
50+
You can generates a random lattice like below:
51+
52+
```Python
53+
import PythonLatticeLibrary as PLL
54+
55+
b = PLL.random_lattice(5) # Generates 5-dimensional lattice basis.
56+
b.print()
57+
```
58+
59+
Output is below:
60+
61+
```
62+
Basis =
63+
[[875 0 0 0 0]
64+
[932 1 0 0 0]
65+
[951 0 1 0 0]
66+
[801 0 0 1 0]
67+
[754 0 0 0 1]]
68+
Rank = 5
69+
Volume = 875.0000000044823
70+
```
71+
72+
You can reduce lattice basis(e.g. LLL-reduction, Deep-LLL-reduction, etc.) like below:
73+
74+
```Python
75+
import PythonLatticeLibrary as PLL
76+
77+
b = PLL.random_lattice(5)
78+
b.print()
79+
b.basis = b.LLL() # LLL-reduction
80+
b.print()
81+
```
82+
83+
Output is below.
84+
85+
```
86+
Basis =
87+
[[711 0 0 0 0]
88+
[940 1 0 0 0]
89+
[500 0 1 0 0]
90+
[592 0 0 1 0]
91+
[555 0 0 0 1]]
92+
Rank = 5
93+
Volume = 710.9999999597205
94+
Basis =
95+
[[ 0 1 2 1 -2]
96+
[-1 2 2 -1 1]
97+
[ 1 -3 2 0 2]
98+
[ 0 2 -1 3 2]
99+
[-4 -1 1 1 1]]
100+
Rank = 5
101+
Volume = 710.9999999999999
102+
```
103+
104+
You can select reduction parameter:
105+
106+
```Python
107+
import PythonLatticeLibrary as PLL
108+
109+
b = PLL.random_lattice(5)
110+
b.print()
111+
b.basis = b.LLL(delta=0.5) # LLL-reduction
112+
b.print()
113+
```
114+
115+
Output is below:
116+
117+
```
118+
Basis =
119+
[[814 0 0 0 0]
120+
[659 1 0 0 0]
121+
[723 0 1 0 0]
122+
[912 0 0 1 0]
123+
[781 0 0 0 1]]
124+
Rank = 5
125+
Volume = 813.9999999534335
126+
Basis =
127+
[[ 1 1 -1 1 1]
128+
[-2 -1 1 0 2]
129+
[ 2 -4 0 2 0]
130+
[-2 -2 -3 2 -1]
131+
[-1 2 2 4 -3]]
132+
Rank = 5
133+
Volume = 813.9999999999997
134+
```
135+
136+
You can use other functions for lattice reduction like LLL. Below is the examples.
137+
138+
```Python
139+
import PythonLatticeLibrary as PLL
140+
141+
b = PLL.random_lattice(5)
142+
b.print()
143+
b.basis = b.DeepLLL() # Deep-LLL-reduction
144+
b.print()
145+
```
146+
147+
Output is below:
148+
149+
```
150+
Basis =
151+
[[726 0 0 0 0]
152+
[952 1 0 0 0]
153+
[676 0 1 0 0]
154+
[655 0 0 1 0]
155+
[900 0 0 0 1]]
156+
Rank = 5
157+
Volume = 726.0000000397084
158+
Basis =
159+
[[-2 -1 -1 0 1]
160+
[ 1 -1 1 1 2]
161+
[ 0 -2 2 0 -1]
162+
[-3 3 4 -1 1]
163+
[ 0 -1 -2 6 -1]]
164+
Rank = 5
165+
Volume = 725.9999999999999
166+
```
167+
168+
You can solve SVP using function ``ENUM_SVP()``:
169+
170+
```Python
171+
import PythonLatticeLibrary as PLL
172+
173+
b = PLL.random_lattice(7)
174+
b.print()
175+
b.basis = b.LLL() # LLL-reduction
176+
v = b.ENUM_SVP()
177+
print(v)
178+
```
179+
180+
Output is below:
181+
182+
```
183+
Basis =
184+
[[924 0 0 0 0 0 0]
185+
[719 1 0 0 0 0 0]
186+
[552 0 1 0 0 0 0]
187+
[723 0 0 1 0 0 0]
188+
[608 0 0 0 1 0 0]
189+
[834 0 0 0 0 1 0]
190+
[995 0 0 0 0 0 1]]
191+
Rank = 7
192+
Volume = 924.000000001688
193+
[ 0 -1 0 -1 1 1 0]
194+
```
195+
196+
As same as solving an SVP, you can solve CVP to target t using ``ENUM_CVP(t)``:
197+
198+
```Python
199+
import PythonLatticeLibrary as PLL
200+
import numpy as np
201+
202+
b = PLL.random_lattice(7)
203+
t = np.random.randint(1, 20, size=7)
204+
b.print()
205+
print(t)
206+
b.basis = b.LLL() # LLL-reduction
207+
v = b.ENUM_CVP(t)
208+
print(v)
209+
```
210+
211+
Output is below:
212+
213+
```
214+
Basis =
215+
[[885 0 0 0 0 0 0]
216+
[799 1 0 0 0 0 0]
217+
[566 0 1 0 0 0 0]
218+
[766 0 0 1 0 0 0]
219+
[650 0 0 0 1 0 0]
220+
[654 0 0 0 0 1 0]
221+
[541 0 0 0 0 0 1]]
222+
Rank = 7
223+
Volume = 884.9999999999998
224+
[18 1 4 17 18 9 15]
225+
[17 2 5 17 18 8 15]
226+
```
227+
228+
## What Functions are Available?
229+
Available functions in this library are below:
230+
- ```vol()```: Computes volume of the lattice.
231+
- ```GSO()```: Computes Gram-Schmidt information of the lattice basis.
232+
- ```potential()```: Computes potential of the lattice basis.
233+
- ```size()```: Size-reduces the lattice basis.
234+
- ```Gauss()```: Gauss-reduces the 2-dimensional lattice basis.
235+
- ```LLL(delta)```: LLL-reduces the lattice basis.
236+
- ```DeepLLL(delta)```: Deep-LLL-reduces the lattice basis.
237+
- ```ENUM_SVP()```: Enumerates the shortest vector on the lattice.
238+
- ````Babai(t)````: Computes an approximate solution of CVP to target t on the lattice with Babai's nearest plane algorithm.
239+
- ```ENUM_CVP(t)```: Enumerates the closest vector to target t on the lattice.

0 commit comments

Comments
 (0)