enigmar 0.1.0

High-performance Enigma Machine simulator (M3/M4) with Python bindings
Documentation
  • Coverage
  • 100%
    32 out of 32 items documented8 out of 32 items with examples
  • Size
  • Source code size: 54.6 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.2 MB This is the summed size of all files generated by rustdoc for all configured targets
  • ร˜ build duration
  • this release: 20s Average build duration of successful builds.
  • all releases: 20s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • MinLee0210/enigmar
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • MinLee0210

๐Ÿ” Enigmar

High-performance Enigma Machine simulator in Rust with Python bindings.

Enigmar is an educational library that accurately simulates the Wehrmacht Enigma M3/M4 cipher machines, including the famous double-stepping anomaly that made the real machine's behavior notoriously complex. Built with PyO3 for seamless Python interoperability.


โœจ Features

  • Historically accurate โ€” Rotors Iโ€“VIII, Reflectors B/C/B-thin/C-thin, with correct double-stepping
  • Reciprocal encryption โ€” encrypting and decrypting are the same operation, just like the real Enigma
  • No letter self-encryption โ€” faithfully reproduces the Enigma's critical weakness
  • Python bindings โ€” use from Python via PyO3 with pip install-ready architecture
  • Key management โ€” export/import full machine state as JSON for reproducibility
  • Zero-allocation core โ€” [u8; 26] lookup tables for O(1) character mapping
  • Comprehensive validation โ€” graceful error handling for invalid configurations

๐Ÿ“ฆ Installation

Rust

Add to your Cargo.toml:

[dependencies]
enigmar = { path = "." }

Python

pip install maturin
maturin develop --features extension-module

๐Ÿš€ Quick Start

Rust

use enigmar::EnigmaBuilder;

fn main() {
    // Configure an M3 Enigma machine
    let mut machine = EnigmaBuilder::new()
        .rotor("I", 0, 0)        // left rotor: type, position (A=0), ring setting
        .rotor("II", 0, 0)       // middle rotor
        .rotor("III", 0, 0)      // right rotor
        .reflector("B")
        .plugboard("AV BS CG DL FU HZ IN KM OW RX")
        .build()
        .unwrap();

    let ciphertext = machine.process_string("HELLOWORLD");
    println!("Encrypted: {}", ciphertext);

    // Decrypt: create a machine with the same settings
    let mut decoder = EnigmaBuilder::new()
        .rotor("I", 0, 0)
        .rotor("II", 0, 0)
        .rotor("III", 0, 0)
        .reflector("B")
        .plugboard("AV BS CG DL FU HZ IN KM OW RX")
        .build()
        .unwrap();

    let plaintext = decoder.process_string(&ciphertext);
    assert_eq!(plaintext, "HELLOWORLD");
}

Python

from enigmar import EnigmaBuilder

# Configure
builder = EnigmaBuilder()
builder.rotor("I", 0, 0)
builder.rotor("II", 0, 0)
builder.rotor("III", 0, 0)
builder.reflector("B")
builder.plugboard("AV BS CG DL FU HZ IN KM OW RX")
machine = builder.build()

# Encrypt
ciphertext = machine.process_string("HELLOWORLD")
print(f"Encrypted: {ciphertext}")

# Save & restore state
key = machine.export_key()      # JSON snapshot
machine.import_key(key)         # restore state
machine.reset()                 # reset rotor positions

๐Ÿ› ๏ธ API Reference

EnigmaBuilder

Method Description
new() Create an empty builder
.rotor(type, position, ring) Add a rotor (left โ†’ right). Types: "I" โ€“ "VIII"
.reflector(type) Set reflector. Types: "B", "C", "B-thin", "C-thin"
.plugboard(pairs) Set plugboard pairs, e.g. "AB CD EF" (up to 13 pairs)
.build() Build the EnigmaMachine

EnigmaMachine

Method Description
process_string(input) Encrypt/decrypt a message (non-alpha chars silently dropped)
export_key() Serialize machine state to JSON string
import_key(key) Restore machine state from JSON string
reset() Reset rotors to initial positions

โš™๏ธ How It Works

Input โ†’ Plugboard โ†’ Rotor III โ†’ Rotor II โ†’ Rotor I โ†’ Reflector
                                                         โ†“
Output โ† Plugboard โ† Rotor III โ† Rotor II โ† Rotor I โ†โ”€โ”€โ”˜

Double-stepping: Before each character, the right rotor always steps. If the right rotor is at its notch, the middle rotor steps. If the middle rotor is at its notch, both the middle and left rotors step โ€” this is the "double-stepping" anomaly of the mechanical Enigma.

๐Ÿงช Testing

cargo test

Runs 18 unit tests and 8 doc-tests covering:

  • Reciprocal encryption (encrypt โ†’ decrypt = original)
  • Double-stepping behavior verification
  • Known test vector: AAAAAAAAAA โ†’ BDZGOWCXLT
  • No letter self-encryption guarantee
  • Plugboard validation (duplicates, self-pairs, max 13 pairs)
  • Key export/import round-trip
  • Long message (260 chars) with plugboard

๐Ÿ“ Project Structure

enigmar/
โ”œโ”€โ”€ Cargo.toml        # Dependencies: pyo3, serde, serde_json
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ lib.rs        # Core library (all components + tests)
โ”œโ”€โ”€ example.py        # Python usage example
โ””โ”€โ”€ README.md

๐Ÿ“œ License

MIT