5 "execution_count": 797,
12 "import collections\n",
13 "import multiprocessing\n",
15 "from enigma import *"
20 "execution_count": 798,
26 "# wheel_i_spec = 'ekmflgdqvzntowyhxuspaibrcj'\n",
27 "# wheel_ii_spec = 'ajdksiruxblhwtmcqgznpyfvoe'\n",
28 "# wheel_iii_spec = 'bdfhjlcprtxvznyeiwgakmusqo'\n",
29 "# wheel_iv_spec = 'esovpzjayquirhxlnftgkdcmwb'\n",
30 "# wheel_v_spec = 'vzbrgityupsdnhlxawmjqofeck'\n",
31 "# wheel_vi_spec = 'jpgvoumfyqbenhzrdkasxlictw'\n",
32 "# wheel_vii_spec = 'nzjhgrcxmyswboufaivlpekqdt'\n",
33 "# wheel_viii_spec = 'fkqhtlxocbjspdzramewniuygv'\n",
34 "# beta_wheel_spec = 'leyjvcnixwpbqmdrtakzgfuhos'\n",
35 "# gamma_wheel_spec = 'fsokanuerhmbtiycwlqpzxvgjd'\n",
37 "# wheel_i_pegs = ['q']\n",
38 "# wheel_ii_pegs = ['e']\n",
39 "# wheel_iii_pegs = ['v']\n",
40 "# wheel_iv_pegs = ['j']\n",
41 "# wheel_v_pegs = ['z']\n",
42 "# wheel_vi_pegs = ['z', 'm']\n",
43 "# wheel_vii_pegs = ['z', 'm']\n",
44 "# wheel_viii_pegs = ['z', 'm']\n",
46 "# reflector_b_spec = 'ay br cu dh eq fs gl ip jx kn mo tz vw'\n",
47 "# reflector_c_spec = 'af bv cp dj ei go hy kr lz mx nw tq su'"
52 "execution_count": 799,
58 "class Bank(object):\n",
59 " def __init__(self):\n",
60 " self.signals = dict(zip(string.ascii_lowercase, [False]*len(string.ascii_lowercase)))"
65 "execution_count": 800,
101 "execution_count": 800,
103 "output_type": "execute_result"
113 "execution_count": 801,
119 "Signal = collections.namedtuple('Signal', ['bank', 'wire'])\n",
120 "Connection = collections.namedtuple('Connection', ['banks', 'scrambler'])\n",
121 "MenuItem = collections.namedtuple('MenuIem', ['before', 'after', 'number'])"
126 "execution_count": 802,
132 "class Scrambler(object):\n",
133 " def __init__(self, wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec,\n",
134 " wheel1_pos='a', wheel2_pos='a', wheel3_pos='a'):\n",
135 " self.wheel1 = SimpleWheel(wheel1_spec, position=wheel1_pos)\n",
136 " self.wheel2 = SimpleWheel(wheel2_spec, position=wheel2_pos)\n",
137 " self.wheel3 = SimpleWheel(wheel3_spec, position=wheel3_pos)\n",
138 " self.reflector = Reflector(reflector_spec)\n",
140 " def __getattribute__(self, name):\n",
141 " if name=='wheel_positions':\n",
142 " return self.wheel1.position, self.wheel2.position, self.wheel3.position \n",
143 " elif name=='wheel_positions_l':\n",
144 " return self.wheel1.position_l, self.wheel2.position_l, self.wheel3.position_l \n",
146 " return object.__getattribute__(self, name)\n",
148 " def advance(self, wheel1=False, wheel2=False, wheel3=True):\n",
149 " if wheel1: self.wheel1.advance()\n",
150 " if wheel2: self.wheel2.advance()\n",
151 " if wheel3: self.wheel3.advance()\n",
153 " def lookup(self, letter):\n",
154 " a = self.wheel3.forward(letter)\n",
155 " b = self.wheel2.forward(a)\n",
156 " c = self.wheel1.forward(b)\n",
157 " d = self.reflector.forward(c)\n",
158 " e = self.wheel1.backward(d)\n",
159 " f = self.wheel2.backward(e)\n",
160 " g = self.wheel3.backward(f)\n",
163 " def set_positions(self, wheel1_pos, wheel2_pos, wheel3_pos):\n",
164 " self.wheel1.set_position(wheel1_pos)\n",
165 " self.wheel2.set_position(wheel2_pos)\n",
166 " self.wheel3.set_position(wheel3_pos) "
171 "execution_count": 803,
177 "class Bombe(object):\n",
178 " def __init__(self, wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec,\n",
179 " menu=None, start_signal=None, use_diagonal_board=True, verify_plugboard=True):\n",
180 " self.connections = []\n",
181 " self.wheel1_spec = wheel1_spec\n",
182 " self.wheel2_spec = wheel2_spec\n",
183 " self.wheel3_spec = wheel3_spec\n",
184 " self.reflector_spec = reflector_spec\n",
186 " self.read_menu(menu)\n",
187 " if start_signal:\n",
188 " self.test_start = start_signal\n",
189 " self.use_diagonal_board = use_diagonal_board\n",
190 " self.verify_plugboard = verify_plugboard\n",
192 " def __getattribute__(self, name):\n",
193 " if name=='wheel_positions':\n",
194 " return self.connections[0].scrambler.wheel_positions\n",
195 " elif name=='wheel_positions_l':\n",
196 " return self.connections[0].scrambler.wheel_positions_l\n",
198 " return object.__getattribute__(self, name)\n",
200 " def __call__(self, start_positions):\n",
201 " return start_positions, self.test(start_positions=start_positions, \n",
202 " use_diagonal_board=self.use_diagonal_board,\n",
203 " verify_plugboard=self.verify_plugboard)\n",
205 " def add_connection(self, bank_before, bank_after, scrambler):\n",
206 " self.connections += [Connection([bank_before, bank_after], scrambler)]\n",
208 " def read_menu(self, menu):\n",
209 " for item in menu:\n",
210 " scrambler = Scrambler(self.wheel1_spec, self.wheel2_spec, self.wheel3_spec,\n",
211 " self.reflector_spec,\n",
212 " wheel3_pos=unpos(item.number - 1))\n",
213 " self.add_connection(item.before, item.after, scrambler)\n",
214 " most_common_letter = (collections.Counter(m.before for m in menu) + \\\n",
215 " collections.Counter(m.after for m in menu)).most_common(1)[0][0]\n",
216 " self.test_start = Signal(most_common_letter, most_common_letter)\n",
218 " def set_positions(self, wheel1_pos, wheel2_pos, wheel3_pos):\n",
219 " for i, c in enumerate(self.connections):\n",
220 " c.scrambler.set_positions(wheel1_pos, wheel2_pos, unpos(pos(wheel3_pos) + i))\n",
222 " def test(self, initial_signal=None, start_positions=None, use_diagonal_board=True,\n",
223 " verify_plugboard=True):\n",
224 " self.banks = {label: \n",
225 " dict(zip(string.ascii_lowercase, [False]*len(string.ascii_lowercase)))\n",
226 " for label in string.ascii_lowercase}\n",
227 " if start_positions:\n",
228 " self.set_positions(*start_positions)\n",
229 " if not initial_signal:\n",
230 " initial_signal = self.test_start\n",
231 " self.pending = [initial_signal]\n",
232 " self.propagate(use_diagonal_board)\n",
233 " live_wire_count = len([self.banks[self.test_start.bank][w] \n",
234 " for w in self.banks[self.test_start.bank] \n",
235 " if self.banks[self.test_start.bank][w]])\n",
236 " if live_wire_count < 26:\n",
237 " if verify_plugboard:\n",
238 " possibles = self.possible_plugboards()\n",
239 " return all(s0.isdisjoint(s1) for s0 in possibles for s1 in possibles if s0 != s1)\n",
245 " def propagate(self, use_diagonal_board):\n",
246 " while self.pending:\n",
247 " current = self.pending[0]\n",
248 " # print(\"processing\", current)\n",
249 " self.pending = self.pending[1:]\n",
250 " if not self.banks[current.bank][current.wire]:\n",
251 " self.banks[current.bank][current.wire] = True\n",
252 " if use_diagonal_board:\n",
253 " self.pending += [Signal(current.wire, current.bank)]\n",
254 " for c in self.connections:\n",
255 " if current.bank in c.banks:\n",
256 " other_bank = [b for b in c.banks if b != current.bank][0]\n",
257 " other_wire = c.scrambler.lookup(current.wire)\n",
258 " # print(\" adding\", other_bank, other_wire, \"because\", c.banks)\n",
259 " self.pending += [Signal(other_bank, other_wire)]\n",
261 " def run(self, run_start=None, wheel1_pos='a', wheel2_pos='a', wheel3_pos='a', use_diagonal_board=True):\n",
262 " if not run_start:\n",
263 " run_start = self.test_start\n",
264 " self.solutions = []\n",
265 " self.set_positions(wheel1_pos, wheel2_pos, wheel3_pos)\n",
266 " for run_index in range(26*26*26):\n",
267 " if self.test(initial_signal=run_start, use_diagonal_board=use_diagonal_board):\n",
268 " self.solutions += [self.connections[0].scrambler.wheel_positions_l]\n",
269 " advance3 = True\n",
270 " advance2 = False\n",
271 " advance1 = False\n",
272 " if (run_index + 1) % 26 == 0: advance2 = True\n",
273 " if (run_index + 1) % (26*26) == 0: advance1 = True\n",
274 " for c in self.connections:\n",
275 " c.scrambler.advance(advance1, advance2, advance3)\n",
276 " return self.solutions\n",
278 " def possible_plugboards(self):\n",
279 " possibles = set()\n",
280 " for b in self.banks:\n",
281 " active = [w for w in self.banks[b] if self.banks[b][w]]\n",
282 " inactive = [w for w in self.banks[b] if not self.banks[b][w]]\n",
283 " if len(active) == 1:\n",
284 " possibles = possibles.union({frozenset((b, active[0]))})\n",
285 " if len(inactive) == 1:\n",
286 " possibles = possibles.union({frozenset((b, inactive[0]))})\n",
292 "execution_count": 804,
298 "bombe = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec)\n",
299 "# len(bombe.banks), bombe.banks['a'] == bombe.banks['b']"
304 "execution_count": 805,
310 "test_enigma = Enigma(reflector_b_spec, \n",
311 " wheel_i_spec, wheel_i_pegs,\n",
312 " wheel_ii_spec, wheel_ii_pegs,\n",
313 " wheel_iii_spec, wheel_iii_pegs,\n",
320 "execution_count": 806,
328 "'opgndxcrwomnlnecjz'"
331 "execution_count": 806,
333 "output_type": "execute_result"
337 "test_enigma.set_wheels('a', 'a', 'a')\n",
338 "pt = 'thisisatestmessage'\n",
339 "ct = test_enigma.encipher(pt)\n",
345 "execution_count": 807,
356 "execution_count": 807,
358 "output_type": "execute_result"
362 "cat(test_enigma.wheel_positions_l)"
367 "execution_count": 808,
375 "[MenuIem(before='t', after='o', number=1),\n",
376 " MenuIem(before='h', after='p', number=2),\n",
377 " MenuIem(before='i', after='g', number=3),\n",
378 " MenuIem(before='s', after='n', number=4),\n",
379 " MenuIem(before='i', after='d', number=5),\n",
380 " MenuIem(before='s', after='x', number=6),\n",
381 " MenuIem(before='a', after='c', number=7),\n",
382 " MenuIem(before='t', after='r', number=8),\n",
383 " MenuIem(before='e', after='w', number=9),\n",
384 " MenuIem(before='s', after='o', number=10),\n",
385 " MenuIem(before='t', after='m', number=11),\n",
386 " MenuIem(before='m', after='n', number=12),\n",
387 " MenuIem(before='e', after='l', number=13),\n",
388 " MenuIem(before='s', after='n', number=14),\n",
389 " MenuIem(before='s', after='e', number=15),\n",
390 " MenuIem(before='a', after='c', number=16),\n",
391 " MenuIem(before='g', after='j', number=17),\n",
392 " MenuIem(before='e', after='z', number=18)]"
395 "execution_count": 808,
397 "output_type": "execute_result"
401 "menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
407 "execution_count": 809,
413 "def make_menu(plaintext, ciphertext):\n",
414 " return [MenuItem(p, c, i+1) \n",
415 " for i, (p, c) in enumerate(zip(plaintext, ciphertext))]"
420 "execution_count": 810,
428 "[MenuIem(before='t', after='o', number=1),\n",
429 " MenuIem(before='h', after='p', number=2),\n",
430 " MenuIem(before='i', after='g', number=3),\n",
431 " MenuIem(before='s', after='n', number=4),\n",
432 " MenuIem(before='i', after='d', number=5),\n",
433 " MenuIem(before='s', after='x', number=6),\n",
434 " MenuIem(before='a', after='c', number=7),\n",
435 " MenuIem(before='t', after='r', number=8),\n",
436 " MenuIem(before='e', after='w', number=9),\n",
437 " MenuIem(before='s', after='o', number=10),\n",
438 " MenuIem(before='t', after='m', number=11),\n",
439 " MenuIem(before='m', after='n', number=12),\n",
440 " MenuIem(before='e', after='l', number=13),\n",
441 " MenuIem(before='s', after='n', number=14),\n",
442 " MenuIem(before='s', after='e', number=15),\n",
443 " MenuIem(before='a', after='c', number=16),\n",
444 " MenuIem(before='g', after='j', number=17),\n",
445 " MenuIem(before='e', after='z', number=18)]"
448 "execution_count": 810,
450 "output_type": "execute_result"
459 "execution_count": 811,
470 "execution_count": 811,
472 "output_type": "execute_result"
476 "(collections.Counter(m.before for m in menu) + collections.Counter(m.after for m in menu)).most_common(1)[0][0]"
481 "execution_count": 812,
487 "bombe.read_menu(menu)"
492 "execution_count": 813,
503 "execution_count": 813,
505 "output_type": "execute_result"
509 "len(bombe.connections)"
514 "execution_count": 814,
521 "output_type": "stream",
545 "for c in bombe.connections:\n",
546 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
551 "execution_count": 815,
562 "execution_count": 815,
564 "output_type": "execute_result"
568 "bombe.test(Signal('t', 't'))"
573 "execution_count": 816,
609 "execution_count": 816,
611 "output_type": "execute_result"
620 "execution_count": 817,
627 "output_type": "stream",
629 "a : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
630 "b : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
631 "c : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
632 "d : ABCDEFGHIJKLMNOPQRSTUVWXyZ\n",
633 "e : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
634 "f : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
635 "g : ABCDEFGHIJKLMNOPQRSTuVWXYZ\n",
636 "h : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
637 "i : ABCDEFGHIJKLMNOPQRSTUvWXYZ\n",
638 "j : ABCDEFGHIjKLMNOPQRSTUVWXYZ\n",
639 "k : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
640 "l : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
641 "m : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
642 "n : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
643 "o : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
644 "p : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
645 "q : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
646 "r : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
647 "s : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
648 "t : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
649 "u : AbCDEfgHIJkLMNOPqRSTuvWXyZ\n",
650 "v : AbCDEfGHiJkLMNOPqRSTuvWXyZ\n",
651 "w : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
652 "x : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
653 "y : AbCdEfGHIJkLMNOPqRSTuvWXyZ\n",
654 "z : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
659 "for b in sorted(bombe.banks):\n",
660 " print(b, ': ', end='')\n",
661 " for w in sorted(bombe.banks[b]):\n",
662 " if bombe.banks[b][w]:\n",
663 " print(w.upper(), end='')\n",
665 " print(w, end='')\n",
671 "execution_count": 818,
682 "execution_count": 818,
684 "output_type": "execute_result"
688 "bombe.wheel_positions_l"
693 "execution_count": 819,
700 "output_type": "stream",
724 "bombe.set_positions('p', 'q', 'r')\n",
725 "for c in bombe.connections:\n",
726 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
731 "execution_count": 820,
743 "execution_count": 821,
750 "output_type": "stream",
752 "a : .b.defghijklmnopqrstuvwxyz\n",
753 "b : a.cde.ghij.lmnop.rst..wx.z\n",
754 "c : .b.defghijklmnopqrstuvwxyz\n",
755 "d : abc.e.gh.jklmnopqrstuvwxyz\n",
756 "e : abcd.fghijklmnopqrstuvwxyz\n",
757 "f : a.c.e.g.ij.lmno..rst..wx.z\n",
758 "g : abcdef.h..klmnopqrstuvwxyz\n",
759 "h : abcde.g.ij.lmno.qrst..wxyz\n",
760 "i : abc.ef.h.jklmnopqrstuvwxyz\n",
761 "j : abcdef.hi.klmnopqrstuvwx.z\n",
762 "k : a.cde.g.ij.lmno..rst..wx.z\n",
763 "l : abcdefghijk.mnopqrstuvwxyz\n",
764 "m : abcdefghijkl.nopqrstuvwxyz\n",
765 "n : abcdefghijklm.opqrstuvwxyz\n",
766 "o : abcdefghijklmn.pqrstuvwxyz\n",
767 "p : abcde.g.ij.lmno.qrst..wxyz\n",
768 "q : a.cde.ghij.lmnop.rst..wx.z\n",
769 "r : abcdefghijklmnopq.stuvwxyz\n",
770 "s : abcdefghijklmnopqr.tuvwxyz\n",
771 "t : abcdefghijklmnopqrs.uvwxyz\n",
772 "u : a.cde.g.ij.lmno..rst..wx.z\n",
773 "v : a.cde.g.ij.lmno..rst..wx.z\n",
774 "w : abcdefghijklmnopqrstuv.xyz\n",
775 "x : abcdefghijklmnopqrstuvw.yz\n",
776 "y : a.cde.ghi..lmnop.rst..wx.z\n",
777 "z : abcdefghijklmnopqrstuvwxy.\n"
782 "bombe.set_positions('a', 'a', 'b')\n",
783 "bombe.test(Signal('s', 'a'))\n",
785 "for b in sorted(bombe.banks):\n",
786 " print(b, ': ', end='')\n",
787 " for w in sorted(bombe.banks[b]):\n",
788 " if bombe.banks[b][w]:\n",
789 " print(w, end='')\n",
791 " print('.', end='')\n",
797 "execution_count": 822,
804 "output_type": "stream",
806 "a : ..........................\n",
807 "b : ..........................\n",
808 "c : ..........................\n",
809 "d : ..........................\n",
810 "e : ....e.....................\n",
811 "f : ..........................\n",
812 "g : ..........................\n",
813 "h : ..........................\n",
814 "i : ..........................\n",
815 "j : ..........................\n",
816 "k : ..........................\n",
817 "l : ...........l..............\n",
818 "m : ............m.............\n",
819 "n : .............n............\n",
820 "o : ..............o...........\n",
821 "p : ..........................\n",
822 "q : ..........................\n",
823 "r : .................r........\n",
824 "s : ..................s.......\n",
825 "t : ...................t......\n",
826 "u : ..........................\n",
827 "v : ..........................\n",
828 "w : ......................w...\n",
829 "x : .......................x..\n",
830 "y : ..........................\n",
831 "z : .........................z\n"
836 "bombe.set_positions('a', 'a', 'b')\n",
839 "for b in sorted(bombe.banks):\n",
840 " print(b, ': ', end='')\n",
841 " for w in sorted(bombe.banks[b]):\n",
842 " if bombe.banks[b][w]:\n",
843 " print(w, end='')\n",
845 " print('.', end='')\n",
851 "execution_count": 823,
862 "execution_count": 823,
864 "output_type": "execute_result"
868 "len([bombe.banks['t'][w] for w in bombe.banks['t'] if bombe.banks['t'][w]])"
873 "execution_count": 824,
880 "# results = bombe.run()\n",
881 "# print(len(results), ('a', 'a', 'b') in results)"
886 "execution_count": 825,
893 "# results = bombe.run(use_diagonal_board=False)\n",
894 "# print(len(results), ('a', 'a', 'b') in results)"
899 "execution_count": 826,
910 "execution_count": 826,
912 "output_type": "execute_result"
916 "bombe.wheel_positions_l"
921 "execution_count": 827,
932 "execution_count": 827,
934 "output_type": "execute_result"
938 "bombe.test(Signal('t', 't'), ('p', 'p', 'p'))"
943 "execution_count": 828,
954 "execution_count": 828,
956 "output_type": "execute_result"
960 "bombe.wheel_positions_l"
965 "execution_count": 829,
972 "output_type": "stream",
974 "a : abcdefghijklmnop.rst.vwxyz\n",
975 "b : a.cde.g.ij.lmno..rst..wx.z\n",
976 "c : abcdefghijklmnop.rst.vwxyz\n",
977 "d : abcdefghijklmnopqrstuvwxyz\n",
978 "e : abcdefghijklmnopqrstuvwxyz\n",
979 "f : a.cde.g.ij.lmno..rst..wx.z\n",
980 "g : abcdefghijklmnopqrstuvwxyz\n",
981 "h : a.cde.ghijklmnop.rst.vwxyz\n",
982 "i : abcdefghijklmnopqrstuvwxyz\n",
983 "j : abcdefghijklmnopqrstuvwxyz\n",
984 "k : a.cde.ghij.lmnop.rst..wx.z\n",
985 "l : abcdefghijklmnopqrstuvwxyz\n",
986 "m : abcdefghijklmnopqrstuvwxyz\n",
987 "n : abcdefghijklmnopqrstuvwxyz\n",
988 "o : abcdefghijklmnopqrstuvwxyz\n",
989 "p : a.cde.ghijklmnop.rst.vwxyz\n",
990 "q : ...de.g.ij.lmno..rst..wx.z\n",
991 "r : abcdefghijklmnopqrstuvwxyz\n",
992 "s : abcdefghijklmnopqrstuvwxyz\n",
993 "t : abcdefghijklmnopqrstuvwxyz\n",
994 "u : ...de.g.ij.lmno..rst..wx.z\n",
995 "v : a.cde.ghij.lmnop.rst..wx.z\n",
996 "w : abcdefghijklmnopqrstuvwxyz\n",
997 "x : abcdefghijklmnopqrstuvwxyz\n",
998 "y : a.cde.ghij.lmnop.rst..wx.z\n",
999 "z : abcdefghijklmnopqrstuvwxyz\n"
1004 "for b in sorted(bombe.banks):\n",
1005 " print(b, ': ', end='')\n",
1006 " for w in sorted(bombe.banks[b]):\n",
1007 " if bombe.banks[b][w]:\n",
1008 " print(w, end='')\n",
1010 " print('.', end='')\n",
1015 "cell_type": "code",
1016 "execution_count": 830,
1027 "execution_count": 830,
1029 "output_type": "execute_result"
1033 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1034 "len(list(allwheels))"
1038 "cell_type": "code",
1039 "execution_count": 831,
1047 "(('a', 'a', 'b'), True)"
1050 "execution_count": 831,
1052 "output_type": "execute_result"
1056 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)\n",
1057 "b(('a', 'a', 'b'))"
1061 "cell_type": "code",
1062 "execution_count": 832,
1068 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)(('a', 'a', 'b'))"
1072 "cell_type": "code",
1073 "execution_count": 833,
1084 "execution_count": 833,
1086 "output_type": "execute_result"
1090 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1092 "with multiprocessing.Pool() as pool:\n",
1093 " res = pool.map(Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu),\n",
1095 "[r[0] for r in res if r[1]]"
1099 "cell_type": "code",
1100 "execution_count": 857,
1106 "def run_multi_bombe(wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec, menu,\n",
1107 " start_signal=None, use_diagonal_board=True, \n",
1108 " verify_plugboard=True):\n",
1109 " allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1111 " with multiprocessing.Pool() as pool:\n",
1112 " res = pool.map(Bombe(wheel1_spec, wheel2_spec, wheel3_spec, \n",
1113 " reflector_spec, menu=menu, start_signal=start_signal, \n",
1114 " use_diagonal_board=use_diagonal_board, \n",
1115 " verify_plugboard=verify_plugboard),\n",
1117 " return [r[0] for r in res if r[1]]"
1121 "cell_type": "code",
1122 "execution_count": 835,
1133 "execution_count": 835,
1135 "output_type": "execute_result"
1139 "[r[0] for r in res if r[1]]"
1143 "cell_type": "code",
1144 "execution_count": 836,
1150 "# Setting sheet line 31 from http://www.codesandciphers.org.uk/enigma/enigma3.htm\n",
1151 "# Enigma simulation settings are \n",
1152 "# http://enigma.louisedade.co.uk/enigma.html?m3;b;b153;AFTX;AJFE;AU-BG-EY-FP-HL-IN-JZ-OS-QR-TX\n",
1153 "w_enigma = Enigma(reflector_b_spec, \n",
1154 " wheel_i_spec, wheel_i_pegs,\n",
1155 " wheel_v_spec, wheel_v_pegs,\n",
1156 " wheel_iii_spec, wheel_iii_pegs,\n",
1158 " 'ua pf rq so ni ey bg hl tx zj')"
1162 "cell_type": "code",
1163 "execution_count": 837,
1174 "execution_count": 837,
1176 "output_type": "execute_result"
1180 "w_enigma.set_wheels('j', 'e', 'b')\n",
1181 "tuple(unpos(p) for p in w_enigma.wheel_positions)"
1185 "cell_type": "code",
1186 "execution_count": 838,
1197 "execution_count": 838,
1199 "output_type": "execute_result"
1203 "w_enigma.set_wheels('j', 'e', 'b')\n",
1204 "pt = 'someplaintext'\n",
1205 "ct = w_enigma.encipher(pt)\n",
1210 "cell_type": "code",
1211 "execution_count": 839,
1222 "execution_count": 839,
1224 "output_type": "execute_result"
1228 "w_enigma.wheel_positions_l"
1232 "cell_type": "code",
1233 "execution_count": 840,
1241 "[MenuIem(before='s', after='d', number=1),\n",
1242 " MenuIem(before='o', after='h', number=2),\n",
1243 " MenuIem(before='m', after='n', number=3),\n",
1244 " MenuIem(before='e', after='p', number=4),\n",
1245 " MenuIem(before='p', after='f', number=5),\n",
1246 " MenuIem(before='l', after='o', number=6),\n",
1247 " MenuIem(before='a', after='r', number=7),\n",
1248 " MenuIem(before='i', after='e', number=8),\n",
1249 " MenuIem(before='n', after='e', number=9),\n",
1250 " MenuIem(before='t', after='i', number=10),\n",
1251 " MenuIem(before='e', after='m', number=11),\n",
1252 " MenuIem(before='x', after='g', number=12),\n",
1253 " MenuIem(before='t', after='g', number=13)]"
1256 "execution_count": 840,
1258 "output_type": "execute_result"
1262 "w_menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
1267 "cell_type": "code",
1268 "execution_count": 841,
1277 "[('a', 'y', 'm'),\n",
1278 " ('c', 'p', 'v'),\n",
1279 " ('c', 's', 'f'),\n",
1280 " ('c', 'w', 'j'),\n",
1281 " ('d', 'r', 'k'),\n",
1282 " ('e', 'l', 'f'),\n",
1283 " ('e', 's', 'v'),\n",
1284 " ('e', 'y', 'd'),\n",
1285 " ('e', 'y', 'o'),\n",
1286 " ('f', 'z', 'x'),\n",
1287 " ('g', 'b', 'l'),\n",
1288 " ('g', 'c', 'd'),\n",
1289 " ('g', 'c', 'f'),\n",
1290 " ('g', 'j', 'p'),\n",
1291 " ('h', 'c', 'i'),\n",
1292 " ('h', 'm', 'w'),\n",
1293 " ('h', 'o', 'd'),\n",
1294 " ('h', 'p', 'b'),\n",
1295 " ('h', 's', 't'),\n",
1296 " ('i', 'b', 's'),\n",
1297 " ('i', 'v', 'b'),\n",
1298 " ('j', 'y', 'u'),\n",
1299 " ('k', 'b', 'x'),\n",
1300 " ('k', 'f', 't'),\n",
1301 " ('k', 'l', 'e'),\n",
1302 " ('k', 'l', 'm'),\n",
1303 " ('k', 'r', 'z'),\n",
1304 " ('k', 's', 'p'),\n",
1305 " ('l', 'd', 'z'),\n",
1306 " ('l', 'i', 'y'),\n",
1307 " ('l', 'y', 'f'),\n",
1308 " ('m', 'b', 'h'),\n",
1309 " ('m', 'p', 'l'),\n",
1310 " ('n', 'l', 'r'),\n",
1311 " ('o', 'k', 'x'),\n",
1312 " ('p', 'a', 'g'),\n",
1313 " ('p', 'c', 'v'),\n",
1314 " ('p', 'f', 'o'),\n",
1315 " ('p', 'm', 'i'),\n",
1316 " ('p', 'x', 'n'),\n",
1317 " ('p', 'x', 'p'),\n",
1318 " ('q', 'q', 'n'),\n",
1319 " ('q', 'r', 'w'),\n",
1320 " ('q', 'v', 'l'),\n",
1321 " ('q', 'x', 't'),\n",
1322 " ('s', 'a', 'h'),\n",
1323 " ('s', 'h', 'v'),\n",
1324 " ('s', 'l', 'p'),\n",
1325 " ('s', 'l', 's'),\n",
1326 " ('u', 'r', 'h'),\n",
1327 " ('v', 'v', 'v'),\n",
1328 " ('v', 'x', 'a'),\n",
1329 " ('w', 'j', 'z'),\n",
1330 " ('w', 'k', 'u'),\n",
1331 " ('x', 'f', 'p'),\n",
1332 " ('x', 'j', 'n'),\n",
1333 " ('x', 'o', 'q'),\n",
1334 " ('x', 'x', 'x'),\n",
1335 " ('y', 'n', 'c'),\n",
1336 " ('y', 'r', 'f'),\n",
1337 " ('z', 't', 'y'),\n",
1341 "execution_count": 841,
1343 "output_type": "execute_result"
1347 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1349 "with multiprocessing.Pool() as pool:\n",
1350 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1351 " menu=w_menu, verify_plugboard=False),\n",
1353 "[r[0] for r in res if r[1]]"
1357 "cell_type": "code",
1358 "execution_count": 842,
1369 "execution_count": 842,
1371 "output_type": "execute_result"
1375 "len([r[0] for r in res if r[1]])"
1379 "cell_type": "code",
1380 "execution_count": 843,
1389 "[('c', 'p', 'v'),\n",
1390 " ('c', 's', 'f'),\n",
1391 " ('e', 'l', 'f'),\n",
1392 " ('g', 'c', 'f'),\n",
1393 " ('j', 'y', 'u'),\n",
1394 " ('o', 'k', 'x'),\n",
1395 " ('p', 'a', 'g'),\n",
1396 " ('q', 'q', 'n'),\n",
1397 " ('q', 'v', 'l'),\n",
1398 " ('q', 'x', 't'),\n",
1399 " ('s', 'l', 'p'),\n",
1400 " ('u', 'r', 'h'),\n",
1404 "execution_count": 843,
1406 "output_type": "execute_result"
1410 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1412 "with multiprocessing.Pool() as pool:\n",
1413 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1414 " menu=w_menu, verify_plugboard=True),\n",
1416 "[r[0] for r in res if r[1]]"
1420 "cell_type": "code",
1421 "execution_count": 858,
1429 "[('c', 'p', 'v'),\n",
1430 " ('c', 's', 'f'),\n",
1431 " ('e', 'l', 'f'),\n",
1432 " ('g', 'c', 'f'),\n",
1433 " ('j', 'y', 'u'),\n",
1434 " ('o', 'k', 'x'),\n",
1435 " ('p', 'a', 'g'),\n",
1436 " ('q', 'q', 'n'),\n",
1437 " ('q', 'v', 'l'),\n",
1438 " ('q', 'x', 't'),\n",
1439 " ('s', 'l', 'p'),\n",
1440 " ('u', 'r', 'h'),\n",
1444 "execution_count": 858,
1446 "output_type": "execute_result"
1450 "run_multi_bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, w_menu)"
1454 "cell_type": "code",
1455 "execution_count": 844,
1461 "# allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1463 "# with multiprocessing.Pool() as pool:\n",
1464 "# res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1465 "# menu=w_menu, start_signal=Signal('t', 'x')),\n",
1467 "# [r[0] for r in res if r[1]]"
1471 "cell_type": "code",
1472 "execution_count": 845,
1483 "execution_count": 845,
1485 "output_type": "execute_result"
1489 "len([r[0] for r in res if r[1]])"
1493 "cell_type": "code",
1494 "execution_count": 846,
1500 "w_bombe = Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1505 "cell_type": "code",
1506 "execution_count": 847,
1514 "Signal(bank='e', wire='e')"
1517 "execution_count": 847,
1519 "output_type": "execute_result"
1523 "w_bombe.test_start"
1527 "cell_type": "code",
1528 "execution_count": 848,
1539 "execution_count": 848,
1541 "output_type": "execute_result"
1545 "w_bombe.test(start_positions=('e', 'l', 'f'))"
1549 "cell_type": "code",
1550 "execution_count": 849,
1561 "execution_count": 849,
1563 "output_type": "execute_result"
1567 "w_bombe.test(Signal('t', 'x'), ('e', 'l', 'f'))"
1571 "cell_type": "code",
1572 "execution_count": 850,
1579 "output_type": "stream",
1582 "a : a.cdefghi..l.nopqrst.vwx..\n",
1583 "b : ....efghi...mnop.......x..\n",
1584 "c : a....fg.i..lmn.p.r.t...x..\n",
1585 "d : a...efghij.lmn...rstu..xyz\n",
1586 "e : ab.defghijklmnopqrstuvwxyz\n",
1587 "f : abcde.ghijklmnopqrstuvwxyz\n",
1588 "g : abcdef.hijklmnopqrstuvwxyz\n",
1589 "h : ab.defghi...mnopq.stuvwx..\n",
1590 "i : abcdefgh.jklmnopqrstuvwxyz\n",
1591 "j : ...defg.i...mn.p...t...x..\n",
1592 "k : ....efg.i..lmnop.r.t...x..\n",
1593 "l : a.cdefg.i.k.mnopqr.t..wxyz\n",
1594 "m : .bcdefghijklmnopqrstuvwxyz\n",
1595 "n : abcdefghijklmnopqr.tuvwxyz\n",
1596 "o : ab..efghi.klmnopqr.t...xyz\n",
1597 "p : abc.efghijklmnopqrstuvwxyz\n",
1598 "q : a...efghi..lmnop.r.t...x..\n",
1599 "r : a.cdefg.i.klmnopqrst..w..z\n",
1600 "s : a..defghi...m..p.rstuv.xyz\n",
1601 "t : a.cdefghijklmnopqrstuvwxyz\n",
1602 "u : ...defghi...mn.p..st...x..\n",
1603 "v : a...efghi...mn.p..st...x..\n",
1604 "w : a...efghi..lmn.p.r.t...x..\n",
1605 "x : abcdefghijklmnopq.stuvwxyz\n",
1606 "y : ...defg.i..lmnop..st...x..\n",
1607 "z : ...defg.i..lmnop.rst...x..\n"
1612 "r = w_bombe.test(start_positions=('c', 'p', 'v'))\n",
1614 "for b in sorted(w_bombe.banks):\n",
1615 " print(b, ': ', end='')\n",
1616 " for w in sorted(w_bombe.banks[b]):\n",
1617 " if w_bombe.banks[b][w]:\n",
1618 " print(w, end='')\n",
1620 " print('.', end='')\n",
1625 "cell_type": "code",
1626 "execution_count": 851,
1633 "output_type": "stream",
1636 "a : abcdefghi..lmnop.rst.v.x.z\n",
1637 "b : a...ef.hi..lmnop.r.t...x..\n",
1638 "c : a..defg.i..lmn.p.rst...x..\n",
1639 "d : a.c.efghi.klmnopqrstu.wxyz\n",
1640 "e : abcdefghijklmnopqrstuvwx.z\n",
1641 "f : abcdefghijklmno.qrstuvwxyz\n",
1642 "g : a.cdefghijklmnopqrstuvwxyz\n",
1643 "h : ab.defghi...mnopqrstuvwxyz\n",
1644 "i : abcdefghijklm.opqrstuvwxyz\n",
1645 "j : ....efg.i..lmnop...t...x..\n",
1646 "k : ...defg.i..lmn.p..st...x..\n",
1647 "l : abcdefg.ijklmnopqrstuv.x..\n",
1648 "m : abcdefghijkl.nopqrstuvwxyz\n",
1649 "n : abcdefgh.jklmnopqrstuvwxyz\n",
1650 "o : ab.defghij.lmnop.r.tuvwxyz\n",
1651 "p : abcde.ghijklmnopqrstuvwxyz\n",
1652 "q : ...defghi..lmn.p..st...x..\n",
1653 "r : abcdefghi..lmnop.rst.v.x.z\n",
1654 "s : a.cdefghi.klmn.pqr.tuvwxyz\n",
1655 "t : abcdefghijklmnopqrstuvw.yz\n",
1656 "u : ...defghi..lmnop..st...x..\n",
1657 "v : a...efghi..lmnop.rst...x..\n",
1658 "w : ...defghi...mnop..st...x..\n",
1659 "x : abcdefghijklmnopqrs.uvwxyz\n",
1660 "y : ...d.fghi...mnop..st...x..\n",
1661 "z : a..defghi...mnop.rst...x..\n"
1666 "r = w_bombe.test(start_positions=('e', 'l', 'f'))\n",
1668 "for b in sorted(w_bombe.banks):\n",
1669 " print(b, ': ', end='')\n",
1670 " for w in sorted(w_bombe.banks[b]):\n",
1671 " if w_bombe.banks[b][w]:\n",
1672 " print(w, end='')\n",
1674 " print('.', end='')\n",
1679 "cell_type": "code",
1680 "execution_count": 852,
1688 "{frozenset({'e', 'y'}),\n",
1689 " frozenset({'t', 'x'}),\n",
1690 " frozenset({'i', 'n'}),\n",
1691 " frozenset({'m'}),\n",
1692 " frozenset({'b', 'g'}),\n",
1693 " frozenset({'f', 'p'})}"
1696 "execution_count": 852,
1698 "output_type": "execute_result"
1702 "ps = w_bombe.possible_plugboards()\n",
1707 "cell_type": "code",
1708 "execution_count": 853,
1719 "execution_count": 853,
1721 "output_type": "execute_result"
1725 "all(s0.isdisjoint(s1) for s0 in ps for s1 in ps if s0 != s1)"
1729 "cell_type": "code",
1730 "execution_count": 854,
1738 "({frozenset({1, 2}), frozenset({2, 3}), frozenset({3, 4})},\n",
1739 " frozenset({1, 2}),\n",
1740 " frozenset({3, 4}),\n",
1741 " frozenset({2, 3}))"
1744 "execution_count": 854,
1746 "output_type": "execute_result"
1751 "f1 = frozenset((1, 2))\n",
1752 "f2 = frozenset((3, 4))\n",
1753 "f3 = frozenset((2, 3))\n",
1754 "s = s.union({f1})\n",
1755 "s = s.union({f2})\n",
1756 "s = s.union({f1})\n",
1757 "s = s.union({f3})\n",
1762 "cell_type": "code",
1763 "execution_count": 855,
1774 "execution_count": 855,
1776 "output_type": "execute_result"
1780 "all(s0.isdisjoint(s1) for s0 in s for s1 in s if s0 != s1)"
1784 "cell_type": "code",
1785 "execution_count": 856,
1796 "execution_count": 856,
1798 "output_type": "execute_result"
1802 "{1, 2}.isdisjoint({1, 6})"
1806 "cell_type": "code",
1807 "execution_count": null,
1817 "display_name": "Python 3",
1818 "language": "python",
1822 "codemirror_mode": {
1826 "file_extension": ".py",
1827 "mimetype": "text/x-python",
1829 "nbconvert_exporter": "python",
1830 "pygments_lexer": "ipython3",