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) "
78 "execution_count": 31,
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 " other_bank = [b for b in c.banks if b != current.bank][0]\n",
168 " other_wire = c.scrambler.lookup(current.wire)\n",
169 " # print(\" adding\", other_bank, other_wire, \"because\", c.banks)\n",
170 " self.pending += [Signal(other_bank, other_wire)]\n",
172 " def run(self, run_start=None, wheel1_pos='a', wheel2_pos='a', wheel3_pos='a', use_diagonal_board=True):\n",
173 " if not run_start:\n",
174 " run_start = self.test_start\n",
175 " self.solutions = []\n",
176 " self.set_positions(wheel1_pos, wheel2_pos, wheel3_pos)\n",
177 " for run_index in range(26*26*26):\n",
178 " if self.test(initial_signal=run_start, use_diagonal_board=use_diagonal_board):\n",
179 " self.solutions += [self.connections[0].scrambler.wheel_positions_l]\n",
180 " advance3 = True\n",
181 " advance2 = False\n",
182 " advance1 = False\n",
183 " if (run_index + 1) % 26 == 0: advance2 = True\n",
184 " if (run_index + 1) % (26*26) == 0: advance1 = True\n",
185 " for c in self.connections:\n",
186 " c.scrambler.advance(advance1, advance2, advance3)\n",
187 " return self.solutions\n",
189 " def possible_plugboards(self):\n",
190 " possibles = set()\n",
191 " for b in self.banks:\n",
192 " active = [w for w in self.banks[b] if self.banks[b][w]]\n",
193 " inactive = [w for w in self.banks[b] if not self.banks[b][w]]\n",
194 " if len(active) == 1:\n",
195 " possibles = possibles.union({frozenset((b, active[0]))})\n",
196 " if len(inactive) == 1:\n",
197 " possibles = possibles.union({frozenset((b, inactive[0]))})\n",
198 " return possibles\n"
203 "execution_count": 32,
209 "bombe = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec)\n",
210 "# len(bombe.banks), bombe.banks['a'] == bombe.banks['b']"
215 "execution_count": 33,
221 "test_enigma = Enigma(reflector_b_spec, \n",
222 " wheel_i_spec, wheel_i_pegs,\n",
223 " wheel_ii_spec, wheel_ii_pegs,\n",
224 " wheel_iii_spec, wheel_iii_pegs,\n",
231 "execution_count": 34,
239 "'opgndxcrwomnlnecjz'"
242 "execution_count": 34,
244 "output_type": "execute_result"
248 "test_enigma.set_wheels('a', 'a', 'a')\n",
249 "pt = 'thisisatestmessage'\n",
250 "ct = test_enigma.encipher(pt)\n",
256 "execution_count": 35,
267 "execution_count": 35,
269 "output_type": "execute_result"
273 "cat(test_enigma.wheel_positions_l)"
278 "execution_count": 36,
286 "[MenuIem(before='t', after='o', number=1),\n",
287 " MenuIem(before='h', after='p', number=2),\n",
288 " MenuIem(before='i', after='g', number=3),\n",
289 " MenuIem(before='s', after='n', number=4),\n",
290 " MenuIem(before='i', after='d', number=5),\n",
291 " MenuIem(before='s', after='x', number=6),\n",
292 " MenuIem(before='a', after='c', number=7),\n",
293 " MenuIem(before='t', after='r', number=8),\n",
294 " MenuIem(before='e', after='w', number=9),\n",
295 " MenuIem(before='s', after='o', number=10),\n",
296 " MenuIem(before='t', after='m', number=11),\n",
297 " MenuIem(before='m', after='n', number=12),\n",
298 " MenuIem(before='e', after='l', number=13),\n",
299 " MenuIem(before='s', after='n', number=14),\n",
300 " MenuIem(before='s', after='e', number=15),\n",
301 " MenuIem(before='a', after='c', number=16),\n",
302 " MenuIem(before='g', after='j', number=17),\n",
303 " MenuIem(before='e', after='z', number=18)]"
306 "execution_count": 36,
308 "output_type": "execute_result"
312 "menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
318 "execution_count": 37,
324 "def make_menu(plaintext, ciphertext):\n",
325 " return [MenuItem(p, c, i+1) \n",
326 " for i, (p, c) in enumerate(zip(plaintext, ciphertext))]"
331 "execution_count": 38,
339 "[MenuIem(before='t', after='o', number=1),\n",
340 " MenuIem(before='h', after='p', number=2),\n",
341 " MenuIem(before='i', after='g', number=3),\n",
342 " MenuIem(before='s', after='n', number=4),\n",
343 " MenuIem(before='i', after='d', number=5),\n",
344 " MenuIem(before='s', after='x', number=6),\n",
345 " MenuIem(before='a', after='c', number=7),\n",
346 " MenuIem(before='t', after='r', number=8),\n",
347 " MenuIem(before='e', after='w', number=9),\n",
348 " MenuIem(before='s', after='o', number=10),\n",
349 " MenuIem(before='t', after='m', number=11),\n",
350 " MenuIem(before='m', after='n', number=12),\n",
351 " MenuIem(before='e', after='l', number=13),\n",
352 " MenuIem(before='s', after='n', number=14),\n",
353 " MenuIem(before='s', after='e', number=15),\n",
354 " MenuIem(before='a', after='c', number=16),\n",
355 " MenuIem(before='g', after='j', number=17),\n",
356 " MenuIem(before='e', after='z', number=18)]"
359 "execution_count": 38,
361 "output_type": "execute_result"
370 "execution_count": 39,
381 "execution_count": 39,
383 "output_type": "execute_result"
387 "(collections.Counter(m.before for m in menu) + collections.Counter(m.after for m in menu)).most_common(1)[0][0]"
392 "execution_count": 42,
398 "bombe.read_menu(menu)"
403 "execution_count": 43,
414 "execution_count": 43,
416 "output_type": "execute_result"
420 "len(bombe.connections)"
425 "execution_count": 27,
432 "output_type": "stream",
474 "for c in bombe.connections:\n",
475 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
480 "execution_count": 44,
488 "'ot:hp:gi:ns:di:sx:ac:rt:ew:os:mt:mn:el:ns:es:ac:gj:ez'"
491 "execution_count": 44,
493 "output_type": "execute_result"
497 "':'.join(cat(sorted(c.banks)) for c in bombe.connections)"
502 "execution_count": 45,
510 "'aaa:aab:aac:aad:aae:aaf:aag:aah:aai:aaj:aak:aal:aam:aan:aao:aap:aaq:aar'"
513 "execution_count": 45,
515 "output_type": "execute_result"
519 "':'.join(cat(c.scrambler.wheel_positions_l) for c in bombe.connections)"
524 "execution_count": 56,
535 "execution_count": 56,
537 "output_type": "execute_result"
541 "bombe.test(Signal('t', 't'))"
546 "execution_count": 57,
582 "execution_count": 57,
584 "output_type": "execute_result"
593 "execution_count": 48,
600 "output_type": "stream",
602 "a : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
603 "b : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
604 "c : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
605 "d : ABCDEFGHIJKLMNOPQRSTUVWXyZ\n",
606 "e : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
607 "f : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
608 "g : ABCDEFGHIJKLMNOPQRSTuVWXYZ\n",
609 "h : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
610 "i : ABCDEFGHIJKLMNOPQRSTUvWXYZ\n",
611 "j : ABCDEFGHIjKLMNOPQRSTUVWXYZ\n",
612 "k : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
613 "l : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
614 "m : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
615 "n : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
616 "o : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
617 "p : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
618 "q : AbCDEfGHIJkLMNOPqRSTuvWXyZ\n",
619 "r : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
620 "s : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
621 "t : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
622 "u : AbCDEfgHIJkLMNOPqRSTuvWXyZ\n",
623 "v : AbCDEfGHiJkLMNOPqRSTuvWXyZ\n",
624 "w : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
625 "x : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n",
626 "y : AbCdEfGHIJkLMNOPqRSTuvWXyZ\n",
627 "z : ABCDEFGHIJKLMNOPQRSTUVWXYZ\n"
632 "for b in sorted(bombe.banks):\n",
633 " print(b, ': ', end='')\n",
634 " for w in sorted(bombe.banks[b]):\n",
635 " if bombe.banks[b][w]:\n",
636 " print(w.upper(), end='')\n",
638 " print(w, end='')\n",
644 "execution_count": 49,
651 "output_type": "stream",
653 "a : abcdefghijklmnopqrstuvwxyz\n",
654 "b : a.cde.ghij.lmnop.rst..wx.z\n",
655 "c : abcdefghijklmnopqrstuvwxyz\n",
656 "d : abcdefghijklmnopqrstuvwx.z\n",
657 "e : abcdefghijklmnopqrstuvwxyz\n",
658 "f : a.cde.ghij.lmnop.rst..wx.z\n",
659 "g : abcdefghijklmnopqrst.vwxyz\n",
660 "h : abcdefghijklmnopqrstuvwxyz\n",
661 "i : abcdefghijklmnopqrstu.wxyz\n",
662 "j : abcdefghi.klmnopqrstuvwxyz\n",
663 "k : a.cde.ghij.lmnop.rst..wx.z\n",
664 "l : abcdefghijklmnopqrstuvwxyz\n",
665 "m : abcdefghijklmnopqrstuvwxyz\n",
666 "n : abcdefghijklmnopqrstuvwxyz\n",
667 "o : abcdefghijklmnopqrstuvwxyz\n",
668 "p : abcdefghijklmnopqrstuvwxyz\n",
669 "q : a.cde.ghij.lmnop.rst..wx.z\n",
670 "r : abcdefghijklmnopqrstuvwxyz\n",
671 "s : abcdefghijklmnopqrstuvwxyz\n",
672 "t : abcdefghijklmnopqrstuvwxyz\n",
673 "u : a.cde..hij.lmnop.rst..wx.z\n",
674 "v : a.cde.gh.j.lmnop.rst..wx.z\n",
675 "w : abcdefghijklmnopqrstuvwxyz\n",
676 "x : abcdefghijklmnopqrstuvwxyz\n",
677 "y : a.c.e.ghij.lmnop.rst..wx.z\n",
678 "z : abcdefghijklmnopqrstuvwxyz\n"
683 "for b in sorted(bombe.banks):\n",
684 " print(b, ': ', end='')\n",
685 " for w in sorted(bombe.banks[b]):\n",
686 " if bombe.banks[b][w]:\n",
687 " print(w, end='')\n",
689 " print('.', end='')\n",
695 "execution_count": 50,
706 "execution_count": 50,
708 "output_type": "execute_result"
712 "bombe.wheel_positions_l"
717 "execution_count": 51,
724 "output_type": "stream",
748 "bombe.set_positions('p', 'q', 'r')\n",
749 "for c in bombe.connections:\n",
750 " print(c.banks, cat(c.scrambler.wheel_positions_l))"
755 "execution_count": 52,
767 "execution_count": 53,
774 "output_type": "stream",
776 "a : .b.defghijklmnopqrstuvwxyz\n",
777 "b : a.cde.ghij.lmnop.rst..wx.z\n",
778 "c : .b.defghijklmnopqrstuvwxyz\n",
779 "d : abc.e.gh.jklmnopqrstuvwxyz\n",
780 "e : abcd.fghijklmnopqrstuvwxyz\n",
781 "f : a.c.e.g.ij.lmno..rst..wx.z\n",
782 "g : abcdef.h..klmnopqrstuvwxyz\n",
783 "h : abcde.g.ij.lmno.qrst..wxyz\n",
784 "i : abc.ef.h.jklmnopqrstuvwxyz\n",
785 "j : abcdef.hi.klmnopqrstuvwx.z\n",
786 "k : a.cde.g.ij.lmno..rst..wx.z\n",
787 "l : abcdefghijk.mnopqrstuvwxyz\n",
788 "m : abcdefghijkl.nopqrstuvwxyz\n",
789 "n : abcdefghijklm.opqrstuvwxyz\n",
790 "o : abcdefghijklmn.pqrstuvwxyz\n",
791 "p : abcde.g.ij.lmno.qrst..wxyz\n",
792 "q : a.cde.ghij.lmnop.rst..wx.z\n",
793 "r : abcdefghijklmnopq.stuvwxyz\n",
794 "s : abcdefghijklmnopqr.tuvwxyz\n",
795 "t : abcdefghijklmnopqrs.uvwxyz\n",
796 "u : a.cde.g.ij.lmno..rst..wx.z\n",
797 "v : a.cde.g.ij.lmno..rst..wx.z\n",
798 "w : abcdefghijklmnopqrstuv.xyz\n",
799 "x : abcdefghijklmnopqrstuvw.yz\n",
800 "y : a.cde.ghi..lmnop.rst..wx.z\n",
801 "z : abcdefghijklmnopqrstuvwxy.\n"
806 "bombe.set_positions('a', 'a', 'b')\n",
807 "bombe.test(Signal('s', 'a'))\n",
809 "for b in sorted(bombe.banks):\n",
810 " print(b, ': ', end='')\n",
811 " for w in sorted(bombe.banks[b]):\n",
812 " if bombe.banks[b][w]:\n",
813 " print(w, end='')\n",
815 " print('.', end='')\n",
821 "execution_count": 54,
828 "output_type": "stream",
830 "a : ..........................\n",
831 "b : ..........................\n",
832 "c : ..........................\n",
833 "d : ..........................\n",
834 "e : ....e.....................\n",
835 "f : ..........................\n",
836 "g : ..........................\n",
837 "h : ..........................\n",
838 "i : ..........................\n",
839 "j : ..........................\n",
840 "k : ..........................\n",
841 "l : ...........l..............\n",
842 "m : ............m.............\n",
843 "n : .............n............\n",
844 "o : ..............o...........\n",
845 "p : ..........................\n",
846 "q : ..........................\n",
847 "r : .................r........\n",
848 "s : ..................s.......\n",
849 "t : ...................t......\n",
850 "u : ..........................\n",
851 "v : ..........................\n",
852 "w : ......................w...\n",
853 "x : .......................x..\n",
854 "y : ..........................\n",
855 "z : .........................z\n"
860 "bombe.set_positions('a', 'a', 'b')\n",
863 "for b in sorted(bombe.banks):\n",
864 " print(b, ': ', end='')\n",
865 " for w in sorted(bombe.banks[b]):\n",
866 " if bombe.banks[b][w]:\n",
867 " print(w, end='')\n",
869 " print('.', end='')\n",
875 "execution_count": 55,
886 "execution_count": 55,
888 "output_type": "execute_result"
892 "len([bombe.banks['t'][w] for w in bombe.banks['t'] if bombe.banks['t'][w]])"
897 "execution_count": 25,
904 "# results = bombe.run()\n",
905 "# print(len(results), ('a', 'a', 'b') in results)"
910 "execution_count": 26,
917 "# results = bombe.run(use_diagonal_board=False)\n",
918 "# print(len(results), ('a', 'a', 'b') in results)"
923 "execution_count": 27,
934 "execution_count": 27,
936 "output_type": "execute_result"
940 "bombe.wheel_positions_l"
945 "execution_count": 28,
956 "execution_count": 28,
958 "output_type": "execute_result"
962 "bombe.test(Signal('t', 't'), ('p', 'p', 'p'))"
967 "execution_count": 29,
978 "execution_count": 29,
980 "output_type": "execute_result"
984 "bombe.wheel_positions_l"
989 "execution_count": 30,
996 "output_type": "stream",
998 "a : abcdefghijklmnop.rst.vwxyz\n",
999 "b : a.cde.g.ij.lmno..rst..wx.z\n",
1000 "c : abcdefghijklmnop.rst.vwxyz\n",
1001 "d : abcdefghijklmnopqrstuvwxyz\n",
1002 "e : abcdefghijklmnopqrstuvwxyz\n",
1003 "f : a.cde.g.ij.lmno..rst..wx.z\n",
1004 "g : abcdefghijklmnopqrstuvwxyz\n",
1005 "h : a.cde.ghijklmnop.rst.vwxyz\n",
1006 "i : abcdefghijklmnopqrstuvwxyz\n",
1007 "j : abcdefghijklmnopqrstuvwxyz\n",
1008 "k : a.cde.ghij.lmnop.rst..wx.z\n",
1009 "l : abcdefghijklmnopqrstuvwxyz\n",
1010 "m : abcdefghijklmnopqrstuvwxyz\n",
1011 "n : abcdefghijklmnopqrstuvwxyz\n",
1012 "o : abcdefghijklmnopqrstuvwxyz\n",
1013 "p : a.cde.ghijklmnop.rst.vwxyz\n",
1014 "q : ...de.g.ij.lmno..rst..wx.z\n",
1015 "r : abcdefghijklmnopqrstuvwxyz\n",
1016 "s : abcdefghijklmnopqrstuvwxyz\n",
1017 "t : abcdefghijklmnopqrstuvwxyz\n",
1018 "u : ...de.g.ij.lmno..rst..wx.z\n",
1019 "v : a.cde.ghij.lmnop.rst..wx.z\n",
1020 "w : abcdefghijklmnopqrstuvwxyz\n",
1021 "x : abcdefghijklmnopqrstuvwxyz\n",
1022 "y : a.cde.ghij.lmnop.rst..wx.z\n",
1023 "z : abcdefghijklmnopqrstuvwxyz\n"
1028 "for b in sorted(bombe.banks):\n",
1029 " print(b, ': ', end='')\n",
1030 " for w in sorted(bombe.banks[b]):\n",
1031 " if bombe.banks[b][w]:\n",
1032 " print(w, end='')\n",
1034 " print('.', end='')\n",
1039 "cell_type": "code",
1040 "execution_count": 31,
1051 "execution_count": 31,
1053 "output_type": "execute_result"
1057 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1058 "len(list(allwheels))"
1062 "cell_type": "code",
1063 "execution_count": 32,
1071 "(('a', 'a', 'b'), True)"
1074 "execution_count": 32,
1076 "output_type": "execute_result"
1080 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)\n",
1081 "b(('a', 'a', 'b'))"
1085 "cell_type": "code",
1086 "execution_count": 33,
1092 "b = Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu)(('a', 'a', 'b'))"
1096 "cell_type": "code",
1097 "execution_count": 34,
1108 "execution_count": 34,
1110 "output_type": "execute_result"
1114 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1116 "with multiprocessing.Pool() as pool:\n",
1117 " res = pool.map(Bombe(wheel_i_spec, wheel_ii_spec, wheel_iii_spec, reflector_b_spec, menu=menu),\n",
1119 "[r[0] for r in res if r[1]]"
1123 "cell_type": "code",
1124 "execution_count": 35,
1130 "def run_multi_bombe(wheel1_spec, wheel2_spec, wheel3_spec, reflector_spec, menu,\n",
1131 " start_signal=None, use_diagonal_board=True, \n",
1132 " verify_plugboard=True):\n",
1133 " allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1135 " with multiprocessing.Pool() as pool:\n",
1136 " res = pool.map(Bombe(wheel1_spec, wheel2_spec, wheel3_spec, \n",
1137 " reflector_spec, menu=menu, start_signal=start_signal, \n",
1138 " use_diagonal_board=use_diagonal_board, \n",
1139 " verify_plugboard=verify_plugboard),\n",
1141 " return [r[0] for r in res if r[1]]"
1145 "cell_type": "code",
1146 "execution_count": 36,
1157 "execution_count": 36,
1159 "output_type": "execute_result"
1163 "[r[0] for r in res if r[1]]"
1167 "cell_type": "code",
1168 "execution_count": 58,
1174 "# Setting sheet line 31 from http://www.codesandciphers.org.uk/enigma/enigma3.htm\n",
1175 "# Enigma simulation settings are \n",
1176 "# http://enigma.louisedade.co.uk/enigma.html?m3;b;b153;AFTX;AJFE;AU-BG-EY-FP-HL-IN-JZ-OS-QR-TX\n",
1177 "w_enigma = Enigma(reflector_b_spec, \n",
1178 " wheel_i_spec, wheel_i_pegs,\n",
1179 " wheel_v_spec, wheel_v_pegs,\n",
1180 " wheel_iii_spec, wheel_iii_pegs,\n",
1182 " 'ua pf rq so ni ey bg hl tx zj')"
1186 "cell_type": "code",
1187 "execution_count": 59,
1198 "execution_count": 59,
1200 "output_type": "execute_result"
1204 "w_enigma.set_wheels('j', 'e', 'b')\n",
1205 "tuple(unpos(p) for p in w_enigma.wheel_positions)"
1209 "cell_type": "code",
1210 "execution_count": 39,
1221 "execution_count": 39,
1223 "output_type": "execute_result"
1227 "w_enigma.set_wheels('j', 'e', 'b')\n",
1228 "pt = 'someplaintext'\n",
1229 "ct = w_enigma.encipher(pt)\n",
1234 "cell_type": "code",
1235 "execution_count": 40,
1246 "execution_count": 40,
1248 "output_type": "execute_result"
1252 "w_enigma.wheel_positions_l"
1256 "cell_type": "code",
1257 "execution_count": 41,
1265 "[MenuIem(before='s', after='d', number=1),\n",
1266 " MenuIem(before='o', after='h', number=2),\n",
1267 " MenuIem(before='m', after='n', number=3),\n",
1268 " MenuIem(before='e', after='p', number=4),\n",
1269 " MenuIem(before='p', after='f', number=5),\n",
1270 " MenuIem(before='l', after='o', number=6),\n",
1271 " MenuIem(before='a', after='r', number=7),\n",
1272 " MenuIem(before='i', after='e', number=8),\n",
1273 " MenuIem(before='n', after='e', number=9),\n",
1274 " MenuIem(before='t', after='i', number=10),\n",
1275 " MenuIem(before='e', after='m', number=11),\n",
1276 " MenuIem(before='x', after='g', number=12),\n",
1277 " MenuIem(before='t', after='g', number=13)]"
1280 "execution_count": 41,
1282 "output_type": "execute_result"
1286 "w_menu = [MenuItem(p, c, i+1) for i, (p, c) in enumerate(zip(pt, ct))]\n",
1291 "cell_type": "code",
1292 "execution_count": 42,
1301 "[('a', 'y', 'm'),\n",
1302 " ('c', 'p', 'v'),\n",
1303 " ('c', 's', 'f'),\n",
1304 " ('c', 'w', 'j'),\n",
1305 " ('d', 'r', 'k'),\n",
1306 " ('e', 'l', 'f'),\n",
1307 " ('e', 's', 'v'),\n",
1308 " ('e', 'y', 'd'),\n",
1309 " ('e', 'y', 'o'),\n",
1310 " ('f', 'z', 'x'),\n",
1311 " ('g', 'b', 'l'),\n",
1312 " ('g', 'c', 'd'),\n",
1313 " ('g', 'c', 'f'),\n",
1314 " ('g', 'j', 'p'),\n",
1315 " ('h', 'c', 'i'),\n",
1316 " ('h', 'm', 'w'),\n",
1317 " ('h', 'o', 'd'),\n",
1318 " ('h', 'p', 'b'),\n",
1319 " ('h', 's', 't'),\n",
1320 " ('i', 'b', 's'),\n",
1321 " ('i', 'v', 'b'),\n",
1322 " ('j', 'y', 'u'),\n",
1323 " ('k', 'b', 'x'),\n",
1324 " ('k', 'f', 't'),\n",
1325 " ('k', 'l', 'e'),\n",
1326 " ('k', 'l', 'm'),\n",
1327 " ('k', 'r', 'z'),\n",
1328 " ('k', 's', 'p'),\n",
1329 " ('l', 'd', 'z'),\n",
1330 " ('l', 'i', 'y'),\n",
1331 " ('l', 'y', 'f'),\n",
1332 " ('m', 'b', 'h'),\n",
1333 " ('m', 'p', 'l'),\n",
1334 " ('n', 'l', 'r'),\n",
1335 " ('o', 'k', 'x'),\n",
1336 " ('p', 'a', 'g'),\n",
1337 " ('p', 'c', 'v'),\n",
1338 " ('p', 'f', 'o'),\n",
1339 " ('p', 'm', 'i'),\n",
1340 " ('p', 'x', 'n'),\n",
1341 " ('p', 'x', 'p'),\n",
1342 " ('q', 'q', 'n'),\n",
1343 " ('q', 'r', 'w'),\n",
1344 " ('q', 'v', 'l'),\n",
1345 " ('q', 'x', 't'),\n",
1346 " ('s', 'a', 'h'),\n",
1347 " ('s', 'h', 'v'),\n",
1348 " ('s', 'l', 'p'),\n",
1349 " ('s', 'l', 's'),\n",
1350 " ('u', 'r', 'h'),\n",
1351 " ('v', 'v', 'v'),\n",
1352 " ('v', 'x', 'a'),\n",
1353 " ('w', 'j', 'z'),\n",
1354 " ('w', 'k', 'u'),\n",
1355 " ('x', 'f', 'p'),\n",
1356 " ('x', 'j', 'n'),\n",
1357 " ('x', 'o', 'q'),\n",
1358 " ('x', 'x', 'x'),\n",
1359 " ('y', 'n', 'c'),\n",
1360 " ('y', 'r', 'f'),\n",
1361 " ('z', 't', 'y'),\n",
1365 "execution_count": 42,
1367 "output_type": "execute_result"
1371 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1373 "with multiprocessing.Pool() as pool:\n",
1374 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1375 " menu=w_menu, verify_plugboard=False),\n",
1377 "[r[0] for r in res if r[1]]"
1381 "cell_type": "code",
1382 "execution_count": 43,
1393 "execution_count": 43,
1395 "output_type": "execute_result"
1399 "len([r[0] for r in res if r[1]])"
1403 "cell_type": "code",
1404 "execution_count": 44,
1413 "[('c', 'p', 'v'),\n",
1414 " ('c', 's', 'f'),\n",
1415 " ('e', 'l', 'f'),\n",
1416 " ('g', 'c', 'f'),\n",
1417 " ('j', 'y', 'u'),\n",
1418 " ('o', 'k', 'x'),\n",
1419 " ('p', 'a', 'g'),\n",
1420 " ('q', 'q', 'n'),\n",
1421 " ('q', 'v', 'l'),\n",
1422 " ('q', 'x', 't'),\n",
1423 " ('s', 'l', 'p'),\n",
1424 " ('u', 'r', 'h'),\n",
1428 "execution_count": 44,
1430 "output_type": "execute_result"
1434 "allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1436 "with multiprocessing.Pool() as pool:\n",
1437 " res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1438 " menu=w_menu, verify_plugboard=True),\n",
1440 "[r[0] for r in res if r[1]]"
1444 "cell_type": "code",
1445 "execution_count": 45,
1453 "[('c', 'p', 'v'),\n",
1454 " ('c', 's', 'f'),\n",
1455 " ('e', 'l', 'f'),\n",
1456 " ('g', 'c', 'f'),\n",
1457 " ('j', 'y', 'u'),\n",
1458 " ('o', 'k', 'x'),\n",
1459 " ('p', 'a', 'g'),\n",
1460 " ('q', 'q', 'n'),\n",
1461 " ('q', 'v', 'l'),\n",
1462 " ('q', 'x', 't'),\n",
1463 " ('s', 'l', 'p'),\n",
1464 " ('u', 'r', 'h'),\n",
1468 "execution_count": 45,
1470 "output_type": "execute_result"
1474 "run_multi_bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, w_menu)"
1478 "cell_type": "code",
1479 "execution_count": 46,
1485 "# allwheels = itertools.product(string.ascii_lowercase, repeat=3)\n",
1487 "# with multiprocessing.Pool() as pool:\n",
1488 "# res = pool.map(Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1489 "# menu=w_menu, start_signal=Signal('t', 'x')),\n",
1491 "# [r[0] for r in res if r[1]]"
1495 "cell_type": "code",
1496 "execution_count": 47,
1507 "execution_count": 47,
1509 "output_type": "execute_result"
1513 "len([r[0] for r in res if r[1]])"
1517 "cell_type": "code",
1518 "execution_count": 48,
1524 "w_bombe = Bombe(wheel_i_spec, wheel_v_spec, wheel_iii_spec, reflector_b_spec, \n",
1529 "cell_type": "code",
1530 "execution_count": 49,
1538 "Signal(bank='e', wire='e')"
1541 "execution_count": 49,
1543 "output_type": "execute_result"
1547 "w_bombe.test_start"
1551 "cell_type": "code",
1552 "execution_count": 50,
1563 "execution_count": 50,
1565 "output_type": "execute_result"
1569 "w_bombe.test(start_positions=('e', 'l', 'f'))"
1573 "cell_type": "code",
1574 "execution_count": 51,
1585 "execution_count": 51,
1587 "output_type": "execute_result"
1591 "w_bombe.test(Signal('t', 'x'), ('e', 'l', 'f'))"
1595 "cell_type": "code",
1596 "execution_count": 52,
1603 "output_type": "stream",
1606 "a : a.cdefghi..l.nopqrst.vwx..\n",
1607 "b : ....efghi...mnop.......x..\n",
1608 "c : a....fg.i..lmn.p.r.t...x..\n",
1609 "d : a...efghij.lmn...rstu..xyz\n",
1610 "e : ab.defghijklmnopqrstuvwxyz\n",
1611 "f : abcde.ghijklmnopqrstuvwxyz\n",
1612 "g : abcdef.hijklmnopqrstuvwxyz\n",
1613 "h : ab.defghi...mnopq.stuvwx..\n",
1614 "i : abcdefgh.jklmnopqrstuvwxyz\n",
1615 "j : ...defg.i...mn.p...t...x..\n",
1616 "k : ....efg.i..lmnop.r.t...x..\n",
1617 "l : a.cdefg.i.k.mnopqr.t..wxyz\n",
1618 "m : .bcdefghijklmnopqrstuvwxyz\n",
1619 "n : abcdefghijklmnopqr.tuvwxyz\n",
1620 "o : ab..efghi.klmnopqr.t...xyz\n",
1621 "p : abc.efghijklmnopqrstuvwxyz\n",
1622 "q : a...efghi..lmnop.r.t...x..\n",
1623 "r : a.cdefg.i.klmnopqrst..w..z\n",
1624 "s : a..defghi...m..p.rstuv.xyz\n",
1625 "t : a.cdefghijklmnopqrstuvwxyz\n",
1626 "u : ...defghi...mn.p..st...x..\n",
1627 "v : a...efghi...mn.p..st...x..\n",
1628 "w : a...efghi..lmn.p.r.t...x..\n",
1629 "x : abcdefghijklmnopq.stuvwxyz\n",
1630 "y : ...defg.i..lmnop..st...x..\n",
1631 "z : ...defg.i..lmnop.rst...x..\n"
1636 "r = w_bombe.test(start_positions=('c', 'p', 'v'))\n",
1638 "for b in sorted(w_bombe.banks):\n",
1639 " print(b, ': ', end='')\n",
1640 " for w in sorted(w_bombe.banks[b]):\n",
1641 " if w_bombe.banks[b][w]:\n",
1642 " print(w, end='')\n",
1644 " print('.', end='')\n",
1649 "cell_type": "code",
1650 "execution_count": 53,
1657 "output_type": "stream",
1660 "a : abcdefghi..lmnop.rst.v.x.z\n",
1661 "b : a...ef.hi..lmnop.r.t...x..\n",
1662 "c : a..defg.i..lmn.p.rst...x..\n",
1663 "d : a.c.efghi.klmnopqrstu.wxyz\n",
1664 "e : abcdefghijklmnopqrstuvwx.z\n",
1665 "f : abcdefghijklmno.qrstuvwxyz\n",
1666 "g : a.cdefghijklmnopqrstuvwxyz\n",
1667 "h : ab.defghi...mnopqrstuvwxyz\n",
1668 "i : abcdefghijklm.opqrstuvwxyz\n",
1669 "j : ....efg.i..lmnop...t...x..\n",
1670 "k : ...defg.i..lmn.p..st...x..\n",
1671 "l : abcdefg.ijklmnopqrstuv.x..\n",
1672 "m : abcdefghijkl.nopqrstuvwxyz\n",
1673 "n : abcdefgh.jklmnopqrstuvwxyz\n",
1674 "o : ab.defghij.lmnop.r.tuvwxyz\n",
1675 "p : abcde.ghijklmnopqrstuvwxyz\n",
1676 "q : ...defghi..lmn.p..st...x..\n",
1677 "r : abcdefghi..lmnop.rst.v.x.z\n",
1678 "s : a.cdefghi.klmn.pqr.tuvwxyz\n",
1679 "t : abcdefghijklmnopqrstuvw.yz\n",
1680 "u : ...defghi..lmnop..st...x..\n",
1681 "v : a...efghi..lmnop.rst...x..\n",
1682 "w : ...defghi...mnop..st...x..\n",
1683 "x : abcdefghijklmnopqrs.uvwxyz\n",
1684 "y : ...d.fghi...mnop..st...x..\n",
1685 "z : a..defghi...mnop.rst...x..\n"
1690 "r = w_bombe.test(start_positions=('e', 'l', 'f'))\n",
1692 "for b in sorted(w_bombe.banks):\n",
1693 " print(b, ': ', end='')\n",
1694 " for w in sorted(w_bombe.banks[b]):\n",
1695 " if w_bombe.banks[b][w]:\n",
1696 " print(w, end='')\n",
1698 " print('.', end='')\n",
1703 "cell_type": "code",
1704 "execution_count": 54,
1712 "{frozenset({'m'}),\n",
1713 " frozenset({'i', 'n'}),\n",
1714 " frozenset({'f', 'p'}),\n",
1715 " frozenset({'t', 'x'}),\n",
1716 " frozenset({'e', 'y'}),\n",
1717 " frozenset({'b', 'g'})}"
1720 "execution_count": 54,
1722 "output_type": "execute_result"
1726 "ps = w_bombe.possible_plugboards()\n",
1731 "cell_type": "code",
1732 "execution_count": 55,
1743 "execution_count": 55,
1745 "output_type": "execute_result"
1749 "all(s0.isdisjoint(s1) for s0 in ps for s1 in ps if s0 != s1)"
1753 "cell_type": "code",
1754 "execution_count": 56,
1762 "({frozenset({1, 2}), frozenset({3, 4}), frozenset({2, 3})},\n",
1763 " frozenset({1, 2}),\n",
1764 " frozenset({3, 4}),\n",
1765 " frozenset({2, 3}))"
1768 "execution_count": 56,
1770 "output_type": "execute_result"
1775 "f1 = frozenset((1, 2))\n",
1776 "f2 = frozenset((3, 4))\n",
1777 "f3 = frozenset((2, 3))\n",
1778 "s = s.union({f1})\n",
1779 "s = s.union({f2})\n",
1780 "s = s.union({f1})\n",
1781 "s = s.union({f3})\n",
1786 "cell_type": "code",
1787 "execution_count": 57,
1798 "execution_count": 57,
1800 "output_type": "execute_result"
1804 "all(s0.isdisjoint(s1) for s0 in s for s1 in s if s0 != s1)"
1808 "cell_type": "code",
1809 "execution_count": 58,
1820 "execution_count": 58,
1822 "output_type": "execute_result"
1826 "{1, 2}.isdisjoint({1, 6})"
1830 "cell_type": "code",
1831 "execution_count": null,
1841 "display_name": "Python 3",
1842 "language": "python",
1846 "codemirror_mode": {
1850 "file_extension": ".py",
1851 "mimetype": "text/x-python",
1853 "nbconvert_exporter": "python",
1854 "pygments_lexer": "ipython3",