12 "import collections\n",
13 "import multiprocessing\n",
15 "from enigma import *"
26 "Signal = collections.namedtuple('Signal', ['bank', 'wire'])\n",
27 "Connection = collections.namedtuple('Connection', ['banks', 'scrambler'])\n",
28 "MenuItem = collections.namedtuple('MenuIem', ['before', 'after', 'number'])"
39 "class Scrambler(object):\n",
40 " def __init__(self, wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec,\n",
41 " wheel1_pos='a', wheel2_pos='a', wheel3_pos='a'):\n",
42 " self.wheel1 = SimpleWheel(wheel1_spec, position=wheel1_pos)\n",
43 " self.wheel2 = SimpleWheel(wheel2_spec, position=wheel2_pos)\n",
44 " self.wheel3 = SimpleWheel(wheel3_spec, position=wheel3_pos)\n",
45 " self.reflector = Reflector(reflector_spec)\n",
47 " def __getattribute__(self, name):\n",
48 " if name=='wheel_positions':\n",
49 " return self.wheel1.position, self.wheel2.position, self.wheel3.position \n",
50 " elif name=='wheel_positions_l':\n",
51 " return self.wheel1.position_l, self.wheel2.position_l, self.wheel3.position_l \n",
53 " return object.__getattribute__(self, name)\n",
55 " def advance(self, wheel1=False, wheel2=False, wheel3=True):\n",
56 " if wheel1: self.wheel1.advance()\n",
57 " if wheel2: self.wheel2.advance()\n",
58 " if wheel3: self.wheel3.advance()\n",
60 " def lookup(self, letter):\n",
61 " a = self.wheel3.forward(letter)\n",
62 " b = self.wheel2.forward(a)\n",
63 " c = self.wheel1.forward(b)\n",
64 " d = self.reflector.forward(c)\n",
65 " e = self.wheel1.backward(d)\n",
66 " f = self.wheel2.backward(e)\n",
67 " g = self.wheel3.backward(f)\n",
70 " def set_positions(self, wheel1_pos, wheel2_pos, wheel3_pos):\n",
71 " self.wheel1.set_position(wheel1_pos)\n",
72 " self.wheel2.set_position(wheel2_pos)\n",
73 " self.wheel3.set_position(wheel3_pos) "
84 "class Bombe(object):\n",
86 " def __init__(self, wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec,\n",
87 " menu=None, start_signal=None, use_diagonal_board=True, \n",
88 " verify_plugboard=True):\n",
89 " self.connections = []\n",
90 " self.wheel1_spec = wheel1_spec\n",
91 " self.wheel2_spec = wheel2_spec\n",
92 " self.wheel3_spec = wheel3_spec\n",
93 " self.reflector_spec = reflector_spec\n",
95 " self.read_menu(menu)\n",
96 " if start_signal:\n",
97 " self.test_start = start_signal\n",
98 " self.use_diagonal_board = use_diagonal_board\n",
99 " self.verify_plugboard = verify_plugboard\n",
101 " def __getattribute__(self, name):\n",
102 " if name=='wheel_positions':\n",
103 " return self.connections[0].scrambler.wheel_positions\n",
104 " elif name=='wheel_positions_l':\n",
105 " return self.connections[0].scrambler.wheel_positions_l\n",
107 " return object.__getattribute__(self, name)\n",
109 " def __call__(self, start_positions):\n",
110 " return start_positions, self.test(initial_signal=self.test_start,\n",
111 " start_positions=start_positions, \n",
112 " use_diagonal_board=self.use_diagonal_board,\n",
113 " verify_plugboard=self.verify_plugboard)\n",
115 " def add_connection(self, bank_before, bank_after, scrambler):\n",
116 " self.connections += [Connection([bank_before, bank_after], scrambler)]\n",
118 " def read_menu(self, menu):\n",
119 " self.connections = []\n",
120 " for item in menu:\n",
121 " scrambler = Scrambler(self.wheel1_spec, self.wheel2_spec, self.wheel3_spec,\n",
122 " self.reflector_spec,\n",
123 " wheel3_pos=unpos(item.number - 1))\n",
124 " self.add_connection(item.before, item.after, scrambler)\n",
125 " most_common_letter = (collections.Counter(m.before for m in menu) + \\\n",
126 " collections.Counter(m.after for m in menu)).most_common(1)[0][0]\n",
127 " self.test_start = Signal(most_common_letter, most_common_letter)\n",
129 " def set_positions(self, wheel1_pos, wheel2_pos, wheel3_pos):\n",
130 " for i, c in enumerate(self.connections):\n",
131 " c.scrambler.set_positions(wheel1_pos, wheel2_pos, unpos(pos(wheel3_pos) + i))\n",
133 " def test(self, initial_signal=None, start_positions=None, use_diagonal_board=True,\n",
134 " verify_plugboard=True):\n",
135 " self.banks = {label: \n",
136 " dict(zip(string.ascii_lowercase, [False]*len(string.ascii_lowercase)))\n",
137 " for label in string.ascii_lowercase}\n",
138 " if start_positions:\n",
139 " self.set_positions(*start_positions)\n",
140 " if not initial_signal:\n",
141 " initial_signal = self.test_start\n",
142 " self.pending = [initial_signal]\n",
143 " self.propagate(use_diagonal_board)\n",
144 " live_wire_count = len([self.banks[self.test_start.bank][w] \n",
145 " for w in self.banks[self.test_start.bank] \n",
146 " if self.banks[self.test_start.bank][w]])\n",
147 " if live_wire_count < 26:\n",
148 " if verify_plugboard:\n",
149 " possibles = self.possible_plugboards()\n",
150 " return all(s0.isdisjoint(s1) for s0 in possibles for s1 in possibles if s0 != s1)\n",
156 " def propagate(self, use_diagonal_board):\n",
157 " while self.pending:\n",
158 " current = self.pending[0]\n",
159 " # print(\"processing\", current)\n",
160 " self.pending = self.pending[1:]\n",
161 " if not self.banks[current.bank][current.wire]:\n",
162 " self.banks[current.bank][current.wire] = True\n",
163 " if use_diagonal_board:\n",
164 " self.pending += [Signal(current.wire, current.bank)]\n",
165 " for c in self.connections:\n",
166 " if current.bank in c.banks:\n",
167 " if len(set(c.banks)) == 1:\n",
168 " other_bank = c.banks[0]\n",
170 " other_bank = [b for b in c.banks if b != current.bank][0]\n",
171 " other_wire = c.scrambler.lookup(current.wire)\n",
172 " # print(\" adding\", other_bank, other_wire, \"because\", c.banks)\n",
173 " self.pending += [Signal(other_bank, other_wire)]\n",
175 " def run(self, run_start=None, wheel1_pos='a', wheel2_pos='a', wheel3_pos='a', use_diagonal_board=True):\n",
176 " if not run_start:\n",
177 " run_start = self.test_start\n",
178 " self.solutions = []\n",
179 " self.set_positions(wheel1_pos, wheel2_pos, wheel3_pos)\n",
180 " for run_index in range(26*26*26):\n",
181 " if self.test(initial_signal=run_start, use_diagonal_board=use_diagonal_board):\n",
182 " self.solutions += [self.connections[0].scrambler.wheel_positions_l]\n",
183 " advance3 = True\n",
184 " advance2 = False\n",
185 " advance1 = False\n",
186 " if (run_index + 1) % 26 == 0: advance2 = True\n",
187 " if (run_index + 1) % (26*26) == 0: advance1 = True\n",
188 " for c in self.connections:\n",
189 " c.scrambler.advance(advance1, advance2, advance3)\n",
190 " return self.solutions\n",
192 " def possible_plugboards(self):\n",
193 " possibles = set()\n",
194 " for b in self.banks:\n",
195 " active = [w for w in self.banks[b] if self.banks[b][w]]\n",
196 " inactive = [w for w in self.banks[b] if not self.banks[b][w]]\n",
197 " if len(active) == 1:\n",
198 " possibles = possibles.union({frozenset((b, active[0]))})\n",
199 " if len(inactive) == 1:\n",
200 " possibles = possibles.union({frozenset((b, inactive[0]))})\n",
201 " return possibles\n"
206 "execution_count": 5,
212 "bombe = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec)\n",
213 "# len(bombe.banks), bombe.banks['a'] == bombe.banks['b']"
218 "execution_count": 6,
224 "test_enigma = Enigma(reflector_b_spec, \n",
225 " wheel_i_spec, wheel_i_pegs,\n",
226 " wheel_ii_spec, wheel_ii_pegs,\n",
227 " wheel_iii_spec, wheel_iii_pegs,\n",
234 "execution_count": 7,
240 "'opgndxcrwomnlnecjz'"
243 "execution_count": 7,
245 "output_type": "execute_result"
249 "test_enigma.set_wheels('a', 'a', 'a')\n",
250 "pt = 'thisisatestmessage'\n",
251 "ct = test_enigma.encipher(pt)\n",
257 "execution_count": 8,
266 "execution_count": 8,
268 "output_type": "execute_result"
272 "cat(test_enigma.wheel_positions_l)"
277 "execution_count": 9,
283 "[MenuIem(before='t', after='o', number=1),\n",
284 " MenuIem(before='h', after='p', number=2),\n",
285 " MenuIem(before='i', after='g', number=3),\n",
286 " MenuIem(before='s', after='n', number=4),\n",
287 " MenuIem(before='i', after='d', number=5),\n",
288 " MenuIem(before='s', after='x', number=6),\n",
289 " MenuIem(before='a', after='c', number=7),\n",
290 " MenuIem(before='t', after='r', number=8),\n",
291 " MenuIem(before='e', after='w', number=9),\n",
292 " MenuIem(before='s', after='o', number=10),\n",
293 " MenuIem(before='t', after='m', number=11),\n",
294 " MenuIem(before='m', after='n', number=12),\n",
295 " MenuIem(before='e', after='l', number=13),\n",
296 " MenuIem(before='s', after='n', number=14),\n",
297 " MenuIem(before='s', after='e', number=15),\n",
298 " MenuIem(before='a', after='c', number=16),\n",
299 " MenuIem(before='g', after='j', number=17),\n",
300 " MenuIem(before='e', after='z', number=18)]"
303 "execution_count": 9,
305 "output_type": "execute_result"
309 "menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
315 "execution_count": 10,
321 "def make_menu(plaintext, ciphertext):\n",
322 " return [MenuItem(p, c, i+1) \n",
323 " for i, (p, c) in enumerate(zip(plaintext, ciphertext))]"
328 "execution_count": 11,
334 "[MenuIem(before='t', after='o', number=1),\n",
335 " MenuIem(before='h', after='p', number=2),\n",
336 " MenuIem(before='i', after='g', number=3),\n",
337 " MenuIem(before='s', after='n', number=4),\n",
338 " MenuIem(before='i', after='d', number=5),\n",
339 " MenuIem(before='s', after='x', number=6),\n",
340 " MenuIem(before='a', after='c', number=7),\n",
341 " MenuIem(before='t', after='r', number=8),\n",
342 " MenuIem(before='e', after='w', number=9),\n",
343 " MenuIem(before='s', after='o', number=10),\n",
344 " MenuIem(before='t', after='m', number=11),\n",
345 " MenuIem(before='m', after='n', number=12),\n",
346 " MenuIem(before='e', after='l', number=13),\n",
347 " MenuIem(before='s', after='n', number=14),\n",
348 " MenuIem(before='s', after='e', number=15),\n",
349 " MenuIem(before='a', after='c', number=16),\n",
350 " MenuIem(before='g', after='j', number=17),\n",
351 " MenuIem(before='e', after='z', number=18)]"
354 "execution_count": 11,
356 "output_type": "execute_result"
365 "execution_count": 12,
374 "execution_count": 12,
376 "output_type": "execute_result"
380 "(collections.Counter(m.before for m in menu) + collections.Counter(m.after for m in menu)).most_common(1)[0][0]"
385 "execution_count": 13,
391 "bombe.read_menu(menu)"
396 "execution_count": 14,
405 "execution_count": 14,
407 "output_type": "execute_result"
411 "len(bombe.connections)"
416 "execution_count": 15,
421 "output_type": "stream",
445 "for c in bombe.connections:\n",
446 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
451 "execution_count": 16,
457 "'ot:hp:gi:ns:di:sx:ac:rt:ew:os:mt:mn:el:ns:es:ac:gj:ez'"
460 "execution_count": 16,
462 "output_type": "execute_result"
466 "':'.join(cat(sorted(c.banks)) for c in bombe.connections)"
471 "execution_count": 17,
477 "'aaa:aab:aac:aad:aae:aaf:aag:aah:aai:aaj:aak:aal:aam:aan:aao:aap:aaq:aar'"
480 "execution_count": 17,
482 "output_type": "execute_result"
486 "':'.join(cat(c.scrambler.wheel_positions_l) for c in bombe.connections)"
491 "execution_count": 18,
500 "execution_count": 18,
502 "output_type": "execute_result"
506 "bombe.test(Signal('t', 't'))"
511 "execution_count": 19,
545 "execution_count": 19,
547 "output_type": "execute_result"
556 "execution_count": 20,
561 "output_type": "stream",
563 "a : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
564 "b : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
565 "c : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
566 "d : ABCDEFGHIJKLMNOPQRSTUVWXyZ\n",
567 "e : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
568 "f : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
569 "g : ABCDEFGHIJKLMNOPQRSTuVWXYZ\n",
570 "h : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
571 "i : ABCDEFGHIJKLMNOPQRSTUvWXYZ\n",
572 "j : ABCDEFGHIjKLMNOPQRSTUVWXYZ\n",
573 "k : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
574 "l : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
575 "m : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
576 "n : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
577 "o : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
578 "p : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
579 "q : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
580 "r : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
581 "s : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
582 "t : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
583 "u : AbCDEfgHIJkLMNOPqRSTuvWXyZ\n",
584 "v : AbCDEfGHiJkLMNOPqRSTuvWXyZ\n",
585 "w : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
586 "x : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
587 "y : AbCdEfGHIJkLMNOPqRSTuvWXyZ\n",
588 "z : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
593 "for b in sorted(bombe.banks):\n",
594 " print(b, ': ', end='')\n",
595 " for w in sorted(bombe.banks[b]):\n",
596 " if bombe.banks[b][w]:\n",
597 " print(w.upper(), end='')\n",
599 " print(w, end='')\n",
605 "execution_count": 21,
610 "output_type": "stream",
612 "a : abcdefghijklmnopqrstuvwxyz\n",
613 "b : a.cde.ghij.lmnop.rst..wx.z\n",
614 "c : abcdefghijklmnopqrstuvwxyz\n",
615 "d : abcdefghijklmnopqrstuvwx.z\n",
616 "e : abcdefghijklmnopqrstuvwxyz\n",
617 "f : a.cde.ghij.lmnop.rst..wx.z\n",
618 "g : abcdefghijklmnopqrst.vwxyz\n",
619 "h : abcdefghijklmnopqrstuvwxyz\n",
620 "i : abcdefghijklmnopqrstu.wxyz\n",
621 "j : abcdefghi.klmnopqrstuvwxyz\n",
622 "k : a.cde.ghij.lmnop.rst..wx.z\n",
623 "l : abcdefghijklmnopqrstuvwxyz\n",
624 "m : abcdefghijklmnopqrstuvwxyz\n",
625 "n : abcdefghijklmnopqrstuvwxyz\n",
626 "o : abcdefghijklmnopqrstuvwxyz\n",
627 "p : abcdefghijklmnopqrstuvwxyz\n",
628 "q : a.cde.ghij.lmnop.rst..wx.z\n",
629 "r : abcdefghijklmnopqrstuvwxyz\n",
630 "s : abcdefghijklmnopqrstuvwxyz\n",
631 "t : abcdefghijklmnopqrstuvwxyz\n",
632 "u : a.cde..hij.lmnop.rst..wx.z\n",
633 "v : a.cde.gh.j.lmnop.rst..wx.z\n",
634 "w : abcdefghijklmnopqrstuvwxyz\n",
635 "x : abcdefghijklmnopqrstuvwxyz\n",
636 "y : a.c.e.ghij.lmnop.rst..wx.z\n",
637 "z : abcdefghijklmnopqrstuvwxyz\n"
642 "for b in sorted(bombe.banks):\n",
643 " print(b, ': ', end='')\n",
644 " for w in sorted(bombe.banks[b]):\n",
645 " if bombe.banks[b][w]:\n",
646 " print(w, end='')\n",
648 " print('.', end='')\n",
654 "execution_count": 22,
663 "execution_count": 22,
665 "output_type": "execute_result"
669 "bombe.wheel_positions_l"
674 "execution_count": 23,
679 "output_type": "stream",
703 "bombe.set_positions('p', 'q', 'r')\n",
704 "for c in bombe.connections:\n",
705 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
710 "execution_count": 24,
722 "execution_count": 25,
727 "output_type": "stream",
729 "a : .b.defghijklmnopqrstuvwxyz\n",
730 "b : a.cde.ghij.lmnop.rst..wx.z\n",
731 "c : .b.defghijklmnopqrstuvwxyz\n",
732 "d : abc.e.gh.jklmnopqrstuvwxyz\n",
733 "e : abcd.fghijklmnopqrstuvwxyz\n",
734 "f : a.c.e.g.ij.lmno..rst..wx.z\n",
735 "g : abcdef.h..klmnopqrstuvwxyz\n",
736 "h : abcde.g.ij.lmno.qrst..wxyz\n",
737 "i : abc.ef.h.jklmnopqrstuvwxyz\n",
738 "j : abcdef.hi.klmnopqrstuvwx.z\n",
739 "k : a.cde.g.ij.lmno..rst..wx.z\n",
740 "l : abcdefghijk.mnopqrstuvwxyz\n",
741 "m : abcdefghijkl.nopqrstuvwxyz\n",
742 "n : abcdefghijklm.opqrstuvwxyz\n",
743 "o : abcdefghijklmn.pqrstuvwxyz\n",
744 "p : abcde.g.ij.lmno.qrst..wxyz\n",
745 "q : a.cde.ghij.lmnop.rst..wx.z\n",
746 "r : abcdefghijklmnopq.stuvwxyz\n",
747 "s : abcdefghijklmnopqr.tuvwxyz\n",
748 "t : abcdefghijklmnopqrs.uvwxyz\n",
749 "u : a.cde.g.ij.lmno..rst..wx.z\n",
750 "v : a.cde.g.ij.lmno..rst..wx.z\n",
751 "w : abcdefghijklmnopqrstuv.xyz\n",
752 "x : abcdefghijklmnopqrstuvw.yz\n",
753 "y : a.cde.ghi..lmnop.rst..wx.z\n",
754 "z : abcdefghijklmnopqrstuvwxy.\n"
759 "bombe.set_positions('a', 'a', 'b')\n",
760 "bombe.test(Signal('s', 'a'))\n",
762 "for b in sorted(bombe.banks):\n",
763 " print(b, ': ', end='')\n",
764 " for w in sorted(bombe.banks[b]):\n",
765 " if bombe.banks[b][w]:\n",
766 " print(w, end='')\n",
768 " print('.', end='')\n",
774 "execution_count": 26,
779 "output_type": "stream",
781 "a : ..........................\n",
782 "b : ..........................\n",
783 "c : ..........................\n",
784 "d : ..........................\n",
785 "e : ....e.....................\n",
786 "f : ..........................\n",
787 "g : ..........................\n",
788 "h : ..........................\n",
789 "i : ..........................\n",
790 "j : ..........................\n",
791 "k : ..........................\n",
792 "l : ...........l..............\n",
793 "m : ............m.............\n",
794 "n : .............n............\n",
795 "o : ..............o...........\n",
796 "p : ..........................\n",
797 "q : ..........................\n",
798 "r : .................r........\n",
799 "s : ..................s.......\n",
800 "t : ...................t......\n",
801 "u : ..........................\n",
802 "v : ..........................\n",
803 "w : ......................w...\n",
804 "x : .......................x..\n",
805 "y : ..........................\n",
806 "z : .........................z\n"
811 "bombe.set_positions('a', 'a', 'b')\n",
814 "for b in sorted(bombe.banks):\n",
815 " print(b, ': ', end='')\n",
816 " for w in sorted(bombe.banks[b]):\n",
817 " if bombe.banks[b][w]:\n",
818 " print(w, end='')\n",
820 " print('.', end='')\n",
826 "execution_count": 27,
835 "execution_count": 27,
837 "output_type": "execute_result"
841 "len([bombe.banks['t'][w] for w in bombe.banks['t'] if bombe.banks['t'][w]])"
846 "execution_count": 28,
853 "# results = bombe.run()\n",
854 "# print(len(results), ('a', 'a', 'b') in results)"
859 "execution_count": 29,
866 "# results = bombe.run(use_diagonal_board=False)\n",
867 "# print(len(results), ('a', 'a', 'b') in results)"
872 "execution_count": 30,
881 "execution_count": 30,
883 "output_type": "execute_result"
887 "bombe.wheel_positions_l"
892 "execution_count": 31,
901 "execution_count": 31,
903 "output_type": "execute_result"
907 "bombe.test(Signal('t', 't'), ('p', 'p', 'p'))"
912 "execution_count": 32,
921 "execution_count": 32,
923 "output_type": "execute_result"
927 "bombe.wheel_positions_l"
932 "execution_count": 33,
937 "output_type": "stream",
939 "a : abcdefghijklmnop.rst.vwxyz\n",
940 "b : a.cde.g.ij.lmno..rst..wx.z\n",
941 "c : abcdefghijklmnop.rst.vwxyz\n",
942 "d : abcdefghijklmnopqrstuvwxyz\n",
943 "e : abcdefghijklmnopqrstuvwxyz\n",
944 "f : a.cde.g.ij.lmno..rst..wx.z\n",
945 "g : abcdefghijklmnopqrstuvwxyz\n",
946 "h : a.cde.ghijklmnop.rst.vwxyz\n",
947 "i : abcdefghijklmnopqrstuvwxyz\n",
948 "j : abcdefghijklmnopqrstuvwxyz\n",
949 "k : a.cde.ghij.lmnop.rst..wx.z\n",
950 "l : abcdefghijklmnopqrstuvwxyz\n",
951 "m : abcdefghijklmnopqrstuvwxyz\n",
952 "n : abcdefghijklmnopqrstuvwxyz\n",
953 "o : abcdefghijklmnopqrstuvwxyz\n",
954 "p : a.cde.ghijklmnop.rst.vwxyz\n",
955 "q : ...de.g.ij.lmno..rst..wx.z\n",
956 "r : abcdefghijklmnopqrstuvwxyz\n",
957 "s : abcdefghijklmnopqrstuvwxyz\n",
958 "t : abcdefghijklmnopqrstuvwxyz\n",
959 "u : ...de.g.ij.lmno..rst..wx.z\n",
960 "v : a.cde.ghij.lmnop.rst..wx.z\n",
961 "w : abcdefghijklmnopqrstuvwxyz\n",
962 "x : abcdefghijklmnopqrstuvwxyz\n",
963 "y : a.cde.ghij.lmnop.rst..wx.z\n",
964 "z : abcdefghijklmnopqrstuvwxyz\n"
969 "for b in sorted(bombe.banks):\n",
970 " print(b, ': ', end='')\n",
971 " for w in sorted(bombe.banks[b]):\n",
972 " if bombe.banks[b][w]:\n",
973 " print(w, end='')\n",
975 " print('.', end='')\n",
981 "execution_count": 34,
990 "execution_count": 34,
992 "output_type": "execute_result"
996 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
997 "len(list(allwheels))"
1001 "cell_type": "code",
1002 "execution_count": 35,
1008 "(('a', 'a', 'b'), True)"
1011 "execution_count": 35,
1013 "output_type": "execute_result"
1017 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)\n",
1018 "b(('a', 'a', 'b'))"
1022 "cell_type": "code",
1023 "execution_count": 36,
1029 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)(('a', 'a', 'b'))"
1033 "cell_type": "code",
1034 "execution_count": 37,
1043 "execution_count": 37,
1045 "output_type": "execute_result"
1049 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1051 "with multiprocessing.Pool() as pool:\n",
1052 " res = pool.map(Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu),\n",
1054 "[r[0] for r in res if r[1]]"
1058 "cell_type": "code",
1059 "execution_count": 38,
1065 "def run_multi_bombe(wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec, menu,\n",
1066 " start_signal=None, use_diagonal_board=True, \n",
1067 " verify_plugboard=True):\n",
1068 " allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1070 " with multiprocessing.Pool() as pool:\n",
1071 " res = pool.map(Bombe(wheel1_spec, wheel2_spec, wheel3_spec, \n",
1072 " reflector_spec, menu=menu, start_signal=start_signal, \n",
1073 " use_diagonal_board=use_diagonal_board, \n",
1074 " verify_plugboard=verify_plugboard),\n",
1076 " return [r[0] for r in res if r[1]]"
1080 "cell_type": "code",
1081 "execution_count": 39,
1090 "execution_count": 39,
1092 "output_type": "execute_result"
1096 "[r[0] for r in res if r[1]]"
1100 "cell_type": "code",
1101 "execution_count": 40,
1107 "# Setting sheet line 31 from http://www.codesandciphers.org.uk/enigma/enigma3.htm\n",
1108 "# Enigma simulation settings are \n",
1109 "# http://enigma.louisedade.co.uk/enigma.html?m3;b;b153;AFTX;AJFE;AU-BG-EY-FP-HL-IN-JZ-OS-QR-TX\n",
1110 "w_enigma = Enigma(reflector_b_spec, \n",
1111 " wheel_i_spec, wheel_i_pegs,\n",
1112 " wheel_v_spec, wheel_v_pegs,\n",
1113 " wheel_iii_spec, wheel_iii_pegs,\n",
1115 " 'ua pf rq so ni ey bg hl tx zj')"
1119 "cell_type": "code",
1120 "execution_count": 41,
1129 "execution_count": 41,
1131 "output_type": "execute_result"
1135 "w_enigma.set_wheels('j', 'e', 'b')\n",
1136 "tuple(unpos(p) for p in w_enigma.wheel_positions)"
1140 "cell_type": "code",
1141 "execution_count": 42,
1150 "execution_count": 42,
1152 "output_type": "execute_result"
1156 "w_enigma.set_wheels('j', 'e', 'b')\n",
1157 "pt = 'someplaintext'\n",
1158 "ct = w_enigma.encipher(pt)\n",
1163 "cell_type": "code",
1164 "execution_count": 43,
1173 "execution_count": 43,
1175 "output_type": "execute_result"
1179 "w_enigma.wheel_positions_l"
1183 "cell_type": "code",
1184 "execution_count": 44,
1190 "[MenuIem(before='s', after='d', number=1),\n",
1191 " MenuIem(before='o', after='h', number=2),\n",
1192 " MenuIem(before='m', after='n', number=3),\n",
1193 " MenuIem(before='e', after='p', number=4),\n",
1194 " MenuIem(before='p', after='f', number=5),\n",
1195 " MenuIem(before='l', after='o', number=6),\n",
1196 " MenuIem(before='a', after='r', number=7),\n",
1197 " MenuIem(before='i', after='e', number=8),\n",
1198 " MenuIem(before='n', after='e', number=9),\n",
1199 " MenuIem(before='t', after='i', number=10),\n",
1200 " MenuIem(before='e', after='m', number=11),\n",
1201 " MenuIem(before='x', after='g', number=12),\n",
1202 " MenuIem(before='t', after='g', number=13)]"
1205 "execution_count": 44,
1207 "output_type": "execute_result"
1211 "w_menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
1216 "cell_type": "code",
1217 "execution_count": 45,
1225 "[('a', 'y', 'm'),\n",
1226 " ('c', 'p', 'v'),\n",
1227 " ('c', 's', 'f'),\n",
1228 " ('c', 'w', 'j'),\n",
1229 " ('d', 'r', 'k'),\n",
1230 " ('e', 'l', 'f'),\n",
1231 " ('e', 's', 'v'),\n",
1232 " ('e', 'y', 'd'),\n",
1233 " ('e', 'y', 'o'),\n",
1234 " ('f', 'z', 'x'),\n",
1235 " ('g', 'b', 'l'),\n",
1236 " ('g', 'c', 'd'),\n",
1237 " ('g', 'c', 'f'),\n",
1238 " ('g', 'j', 'p'),\n",
1239 " ('h', 'c', 'i'),\n",
1240 " ('h', 'm', 'w'),\n",
1241 " ('h', 'o', 'd'),\n",
1242 " ('h', 'p', 'b'),\n",
1243 " ('h', 's', 't'),\n",
1244 " ('i', 'b', 's'),\n",
1245 " ('i', 'v', 'b'),\n",
1246 " ('j', 'y', 'u'),\n",
1247 " ('k', 'b', 'x'),\n",
1248 " ('k', 'f', 't'),\n",
1249 " ('k', 'l', 'e'),\n",
1250 " ('k', 'l', 'm'),\n",
1251 " ('k', 'r', 'z'),\n",
1252 " ('k', 's', 'p'),\n",
1253 " ('l', 'd', 'z'),\n",
1254 " ('l', 'i', 'y'),\n",
1255 " ('l', 'y', 'f'),\n",
1256 " ('m', 'b', 'h'),\n",
1257 " ('m', 'p', 'l'),\n",
1258 " ('n', 'l', 'r'),\n",
1259 " ('o', 'k', 'x'),\n",
1260 " ('p', 'a', 'g'),\n",
1261 " ('p', 'c', 'v'),\n",
1262 " ('p', 'f', 'o'),\n",
1263 " ('p', 'm', 'i'),\n",
1264 " ('p', 'x', 'n'),\n",
1265 " ('p', 'x', 'p'),\n",
1266 " ('q', 'q', 'n'),\n",
1267 " ('q', 'r', 'w'),\n",
1268 " ('q', 'v', 'l'),\n",
1269 " ('q', 'x', 't'),\n",
1270 " ('s', 'a', 'h'),\n",
1271 " ('s', 'h', 'v'),\n",
1272 " ('s', 'l', 'p'),\n",
1273 " ('s', 'l', 's'),\n",
1274 " ('u', 'r', 'h'),\n",
1275 " ('v', 'v', 'v'),\n",
1276 " ('v', 'x', 'a'),\n",
1277 " ('w', 'j', 'z'),\n",
1278 " ('w', 'k', 'u'),\n",
1279 " ('x', 'f', 'p'),\n",
1280 " ('x', 'j', 'n'),\n",
1281 " ('x', 'o', 'q'),\n",
1282 " ('x', 'x', 'x'),\n",
1283 " ('y', 'n', 'c'),\n",
1284 " ('y', 'r', 'f'),\n",
1285 " ('z', 't', 'y'),\n",
1289 "execution_count": 45,
1291 "output_type": "execute_result"
1295 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1297 "with multiprocessing.Pool() as pool:\n",
1298 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1299 " menu=w_menu, verify_plugboard=False),\n",
1301 "[r[0] for r in res if r[1]]"
1305 "cell_type": "code",
1306 "execution_count": 46,
1315 "execution_count": 46,
1317 "output_type": "execute_result"
1321 "len([r[0] for r in res if r[1]])"
1325 "cell_type": "code",
1326 "execution_count": 47,
1334 "[('c', 'p', 'v'),\n",
1335 " ('c', 's', 'f'),\n",
1336 " ('e', 'l', 'f'),\n",
1337 " ('g', 'c', 'f'),\n",
1338 " ('j', 'y', 'u'),\n",
1339 " ('o', 'k', 'x'),\n",
1340 " ('p', 'a', 'g'),\n",
1341 " ('q', 'q', 'n'),\n",
1342 " ('q', 'v', 'l'),\n",
1343 " ('q', 'x', 't'),\n",
1344 " ('s', 'l', 'p'),\n",
1345 " ('u', 'r', 'h'),\n",
1349 "execution_count": 47,
1351 "output_type": "execute_result"
1355 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1357 "with multiprocessing.Pool() as pool:\n",
1358 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1359 " menu=w_menu, verify_plugboard=True),\n",
1361 "[r[0] for r in res if r[1]]"
1365 "cell_type": "code",
1366 "execution_count": 48,
1372 "[('c', 'p', 'v'),\n",
1373 " ('c', 's', 'f'),\n",
1374 " ('e', 'l', 'f'),\n",
1375 " ('g', 'c', 'f'),\n",
1376 " ('j', 'y', 'u'),\n",
1377 " ('o', 'k', 'x'),\n",
1378 " ('p', 'a', 'g'),\n",
1379 " ('q', 'q', 'n'),\n",
1380 " ('q', 'v', 'l'),\n",
1381 " ('q', 'x', 't'),\n",
1382 " ('s', 'l', 'p'),\n",
1383 " ('u', 'r', 'h'),\n",
1387 "execution_count": 48,
1389 "output_type": "execute_result"
1393 "run_multi_bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, w_menu)"
1397 "cell_type": "code",
1398 "execution_count": 49,
1404 "# allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1406 "# with multiprocessing.Pool() as pool:\n",
1407 "# res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1408 "# menu=w_menu, start_signal=Signal('t', 'x')),\n",
1410 "# [r[0] for r in res if r[1]]"
1414 "cell_type": "code",
1415 "execution_count": 50,
1424 "execution_count": 50,
1426 "output_type": "execute_result"
1430 "len([r[0] for r in res if r[1]])"
1434 "cell_type": "code",
1435 "execution_count": 51,
1441 "w_bombe = Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1446 "cell_type": "code",
1447 "execution_count": 52,
1453 "Signal(bank='e', wire='e')"
1456 "execution_count": 52,
1458 "output_type": "execute_result"
1462 "w_bombe.test_start"
1466 "cell_type": "code",
1467 "execution_count": 53,
1476 "execution_count": 53,
1478 "output_type": "execute_result"
1482 "w_bombe.test(start_positions=('e', 'l', 'f'))"
1486 "cell_type": "code",
1487 "execution_count": 54,
1496 "execution_count": 54,
1498 "output_type": "execute_result"
1502 "w_bombe.test(Signal('t', 'x'), ('e', 'l', 'f'))"
1506 "cell_type": "code",
1507 "execution_count": 55,
1512 "output_type": "stream",
1515 "a : a.cdefghi..l.nopqrst.vwx..\n",
1516 "b : ....efghi...mnop.......x..\n",
1517 "c : a....fg.i..lmn.p.r.t...x..\n",
1518 "d : a...efghij.lmn...rstu..xyz\n",
1519 "e : ab.defghijklmnopqrstuvwxyz\n",
1520 "f : abcde.ghijklmnopqrstuvwxyz\n",
1521 "g : abcdef.hijklmnopqrstuvwxyz\n",
1522 "h : ab.defghi...mnopq.stuvwx..\n",
1523 "i : abcdefgh.jklmnopqrstuvwxyz\n",
1524 "j : ...defg.i...mn.p...t...x..\n",
1525 "k : ....efg.i..lmnop.r.t...x..\n",
1526 "l : a.cdefg.i.k.mnopqr.t..wxyz\n",
1527 "m : .bcdefghijklmnopqrstuvwxyz\n",
1528 "n : abcdefghijklmnopqr.tuvwxyz\n",
1529 "o : ab..efghi.klmnopqr.t...xyz\n",
1530 "p : abc.efghijklmnopqrstuvwxyz\n",
1531 "q : a...efghi..lmnop.r.t...x..\n",
1532 "r : a.cdefg.i.klmnopqrst..w..z\n",
1533 "s : a..defghi...m..p.rstuv.xyz\n",
1534 "t : a.cdefghijklmnopqrstuvwxyz\n",
1535 "u : ...defghi...mn.p..st...x..\n",
1536 "v : a...efghi...mn.p..st...x..\n",
1537 "w : a...efghi..lmn.p.r.t...x..\n",
1538 "x : abcdefghijklmnopq.stuvwxyz\n",
1539 "y : ...defg.i..lmnop..st...x..\n",
1540 "z : ...defg.i..lmnop.rst...x..\n"
1545 "r = w_bombe.test(start_positions=('c', 'p', 'v'))\n",
1547 "for b in sorted(w_bombe.banks):\n",
1548 " print(b, ': ', end='')\n",
1549 " for w in sorted(w_bombe.banks[b]):\n",
1550 " if w_bombe.banks[b][w]:\n",
1551 " print(w, end='')\n",
1553 " print('.', end='')\n",
1558 "cell_type": "code",
1559 "execution_count": 56,
1564 "output_type": "stream",
1567 "a : abcdefghi..lmnop.rst.v.x.z\n",
1568 "b : a...ef.hi..lmnop.r.t...x..\n",
1569 "c : a..defg.i..lmn.p.rst...x..\n",
1570 "d : a.c.efghi.klmnopqrstu.wxyz\n",
1571 "e : abcdefghijklmnopqrstuvwx.z\n",
1572 "f : abcdefghijklmno.qrstuvwxyz\n",
1573 "g : a.cdefghijklmnopqrstuvwxyz\n",
1574 "h : ab.defghi...mnopqrstuvwxyz\n",
1575 "i : abcdefghijklm.opqrstuvwxyz\n",
1576 "j : ....efg.i..lmnop...t...x..\n",
1577 "k : ...defg.i..lmn.p..st...x..\n",
1578 "l : abcdefg.ijklmnopqrstuv.x..\n",
1579 "m : abcdefghijkl.nopqrstuvwxyz\n",
1580 "n : abcdefgh.jklmnopqrstuvwxyz\n",
1581 "o : ab.defghij.lmnop.r.tuvwxyz\n",
1582 "p : abcde.ghijklmnopqrstuvwxyz\n",
1583 "q : ...defghi..lmn.p..st...x..\n",
1584 "r : abcdefghi..lmnop.rst.v.x.z\n",
1585 "s : a.cdefghi.klmn.pqr.tuvwxyz\n",
1586 "t : abcdefghijklmnopqrstuvw.yz\n",
1587 "u : ...defghi..lmnop..st...x..\n",
1588 "v : a...efghi..lmnop.rst...x..\n",
1589 "w : ...defghi...mnop..st...x..\n",
1590 "x : abcdefghijklmnopqrs.uvwxyz\n",
1591 "y : ...d.fghi...mnop..st...x..\n",
1592 "z : a..defghi...mnop.rst...x..\n"
1597 "r = w_bombe.test(start_positions=('e', 'l', 'f'))\n",
1599 "for b in sorted(w_bombe.banks):\n",
1600 " print(b, ': ', end='')\n",
1601 " for w in sorted(w_bombe.banks[b]):\n",
1602 " if w_bombe.banks[b][w]:\n",
1603 " print(w, end='')\n",
1605 " print('.', end='')\n",
1610 "cell_type": "code",
1611 "execution_count": 57,
1617 "{frozenset({'t', 'x'}),\n",
1618 " frozenset({'i', 'n'}),\n",
1619 " frozenset({'m'}),\n",
1620 " frozenset({'e', 'y'}),\n",
1621 " frozenset({'f', 'p'}),\n",
1622 " frozenset({'b', 'g'})}"
1625 "execution_count": 57,
1627 "output_type": "execute_result"
1631 "ps = w_bombe.possible_plugboards()\n",
1636 "cell_type": "code",
1637 "execution_count": 58,
1646 "execution_count": 58,
1648 "output_type": "execute_result"
1652 "all(s0.isdisjoint(s1) for s0 in ps for s1 in ps if s0 != s1)"
1656 "cell_type": "code",
1657 "execution_count": 59,
1663 "({frozenset({1, 2}), frozenset({2, 3}), frozenset({3, 4})},\n",
1664 " frozenset({1, 2}),\n",
1665 " frozenset({3, 4}),\n",
1666 " frozenset({2, 3}))"
1669 "execution_count": 59,
1671 "output_type": "execute_result"
1676 "f1 = frozenset((1, 2))\n",
1677 "f2 = frozenset((3, 4))\n",
1678 "f3 = frozenset((2, 3))\n",
1679 "s = s.union({f1})\n",
1680 "s = s.union({f2})\n",
1681 "s = s.union({f1})\n",
1682 "s = s.union({f3})\n",
1687 "cell_type": "code",
1688 "execution_count": 60,
1697 "execution_count": 60,
1699 "output_type": "execute_result"
1703 "all(s0.isdisjoint(s1) for s0 in s for s1 in s if s0 != s1)"
1707 "cell_type": "code",
1708 "execution_count": 61,
1717 "execution_count": 61,
1719 "output_type": "execute_result"
1723 "{1, 2}.isdisjoint({1, 6})"
1727 "cell_type": "markdown",
1736 "cell_type": "code",
1737 "execution_count": 62,
1743 "'slgncszxltkzebghstgywdmpr'"
1746 "execution_count": 62,
1748 "output_type": "execute_result"
1752 "target_ct = ''.join(c.lower() for c in 'SLGNC SZXLT KZEBG HSTGY WDMPR' if c in string.ascii_letters)\n",
1757 "cell_type": "code",
1758 "execution_count": 63,
1764 "'theyweredetectedbybritishshipsinclud'"
1767 "execution_count": 63,
1769 "output_type": "execute_result"
1773 "target_pt = ''.join(c.lower() for c in 'Theyw erede tecte d byBri tishs hipsi nclud' if c in string.ascii_letters)\n",
1778 "cell_type": "code",
1779 "execution_count": 64,
1785 "[MenuIem(before='w', after='c', number=1),\n",
1786 " MenuIem(before='e', after='s', number=2),\n",
1787 " MenuIem(before='r', after='z', number=3),\n",
1788 " MenuIem(before='e', after='x', number=4),\n",
1789 " MenuIem(before='d', after='l', number=5),\n",
1790 " MenuIem(before='e', after='t', number=6),\n",
1791 " MenuIem(before='t', after='k', number=7),\n",
1792 " MenuIem(before='e', after='z', number=8),\n",
1793 " MenuIem(before='c', after='e', number=9),\n",
1794 " MenuIem(before='t', after='b', number=10),\n",
1795 " MenuIem(before='e', after='g', number=11),\n",
1796 " MenuIem(before='d', after='h', number=12),\n",
1797 " MenuIem(before='b', after='s', number=13),\n",
1798 " MenuIem(before='y', after='t', number=14),\n",
1799 " MenuIem(before='b', after='g', number=15),\n",
1800 " MenuIem(before='r', after='y', number=16),\n",
1801 " MenuIem(before='i', after='w', number=17),\n",
1802 " MenuIem(before='t', after='d', number=18),\n",
1803 " MenuIem(before='i', after='m', number=19),\n",
1804 " MenuIem(before='s', after='p', number=20)]"
1807 "execution_count": 64,
1809 "output_type": "execute_result"
1813 "tbt_menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(target_pt[4:24], target_ct[4:24]))]\n",
1818 "cell_type": "code",
1819 "execution_count": 65,
1825 "tbt_bombe = Bombe(wheel_iii_spec, wheel_i_spec, wheel_ii_spec, reflector_b_spec, \n",
1830 "cell_type": "code",
1831 "execution_count": 66,
1840 "execution_count": 66,
1842 "output_type": "execute_result"
1846 "tbt_wheel_posns = run_multi_bombe(wheel_iii_spec, wheel_i_spec, wheel_ii_spec, reflector_b_spec, tbt_menu)\n",
1851 "cell_type": "code",
1852 "execution_count": 67,
1858 "Signal(bank='e', wire='e')"
1861 "execution_count": 67,
1863 "output_type": "execute_result"
1867 "tbt_bombe.test_start"
1871 "cell_type": "code",
1872 "execution_count": 68,
1877 "output_type": "stream",
1880 "a : abcdefghi..lmnop.rst.v.x.z\n",
1881 "b : a...ef.hi..lmnop.r.t...x..\n",
1882 "c : a..defg.i..lmn.p.rst...x..\n",
1883 "d : a.c.efghi.klmnopqrstu.wxyz\n",
1884 "e : abcdefghijklmnopqrstuvwx.z\n",
1885 "f : abcdefghijklmno.qrstuvwxyz\n",
1886 "g : a.cdefghijklmnopqrstuvwxyz\n",
1887 "h : ab.defghi...mnopqrstuvwxyz\n",
1888 "i : abcdefghijklm.opqrstuvwxyz\n",
1889 "j : ....efg.i..lmnop...t...x..\n",
1890 "k : ...defg.i..lmn.p..st...x..\n",
1891 "l : abcdefg.ijklmnopqrstuv.x..\n",
1892 "m : abcdefghijkl.nopqrstuvwxyz\n",
1893 "n : abcdefgh.jklmnopqrstuvwxyz\n",
1894 "o : ab.defghij.lmnop.r.tuvwxyz\n",
1895 "p : abcde.ghijklmnopqrstuvwxyz\n",
1896 "q : ...defghi..lmn.p..st...x..\n",
1897 "r : abcdefghi..lmnop.rst.v.x.z\n",
1898 "s : a.cdefghi.klmn.pqr.tuvwxyz\n",
1899 "t : abcdefghijklmnopqrstuvw.yz\n",
1900 "u : ...defghi..lmnop..st...x..\n",
1901 "v : a...efghi..lmnop.rst...x..\n",
1902 "w : ...defghi...mnop..st...x..\n",
1903 "x : abcdefghijklmnopqrs.uvwxyz\n",
1904 "y : ...d.fghi...mnop..st...x..\n",
1905 "z : a..defghi...mnop.rst...x..\n"
1910 "r = tbt_bombe.test(start_positions=('l', 's', 'd'))\n",
1912 "for b in sorted(w_bombe.banks):\n",
1913 " print(b, ': ', end='')\n",
1914 " for w in sorted(w_bombe.banks[b]):\n",
1915 " if w_bombe.banks[b][w]:\n",
1916 " print(w, end='')\n",
1918 " print('.', end='')\n",
1923 "cell_type": "code",
1924 "execution_count": 69,
1933 "execution_count": 69,
1935 "output_type": "execute_result"
1939 "ps = tbt_bombe.possible_plugboards()\n",
1944 "cell_type": "code",
1945 "execution_count": null,
1955 "display_name": "Python 3",
1956 "language": "python",
1960 "codemirror_mode": {
1964 "file_extension": ".py",
1965 "mimetype": "text/x-python",
1967 "nbconvert_exporter": "python",
1968 "pygments_lexer": "ipython3",