Shrinking a Rust Cross-Platform Keygen GUI from 3.1 MB to 296 KB
I wanted an offline cryptographic key generator (Keyforge:
Ed25519 SSH, WireGuard, age, JWT secrets, diceware passphrases,
.env bundles) that felt like an old-school demoscene keygen — tiny binary, pixel UI,
and a 4-channel chiptune tracker — while staying genuinely useful in bash scripts via
--cli --raw. Source & binaries:
github.com/kwaadrat/keyforge/releases.
1. The 3.1 MB Problem: eframe + clap
The initial prototype used eframe/egui and clap. Even with
opt-level = "z", fat LTO, and stripped symbols, the Windows release executable landed at
3.1 MB. For a 480x300 utility, shipping a full immediate-mode GPU UI toolkit and derive-based
CLI parser felt wrong.
| Version | Stack | Windows .exe size |
|---|---|---|
v0.1.0 |
eframe + egui + clap |
3.1 MB |
v0.1.1 |
minifb + 5x7 bitmap font + std::env::args() |
297 KB |
v0.1.6 |
ssh-key pruned (ed25519 only) + minifb X11 + --pub |
296 KB |
2. Software Framebuffer + 5x7 Bitmap Font
Replacing eframe with minifb cut out OpenGL/winit/egui overhead completely.
The entire UI renders into a 480x300 Vec<u32> pixel buffer at 60 FPS using
a hand-packed 5x7 ASCII bitmap font (~95 glyphs), a copperbar starfield header, and a sine-wave scroller.
Replacing clap with a 40-line std::env::args() loop removed another chunk of
binary size and compile time.
3. Zero-Asset Amiga-Style Chiptune Synth
Shipping WAV or MOD files would bloat the binary. Instead, synth.rs runs a real-time
4-channel tracker via tinyaudio at 44.1 kHz in ~120 lines of Rust:
- Lead: 25% duty pulse wave with 6 Hz vibrato across a 128-step minor progression.
- Bass: 16th-note octave-pumping sawtooth/square hybrid.
- Arpeggiator: PAL Amiga 50 Hz-rate rapid triad shimmer (882 samples/step).
- Drums: pitch-swept sine kick + 16-bit LFSR noise snare and hi-hats.
Why not hardware PC Speaker (PIT 8254)? Modern laptops rarely expose a real 8254 buzzer
through UEFI/HD Audio, Windows Beep() routes monophonically through the OS audio mixer, and
Linux KIOCSOUND requires elevated privileges (CAP_SYS_TTY_CONFIG).
4. GNOME Wayland Surprise: xdg-decoration
Running the Linux build under Ubuntu GNOME Wayland printed:
Failed to create server-side surface decoration: Missing
GNOME Shell does not implement server-side Wayland decorations, and Wayland intentionally prevents clients
from repositioning their own windows. Disabling minifb's default wayland feature
and keeping features = ["x11"] makes Linux builds run cleanly over XWayland with native window
decorations, dragging, and zero Wayland proxy warnings.
5. Supply Chain & Accidental RSA Advisories
cargo-audit flagged rsa (RUSTSEC-2023-0071) and
crypto-bigint (RUSTSEC-2026-0387) even though Keyforge only generates
Ed25519 SSH keys. The culprit was ssh-key's default features pulling in RSA.
Switching to default-features = false, features = ["ed25519", "rand_core", "std"] removed
rsa from the compiled dependency graph completely.
6. Scriptable CLI Mode
Human-readable terminal runs wrap output in clear ASCII separator bars, while --raw and
--pub emit exact values for shell scripts with zero banner noise:
keyforge --cli ssh --raw > ~/.ssh/id_ed25519
keyforge --cli ssh --raw --pub > ~/.ssh/id_ed25519.pub
WG_PRIV=$(keyforge --cli wireguard --raw)