SLH-DSA Python Code
This page has the Python code we used to reproduce the example values in the previous pages and a complete FIPS.205 SLH-DSA implementation that supports all 12 parameter sets.
The core SHA2 and SHAKE hash functions in hashlib_pure.py use
the Python hashlib
and hmac libraries.
There is an alternative set of hash functions using our CryptoSys PKI toolkit library
and its Python interface.
Note that all functions below pass arguments and return values encoded in hexadecimal. The internal functions convert to binary and then encode the result back to hexadecimal. This is obviously inefficient but is done for clarity and demonstration purposes and ease of debugging.
This code was tested using Python 3.12.9 on 64-bit Windows 11.
A complete SLH-DSA signature program
This is meant as a demonstration of how SLH-DSA works, not an efficient or secure implementation. It uses hex-encoded representations of all variables throughout, so it is obviously slower.
| slhdsa_makesig.py | Make a SLH-DSA signature using all 12 defined FIPS.205 parameter sets . |
| slhdsa_makesig_test.py | Tests for SLH-DSA. |
| slh_params.py | Parameters for SLH-DSA. |
| slh_adrs.py | SLH-DSA ADRS Class. |
| slh_tree.py | Utilities for tree calculations. |
| slh_hashfuncs.py | SLH-DSA hash functions tailored for parameter sets. |
| hashlib_pure.py | SHA2 and SHAKE functions using hashlib and hmac libraries. |
| hashlib_pki.py | Alternative SHA2 and SHAKE functions using cryptosyspki. |
All the above Python code in a zip file (27 kB).
To make this more efficient you would work directly with byte arrays throughout, and use the incremental forms of SHA-256 and SHA-512 to take advantage of pre-computing the first block of input to the SHA-2 hash functions.
Short Python Programs
Short Python programs to carry out calculations for examples in previous pages
| merkle-authpath-simple.py | Show authpath indices for idx=6 and a=3. |
| merkle_authpath_calc_basic.py | Weak hash function that returns a 4-byte hash. |
| merkle_authpath_verify.py | Verify authentication path for a simple Merkle tree. |
| merkle_tree_4byte.py | Generate a Merkle tree using weak 4-byte hash function. |
| merkle_tree_4byte_pairwise.py | Generate a Merkle tree using weak 4-byte hash function and compute AUTH using hash_pairwise algorithm. |
| merkle_tree_4byte_treehash.py | Generate a Merkle tree using weak 4-byte hash function and compute AUTH using TreeHash algorithm. |
| sig_1_PRF_msg.py | Generate the 16-byte randomizer R using PRF_msg. |
| sig_2_H_msg.py | Compute H_msg from the message, public key and randomizer. |
| sig_3_message_to_indices.py | Interpret 25-byte mhash as 33 * 6-bit unsigned integers using base_2^b function Algorithm 4 FIPS.205 |
| fors_sk_basic.py | Compute the FORS secret keys for i=[0,1,32] with hardcoded ADRS. |
| fors_authpath_00.py | Compute the authpaths for the FORS trees with i=0. |
| fors_authpath_01_32.py | Compute the authpaths from scratch for the FORS trees with i=0..32 and the fors_pk_root value. |
| fors_authpath_basic.py | Compute the first authpath values for index 50 in first tree |
| fors_pkFromSig.py | Compute the public key from the signature. |
| wots_checksum.py | Split 16-byte message into 4-bit blocks then append 3 x 4-bit checksum. |
| wots_gen_pk.py | Compute root node of the top-most HT subtree = PK.root. |
| wots_ht_0.py | Generate the first and last HT signature values ht_sig[0][0] and ht_sig[0][34]. |
| wots_ht_0_authpath.py | Compute root node and authpath for bottom HT subtree at layer 0. |
| wots_layers.py | Compute the WOTS tree address for each layer. |
| wots_sk_0.py | Compute the first and last WOTS sk wots_sk_0 and wots_sk_34. |
| slh_adrs.py | SLH-DSA ADRS Class. |
| slh_sha256.py | SHL-DSA-SHA-256 crypto functions. |
| slh_util.py | SLH-DSA utilities for tree calculations. |
| hashlib_pure.py | SHA-2/SHAKE functions using hashlib and hmac libraries. |
| hashlib_pki.py | SHA-2/SHAKE functions using cryptosyspki. An alternative to hashlib_pure.py. |
| << previous: Generating and Verifying the Public Key Root | Contents | next: SLH-DSA References >> |
Contact us
To comment on this page or to contact us, please send us a message.
This page first published 17 March 2023. Last updated 16 February 2026.

