NationalSecurityAgency / enigma-simulator

An educational demonstration of breaking the Enigma machine
Other
465 stars 84 forks source link

Fix rotor stepping; Implement double step of mid-rotor #6

Open Zanion opened 4 years ago

Zanion commented 4 years ago

Summary


The simulator was failing to encode messages consistently with known reliably correct simulators see: Issue 5

Identified root error being that the rotor was being stepped on each keystroke from the left rotor rightwards, this is backwards from reality. The Enigma machine steps the first rotor by the entrywheel which is the rightmost rotor from perspective of the operator. The simulator had the stepping execution reversed in code accordingly and is now producing the correct encodings.

Reference: http://users.telenet.be/d.rijmenants/en/enigmatech.htm

Each time a key is depressed, the position of the rotors is changed. This results in a different substitution encryption each time for the same given letter. The first rotor, on the right side, steps on each depressed key. The middle rotor advances once on every 26 advances of the first rotor. The third, slowest, rotor, on every 26 advances of the middle rotor.

TC1 Settings:

Rotors: I, II, III Initial position: M, E, U Ring setting: A, A, A Reflector: UKW-B Input: AAAAA Expected result: GDXTZ

Double Stepping Mid-Rotor


Additionally added lacking double-step functionality for the mid-rotor. The middle rotor will advance on the next step of the first rotor a second time if the middle rotor notch is aligned with the pawl.

Reference: http://users.telenet.be/d.rijmenants/en/enigmatech.htm

The rotors appear to work as a normal odometer, with the right-most rotor always stepping on each key stroke and the other rotors stepping after a complete cycle of the previous rotor, but there is an important difference due to the system of pawls and teeth. The middle rotor will advance on the next step of the first rotor a second time in a row, if the middle rotor is in its own turnover position. This is called the double-step.

TC2 Settings:

Tested based on sequence for doublesteps from: http://users.telenet.be/d.rijmenants/en/enigmatech.htm

Rotors: III, II, I Initial position: K, D, O Ring setting: A, A, A Reflector: UKW-B

Verified window sequence: KDO->KDP->KDQ->KER->LFS->LFT->LFU

Testing

The tests to validate the functionality depend upon pytest being installed on the system. They can be executed from the project directory and ran with the pytest command:

pytest
Zanion commented 4 years ago

I'll add automated tests over this weekend to complete the PR prior to approval/merge. I intend to create a test directory in root using pytest unless you have a different convention.

ctubbsii commented 4 years ago

Thanks for the pull request. I'll try to find somebody to code review this after the U.S. winter holiday season. I did have one question though: the pipenv changes for dependency management seem unrelated to the bug being addressed. Would it be possible to separate those changes into another pull request, so they can be considered and reviewed separately?

Zanion commented 4 years ago

Not a problem, thank you. I rebased and cherry-picked just the tests without the Pipenv additions. I added documentation above for running the included automated tests with natively installed pytest.