49160bbb4fe394a3a5f9d88a05a797c62b732372
[cipher-training.git] / slides / keyword-break.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Breaking keyword ciphers</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 # Breaking keyword ciphers
49
50 a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z
51 --|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|--
52 k | e | y | w | o | r | d | a | b | c | f | g | h | i | j | l | m | n | p | q | s | t | u | v | x | z
53
54 ---
55
56 # Duplicate and extend your `affine_break()` function
57
58 * How to cycle through all the keys? What _are_ all the keys?
59
60 * Look at `words.txt`
61
62 ---
63
64 # Test it.
65
66 * `2013/4a.ciphertext`
67 * `2013/4b.ciphertext`
68
69 This will take a while. Fire up a system monitor. What's wrong?
70
71 ---
72
73 # Python, threads, and the GIL
74
75 Thread-safe shared-memory code is hard.
76
77 Python's Global Interpreter Lock prevents shooting yourself in the foot.
78
79 Where you want true parallelism, need different threads (Python processes).
80
81 * Thread-safe shared-memory code is hard.
82
83 The `multiprocessing` library makes this easier.
84
85 But before we get there, a couple of diversions...
86
87 ---
88
89 # DRYing code
90
91 Three cipher breaking tasks so far.
92
93 All working on the same principle:
94
95 ```
96 find a way to enumerate all the possible keys
97 initialise 'best so far'
98 for each key:
99 decipher message with this key
100 score it
101 if it's better than the best so far:
102 update best so far
103 ```
104
105 Repetition of code is a bad smell.
106
107 Separate the 'try all keys, keep the best' logic from the 'score this one key' logic.
108
109 ---
110
111 # map()
112
113 A common task is to apply a function to each item in a sequence, returning a sequence of the results.
114
115 ```python
116 def double(x):
117 return x * 2
118
119 >>> map(double, [1,2,3])
120 [2,4,6]
121 ```
122
123 * `map()` is a higher-order function: its first argument is the function that's applied.
124
125 How can we use this for keyword cipher breaking?
126
127 ---
128
129 # Mapping keyword decipherings
130
131 Define a function that takes a possible key (keyword and cipher type) and returns the key and its fitness.
132
133 * (Also pass in the message and the fitness function)
134
135 Use `map()` and `max()` to find the best key
136
137 ---
138
139 # Arity of print()
140
141 How many arguments does this take?
142
143 How do you write a function that takes this many arguments?
144
145 ---
146
147 # Function arguments
148
149 ## Positional, keyword
150
151 * Common or garden parameters, as you're used to.
152 * `def keyword_encipher(message, keyword, Keyword_wrap_alphabet.from_a):`
153
154 ## Excess positional
155 * `def mean(x, *xs):`
156
157 First number goes in `x`, remaining go in the tuple `xs`
158
159 ## Excess keyword
160
161 * `def myfunc(arg1=0, **kwargs):`
162
163 `kwargs` will be a Dict of the remaining keywords (not `arg1`)
164
165 ---
166
167 # Back to `multiprocessing`
168
169 What does `Pool.starmap()` do?
170
171 ---
172
173 ```python
174 from multiprocessing import Pool
175
176 def keyword_break_mp(message, wordlist=keywords, fitness=Pletters, chunksize=500):
177 helper_args = [??? for word in wordlist] # One tuple for each possible key
178 with Pool() as pool:
179 breaks = pool.starmap(keyword_break_worker, helper_args, chunksize)
180 return max(breaks, key=lambda k: k[1])
181
182 def keyword_break_worker(???):
183 ???
184 return (key, fitness)
185 ```
186
187 * Gotcha: the function in `Pool.starmap()` must be defined at the top level
188 * This is definitely a "feature"
189
190 ---
191
192 # Performance and chunksize
193
194 Try the multiprocessing keyword break. Is it using all the resources?
195
196 Setting `chunksize` is an art.
197
198 ## Map-reduce as a general pattern for multiprocessing
199
200 </textarea>
201 <script src="http://gnab.github.io/remark/downloads/remark-0.6.0.min.js" type="text/javascript">
202 </script>
203
204 <script type="text/javascript"
205 src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&delayStartupUntil=configured"></script>
206
207 <script type="text/javascript">
208 var slideshow = remark.create({ ratio: "16:9" });
209
210 // Setup MathJax
211 MathJax.Hub.Config({
212 tex2jax: {
213 skipTags: ['script', 'noscript', 'style', 'textarea', 'pre']
214 }
215 });
216 MathJax.Hub.Queue(function() {
217 $(MathJax.Hub.getAllJax()).map(function(index, elem) {
218 return(elem.SourceElement());
219 }).parent().addClass('has-jax');
220 });
221 MathJax.Hub.Configured();
222 </script>
223 </body>
224 </html>