// SPDX-License-Identifier: MIT pragma solidity 0.8.24; /// @title HashSig: post-quantum signature verification using only keccak256. /// @notice A stateful hash-based signature in the XMSS family (NIST SP 800-208 standardises XMSS/LMS): /// a Merkle tree of 2^h WOTS+ one-time keys under one root. Its security rests on the hash function /// alone, so Shor's algorithm (what a quantum computer uses to break ECDSA) does not apply; Grover's /// search only halves the security level, leaving about 128 bits for a 256-bit hash. /// /// Parameters: n = 32 bytes, Winternitz w = 16, 64 message digits + 3 checksum digits = 67 chains. /// Every hash is tweaked with (pubSeed, leaf, chain, step) or (pubSeed, level, position), so an attacker /// cannot attack many targets at once. /// /// Each leaf (one-time key) must sign only one message. The account that uses this library enforces that /// on chain: the leaf index must grow with every accepted signature. library HashSig { uint256 internal constant CHAINS = 67; uint256 internal constant SIG_WORDS = 67; /// @notice Root of the tree that `sig` + `path` commit to for `digest` at `leafIndex`. /// The signature is valid if and only if this equals the root stored for the account. function rootOf(bytes32 pubSeed, uint256 leafIndex, bytes32 digest, bytes32[67] calldata sig, bytes32[] calldata path) internal pure returns (bytes32) { return _climb(pubSeed, _leaf(pubSeed, leafIndex, digest, sig), leafIndex, path); } /// WOTS+: finish every chain from the signed position to the end (step 15) and hash the 67 ends into a leaf. function _leaf(bytes32 pubSeed, uint256 leafIndex, bytes32 digest, bytes32[67] calldata sig) private pure returns (bytes32 leaf) { assembly ("memory-safe") { let buf := mload(0x40) // [pubSeed | leafIndex | pk_0 .. pk_66] = 64 + 67*32 = 2208 bytes let scr := add(buf, 2208) // [pubSeed | tweak | x] = 96 bytes, reused for every chain step mstore(buf, pubSeed) mstore(add(buf, 32), leafIndex) mstore(scr, pubSeed) let csum := 0 for { let i := 0 } lt(i, 67) { i := add(i, 1) } { let a switch lt(i, 64) case 1 { a := and(shr(sub(252, mul(4, i)), digest), 15) // nibble i, most significant first csum := add(csum, sub(15, a)) } default { a := and(shr(mul(4, sub(66, i)), csum), 15) } // checksum, 3 nibbles, max 960 let x := calldataload(add(sig, mul(32, i))) let tbase := or(shl(64, leafIndex), shl(16, i)) for { let j := a } lt(j, 15) { j := add(j, 1) } { mstore(add(scr, 32), or(tbase, j)) mstore(add(scr, 64), x) x := keccak256(scr, 96) } mstore(add(buf, add(64, mul(32, i))), x) } leaf := keccak256(buf, 2208) } } /// Merkle authentication path from the leaf to the root, one tweaked hash per level. function _climb(bytes32 pubSeed, bytes32 node, uint256 index, bytes32[] calldata path) private pure returns (bytes32) { for (uint256 h; h < path.length; ++h) { uint256 tweak = (uint256(1) << 255) | ((h + 1) << 32) | (index >> 1); node = index & 1 == 0 ? keccak256(abi.encodePacked(pubSeed, tweak, node, path[h])) : keccak256(abi.encodePacked(pubSeed, tweak, path[h], node)); index >>= 1; } return node; } }