A simple, dependency-free Go implementation of SLH-DSA (Stateless Hash-Based Digital Signature Algorithm) as specified in FIPS 205.
SLH-DSA is a post-quantum digital signature scheme, designed to be secure against attacks by quantum computers.
- Zero external dependencies - Uses only Go 1.24+ standard library
- Simple API - Just
GenerateKey,Sign, andVerify - Easy to read - Clean, straightforward implementation
- FIPS 205 compliant - Pure SLH-DSA and HashSLH-DSA (pre-hash), validated against the official NIST ACVP test vectors
go get github.com/KarpelesLab/slhdsaRequires Go 1.24 or later.
package main
import (
"crypto/rand"
"fmt"
"github.com/KarpelesLab/slhdsa"
)
func main() {
// Generate a key pair
sk, err := slhdsa.GenerateKey(rand.Reader, slhdsa.SHA2_128f)
if err != nil {
panic(err)
}
// Sign a message (implements crypto.Signer). Passing rand.Reader selects
// hedged signing, the FIPS 205 default.
message := []byte("Hello, post-quantum world!")
signature, err := sk.Sign(rand.Reader, message, nil)
if err != nil {
panic(err)
}
// Verify the signature
pk := sk.Public().(*slhdsa.PublicKey)
valid := pk.Verify(signature, message, nil)
fmt.Println("Signature valid:", valid)
}All 12 FIPS 205 parameter sets are supported:
| Parameter Set | Security | Signature Size | Public Key | Private Key |
|---|---|---|---|---|
SHA2_128s |
128-bit | 7,856 bytes | 32 bytes | 64 bytes |
SHA2_128f |
128-bit | 17,088 bytes | 32 bytes | 64 bytes |
SHA2_192s |
192-bit | 16,224 bytes | 48 bytes | 96 bytes |
SHA2_192f |
192-bit | 35,664 bytes | 48 bytes | 96 bytes |
SHA2_256s |
256-bit | 29,792 bytes | 64 bytes | 128 bytes |
SHA2_256f |
256-bit | 49,856 bytes | 64 bytes | 128 bytes |
SHAKE_128s |
128-bit | 7,856 bytes | 32 bytes | 64 bytes |
SHAKE_128f |
128-bit | 17,088 bytes | 32 bytes | 64 bytes |
SHAKE_192s |
192-bit | 16,224 bytes | 48 bytes | 96 bytes |
SHAKE_192f |
192-bit | 35,664 bytes | 48 bytes | 96 bytes |
SHAKE_256s |
256-bit | 29,792 bytes | 64 bytes | 128 bytes |
SHAKE_256f |
256-bit | 49,856 bytes | 64 bytes | 128 bytes |
The s variants are slower but produce smaller signatures. The f variants are faster but produce larger signatures.
The PrivateKey type implements crypto.Signer and crypto.MessageSigner (Go 1.25+).
A PrivateKey or PublicKey is safe for concurrent use by multiple goroutines.
sk, err := slhdsa.GenerateKey(rand.Reader, slhdsa.SHA2_128f)// Hedged signing, the FIPS 205 default - implements crypto.Signer
sig, err := sk.Sign(rand.Reader, message, nil)
// With context (for domain separation)
sig, err := sk.Sign(rand.Reader, message, &slhdsa.Options{Context: []byte("my-context")})Deterministic signing is the explicit exception: pass a nil reader and the same key, message and options always produce the same signature.
sig, err := sk.Sign(nil, message, nil)Use it only when reproducible signatures are required. Without fresh randomness, signing gives less protection against fault injection and side-channel attacks, which is why FIPS 205 makes the hedged variant the default.
pk := sk.Public().(*slhdsa.PublicKey)
valid := pk.Verify(signature, message, nil)
// With context (must match what was used during signing)
valid := pk.Verify(signature, message, []byte("my-context"))Set Options.Hash to use HashSLH-DSA. Sign and VerifyWithOptions then
take a digest of the message instead of the message itself, following the
crypto.Signer convention. The digest must be computed with the same hash
function:
digest := sha256.Sum256(message)
opts := &slhdsa.Options{Hash: crypto.SHA256, Context: []byte("my-context")}
sig, err := sk.Sign(rand.Reader, digest[:], opts)
valid := pk.VerifyWithOptions(sig, digest[:], opts)All 12 pre-hash functions allowed by FIPS 205 are supported: crypto.SHA224,
crypto.SHA256, crypto.SHA384, crypto.SHA512, crypto.SHA512_224,
crypto.SHA512_256, crypto.SHA3_224, crypto.SHA3_256, crypto.SHA3_384,
crypto.SHA3_512, slhdsa.SHAKE128 and slhdsa.SHAKE256.
SHAKE is an extendable-output function and has no crypto.Hash value, so this
package defines slhdsa.SHAKE128 and slhdsa.SHAKE256. FIPS 205 fixes their
output at 32 and 64 bytes:
digest := sha3.SumSHAKE256(message, 64)
sig, err := sk.Sign(rand.Reader, digest, &slhdsa.Options{Hash: slhdsa.SHAKE256})SignMessage (crypto.MessageSigner) takes the message itself. With
Options.Hash set it computes the digest for you, so the result verifies with
VerifyWithOptions over the digest:
sig, err := sk.SignMessage(rand.Reader, message, opts)Choose a pre-hash function that matches the security category of the parameter
set, otherwise the digest becomes the weakest link. crypto.SHA256 and
slhdsa.SHAKE128 only suit the 128-bit parameter sets; with the 192-bit and
256-bit sets use crypto.SHA512 or slhdsa.SHAKE256.
A pre-hash signature only verifies with the same hash function and context, and never as a pure SLH-DSA signature (or the other way round).
// Export keys
skBytes := sk.Bytes()
pkBytes := sk.Public().(*slhdsa.PublicKey).Bytes()
// Import keys
sk, err := slhdsa.NewPrivateKey(slhdsa.SHA2_128f, skBytes)
pk, err := slhdsa.NewPublicKey(slhdsa.SHA2_128f, pkBytes)A parameter set can also be looked up by its FIPS 205 name:
params, ok := slhdsa.ParamsByName("SLH-DSA-SHA2-128f")sk.Destroy()Destroy zeroes the secret key material; the key can no longer sign, while its
public key stays usable. It must not be called while another goroutine is using
the key.
MIT License - see LICENSE file.