12 "import collections\n",
13 "import multiprocessing\n",
15 "from enigma import *"
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'"
58 "Signal = collections.namedtuple('Signal', ['bank', 'wire'])\n",
59 "Connection = collections.namedtuple('Connection', ['banks', 'scrambler'])\n",
60 "MenuItem = collections.namedtuple('MenuIem', ['before', 'after', 'number'])"
71 "class Scrambler(object):\n",
72 " def __init__(self, wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec,\n",
73 " wheel1_pos='a', wheel2_pos='a', wheel3_pos='a'):\n",
74 " self.wheel1 = SimpleWheel(wheel1_spec, position=wheel1_pos)\n",
75 " self.wheel2 = SimpleWheel(wheel2_spec, position=wheel2_pos)\n",
76 " self.wheel3 = SimpleWheel(wheel3_spec, position=wheel3_pos)\n",
77 " self.reflector = Reflector(reflector_spec)\n",
79 " def __getattribute__(self, name):\n",
80 " if name=='wheel_positions':\n",
81 " return self.wheel1.position, self.wheel2.position, self.wheel3.position \n",
82 " elif name=='wheel_positions_l':\n",
83 " return self.wheel1.position_l, self.wheel2.position_l, self.wheel3.position_l \n",
85 " return object.__getattribute__(self, name)\n",
87 " def advance(self, wheel1=False, wheel2=False, wheel3=True):\n",
88 " if wheel1: self.wheel1.advance()\n",
89 " if wheel2: self.wheel2.advance()\n",
90 " if wheel3: self.wheel3.advance()\n",
92 " def lookup(self, letter):\n",
93 " a = self.wheel3.forward(letter)\n",
94 " b = self.wheel2.forward(a)\n",
95 " c = self.wheel1.forward(b)\n",
96 " d = self.reflector.forward(c)\n",
97 " e = self.wheel1.backward(d)\n",
98 " f = self.wheel2.backward(e)\n",
99 " g = self.wheel3.backward(f)\n",
102 " def set_positions(self, wheel1_pos, wheel2_pos, wheel3_pos):\n",
103 " self.wheel1.set_position(wheel1_pos)\n",
104 " self.wheel2.set_position(wheel2_pos)\n",
105 " self.wheel3.set_position(wheel3_pos) "
110 "execution_count": 6,
116 "class Bombe(object):\n",
118 " def __init__(self, wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec,\n",
119 " menu=None, start_signal=None, use_diagonal_board=True, \n",
120 " verify_plugboard=True):\n",
121 " self.connections = []\n",
122 " self.wheel1_spec = wheel1_spec\n",
123 " self.wheel2_spec = wheel2_spec\n",
124 " self.wheel3_spec = wheel3_spec\n",
125 " self.reflector_spec = reflector_spec\n",
127 " self.read_menu(menu)\n",
128 " if start_signal:\n",
129 " self.test_start = start_signal\n",
130 " self.use_diagonal_board = use_diagonal_board\n",
131 " self.verify_plugboard = verify_plugboard\n",
133 " def __getattribute__(self, name):\n",
134 " if name=='wheel_positions':\n",
135 " return self.connections[0].scrambler.wheel_positions\n",
136 " elif name=='wheel_positions_l':\n",
137 " return self.connections[0].scrambler.wheel_positions_l\n",
139 " return object.__getattribute__(self, name)\n",
141 " def __call__(self, start_positions):\n",
142 " return start_positions, self.test(initial_signal=self.test_start,\n",
143 " start_positions=start_positions, \n",
144 " use_diagonal_board=self.use_diagonal_board,\n",
145 " verify_plugboard=self.verify_plugboard)\n",
147 " def add_connection(self, bank_before, bank_after, scrambler):\n",
148 " self.connections += [Connection([bank_before, bank_after], scrambler)]\n",
150 " def read_menu(self, menu):\n",
151 " for item in menu:\n",
152 " scrambler = Scrambler(self.wheel1_spec, self.wheel2_spec, self.wheel3_spec,\n",
153 " self.reflector_spec,\n",
154 " wheel3_pos=unpos(item.number - 1))\n",
155 " self.add_connection(item.before, item.after, scrambler)\n",
156 " most_common_letter = (collections.Counter(m.before for m in menu) + \\\n",
157 " collections.Counter(m.after for m in menu)).most_common(1)[0][0]\n",
158 " self.test_start = Signal(most_common_letter, most_common_letter)\n",
160 " def set_positions(self, wheel1_pos, wheel2_pos, wheel3_pos):\n",
161 " for i, c in enumerate(self.connections):\n",
162 " c.scrambler.set_positions(wheel1_pos, wheel2_pos, unpos(pos(wheel3_pos) + i))\n",
164 " def test(self, initial_signal=None, start_positions=None, use_diagonal_board=True,\n",
165 " verify_plugboard=True):\n",
166 " self.banks = {label: \n",
167 " dict(zip(string.ascii_lowercase, [False]*len(string.ascii_lowercase)))\n",
168 " for label in string.ascii_lowercase}\n",
169 " if start_positions:\n",
170 " self.set_positions(*start_positions)\n",
171 " if not initial_signal:\n",
172 " initial_signal = self.test_start\n",
173 " self.pending = [initial_signal]\n",
174 " self.propagate(use_diagonal_board)\n",
175 " live_wire_count = len([self.banks[self.test_start.bank][w] \n",
176 " for w in self.banks[self.test_start.bank] \n",
177 " if self.banks[self.test_start.bank][w]])\n",
178 " if live_wire_count < 26:\n",
179 " if verify_plugboard:\n",
180 " possibles = self.possible_plugboards()\n",
181 " return all(s0.isdisjoint(s1) for s0 in possibles for s1 in possibles if s0 != s1)\n",
187 " def propagate(self, use_diagonal_board):\n",
188 " while self.pending:\n",
189 " current = self.pending[0]\n",
190 " # print(\"processing\", current)\n",
191 " self.pending = self.pending[1:]\n",
192 " if not self.banks[current.bank][current.wire]:\n",
193 " self.banks[current.bank][current.wire] = True\n",
194 " if use_diagonal_board:\n",
195 " self.pending += [Signal(current.wire, current.bank)]\n",
196 " for c in self.connections:\n",
197 " if current.bank in c.banks:\n",
198 " other_bank = [b for b in c.banks if b != current.bank][0]\n",
199 " other_wire = c.scrambler.lookup(current.wire)\n",
200 " # print(\" adding\", other_bank, other_wire, \"because\", c.banks)\n",
201 " self.pending += [Signal(other_bank, other_wire)]\n",
203 " def run(self, run_start=None, wheel1_pos='a', wheel2_pos='a', wheel3_pos='a', use_diagonal_board=True):\n",
204 " if not run_start:\n",
205 " run_start = self.test_start\n",
206 " self.solutions = []\n",
207 " self.set_positions(wheel1_pos, wheel2_pos, wheel3_pos)\n",
208 " for run_index in range(26*26*26):\n",
209 " if self.test(initial_signal=run_start, use_diagonal_board=use_diagonal_board):\n",
210 " self.solutions += [self.connections[0].scrambler.wheel_positions_l]\n",
211 " advance3 = True\n",
212 " advance2 = False\n",
213 " advance1 = False\n",
214 " if (run_index + 1) % 26 == 0: advance2 = True\n",
215 " if (run_index + 1) % (26*26) == 0: advance1 = True\n",
216 " for c in self.connections:\n",
217 " c.scrambler.advance(advance1, advance2, advance3)\n",
218 " return self.solutions\n",
220 " def possible_plugboards(self):\n",
221 " possibles = set()\n",
222 " for b in self.banks:\n",
223 " active = [w for w in self.banks[b] if self.banks[b][w]]\n",
224 " inactive = [w for w in self.banks[b] if not self.banks[b][w]]\n",
225 " if len(active) == 1:\n",
226 " possibles = possibles.union({frozenset((b, active[0]))})\n",
227 " if len(inactive) == 1:\n",
228 " possibles = possibles.union({frozenset((b, inactive[0]))})\n",
229 " return possibles\n"
234 "execution_count": 804,
240 "bombe = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec)\n",
241 "# len(bombe.banks), bombe.banks['a'] == bombe.banks['b']"
246 "execution_count": 805,
252 "test_enigma = Enigma(reflector_b_spec, \n",
253 " wheel_i_spec, wheel_i_pegs,\n",
254 " wheel_ii_spec, wheel_ii_pegs,\n",
255 " wheel_iii_spec, wheel_iii_pegs,\n",
262 "execution_count": 806,
270 "'opgndxcrwomnlnecjz'"
273 "execution_count": 806,
275 "output_type": "execute_result"
279 "test_enigma.set_wheels('a', 'a', 'a')\n",
280 "pt = 'thisisatestmessage'\n",
281 "ct = test_enigma.encipher(pt)\n",
287 "execution_count": 807,
298 "execution_count": 807,
300 "output_type": "execute_result"
304 "cat(test_enigma.wheel_positions_l)"
309 "execution_count": 808,
317 "[MenuIem(before='t', after='o', number=1),\n",
318 " MenuIem(before='h', after='p', number=2),\n",
319 " MenuIem(before='i', after='g', number=3),\n",
320 " MenuIem(before='s', after='n', number=4),\n",
321 " MenuIem(before='i', after='d', number=5),\n",
322 " MenuIem(before='s', after='x', number=6),\n",
323 " MenuIem(before='a', after='c', number=7),\n",
324 " MenuIem(before='t', after='r', number=8),\n",
325 " MenuIem(before='e', after='w', number=9),\n",
326 " MenuIem(before='s', after='o', number=10),\n",
327 " MenuIem(before='t', after='m', number=11),\n",
328 " MenuIem(before='m', after='n', number=12),\n",
329 " MenuIem(before='e', after='l', number=13),\n",
330 " MenuIem(before='s', after='n', number=14),\n",
331 " MenuIem(before='s', after='e', number=15),\n",
332 " MenuIem(before='a', after='c', number=16),\n",
333 " MenuIem(before='g', after='j', number=17),\n",
334 " MenuIem(before='e', after='z', number=18)]"
337 "execution_count": 808,
339 "output_type": "execute_result"
343 "menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
349 "execution_count": 809,
355 "def make_menu(plaintext, ciphertext):\n",
356 " return [MenuItem(p, c, i+1) \n",
357 " for i, (p, c) in enumerate(zip(plaintext, ciphertext))]"
362 "execution_count": 810,
370 "[MenuIem(before='t', after='o', number=1),\n",
371 " MenuIem(before='h', after='p', number=2),\n",
372 " MenuIem(before='i', after='g', number=3),\n",
373 " MenuIem(before='s', after='n', number=4),\n",
374 " MenuIem(before='i', after='d', number=5),\n",
375 " MenuIem(before='s', after='x', number=6),\n",
376 " MenuIem(before='a', after='c', number=7),\n",
377 " MenuIem(before='t', after='r', number=8),\n",
378 " MenuIem(before='e', after='w', number=9),\n",
379 " MenuIem(before='s', after='o', number=10),\n",
380 " MenuIem(before='t', after='m', number=11),\n",
381 " MenuIem(before='m', after='n', number=12),\n",
382 " MenuIem(before='e', after='l', number=13),\n",
383 " MenuIem(before='s', after='n', number=14),\n",
384 " MenuIem(before='s', after='e', number=15),\n",
385 " MenuIem(before='a', after='c', number=16),\n",
386 " MenuIem(before='g', after='j', number=17),\n",
387 " MenuIem(before='e', after='z', number=18)]"
390 "execution_count": 810,
392 "output_type": "execute_result"
401 "execution_count": 811,
412 "execution_count": 811,
414 "output_type": "execute_result"
418 "(collections.Counter(m.before for m in menu) + collections.Counter(m.after for m in menu)).most_common(1)[0][0]"
423 "execution_count": 812,
429 "bombe.read_menu(menu)"
434 "execution_count": 813,
445 "execution_count": 813,
447 "output_type": "execute_result"
451 "len(bombe.connections)"
456 "execution_count": 814,
463 "output_type": "stream",
487 "for c in bombe.connections:\n",
488 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
493 "execution_count": 815,
504 "execution_count": 815,
506 "output_type": "execute_result"
510 "bombe.test(Signal('t', 't'))"
515 "execution_count": 816,
551 "execution_count": 816,
553 "output_type": "execute_result"
562 "execution_count": 817,
569 "output_type": "stream",
571 "a : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
572 "b : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
573 "c : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
574 "d : ABCDEFGHIJKLMNOPQRSTUVWXyZ\n",
575 "e : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
576 "f : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
577 "g : ABCDEFGHIJKLMNOPQRSTuVWXYZ\n",
578 "h : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
579 "i : ABCDEFGHIJKLMNOPQRSTUvWXYZ\n",
580 "j : ABCDEFGHIjKLMNOPQRSTUVWXYZ\n",
581 "k : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
582 "l : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
583 "m : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
584 "n : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
585 "o : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
586 "p : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
587 "q : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
588 "r : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
589 "s : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
590 "t : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
591 "u : AbCDEfgHIJkLMNOPqRSTuvWXyZ\n",
592 "v : AbCDEfGHiJkLMNOPqRSTuvWXyZ\n",
593 "w : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
594 "x : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
595 "y : AbCdEfGHIJkLMNOPqRSTuvWXyZ\n",
596 "z : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
601 "for b in sorted(bombe.banks):\n",
602 " print(b, ': ', end='')\n",
603 " for w in sorted(bombe.banks[b]):\n",
604 " if bombe.banks[b][w]:\n",
605 " print(w.upper(), end='')\n",
607 " print(w, end='')\n",
613 "execution_count": 818,
624 "execution_count": 818,
626 "output_type": "execute_result"
630 "bombe.wheel_positions_l"
635 "execution_count": 819,
642 "output_type": "stream",
666 "bombe.set_positions('p', 'q', 'r')\n",
667 "for c in bombe.connections:\n",
668 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
673 "execution_count": 820,
685 "execution_count": 821,
692 "output_type": "stream",
694 "a : .b.defghijklmnopqrstuvwxyz\n",
695 "b : a.cde.ghij.lmnop.rst..wx.z\n",
696 "c : .b.defghijklmnopqrstuvwxyz\n",
697 "d : abc.e.gh.jklmnopqrstuvwxyz\n",
698 "e : abcd.fghijklmnopqrstuvwxyz\n",
699 "f : a.c.e.g.ij.lmno..rst..wx.z\n",
700 "g : abcdef.h..klmnopqrstuvwxyz\n",
701 "h : abcde.g.ij.lmno.qrst..wxyz\n",
702 "i : abc.ef.h.jklmnopqrstuvwxyz\n",
703 "j : abcdef.hi.klmnopqrstuvwx.z\n",
704 "k : a.cde.g.ij.lmno..rst..wx.z\n",
705 "l : abcdefghijk.mnopqrstuvwxyz\n",
706 "m : abcdefghijkl.nopqrstuvwxyz\n",
707 "n : abcdefghijklm.opqrstuvwxyz\n",
708 "o : abcdefghijklmn.pqrstuvwxyz\n",
709 "p : abcde.g.ij.lmno.qrst..wxyz\n",
710 "q : a.cde.ghij.lmnop.rst..wx.z\n",
711 "r : abcdefghijklmnopq.stuvwxyz\n",
712 "s : abcdefghijklmnopqr.tuvwxyz\n",
713 "t : abcdefghijklmnopqrs.uvwxyz\n",
714 "u : a.cde.g.ij.lmno..rst..wx.z\n",
715 "v : a.cde.g.ij.lmno..rst..wx.z\n",
716 "w : abcdefghijklmnopqrstuv.xyz\n",
717 "x : abcdefghijklmnopqrstuvw.yz\n",
718 "y : a.cde.ghi..lmnop.rst..wx.z\n",
719 "z : abcdefghijklmnopqrstuvwxy.\n"
724 "bombe.set_positions('a', 'a', 'b')\n",
725 "bombe.test(Signal('s', 'a'))\n",
727 "for b in sorted(bombe.banks):\n",
728 " print(b, ': ', end='')\n",
729 " for w in sorted(bombe.banks[b]):\n",
730 " if bombe.banks[b][w]:\n",
731 " print(w, end='')\n",
733 " print('.', end='')\n",
739 "execution_count": 822,
746 "output_type": "stream",
748 "a : ..........................\n",
749 "b : ..........................\n",
750 "c : ..........................\n",
751 "d : ..........................\n",
752 "e : ....e.....................\n",
753 "f : ..........................\n",
754 "g : ..........................\n",
755 "h : ..........................\n",
756 "i : ..........................\n",
757 "j : ..........................\n",
758 "k : ..........................\n",
759 "l : ...........l..............\n",
760 "m : ............m.............\n",
761 "n : .............n............\n",
762 "o : ..............o...........\n",
763 "p : ..........................\n",
764 "q : ..........................\n",
765 "r : .................r........\n",
766 "s : ..................s.......\n",
767 "t : ...................t......\n",
768 "u : ..........................\n",
769 "v : ..........................\n",
770 "w : ......................w...\n",
771 "x : .......................x..\n",
772 "y : ..........................\n",
773 "z : .........................z\n"
778 "bombe.set_positions('a', 'a', 'b')\n",
781 "for b in sorted(bombe.banks):\n",
782 " print(b, ': ', end='')\n",
783 " for w in sorted(bombe.banks[b]):\n",
784 " if bombe.banks[b][w]:\n",
785 " print(w, end='')\n",
787 " print('.', end='')\n",
793 "execution_count": 823,
804 "execution_count": 823,
806 "output_type": "execute_result"
810 "len([bombe.banks['t'][w] for w in bombe.banks['t'] if bombe.banks['t'][w]])"
815 "execution_count": 824,
822 "# results = bombe.run()\n",
823 "# print(len(results), ('a', 'a', 'b') in results)"
828 "execution_count": 825,
835 "# results = bombe.run(use_diagonal_board=False)\n",
836 "# print(len(results), ('a', 'a', 'b') in results)"
841 "execution_count": 826,
852 "execution_count": 826,
854 "output_type": "execute_result"
858 "bombe.wheel_positions_l"
863 "execution_count": 827,
874 "execution_count": 827,
876 "output_type": "execute_result"
880 "bombe.test(Signal('t', 't'), ('p', 'p', 'p'))"
885 "execution_count": 828,
896 "execution_count": 828,
898 "output_type": "execute_result"
902 "bombe.wheel_positions_l"
907 "execution_count": 829,
914 "output_type": "stream",
916 "a : abcdefghijklmnop.rst.vwxyz\n",
917 "b : a.cde.g.ij.lmno..rst..wx.z\n",
918 "c : abcdefghijklmnop.rst.vwxyz\n",
919 "d : abcdefghijklmnopqrstuvwxyz\n",
920 "e : abcdefghijklmnopqrstuvwxyz\n",
921 "f : a.cde.g.ij.lmno..rst..wx.z\n",
922 "g : abcdefghijklmnopqrstuvwxyz\n",
923 "h : a.cde.ghijklmnop.rst.vwxyz\n",
924 "i : abcdefghijklmnopqrstuvwxyz\n",
925 "j : abcdefghijklmnopqrstuvwxyz\n",
926 "k : a.cde.ghij.lmnop.rst..wx.z\n",
927 "l : abcdefghijklmnopqrstuvwxyz\n",
928 "m : abcdefghijklmnopqrstuvwxyz\n",
929 "n : abcdefghijklmnopqrstuvwxyz\n",
930 "o : abcdefghijklmnopqrstuvwxyz\n",
931 "p : a.cde.ghijklmnop.rst.vwxyz\n",
932 "q : ...de.g.ij.lmno..rst..wx.z\n",
933 "r : abcdefghijklmnopqrstuvwxyz\n",
934 "s : abcdefghijklmnopqrstuvwxyz\n",
935 "t : abcdefghijklmnopqrstuvwxyz\n",
936 "u : ...de.g.ij.lmno..rst..wx.z\n",
937 "v : a.cde.ghij.lmnop.rst..wx.z\n",
938 "w : abcdefghijklmnopqrstuvwxyz\n",
939 "x : abcdefghijklmnopqrstuvwxyz\n",
940 "y : a.cde.ghij.lmnop.rst..wx.z\n",
941 "z : abcdefghijklmnopqrstuvwxyz\n"
946 "for b in sorted(bombe.banks):\n",
947 " print(b, ': ', end='')\n",
948 " for w in sorted(bombe.banks[b]):\n",
949 " if bombe.banks[b][w]:\n",
950 " print(w, end='')\n",
952 " print('.', end='')\n",
958 "execution_count": 830,
969 "execution_count": 830,
971 "output_type": "execute_result"
975 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
976 "len(list(allwheels))"
981 "execution_count": 831,
989 "(('a', 'a', 'b'), True)"
992 "execution_count": 831,
994 "output_type": "execute_result"
998 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)\n",
1003 "cell_type": "code",
1004 "execution_count": 832,
1010 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)(('a', 'a', 'b'))"
1014 "cell_type": "code",
1015 "execution_count": 833,
1026 "execution_count": 833,
1028 "output_type": "execute_result"
1032 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1034 "with multiprocessing.Pool() as pool:\n",
1035 " res = pool.map(Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu),\n",
1037 "[r[0] for r in res if r[1]]"
1041 "cell_type": "code",
1042 "execution_count": 857,
1048 "def run_multi_bombe(wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec, menu,\n",
1049 " start_signal=None, use_diagonal_board=True, \n",
1050 " verify_plugboard=True):\n",
1051 " allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1053 " with multiprocessing.Pool() as pool:\n",
1054 " res = pool.map(Bombe(wheel1_spec, wheel2_spec, wheel3_spec, \n",
1055 " reflector_spec, menu=menu, start_signal=start_signal, \n",
1056 " use_diagonal_board=use_diagonal_board, \n",
1057 " verify_plugboard=verify_plugboard),\n",
1059 " return [r[0] for r in res if r[1]]"
1063 "cell_type": "code",
1064 "execution_count": 835,
1075 "execution_count": 835,
1077 "output_type": "execute_result"
1081 "[r[0] for r in res if r[1]]"
1085 "cell_type": "code",
1086 "execution_count": 836,
1092 "# Setting sheet line 31 from http://www.codesandciphers.org.uk/enigma/enigma3.htm\n",
1093 "# Enigma simulation settings are \n",
1094 "# http://enigma.louisedade.co.uk/enigma.html?m3;b;b153;AFTX;AJFE;AU-BG-EY-FP-HL-IN-JZ-OS-QR-TX\n",
1095 "w_enigma = Enigma(reflector_b_spec, \n",
1096 " wheel_i_spec, wheel_i_pegs,\n",
1097 " wheel_v_spec, wheel_v_pegs,\n",
1098 " wheel_iii_spec, wheel_iii_pegs,\n",
1100 " 'ua pf rq so ni ey bg hl tx zj')"
1104 "cell_type": "code",
1105 "execution_count": 837,
1116 "execution_count": 837,
1118 "output_type": "execute_result"
1122 "w_enigma.set_wheels('j', 'e', 'b')\n",
1123 "tuple(unpos(p) for p in w_enigma.wheel_positions)"
1127 "cell_type": "code",
1128 "execution_count": 838,
1139 "execution_count": 838,
1141 "output_type": "execute_result"
1145 "w_enigma.set_wheels('j', 'e', 'b')\n",
1146 "pt = 'someplaintext'\n",
1147 "ct = w_enigma.encipher(pt)\n",
1152 "cell_type": "code",
1153 "execution_count": 839,
1164 "execution_count": 839,
1166 "output_type": "execute_result"
1170 "w_enigma.wheel_positions_l"
1174 "cell_type": "code",
1175 "execution_count": 840,
1183 "[MenuIem(before='s', after='d', number=1),\n",
1184 " MenuIem(before='o', after='h', number=2),\n",
1185 " MenuIem(before='m', after='n', number=3),\n",
1186 " MenuIem(before='e', after='p', number=4),\n",
1187 " MenuIem(before='p', after='f', number=5),\n",
1188 " MenuIem(before='l', after='o', number=6),\n",
1189 " MenuIem(before='a', after='r', number=7),\n",
1190 " MenuIem(before='i', after='e', number=8),\n",
1191 " MenuIem(before='n', after='e', number=9),\n",
1192 " MenuIem(before='t', after='i', number=10),\n",
1193 " MenuIem(before='e', after='m', number=11),\n",
1194 " MenuIem(before='x', after='g', number=12),\n",
1195 " MenuIem(before='t', after='g', number=13)]"
1198 "execution_count": 840,
1200 "output_type": "execute_result"
1204 "w_menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
1209 "cell_type": "code",
1210 "execution_count": 841,
1219 "[('a', 'y', 'm'),\n",
1220 " ('c', 'p', 'v'),\n",
1221 " ('c', 's', 'f'),\n",
1222 " ('c', 'w', 'j'),\n",
1223 " ('d', 'r', 'k'),\n",
1224 " ('e', 'l', 'f'),\n",
1225 " ('e', 's', 'v'),\n",
1226 " ('e', 'y', 'd'),\n",
1227 " ('e', 'y', 'o'),\n",
1228 " ('f', 'z', 'x'),\n",
1229 " ('g', 'b', 'l'),\n",
1230 " ('g', 'c', 'd'),\n",
1231 " ('g', 'c', 'f'),\n",
1232 " ('g', 'j', 'p'),\n",
1233 " ('h', 'c', 'i'),\n",
1234 " ('h', 'm', 'w'),\n",
1235 " ('h', 'o', 'd'),\n",
1236 " ('h', 'p', 'b'),\n",
1237 " ('h', 's', 't'),\n",
1238 " ('i', 'b', 's'),\n",
1239 " ('i', 'v', 'b'),\n",
1240 " ('j', 'y', 'u'),\n",
1241 " ('k', 'b', 'x'),\n",
1242 " ('k', 'f', 't'),\n",
1243 " ('k', 'l', 'e'),\n",
1244 " ('k', 'l', 'm'),\n",
1245 " ('k', 'r', 'z'),\n",
1246 " ('k', 's', 'p'),\n",
1247 " ('l', 'd', 'z'),\n",
1248 " ('l', 'i', 'y'),\n",
1249 " ('l', 'y', 'f'),\n",
1250 " ('m', 'b', 'h'),\n",
1251 " ('m', 'p', 'l'),\n",
1252 " ('n', 'l', 'r'),\n",
1253 " ('o', 'k', 'x'),\n",
1254 " ('p', 'a', 'g'),\n",
1255 " ('p', 'c', 'v'),\n",
1256 " ('p', 'f', 'o'),\n",
1257 " ('p', 'm', 'i'),\n",
1258 " ('p', 'x', 'n'),\n",
1259 " ('p', 'x', 'p'),\n",
1260 " ('q', 'q', 'n'),\n",
1261 " ('q', 'r', 'w'),\n",
1262 " ('q', 'v', 'l'),\n",
1263 " ('q', 'x', 't'),\n",
1264 " ('s', 'a', 'h'),\n",
1265 " ('s', 'h', 'v'),\n",
1266 " ('s', 'l', 'p'),\n",
1267 " ('s', 'l', 's'),\n",
1268 " ('u', 'r', 'h'),\n",
1269 " ('v', 'v', 'v'),\n",
1270 " ('v', 'x', 'a'),\n",
1271 " ('w', 'j', 'z'),\n",
1272 " ('w', 'k', 'u'),\n",
1273 " ('x', 'f', 'p'),\n",
1274 " ('x', 'j', 'n'),\n",
1275 " ('x', 'o', 'q'),\n",
1276 " ('x', 'x', 'x'),\n",
1277 " ('y', 'n', 'c'),\n",
1278 " ('y', 'r', 'f'),\n",
1279 " ('z', 't', 'y'),\n",
1283 "execution_count": 841,
1285 "output_type": "execute_result"
1289 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1291 "with multiprocessing.Pool() as pool:\n",
1292 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1293 " menu=w_menu, verify_plugboard=False),\n",
1295 "[r[0] for r in res if r[1]]"
1299 "cell_type": "code",
1300 "execution_count": 842,
1311 "execution_count": 842,
1313 "output_type": "execute_result"
1317 "len([r[0] for r in res if r[1]])"
1321 "cell_type": "code",
1322 "execution_count": 843,
1331 "[('c', 'p', 'v'),\n",
1332 " ('c', 's', 'f'),\n",
1333 " ('e', 'l', 'f'),\n",
1334 " ('g', 'c', 'f'),\n",
1335 " ('j', 'y', 'u'),\n",
1336 " ('o', 'k', 'x'),\n",
1337 " ('p', 'a', 'g'),\n",
1338 " ('q', 'q', 'n'),\n",
1339 " ('q', 'v', 'l'),\n",
1340 " ('q', 'x', 't'),\n",
1341 " ('s', 'l', 'p'),\n",
1342 " ('u', 'r', 'h'),\n",
1346 "execution_count": 843,
1348 "output_type": "execute_result"
1352 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1354 "with multiprocessing.Pool() as pool:\n",
1355 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1356 " menu=w_menu, verify_plugboard=True),\n",
1358 "[r[0] for r in res if r[1]]"
1362 "cell_type": "code",
1363 "execution_count": 858,
1371 "[('c', 'p', 'v'),\n",
1372 " ('c', 's', 'f'),\n",
1373 " ('e', 'l', 'f'),\n",
1374 " ('g', 'c', 'f'),\n",
1375 " ('j', 'y', 'u'),\n",
1376 " ('o', 'k', 'x'),\n",
1377 " ('p', 'a', 'g'),\n",
1378 " ('q', 'q', 'n'),\n",
1379 " ('q', 'v', 'l'),\n",
1380 " ('q', 'x', 't'),\n",
1381 " ('s', 'l', 'p'),\n",
1382 " ('u', 'r', 'h'),\n",
1386 "execution_count": 858,
1388 "output_type": "execute_result"
1392 "run_multi_bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, w_menu)"
1396 "cell_type": "code",
1397 "execution_count": 844,
1403 "# allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1405 "# with multiprocessing.Pool() as pool:\n",
1406 "# res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1407 "# menu=w_menu, start_signal=Signal('t', 'x')),\n",
1409 "# [r[0] for r in res if r[1]]"
1413 "cell_type": "code",
1414 "execution_count": 845,
1425 "execution_count": 845,
1427 "output_type": "execute_result"
1431 "len([r[0] for r in res if r[1]])"
1435 "cell_type": "code",
1436 "execution_count": 846,
1442 "w_bombe = Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1447 "cell_type": "code",
1448 "execution_count": 847,
1456 "Signal(bank='e', wire='e')"
1459 "execution_count": 847,
1461 "output_type": "execute_result"
1465 "w_bombe.test_start"
1469 "cell_type": "code",
1470 "execution_count": 848,
1481 "execution_count": 848,
1483 "output_type": "execute_result"
1487 "w_bombe.test(start_positions=('e', 'l', 'f'))"
1491 "cell_type": "code",
1492 "execution_count": 849,
1503 "execution_count": 849,
1505 "output_type": "execute_result"
1509 "w_bombe.test(Signal('t', 'x'), ('e', 'l', 'f'))"
1513 "cell_type": "code",
1514 "execution_count": 850,
1521 "output_type": "stream",
1524 "a : a.cdefghi..l.nopqrst.vwx..\n",
1525 "b : ....efghi...mnop.......x..\n",
1526 "c : a....fg.i..lmn.p.r.t...x..\n",
1527 "d : a...efghij.lmn...rstu..xyz\n",
1528 "e : ab.defghijklmnopqrstuvwxyz\n",
1529 "f : abcde.ghijklmnopqrstuvwxyz\n",
1530 "g : abcdef.hijklmnopqrstuvwxyz\n",
1531 "h : ab.defghi...mnopq.stuvwx..\n",
1532 "i : abcdefgh.jklmnopqrstuvwxyz\n",
1533 "j : ...defg.i...mn.p...t...x..\n",
1534 "k : ....efg.i..lmnop.r.t...x..\n",
1535 "l : a.cdefg.i.k.mnopqr.t..wxyz\n",
1536 "m : .bcdefghijklmnopqrstuvwxyz\n",
1537 "n : abcdefghijklmnopqr.tuvwxyz\n",
1538 "o : ab..efghi.klmnopqr.t...xyz\n",
1539 "p : abc.efghijklmnopqrstuvwxyz\n",
1540 "q : a...efghi..lmnop.r.t...x..\n",
1541 "r : a.cdefg.i.klmnopqrst..w..z\n",
1542 "s : a..defghi...m..p.rstuv.xyz\n",
1543 "t : a.cdefghijklmnopqrstuvwxyz\n",
1544 "u : ...defghi...mn.p..st...x..\n",
1545 "v : a...efghi...mn.p..st...x..\n",
1546 "w : a...efghi..lmn.p.r.t...x..\n",
1547 "x : abcdefghijklmnopq.stuvwxyz\n",
1548 "y : ...defg.i..lmnop..st...x..\n",
1549 "z : ...defg.i..lmnop.rst...x..\n"
1554 "r = w_bombe.test(start_positions=('c', 'p', 'v'))\n",
1556 "for b in sorted(w_bombe.banks):\n",
1557 " print(b, ': ', end='')\n",
1558 " for w in sorted(w_bombe.banks[b]):\n",
1559 " if w_bombe.banks[b][w]:\n",
1560 " print(w, end='')\n",
1562 " print('.', end='')\n",
1567 "cell_type": "code",
1568 "execution_count": 851,
1575 "output_type": "stream",
1578 "a : abcdefghi..lmnop.rst.v.x.z\n",
1579 "b : a...ef.hi..lmnop.r.t...x..\n",
1580 "c : a..defg.i..lmn.p.rst...x..\n",
1581 "d : a.c.efghi.klmnopqrstu.wxyz\n",
1582 "e : abcdefghijklmnopqrstuvwx.z\n",
1583 "f : abcdefghijklmno.qrstuvwxyz\n",
1584 "g : a.cdefghijklmnopqrstuvwxyz\n",
1585 "h : ab.defghi...mnopqrstuvwxyz\n",
1586 "i : abcdefghijklm.opqrstuvwxyz\n",
1587 "j : ....efg.i..lmnop...t...x..\n",
1588 "k : ...defg.i..lmn.p..st...x..\n",
1589 "l : abcdefg.ijklmnopqrstuv.x..\n",
1590 "m : abcdefghijkl.nopqrstuvwxyz\n",
1591 "n : abcdefgh.jklmnopqrstuvwxyz\n",
1592 "o : ab.defghij.lmnop.r.tuvwxyz\n",
1593 "p : abcde.ghijklmnopqrstuvwxyz\n",
1594 "q : ...defghi..lmn.p..st...x..\n",
1595 "r : abcdefghi..lmnop.rst.v.x.z\n",
1596 "s : a.cdefghi.klmn.pqr.tuvwxyz\n",
1597 "t : abcdefghijklmnopqrstuvw.yz\n",
1598 "u : ...defghi..lmnop..st...x..\n",
1599 "v : a...efghi..lmnop.rst...x..\n",
1600 "w : ...defghi...mnop..st...x..\n",
1601 "x : abcdefghijklmnopqrs.uvwxyz\n",
1602 "y : ...d.fghi...mnop..st...x..\n",
1603 "z : a..defghi...mnop.rst...x..\n"
1608 "r = w_bombe.test(start_positions=('e', 'l', 'f'))\n",
1610 "for b in sorted(w_bombe.banks):\n",
1611 " print(b, ': ', end='')\n",
1612 " for w in sorted(w_bombe.banks[b]):\n",
1613 " if w_bombe.banks[b][w]:\n",
1614 " print(w, end='')\n",
1616 " print('.', end='')\n",
1621 "cell_type": "code",
1622 "execution_count": 852,
1630 "{frozenset({'e', 'y'}),\n",
1631 " frozenset({'t', 'x'}),\n",
1632 " frozenset({'i', 'n'}),\n",
1633 " frozenset({'m'}),\n",
1634 " frozenset({'b', 'g'}),\n",
1635 " frozenset({'f', 'p'})}"
1638 "execution_count": 852,
1640 "output_type": "execute_result"
1644 "ps = w_bombe.possible_plugboards()\n",
1649 "cell_type": "code",
1650 "execution_count": 853,
1661 "execution_count": 853,
1663 "output_type": "execute_result"
1667 "all(s0.isdisjoint(s1) for s0 in ps for s1 in ps if s0 != s1)"
1671 "cell_type": "code",
1672 "execution_count": 854,
1680 "({frozenset({1, 2}), frozenset({2, 3}), frozenset({3, 4})},\n",
1681 " frozenset({1, 2}),\n",
1682 " frozenset({3, 4}),\n",
1683 " frozenset({2, 3}))"
1686 "execution_count": 854,
1688 "output_type": "execute_result"
1693 "f1 = frozenset((1, 2))\n",
1694 "f2 = frozenset((3, 4))\n",
1695 "f3 = frozenset((2, 3))\n",
1696 "s = s.union({f1})\n",
1697 "s = s.union({f2})\n",
1698 "s = s.union({f1})\n",
1699 "s = s.union({f3})\n",
1704 "cell_type": "code",
1705 "execution_count": 855,
1716 "execution_count": 855,
1718 "output_type": "execute_result"
1722 "all(s0.isdisjoint(s1) for s0 in s for s1 in s if s0 != s1)"
1726 "cell_type": "code",
1727 "execution_count": 856,
1738 "execution_count": 856,
1740 "output_type": "execute_result"
1744 "{1, 2}.isdisjoint({1, 6})"
1748 "cell_type": "code",
1749 "execution_count": null,
1759 "display_name": "Python 3",
1760 "language": "python",
1764 "codemirror_mode": {
1768 "file_extension": ".py",
1769 "mimetype": "text/x-python",
1771 "nbconvert_exporter": "python",
1772 "pygments_lexer": "ipython3",