# Bonsai specification Bonsai (ZPQ-SHA2-160) is an experimental hash-based signature scheme proposed for Zcash transparent outputs. It uses the FORS and XMSS constructions of [FIPS 205](https://csrc.nist.gov/pubs/fips/205/final) and fixed-sum one-time signatures. The [security notes](security.md) describe the assumptions and modeled signing budgets. ## 1. Structure FORS signs the message digest by revealing one secret and a Merkle authentication path in each of eighteen trees. A three-layer hypertree connects that FORS public key to the signer's public root. A one-time key in the bottom layer signs the FORS public key. Each layer above signs the root of the tree below it. Verification reconstructs all three roots and compares the last one with the public key. The digest selects a 32-bit FORS key position. A custody signer can use any position. A regular signer searches for a randomizer that selects a position with a live leaf in each layer. The signature carries the same authentication path lengths for both modes. ## 2. Parameters | Parameter | Value | | --- | --- | | Node, secret and seed size | 20 bytes | | FORS | 18 trees of height 10 | | XMSS heights | 16, 8, 8 from the top, 32 levels in all | | Regular live heights | 9, 7, 7 from the top (512, 128, 128 leaves per tree) | | Grinding bits | 0 | | One-time signature | 53 base-8 digits, seven steps per chain, leading digest bit zero and digit sum 232 | | Counter per layer | 4 bytes, big-endian | | Message label | ASCII `ZPQ-SHA2-160/FORS/v4` | | Digest label | ASCII `ZPQ-SHA2-160/DGST/v1` | | Digit label | ASCII `ZPQ-SHA2-160/WOTS/v1` | | Derived sibling label | ASCII `ZPQ-SHA2-160/pruned-node/v3` | | Key label | ASCII `ZPQ-SHA2-160/key/v4` | | Public key | 40 bytes | | Signature | 20 + 18 × 220 + 3 × 1,064 + 32 × 20 = 7,812 bytes | All ASCII labels in this specification are literal bytes without a terminator. ## 3. Hashes and addresses `||` concatenates bytes. `first20` keeps the first 20 bytes. `be32(x)` is x as four big-endian bytes. S is the public seed, the first half of the public key. An address A is 22 bytes: a one-byte layer, an eight-byte big-endian tree number, a one-byte type, then three four-byte big-endian words (a, b, c). Unused words are zero. It is the compressed address of FIPS 205 with the same type numbers. | Type | Used for | (a, b, c) | | --- | --- | --- | | 0 | One-time signature chain step | (leaf, chain, step) | | 1 | Chain ends to one-time public key | (leaf, 0, 0) | | 2 | Hypertree parent node | (0, height, index) | | 3 | FORS leaf or parent node | (hypertree leaf, height, index) | | 4 | FORS roots to FORS public key | (hypertree leaf, 0, 0) | | 5 | One-time signature secret | (leaf, chain, 0) | | 6 | FORS secret | (hypertree leaf, 0, index) | FORS node indices count across all eighteen trees: the node at height j and position i of tree t has index t·2^(10−j) + i. - **F**(A, X) = first20(SHA-256(S || 44 zero bytes || A || X)), for a 20-byte X. - **T**(A, X) = first20(SHA-512(S || 108 zero bytes || A || X)), for X of any whole number of nodes. A Merkle parent is T over its left child followed by its right child. A node with an even index is a left child. - **Message digest.** For caller-supplied message bytes M, I = SHA-512(message label || R || public key || M), then D = SHA-512(digest label || R || S || I || four zero bytes). The input of I is 80 bytes plus the message, and the input of D is exactly 128 bytes. The two labels differ, so the stages never share an input, whatever the length of M. The randomizer comes before the message, so two messages whose hash states collide partway through do not stay collided under a new randomizer. The public key enters I, so the same message gets unrelated digests under different keys. Read D most-significant bit first within each byte. Bits [0, 180) are eighteen ten-bit FORS indices. Bits [180, 212) are the 32-bit hypertree position J. The grinding field begins at bit 212 and has width zero. Subsequent bits are unused. - **Digits.** For a node V signed by the one-time key at a given layer, tree and leaf, and a four-byte counter c: G = first20(SHA-256(digit label || S || A || V || c)), where A has type 0 and words (leaf, 0, 0). Require the most significant bit of G to be zero. Read bits [1, 160) in order as 53 three-bit digits, most significant first within each digit. Its input is 86 bytes and an input of F is 106, so the two can never be equal. - **Position.** At layer ℓ with height h, a 32-bit or shorter index J names tree J >> h and leaf J mod 2^h. Layer 0 is the bottom layer. **Separate inputs for separate roles.** The table lists every hash call. An HMAC-SHA-512 call is two SHA-512 calls: the inner one over the key padded with zeros to 128 bytes and XORed with 0x36, followed by the message, and the outer one over the padded key XORed with 0x5c, followed by the 64-byte inner result. |M| is the message length. | Call | Hash | Input, in order | Bytes | | --- | --- | --- | ---: | | F: chain step, FORS leaf, one-time and FORS secret | SHA-256 | S, 44 zero bytes, A, one node | 106 | | Digit hash G | SHA-256 | digit label, S, A, signed node, counter | 86 | | T: tree parent, FORS roots, one-time public key | SHA-512 | S, 108 zero bytes, A, 2, 18 or 53 nodes | 190, 510 or 1,210 | | First digest stage I | SHA-512 | message label, R, public key, M | 80 + \|M\| | | Digest D | SHA-512 | digest label, R, S, I, four zero bytes | 128 | | Parameter hash | SHA-512 | P | 167 | | Secret seeds (section 4) | HMAC-SHA-512 keyed by both secret inputs (40 bytes) | key label, mode, role, SHA-512(P) | 85 | | Randomizer R | HMAC-SHA-512 keyed by SK.prf | S, message label, M, counter | 44 + \|M\| | | Derived sibling | HMAC-SHA-512 keyed by SK.prf | S, derived sibling label, A | 69 | Why no two calls share an input: - The two SHA-256 calls differ in length. Within F, and within T, the address differs in type or words. The three uses of T also differ in length. The digit hash shares type 0 and words (leaf, 0, 0) with the first step of chain 0, so only its label prefix and length set it apart from F. - I and D begin with different 20-byte labels. R, S and the public key always follow a label, so no choice of them moves one. - T and every HMAC call are longer than 128 bytes. In T, bytes 20 to 127 are zero. In an HMAC call they are 0x36 or 0x5c from byte 20 or 40 on. P is one fixed input that starts with the bytes 0, 0, 0, 20, and its bytes 20 to 127 are neither all zero nor all one pad value. - The randomizer and derived-sibling HMACs share the key SK.prf. Both messages start with S and then differ in the label that follows it. The seed derivation has another key, and its message has the role byte 0 or 1 at offset 20, where both SK.prf messages have the first byte of a label. For the first digest stage I to share a SHA-512 input with T or HMAC, its bytes 60 to 79, which hold PK.root, must all equal the zero or pad byte at those offsets. PK.root is an output of T, so this requires a preimage of T for one of those fixed values, even for a key an attacker builds. D is shorter than either input. ## 4. Key generation Key generation takes three 20-byte inputs and a mode. Encode regular as byte 0 and custody as byte 1. The first two inputs are secret, and the third is the public seed S. **The public seed.** S MUST be chosen uniformly at random for each key, independently of every other key, or derived from the wallet's secrets by a keyed pseudorandom function under a derivation path used for no other key. No one else may choose it or influence it. S begins every input of F and T and enters every digit hash, and the address in those inputs names a layer, tree, leaf, chain and step within a key but not the key itself. Two keys with one S therefore hash with the same function at every address, so each chain value, FORS leaf and tree node of one key is a second-preimage target in the same function as the other key's: an attacker attacks all of them at once, and the work to hit one falls in proportion to the number of keys that share S. A predictable S lets an attacker start that work before the key exists. Restoring the same three inputs in both modes breaks this rule in the mildest way: it gives a regular and a custody key with one public seed, whose secrets are unrelated but whose hash functions are shared, which doubles the targets under that seed. For role byte 0 and role byte 1, compute first20(HMAC-SHA-512(key = first input || second input, message = ASCII `ZPQ-SHA2-160/key/v4` || mode byte || role byte || SHA-512(P))) where P is the parameter block below. The key is 40 bytes. The message is 85 bytes: the 19-byte key label, the two bytes, then the 64-byte hash. Role 0 gives SK.seed and role 1 gives SK.prf. The mode must be retained for recovery. P is 167 bytes. Every integer is be32, and every label is be32 of its length followed by its bytes. Heights are listed bottom layer first. | Offset | Bytes | Field | Value | | ---: | ---: | --- | ---: | | 0 | 4 | Node size in bytes | 20 | | 4 | 4 | FORS trees | 18 | | 8 | 4 | FORS tree height | 10 | | 12 | 4 | Grinding bits | 0 | | 16 | 4 | Hypertree layers | 3 | | 20 | 4 | Layer 0 full height | 8 | | 24 | 4 | Layer 0 regular live height | 7 | | 28 | 4 | Layer 1 full height | 8 | | 32 | 4 | Layer 1 regular live height | 7 | | 36 | 4 | Layer 2 full height | 16 | | 40 | 4 | Layer 2 regular live height | 9 | | 44 | 4 | Bits per digit | 3 | | 48 | 4 | Chains per one-time signature | 53 | | 52 | 4 | Required digit sum | 232 | | 56 | 4 | Leading digit-hash bits required to be zero | 1 | | 60 | 4 | Counter bytes | 4 | | 64 | 24 | Message label, length 20 | `ZPQ-SHA2-160/FORS/v4` | | 88 | 24 | Digest label, length 20 | `ZPQ-SHA2-160/DGST/v1` | | 112 | 24 | Digit label, length 20 | `ZPQ-SHA2-160/WOTS/v1` | | 136 | 31 | Derived sibling label, length 27 | `ZPQ-SHA2-160/pruned-node/v3` | SHA-512(P) is `3051b200db3eff5d1829d183a981b7f6508f8c86959d553b00624b337f8f4498e7b4dcb187101ad3ee00c6288f80a52738420a54fb1181a4acf76a9cedaf1f10`. P lists everything that decides which node a one-time key signs and which digits it reveals. Any change to a parameter or label in P therefore gives unrelated secrets from the same inputs. Several of these values, such as the digit sum and the live heights, change no signature byte, so without this binding a retuned parameter set could make the same one-time keys sign different nodes or digit sets. A parameter set can be retuned only by also changing the keys. A change to the construction that P does not describe, such as the hash functions, the address layout or how digits are read, MUST change the key label. 1. A one-time signature secret is F(A, SK.seed) with A of type 5 for that layer, tree, leaf and chain. 2. A one-time public key is computed from its 53 secrets. Advance each secret through seven chain steps, where step s (0 to 6) applies F with type 0 and words (leaf, chain, s). Hash the 53 chain ends in order with T at type 1. The result is a leaf of the hypertree. 3. The top tree is at layer 2, tree 0. For custody, build all 65,536 leaves and fold them to the root in index order. For regular, build only the first 512 leaves and fold them to the height-9 left subtree root. At each height h from 9 through 15, derive the right sibling as the first 20 bytes of HMAC-SHA-512 keyed by SK.prf over (S || ASCII `ZPQ-SHA2-160/pruned-node/v3` || A), where A is type 2 at the current layer and tree (here layer 2, tree 0) with words (0, h, 1). Combine the accumulated left node and this sibling using T at (0, h+1, 0). The height-16 result is PK.root. 4. The public key is S || PK.root. Both secret inputs enter both SK.seed and SK.prf. Every one-time secret comes from SK.seed, but in regular mode the derived right siblings come from SK.prf and enter the tree roots that the one-time keys of layers 1 and 2 sign. If each secret came from one input alone, reusing a first input with another second input would make those one-time keys sign two different roots. Here a change to either input gives unrelated SK.seed and SK.prf, so it makes a different key instead. ## 5. Signing To sign a message M of any length: 1. For c from 0 to 2^32−1, compute R = the first 20 bytes of HMAC-SHA-512 keyed by SK.prf over (S || message label || M || be32(c)). Compute D and J. Custody accepts the first candidate. Regular accepts the first J for which `(J & 0xFE008080) == 0`, about one in 512. Counting from the least significant bit as zero, bits 25–31, 15 and 7 select omitted subtrees. Exhausting a counter ends the operation without a signature. 2. **FORS.** At layer 0, tree J >> 8, leaf J mod 256, for each tree t from 0 through 17, generate the secret at absolute index x as F(A, SK.seed) with type 6 and words (hypertree leaf, 0, x). Hash that secret with F at type 3 and the same words for the leaf. Output the secret at x = 1024·t + i_t and its ten siblings from leaf to root. Use T at type 3 for parents. Hash the eighteen roots in order with T at type 4 to obtain the first signed node V. 3. **XMSS.** For layers 0, 1 and 2 take the tree and leaf from J using that layer's height. Search four-byte counters upward from zero and use the smallest for which G has a zero leading bit and its 53 digits sum to 232. This choice is a requirement, not an example (see "One counter per one-time key" below). The mean is about 4,141 trials per layer. Output the counter, each chain secret advanced by its digit, and the authentication path from leaf to root. For a regular key in either lower layer, build leaves 0 through 127 and append the omitted right sibling at height 7, index 1, using the derivation in section 4 with the current layer and tree. For custody, build all 256 lower leaves. Set V to that root and J to the tree number for the next layer. **Why a fixed sum.** A one-time signature reveals each chain at the step given by its digit, and anyone can advance a revealed value further. A forger could therefore raise digits at will unless something forces another digit down. FIPS 205 adds checksum chains for this. Here every valid set of digits has the same sum, so raising one digit means lowering another, which would need a hash preimage. That removes the checksum chains and makes the verifier's work the same for every signature. **One counter per one-time key.** A signer MUST use the smallest qualifying counter, searching upward from zero, at every layer of every signature. Each one-time key always signs the same node: the FORS public key at its position, or the root of the tree below it. With this rule it therefore releases exactly one set of digits, however many signatures pass through it. A one-time key that releases two different digit sets is broken. An attacker learns each chain at the lower of its two digits, and forging a one-time signature for that key then takes about 2^62 hash trials (median) instead of 2^160. After three digit sets it takes about 2^41. So a signer MUST produce the same counter for the same key and node on every implementation, build and run. A parallel search MUST return the smallest qualifying counter, not the first one any worker finds, and an interrupted search MUST NOT resume past a counter it has not checked. Verifiers accept any qualifying counter and do not check that it is the smallest. Checking would mean recomputing the digit hash for every smaller counter, roughly 12,400 extra SHA-256 compressions per verification, many times the work of the rest of verification. The rule is therefore a requirement on signers, not a validity rule, and no verifier can detect a signer that breaks it. The [conformance vectors](../vectors/) include a valid signature that breaks it on purpose. The randomizer search in step 1 needs no such rule. Whatever randomizer a signer picks, every one-time key on the selected path signs the same node as before: the bottom key signs the FORS public key at its position, which depends only on SK.seed, S and that position, and every key above signs a tree root. Another randomizer exposes FORS secrets for another digest, which counts against the FORS signing budget like signing one more message. Verifiers accept any randomizer. The first qualifying candidate keeps signing deterministic. The randomizer takes M, so an attacker who chooses messages cannot predict it, and so cannot predict which FORS secrets a signature will reveal. The counter comes last, so the HMAC state after M is computed once per signature and each candidate hashes only the counter and the padding. **Cost of a candidate.** Each candidate also computes I over M again, because R precedes M. A candidate therefore costs about one SHA-512 compression per 128 bytes of message on top of a fixed part: one or two compressions to finish the HMAC inner hash, one for the HMAC outer hash, one for the start of I and two for D. For the 36-byte messages of the Ledger fixture a candidate takes six compressions. The Ledger app computes the message schedule of every block that does not depend on the candidate when it is compiled: per candidate it runs four full compressions and two without a schedule. A regular signer tries about 512 candidates per signature; custody tries one. Signing is deterministic. A verifier cannot tell which counters the signer skipped: it verifies the position selected by R and checks both the leading bit and digit sum at every layer. The verifier never applies the regular signer’s live-position restriction. **Signing budget.** A signer MUST NOT make more than 2^21 signatures with a regular key or 2^28 with a custody key. These are the required lifetimes in the [security notes](security.md#signing-lifetime). Every signature reveals FORS secrets at one position, and security falls quickly once a key has made more signatures than its budget: | Signer | Signatures | Forger's chance per guess, averaged over keys | | --- | ---: | ---: | | Regular | 2^21 (budget) | 2^−162.8 | | Regular | 2^24 | 2^−141.6 | | Regular | 2^26 | 2^−121.0 | | Regular | 2^28 | 2^−94.3 | | Custody | 2^28 (budget) | 2^−163.5 | | Custody | 2^30 | 2^−153.8 | | Custody | 2^32 | 2^−140.8 | The [security notes](security.md#average-and-unlucky-keys) give the model and the figures for unlucky keys. Nothing in a signature shows how many signatures its key has made, and the reference signer keeps no state, so no verifier and no signer in this repository enforces the budget: it is a requirement on whoever operates the key. A hardware wallet that takes about 24 seconds per signature would need more than a year and a half of continuous signing to reach 2^21. A software signer with a regular key can reach it. A signer that could exceed its budget SHOULD use a custody key. If even 2^28 could be exceeded, it MUST count its signatures in storage that survives restarts, update the count before it releases each signature, and stop at the budget or move to a new key. The budget covers all copies of the same key. Concurrent signers MUST reserve capacity atomically from one count or use disjoint allocations whose sum fits the budget. Recovery MUST preserve spent reservations, including those made since a backup. If a signer cannot establish how much of its allocation remains, it MUST retire that allocation or switch to a new key. ## 6. Verification Given a public key, a message M and a signature: 1. Require exactly 40 public-key bytes and 7,812 signature bytes. 2. Compute D using the first 20 bytes as R, the supplied public key and the exact message M. Extract all eighteen indices and the full unsigned position J. 3. Recover each FORS root. Hash its secret into a leaf with type 3 at height 0. At each subsequent height, use the low bit of the current absolute index to order the node and its sibling, shift the index right and hash the pair with T at the parent address. Compress the eighteen roots with type 4 to obtain V. 4. At each XMSS layer read the counter, derive the digits of V and reject unless the leading bit of G is zero and the digits sum to 232. Advance the supplied chains from their digits through step 7 and hash their endpoints with T at type 1. Fold the authentication path into that leaf with T at type 2. Pass the resulting V and the tree number to the next layer. 5. Accept only when the final V equals PK.root. Each valid signature takes 139 remaining chain steps per layer (53 × 7 − 232), plus one digit hash per layer. ## 7. Signature layout Offsets are zero-based and end-exclusive. There are no headers, length prefixes, optional fields or trailing bytes. | Bytes | Field | | --- | --- | | 0–20 | Randomizer R | | 20–3980 | Eighteen FORS proofs of 220 bytes | | 3980–5044 | Layer 0 counter and 53 chain values | | 5044–5204 | Layer 0 path, eight siblings | | 5204–6268 | Layer 1 counter and chain values | | 6268–6428 | Layer 1 path, eight siblings | | 6428–7492 | Layer 2 counter and chain values | | 7492–7812 | Layer 2 path, sixteen siblings | ## 8. Relationship to FIPS 205 The construction retains address-separated tree hashing, chain steps and FORS proofs. Its 20-byte nodes, uneven heights, base-8 chains and bit-packed digest are specific to this profile. WOTS+C replaces checksum chains with a counter and a fixed digit sum. The selected layout opens every FORS tree and requires zero ordinary grinding bits. Regular-key pruning, mode-separated seeds and deterministic randomizer search are specified above. Like FIPS 205's pure SLH-DSA, the message hash takes the message itself after a randomizer and the public key; there is no pre-hash and no separate key and message binding. [SPHINCS+C](https://csrc.nist.gov/csrc/media/Events/2022/fourth-pqc-standardization-conference/documents/papers/sphincs-plus-c-pqc2022.pdf) ## 9. Test vectors The [vectors](../vectors/) include ten numbered Ledger fixtures and labeled cases for both modes. The file records the key label and the parameter block P with its SHA-512 hash. Honest cases record the input seeds, the derived SK.seed and SK.prf, randomizer counter, FORS indices and public key, full position, counters and all three layer roots. Regular cases also record all nine omitted siblings in wire order (one bottom, one middle, seven top). The custody case selects a position with bit 31 set. Negative cases cover an incorrect digit sum or a set leading bit in each layer, a valid signature of another message, omitted nodes, and message, key and length changes. The case `second-counter-in-layer-0` is valid and verifiers must accept it, but no signer may produce it: its bottom layer uses the second qualifying counter, so with the honest signature of the same message it releases a second digit set from one one-time key of a public test key. The Rust tests regenerate the vectors and check that every honest counter is the smallest. The independent Python implementation verifies the vectors, repeats the counter check, and rebuilds P, the derived seeds and every honest randomizer from this specification, checking that each randomizer counter is the first that qualifies.