# ๐ 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](https://pyo3.rs) 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`:
```toml
[dependencies]
enigmar = { path = "." }
```
### Python
```bash
pip install maturin
maturin develop --features extension-module
```
## ๐ Quick Start
### Rust
```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
```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`
| `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`
| `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
```bash
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