Updated further work to include Enigma
[cipher-training.git] / slides / pocket-enigma-encipher.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Pocket enigma</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6 <style type="text/css">
7 /* Slideshow styles */
8 body {
9 font-size: 20px;
10 }
11 h1, h2, h3 {
12 font-weight: 400;
13 margin-bottom: 0;
14 }
15 h1 { font-size: 3em; }
16 h2 { font-size: 2em; }
17 h3 { font-size: 1.6em; }
18 a, a > code {
19 text-decoration: none;
20 }
21 code {
22 -moz-border-radius: 5px;
23 -web-border-radius: 5px;
24 background: #e7e8e2;
25 border-radius: 5px;
26 font-size: 16px;
27 }
28 .plaintext {
29 background: #272822;
30 color: #80ff80;
31 text-shadow: 0 0 20px #333;
32 padding: 2px 5px;
33 }
34 .ciphertext {
35 background: #272822;
36 color: #ff6666;
37 text-shadow: 0 0 20px #333;
38 padding: 2px 5px;
39 }
40 .float-right {
41 float: right;
42 }
43 </style>
44 </head>
45 <body>
46 <textarea id="source">
47
48 # Pocket Enigma
49
50 ![centre-aligned Pocket Engima](pocket-enigma-small.jpg)
51
52 Stateful cipher
53
54 ---
55
56 # Pocket Enigma
57
58 Emulates the Enigma machine from WWII
59
60 Mechanical cipher machine
61
62 * Substitution cipher
63 * Substitution changes with every letter
64
65 Ciphering method: advance the wheel, then follow the lines to encipher the letter
66
67 ## Stateful enciphering
68
69 The cipher depends on the position of the wheel
70
71 We need to encapsulate that state
72
73 Objects have state
74
75 ---
76
77 # The PocketEnigma object
78
79 What do we want it to do?
80
81 What data should the object hold?
82
83 ---
84
85 # The PocketEnigma object
86
87 What do we want it to do?
88
89 * Initialise with the appropriate wheel (and possible starting position)
90 * Spin the wheel to a given position
91 * Advance the wheel one position
92 * Look up a letter given the wheel position
93 * Encipher a letter (advance the wheel then look up the letter)
94 * Encipher a message (optionally give the key)
95 * Make aliases for deciphering (same as enciphering)
96
97
98 * Accept user-defined wheels
99 * ...and validate them
100
101 What data should it hold?
102
103 * A description of the wheel being used
104 * The current position of the wheel
105
106 ---
107
108 # A note on testing
109
110 Testing's easier if everything returns a meaningful value
111
112 Saves having to look up different values after performing each operation
113
114 `__init__` can't return a value (restriction of Python)
115
116 ```python
117 if __name__ == "__main__":
118 import doctest
119 doctest.testmod(extraglobs={'pe': PocketEnigma(1, 'a')})
120 ```
121
122 `pe` is now available in all tests.
123
124 ---
125
126 # Data structures
127
128 What's a convenient representation of the wheel
129
130 1. for the object to use internally
131 2. for a person to use to describe the wheel
132
133 They may not be the same, and we'll have to translate between them
134
135 ---
136
137 # Data structures
138
139 ### Internal use: list of transpositions.
140
141 ```python
142 [2, 3, 0, 1, 22, 8, 15, 12, 5, ...
143 ```
144
145 so position 0 ('a') swaps with position 2 ('c'), position 3 ('d') swaps with position 1 ('b'), and so on.
146
147 * This will be a nightmare to enter correctly
148
149 ### Exernal use: list of pairs
150
151 ```python
152 [('a', 'c'), ('b', 'd'), ...]
153 ```
154
155 Easier to enter
156
157 * Need to validate the human-entered list, to check it's valid
158
159 ---
160
161 # Validating the wheel description
162
163 What tests?
164
165 ---
166
167 # Validating the wheel specification
168
169 What tests?
170
171 * 13 elements...
172 * ...each a pair...
173 * ...and 26 letters mentioned overall
174
175 Raise exceptions if the specification is invalid
176
177 ---
178
179 # Making the PocketEnigma class
180
181 ```python
182 class PocketEnigma(object):
183 def __init__(self, wheel=1, position='a'):
184 self.wheel1 = [('a', 'z'), ('b', 'e'), ('c', 'x'), ('d', 'k'),
185 ('f', 'h'), ('g', 'j'), ('i', 'm'), ('l', 'r'), ('n', 'o'),
186 ('p', 'v'), ('q', 't'), ('s', 'u'), ('w', 'y')]
187 self.wheel2 = [('a', 'c'), ('b', 'd'), ('e', 'w'), ('f', 'i'),
188 ('g', 'p'), ('h', 'm'), ('j', 'k'), ('l', 'n'), ('o', 'q'),
189 ('r', 'z'), ('s', 'u'), ('t', 'v'), ('x', 'y')]
190 # Rest of initialisation code here
191
192 def make_wheel_map(self, wheel_spec):
193 ...
194 self.wheel_map = ...
195 ...
196
197 def validate_wheel_spec(self, wheel_spec):
198 if len(wheel_spec) != 13:
199 raise ValueError("Wheel specification has {} pairs, requires 13".
200 format(len(wheel_spec)))
201 ...
202 ```
203
204 ---
205
206 # Looking up the enciphered version of a letter
207
208 *Not* advancing the wheel before
209
210 Keep `self.position` to record where the wheel is
211
212 * `__init__` can be passed a letter, but internally it's a number
213
214 But the wheel map only works if the wheel arrow is pointing at 'a'
215
216 Idea:
217
218 1. Rotate the source letter back `position` spaces
219 2. Do the lookup
220 3. Rotate the destination letter forward `position` spaces
221
222 (all mod 26)
223
224 i.e. source → subtract position → lookup destination → add position
225
226 ---
227
228 # Advance the wheel
229
230 Trivial...
231
232 # Encipher a letter
233
234 Advance the wheel, then look up the letter
235
236 ---
237
238 # Encipher a message
239
240 ```python
241 ciphertext = ''
242 for letter in plaintext:
243 ciphertext += encipher_letter(letter)
244 return ciphertext
245 ```
246
247 Have to be explicit as the order of the operations is important
248
249 * Something like `map` might choose an order different from strict left-to-right
250
251 ## Test it against the physical object
252
253 </textarea>
254 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
255 </script>
256
257 <script type="text/javascript"
258 src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
259
260 <script type="text/javascript">
261 var slideshow = remark.create({ ratio: "16:9" });
262
263 // Setup MathJax
264 MathJax.Hub.Config({
265 tex2jax: {
266 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
267 }
268 });
269 MathJax.Hub.Queue(function() {
270 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
271 return(elem.SourceElement());
272 }).parent().addClass('has-jax');
273 });
274 MathJax.Hub.Configured();
275 </script>
276 </body>
277 </html>