-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawdistinguisher.py
152 lines (140 loc) · 7.2 KB
/
drawdistinguisher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""
MIT License
Copyright (c) 2023 Hosein Hadipour
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import sys
def trim(docstring):
if not docstring:
return ''
# Convert tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxsize
for line in lines[1:]:
stripped = line.lstrip()
if stripped:
indent = min(indent, len(line) - len(stripped))
# Remove indentation (first line is special):
trimmed = [lines[0].strip()]
if indent < sys.maxsize:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
# Return a single string:
return '\n'.join(trimmed)
class DrawDL():
"""
Draw the shape of a given differential-linear distinguisher
"""
def __init__(self, idobject, output_file_name="output.tex"):
self.result = idobject.result
self.RD = idobject.RD
self.output_file_name = output_file_name
self.upper_trail = idobject.upper_trail
self.lower_trail = idobject.lower_trail
self.attack_summary = idobject.attack_summary
def generate_distinguisher_shape(self):
"""
Draw the figure of the Rectangle distinguisher
"""
contents = ""
# head lines
contents += trim(r"""
\documentclass[varwidth=100cm]{standalone}
\usepackage{ascon}
\usepackage{comment}
\begin{document}
\begin{tikzpicture}""") + "\n\n"
# draw EU
for r in range(self.RD + 1):
fillcolor_x = []
for row in range(5):
for column in range(64):
if self.upper_trail["x"][r][row][column] == 1:
fillcolor_x.append(r"""\TFill[one]""" + f"{{{row}}}{{{column}}}")
elif self.upper_trail["x"][r][row][column] == -1:
fillcolor_x.append(r"""\TFill[upperunknown]""" + f"{{{row}}}{{{column}}}")
fillcolor_x = ",".join(fillcolor_x)
if r < self.RD:
fillcolor_y = []
for row in range(5):
for column in range(64):
if self.upper_trail["y"][r][row][column] == 1:
fillcolor_y.append(r"""\TFill[one]""" + f"{{{row}}}{{{column}}}")
elif self.upper_trail["y"][r][row][column] == -1:
fillcolor_y.append(r"""\TFill[upperunknown]""" + f"{{{row}}}{{{column}}}")
fillcolor_y = ",".join(fillcolor_y)
if r == 0:
contents += trim(r"""
\node[matrix node]""" + "(x" + str(r) + "){" +
r"""\drawArray{""" + fillcolor_x + r"""}};""") + "\n"
else:
contents += trim(r"""
\node[matrix node, below=5cm of """ + "y" + str(r - 1) + ".center]" + "(x" + str(r) + "){" +
r"""\drawArray{""" + fillcolor_x + r"""}};""") + "\n"
contents += r""" \draw[-latex, line width=3.5pt]""" + "(y" + str(r - 1) + r""".south) --node[right]{\Huge$L$}""" + "(x" + str(r) + ".north);"
if r < self.RD:
contents += trim(r"""
\node[matrix node, below=5cm of """ + "x" + str(r) + ".center]" + "(y" + str(r) + "){" +
r"""\drawArray{""" + fillcolor_y + r"""}};""") + "\n"
contents += r""" \draw[-latex, line width=3.5pt]""" + "(x" + str(r) + r""".south) --node[right]{\Huge$S$}""" + "(y" + str(r) + ".north);"
# draw EL
for r in range(self.RD + 1):
fillcolor_x = []
for row in range(5):
for column in range(64):
if self.lower_trail["x"][r][row][column] == 1:
fillcolor_x.append(r"""\BFill[one]""" + f"{{{row}}}{{{column}}}")
elif self.lower_trail["x"][r][row][column] == -1:
fillcolor_x.append(r"""\BFill[lowerunknown]""" + f"{{{row}}}{{{column}}}")
fillcolor_x = ",".join(fillcolor_x)
if r < self.RD:
fillcolor_y = []
for row in range(5):
for column in range(64):
if self.lower_trail["y"][r][row][column] == 1:
fillcolor_y.append(r"""\BFill[one]""" + f"{{{row}}}{{{column}}}")
elif self.lower_trail["y"][r][row][column] == -1:
fillcolor_y.append(r"""\BFill[lowerunknown]""" + f"{{{row}}}{{{column}}}")
fillcolor_y = ",".join(fillcolor_y)
if r == 0:
contents += trim(r"""
\node[matrix node]""" + "(x" + str(r) + "){" +
r"""\drawArray{""" + fillcolor_x + r"""}};""") + "\n"
else:
contents += trim(r"""
\node[matrix node, below=5cm of """ + "y" + str(r - 1) + ".center]" + "(x" + str(r) + "){" +
r"""\drawArray{""" + fillcolor_x + r"""}};""") + "\n"
contents += r""" \draw[-latex, line width=3.5pt]""" + "(y" + str(r - 1) + r""".south) --node[right]{\Huge$L$}""" + "(x" + str(r) + ".north);" + "\n"
if r < self.RD:
contents += trim(r"""
\node[matrix node, below=5cm of """ + "x" + str(r) + ".center]" + "(y" + str(r) + "){" +
r"""\drawArray{""" + fillcolor_y + r"""}};""") + "\n"
contents += r""" \draw[-latex, line width=3.5pt]""" + "(x" + str(r) + r""".south) --node[right]{\Huge$S$}""" + "(y" + str(r) + ".north);" + "\n"
contents += "\n\n" + r"""\begin{comment}""" + "\n"
contents += self.attack_summary
contents += r"""\end{comment}""" + "\n"
contents += r"""\end{tikzpicture}""" + "\n"
contents += trim(r"""\end{document}""")
with open(self.output_file_name, "w") as output_file:
output_file.write(contents)