Note

This documentation is in draft form. Reports of any errors or suggestions for improvement are welcomed and should be submitted as new issues.

Components - crypto_enigma.components

This is a supporting module that defines the components used to construct an Enigma machine. It will not generally be used directly.

Overview

Component A component of an Enigma machine.
name The specification a component of an Enigma machine.
wiring The physical wiring of a component, expressed as a mapping.
turnovers The turnover positions for a rotor.
mapping The mapping performed by a component based on its rotational position.
Direction The direction that a signal flows through a component.
component Retrieve a specified component.
rotors The list of valid (non-reflector) rotor names.
reflectors The list of valid reflector rotor names

Machine components

class crypto_enigma.components.Component(name, wiring, turnovers)[source]

A component of an Enigma machine.

A component used to construct an Enigma machine (as embodied in an EnigmaConfig) identified by its specification (see name), and characterized by its physical wiring and additionally — for rotors other than the reflector — by turnovers which govern the stepping (see step) of the machine in which it is installed.

There is no reason to construct a component directly, and no directly instantiated component can be used in an EnigmaConfig. The properties of components “outside of” an EnigmaConfig can be examined using component.

Component properties

Component.name

The specification a component of an Enigma machine.

For rotors (including the reflector) this is one of the conventional letter or Roman numeral designations (e.g., 'IV' or 'β') or rotor “names”. For the plugboard this is the conventional string of letter pairs, indicating letters wired together by plugging (e.g., 'AU.ZM.ZL.RQ'). Absence or non-use of a plugboard can be indicated with '~' (or almost anything that isn’t a valid plugboard spec).

Returns:A string uniquely specifying a Component.
Return type:unicode
Component.wiring

The physical wiring of a component, expressed as a mapping.

Returns:
The mapping established by the physical wiring of a Component:
the forward mapping when 01 is at the window position for rotors; by the plug arrangement for the plugboard.
Return type:Mapping

Examples

A rotor’s wiring is fixed by the physical connections of the wires inside the rotor:

>>> cmp = component('V')
>>> cmp.wiring
u'VZBRGITYUPSDNHLXAWMJQOFECK'
>>> component('VI').wiring
u'JPGVOUMFYQBENHZRDKASXLICTW'

For plugboards, it is established by the specified connections:

>>> component('AZ.BY').wiring
u'ZYCDEFGHIJKLMNOPQRSTUVWXBA'
Component.turnovers

The turnover positions for a rotor.

Returns:
The letters on a rotor’s ring that appear at the window (see windows)
when the ring is in the turnover position. Not applicable (and empty) for the plugboard and for reflectors. (See step.)
Return type:unicode

Examples

Only “full-width” rotors have turnovers:

>>> component('V').turnovers
u'Z'
>>> component('VI').turnovers
u'ZM'
>>> component('I').turnovers
u'Q'

Reflectors, “half-width” rotors, and the plugboard never do:

>>> component('B').turnovers
u''
>>> component('β').turnovers
u''
>>> component('AG.OI.LM.ER.KU').turnovers
u''

The component mapping

Component.mapping(**kwargs)[source]

The mapping performed by a component based on its rotational position.

The mapping performed by a Component as a function of its position (see positions) in an Enigma machine and the Direction of the signal passing through it.

For all other positions of rotors, the mapping is a cyclic permutation this wiring’s inputs (backward) and outputs (forward) by the rotational offset of the rotor away from the 01 position.

Parameters:
  • position (int) – The rotational offset of the Component in the Enigma machine.
  • direction (Direction) – The direction of signal passage through the component.
Returns:

The mapping performed by the component in the direction when position is

at the window position.

Return type:

Mapping

Examples

Note that because the wiring of reflectors generates mappings that consist entirely of paired exchanges of letters, reflectors (at any position) produce the same mapping in both directions (the same is true of the plugboard):

>>> all(c.mapping(p, Direction.FWD) == c.mapping(p, Direction.REV) for c in map(component, reflectors) for p in range(1,26))
True

For rotors in their base position, with 01 at the window position, and for the plugboard, this is just the wiring:

>>> cmp.wiring == cmp.mapping(1, Direction.FWD)
True
class crypto_enigma.components.Direction[source]

The direction that a signal flows through a component.

During encoding of a character, the signal passes first through the wiring of each Component, from right to left in the machine, in a forward (FWD) direction, then through the reflector, and then, from left to right, through each component again, in reverse (REV). This direction affects the encoding performed by the component (see mapping).

Getting components

crypto_enigma.components.component(*args, **kwargs)[source]

Retrieve a specified component.

Parameters:name – The name of a Component
Returns:The component with the specified name.
Return type:Component

Examples

Components are displayed as a string consisting of their properties:

>>> print(component('VI'))
VI JPGVOUMFYQBENHZRDKASXLICTW ZM

Components with the same name are always identical:

>>> component('AG.OI.LM.ER.KU') is component('AG.OI.LM.ER.KU')
True
crypto_enigma.components.rotors

The list of valid (non-reflector) rotor names.

>>> rotors 
[u'I', u'II', u'III', u'IV', u'V', u'VI', u'VII', u'VIII', u'β', u'γ']
crypto_enigma.components.reflectors

The list of valid reflector rotor names

>>> reflectors
[u'A', u'B', u'C', u'b', u'c']