CryptoBook
Search…
Interactive fun
Lattice + LLL + Fundamental mesh plot
1
@interact
2
def draw_lattice(v1x = input_box(label = "v1 x =", default = 1),
3
v1y = input_box(label = "v1 y =", default = 0),
4
v2x = input_box(label = "v2 x =", default = 0),
5
v2y = input_box(label = "v2 y =", default = 1),
6
box = 5, search = 10,
7
plot_LLL = True,
8
plot_C = True,
9
plot_F = True):
10
11
v1 = vector((v1x, v1y))
12
v2 = vector((v2x, v2y))
13
vecs = []
14
# Generate vectors
15
for i in range(-search,search):
16
for j in range(-search,search):
17
vecs.append(i*v1 + j*v2)
18
# Plot stuff
19
G = Graphics()
20
for p1 in vecs:
21
x1, y1 = p1
22
if x1 > -box and x1 < box and y1 > -box and y1 < box:
23
G += point(p1, color = 'green', size = 30)
24
G += line([p1, p1 + v2], linestyle = '--', alpha = .20)
25
G += line([p1, p1 + v1], linestyle = '--', alpha = .20)
26
G+= arrow((0, 0), v1, color = 'red', arrowsize = 2)
27
G+= arrow((0, 0), v2, color = 'red', arrowsize = 2)
28
G+= text('v1', v1 + .2 * v1, color = 'red')
29
G+= text('v2', v2 + .2 * v2, color = 'red')
30
31
# LLL
32
if plot_LLL:
33
v1_, v2_ = matrix([v1, v2]).LLL()
34
G+= arrow((0, 0), v1_, color = 'purple', arrowsize = 2)
35
G+= arrow((0, 0), v2_, color = 'purple', arrowsize = 2)
36
if plot_C:
37
G += circle(center = (0, 0), radius = norm(v1_) if norm(v1_) > norm(v2_) else norm(v2_), alpha = .5, color = 'purple')
38
# Fundamental mesh
39
if plot_F:
40
F = polygon([[0, 0], v1, v1 + v2, v2], color='red', alpha = .1)
41
G += F
42
G.show(axes = False, figsize = (7, 7))
43
44
Copied!

Lattice + CVP

1
@interact
2
def draw_cvp(v1x = input_box(label = "v1 x =", default = 1),
3
v1y = input_box(label = "v1 y =", default = 0),
4
v2x = input_box(label = "v2 x =", default = 0),
5
v2y = input_box(label = "v2 y =", default = 1),
6
wx = input_box(label = "w x =", default = 1.8),
7
wy = input_box(label = "w y =", default = 1.7),
8
box = 5, search = 10):
9
10
v1 = vector((v1x, v1y))
11
v2 = vector((v2x, v2y))
12
print(v1, v2)
13
vecs = []
14
# Generate vectors
15
for i in range(-search,search):
16
for j in range(-search,search):
17
vecs.append(i*v1 + j*v2)
18
# Plot stuff
19
G = Graphics()
20
for p1 in vecs:
21
x1, y1 = p1
22
if x1 > -box and x1 < box and y1 > -box and y1 < box:
23
G += point(p1, color = 'green', size = 30)
24
G += line([p1, p1 + v2], linestyle = '--', alpha = .20)
25
G += line([p1, p1 + v1], linestyle = '--', alpha = .20)
26
G+= arrow((0, 0), v1, color = 'red', arrowsize = 2)
27
G+= arrow((0, 0), v2, color = 'red', arrowsize = 2)
28
G+= text('v1', v1 + .2 * v1, color = 'red')
29
G+= text('v2', v2 + .2 * v2, color = 'red')
30
31
# Cvp
32
L = IntegerLattice(matrix([v1, v2]))
33
w = vector((wx, wy))
34
t = L.closest_vector(w)
35
G += point(w, color = 'purple', size = 30)
36
G+= text('w', w + .2 * v1, color = 'purple')
37
38
G += point(t, color = 'red', size = 30)
39
G+= text('t', t + .2 * v1, color = 'red')
40
G+= circle(center = w, radius=norm(t - w), color = 'purple', alpha = .5)
41
G.show(axes = False, figsize = (7, 7))
Copied!
1
@interact
2
def draw_dual(v1x = input_box(label = "v1 x =", default = 1),
3
v1y = input_box(label = "v1 y =", default = 0),
4
v2x = input_box(label = "v2 x =", default = 0),
5
v2y = input_box(label = "v2 y =", default = 1),
6
box = 3, search = 6,
7
plot_lattice_points = True,
8
plot_lattice_lines = True,
9
plot_dual_points = True,
10
plot_dual_lines = True):
11
12
v1 = vector((v1x, v1y))
13
v2 = vector((v2x, v2y))
14
vecs = []
15
v1_, v2_ = matrix([v1,v2]).inverse().T
16
vecs_ = []
17
# Generate vectors
18
for i in range(-search,search):
19
for j in range(-search,search):
20
vecs.append(i*v1 + j*v2)
21
for i in range(-search,search):
22
for j in range(-search,search):
23
vecs_.append(i*v1_ + j*v2_)
24
# Plot stuff
25
G = Graphics()
26
for p1 in vecs:
27
x1, y1 = p1
28
if x1 > -box and x1 < box and y1 > -box and y1 < box:
29
if plot_lattice_points:
30
G += point(p1, color = 'green', size = 70)
31
if plot_lattice_lines:
32
G += line([p1, p1 + v2], linestyle = '--', alpha = .20, color = 'green')
33
G += line([p1, p1 + v1], linestyle = '--', alpha = .20, color = 'green')
34
if plot_lattice_lines:
35
G+= arrow((0, 0), v1, color = 'green', arrowsize = 2)
36
G+= arrow((0, 0), v2, color = 'green', arrowsize = 2)
37
G+= text('v1', v1 + .2 * v1, color = 'green')
38
G+= text('v2', v2 + .2 * v2, color = 'green')
39
40
# Dual
41
for p1 in vecs_:
42
x1, y1 = p1
43
if x1 > -box and x1 < box and y1 > -box and y1 < box:
44
if plot_dual_points:
45
G += point(p1, color = 'red', size = 25)
46
if plot_dual_lines:
47
G += line([p1, p1 + v2_], linestyle = '--', alpha = .20, color = 'red')
48
G += line([p1, p1 + v1_], linestyle = '--', alpha = .20, color = 'red')
49
if plot_dual_lines:
50
G+= arrow((0, 0), v1_, color = 'red', arrowsize = 2)
51
G+= arrow((0, 0), v2_, color = 'red', arrowsize = 2)
52
G+= text('v1\'', v1_ + .2 * v1, color = 'red')
53
G+= text('v2\'', v2_ + .2 * v2, color = 'red')
54
55
G.show(axes = False, figsize = (7, 7))
Copied!
Q-ary plots
1
# I'm not sure this does what is supposed to do
2
# but the plots are nice
3
4
from sage.modules.free_module_integer import IntegerLattice
5
@interact
6
def draw_qary(v1x = input_box(label = "v1 x =", default = 1),
7
v1y = input_box(label = "v1 y =", default = 0),
8
v2x = input_box(label = "v2 x =", default = 0),
9
v2y = input_box(label = "v2 y =", default = 1),
10
q = input_box(label = "q =", default = 5),
11
box = 7, search = 6,
12
plot_lattice_points = True,
13
plot_lattice_lines = True,
14
plot_qary_points = True,
15
plot_qary_lines = True):
16
17
v1 = vector((v1x, v1y))
18
v2 = vector((v2x, v2y))
19
L = IntegerLattice(matrix([v1, v2]))
20
vecs = []
21
v1_ = v1.change_ring(Zmod(q))
22
v2_ = v2.change_ring(Zmod(q))
23
vecs_ = []
24
# Generate vectors
25
for i in range(-search,search):
26
for j in range(-search,search):
27
v = i*v1 + j*v2
28
vecs.append(v)
29
v = v.change_ring(Zmod(q)).change_ring(ZZ)
30
if v not in vecs_:
31
vecs_.append(v)
32
33
# Lattice
34
G = Graphics()
35
for p1 in vecs:
36
x1, y1 = p1
37
if x1 > -box and x1 < box and y1 > -box and y1 < box:
38
if plot_lattice_points:
39
G += point(p1, color = 'green', size = 70)
40
if plot_lattice_lines:
41
G += line([p1, p1 + v2], linestyle = '--', alpha = .20, color = 'green')
42
G += line([p1, p1 + v1], linestyle = '--', alpha = .20, color = 'green')
43
if plot_lattice_lines:
44
G+= arrow((0, 0), v1, color = 'green', arrowsize = 2)
45
G+= arrow((0, 0), v2, color = 'green', arrowsize = 2)
46
G+= text('v1', v1 + .2 * v1, color = 'green')
47
G+= text('v2', v2 + .2 * v2, color = 'green')
48
49
# qary
50
for p1 in vecs_:
51
p1 = p1
52
x1, y1 = p1
53
if x1 > -box and x1 < box and y1 > -box and y1 < box:
54
if plot_qary_points:
55
G += point(p1, color = 'red', size = 25)
56
if plot_qary_lines:
57
G += line([p1, p1 + v2_], linestyle = '--', alpha = .20, color = 'red')
58
G += line([p1, p1 + v1_], linestyle = '--', alpha = .20, color = 'red')
59
if plot_qary_lines:
60
G+= arrow((0, 0), v1_, color = 'purple', arrowsize = 2)
61
G+= arrow((0, 0), v2_, color = 'purple', arrowsize = 2)
62
G+= text('v1\'', v1_.change_ring(ZZ) + .2 * v1, color = 'purple')
63
G+= text('v2\'', v2_.change_ring(ZZ) + .2 * v2, color = 'purple')
64
65
G.show(axes = False, figsize = (10, 10))
Copied!
Last modified 5mo ago
Export as PDF
Copy link