// SPDX-License-Identifier: MIT pragma solidity 0.8.24; /// @title SLHDSA128sVerifier: SLH-DSA-SHA2-128s signature verification (FIPS 205, NIST security category 1), on chain. /// @notice Stateless: no keys, no funds, no owner. Hashfort v3 vaults use it for the recovery key, a hash-based /// signature that relies only on SHA-256, kept offline and used rarely. /// @dev Parameters: n = 16, h = 63, d = 7, h' = 9, a = 12, k = 14, lg_w = 4, m = 30; signature 7,856 bytes, /// public key 32 bytes (PK.seed || PK.root). Every SHA-256 call uses the EVM precompile at address 0x02. /// Implements Algorithm 20 (slh_verify_internal) with M' = 0 || |ctx| || ctx || M (pure SLH-DSA, Algorithm 24). contract SLHDSA128sVerifier { uint256 internal constant SIG_LEN = 7856; uint256 internal constant FORS_OFF = 16; // after R uint256 internal constant HT_OFF = 2928; // 16 + 14 * 13 * 16 uint256 internal constant XMSS_LEN = 704; // (35 + 9) * 16 /// @return true when sig is a valid SLH-DSA-SHA2-128s signature over message m with context ctx by key pk. function verify(bytes calldata m, bytes calldata ctx, bytes calldata sig, bytes32 pk) external view returns (bool) { if (sig.length != SIG_LEN || ctx.length > 255) return false; uint256 digest = _hmsg(bytes16(sig[0:16]), pk, abi.encodePacked(uint8(0), uint8(ctx.length), ctx, m)); return _verifyDigest(sig, digest, uint256(pk) >> 128, uint256(uint128(uint256(pk)))); } /// H_msg: MGF1-SHA-256(R || PK.seed || SHA-256(R || PK.seed || PK.root || M'), 30); returned as the first 32 bytes. function _hmsg(bytes16 r, bytes32 pk, bytes memory mp) private view returns (uint256) { bytes32 inner = sha256(abi.encodePacked(r, pk, mp)); return uint256(sha256(abi.encodePacked(r, bytes16(pk), inner, uint32(0)))); } function algorithm() external pure returns (string memory) { return "SLH-DSA-SHA2-128s (FIPS 205)"; } function _verifyDigest(bytes calldata sig, uint256 digest, uint256 seed, uint256 root) private view returns (bool ok) { assembly ("memory-safe") { // Scratch buffers, each: PK.seed (16) || zeros (48) || ADRSc (22) || message. let a := mload(0x40) let t := add(a, 0x100) mstore(0x40, add(t, 0x300)) mstore(a, shl(128, seed)) mstore(add(a, 32), 0) mstore(t, shl(128, seed)) mstore(add(t, 32), 0) // ADRSc = layer(1) || tree(8) || type(1) || keypair(4) || word2(4) || word3(4) function adrs(layer, tree, typ, kp, w2, w3) -> v { v := or(or(or(or(or(shl(168, layer), shl(104, tree)), shl(96, typ)), shl(64, kp)), shl(32, w2)), w3) } function sha(p, len) -> h { if iszero(staticcall(gas(), 2, p, len, 0, 32)) { revert(0, 0) } h := shr(128, mload(0)) } function f(buf, ad, x) -> h { mstore(add(buf, 64), shl(80, ad)) mstore(add(buf, 86), shl(128, x)) h := sha(buf, 102) } function hh(buf, ad, l, r) -> h { mstore(add(buf, 64), shl(80, ad)) mstore(add(buf, 86), shl(128, l)) mstore(add(buf, 102), shl(128, r)) h := sha(buf, 118) } function node(off) -> v { v := shr(128, calldataload(off)) } // H over (l, r) with base address ad0 (type and key pair set), tree height h and tree index ti function up(buf, ad0, h, ti, nd, au, odd) -> out { switch odd case 0 { out := hh(buf, or(ad0, or(shl(32, h), ti)), nd, au) } default { out := hh(buf, or(ad0, or(shl(32, h), ti)), au, nd) } } // one FORS tree (height 12): leaf from the secret value, then the authentication path function forsTree(buf, ad0, base, ti0) -> nd { let ti := ti0 nd := f(buf, or(ad0, ti), node(base)) for { let j := 0 } lt(j, 12) { j := add(j, 1) } { let odd := and(ti, 1) ti := shr(1, ti) nd := up(buf, ad0, add(j, 1), ti, nd, node(add(base, add(16, mul(j, 16)))), odd) } } // FORS (Algorithm 17): 14 trees, then T_k over the 14 roots -> FORS public key function fors(buf, tb, sp, dg, ad0) -> pk { // FORS_ROOTS: type 4, same key pair, words 2 and 3 cleared (written first: mstore spans 32 bytes) mstore(add(tb, 64), shl(80, xor(ad0, shl(96, 7)))) for { let i := 0 } lt(i, 14) { i := add(i, 1) } { let ti := add(shl(12, i), and(shr(sub(244, mul(i, 12)), dg), 0xfff)) mstore(add(tb, add(86, mul(i, 16))), shl(128, forsTree(buf, ad0, add(sp, add(16, mul(i, 208))), ti))) } pk := sha(tb, 310) } // one WOTS+ chain from digit d to the top (w - 1 = 15); ad0 has type WOTS_HASH and the key pair function chain(buf, x, ad0, c, d) -> y { y := x for { let s := d } lt(s, 15) { s := add(s, 1) } { y := f(buf, or(ad0, or(shl(32, c), s)), y) } } // WOTS+ public key from a signature over node msg (Algorithm 8), then T_len (type WOTS_PK = 1) function wotsPk(buf, tb, xs, ad0, msg) -> pk { mstore(add(tb, 64), shl(80, or(ad0, shl(96, 1)))) // WOTS_PK address, written before the chain ends let csum := 0 for { let c := 0 } lt(c, 32) { c := add(c, 1) } { let d := and(shr(sub(124, mul(c, 4)), msg), 15) csum := add(csum, sub(15, d)) mstore(add(tb, add(86, mul(c, 16))), shl(128, chain(buf, node(add(xs, mul(c, 16))), ad0, c, d))) } csum := shl(4, csum) for { let c := 32 } lt(c, 35) { c := add(c, 1) } { let d := and(shr(sub(12, mul(sub(c, 32), 4)), csum), 15) mstore(add(tb, add(86, mul(c, 16))), shl(128, chain(buf, node(add(xs, mul(c, 16))), ad0, c, d))) } pk := sha(tb, 646) } // XMSS authentication path of height 9 (Algorithm 11); ad0 has type TREE and no key pair function merkle(buf, xs, ad0, leaf, nd0) -> nd { nd := nd0 let ti := leaf for { let j := 0 } lt(j, 9) { j := add(j, 1) } { let odd := and(ti, 1) ti := shr(1, ti) nd := up(buf, ad0, add(j, 1), ti, nd, node(add(xs, add(560, mul(j, 16)))), odd) } } let idxTree := and(shr(32, digest), 0x3fffffffffffff) // 54 bits of bytes 21..27 let idxLeaf := and(shr(16, digest), 0x1ff) // 9 bits of bytes 28..29 let msgNode := fors(a, t, sig.offset, digest, adrs(0, idxTree, 3, idxLeaf, 0, 0)) // hypertree (Algorithm 13): 7 XMSS layers for { let layer := 0 } lt(layer, 7) { layer := add(layer, 1) } { if gt(layer, 0) { idxLeaf := and(idxTree, 0x1ff) idxTree := shr(9, idxTree) } let xs := add(sig.offset, add(2928, mul(layer, 704))) msgNode := merkle(a, xs, adrs(layer, idxTree, 2, 0, 0, 0), idxLeaf, wotsPk(a, t, xs, adrs(layer, idxTree, 0, idxLeaf, 0, 0), msgNode)) } ok := eq(msgNode, root) } } }