A from-scratch implementation of the Lamport one-time signature (OTS) scheme — a hash-based, post-quantum digital signature whose security rests only on the one-wayness of a hash function (here SHA-256), not on factoring or discrete logs. That's what makes hash-based signatures interesting: they remain secure even against a quantum adversary.
Standard-library Python only. No external crypto dependencies.
- Key generation. For each of the 256 bits of a message hash, generate two
random secret values — one for a
0bit, one for a1bit (a 2×256 secret key). The public key is the SHA-256 hash of every secret value. - Signing. Hash the message (256 bits). For each bit, reveal the secret value corresponding to that bit's value. The revealed values are the signature.
- Verification. Hash each signature element and check it equals the matching public-key entry for that message bit.
The scheme is one-time: signing two different messages with the same key leaks enough secrets to forge, which is exactly why the code and tests focus on a single message per key.
python src/demo.pymessage : b'transfer 100 to alice'
verify(correct message) : True
verify(tampered message): False <- one flipped byte breaks the signature
pip install pytest && python -m pytest tests/ -vThree tests run against fixed test vectors (tests/test_case.py): correct
key generation + signing + verification, and that verification fails when either
the message or the signature is tampered with.
Lamport signatures are the conceptual foundation of modern stateless hash-based signatures like SPHINCS+, which NIST standardized (as SLH-DSA) for the post-quantum era. This project implements the core idea end to end.
Educational implementation. For production post-quantum signatures, use a vetted library implementing a standardized scheme (SLH-DSA / ML-DSA).