// SPDX-License-Identifier: MIT pragma solidity 0.8.24; import "./Shake.sol"; /// @title MLDSA65Verifier: ML-DSA-65 signature verification (FIPS 204, NIST security category 3), fully on chain. /// @notice Stateless: no keys, no funds, no owner. Hashfort v3 vaults call it for every primary-key operation. /// @dev The parts of verification that depend only on the public key are computed once and kept in two data /// contracts (SSTORE2 layout: a STOP byte, then the data): /// A = Â, the k×l matrix ExpandA(ρ) in the NTT domain 30 polynomials × 256 × 3 bytes /// B = tr = H(pk, 64) || NTT(t1 · 2^d) 64 + 6 polynomials × 256 × 3 bytes /// checkExpansion() recomputes both from the public key, so anyone can confirm what a vault relies on. /// verify() then performs Algorithm 8 (ML-DSA.Verify_internal) on M' = 0 || |ctx| || ctx || M. contract MLDSA65Verifier { uint256 internal constant Q = 8380417; uint256 internal constant K = 6; uint256 internal constant L = 5; uint256 internal constant GAMMA1 = 524288; // 2^19 uint256 internal constant TWO_GAMMA2 = 523776; // (q-1)/16 uint256 internal constant GAMMA2 = 261888; uint256 internal constant BETA = 196; // tau * eta uint256 internal constant OMEGA = 55; uint256 internal constant TAU = 49; uint256 internal constant CT_LEN = 48; // lambda/4 uint256 internal constant SIG_LEN = 3309; uint256 internal constant PK_LEN = 1952; uint256 internal constant A_LEN = 23040; // 30 * 768 uint256 internal constant B_LEN = 4672; // 64 + 6 * 768 uint256 internal constant N_INV = 8347681; // 256^-1 mod q bytes internal constant ZETAS = hex"000001495e023975673965694f062b53df734fe0334f066b76b1ae360dd528edb0207fe439728370894a0881926d3dc84c729441e0b428a3d266528a4a18a77940340a52ee6b7d814e9f1d1a28772571df1649ee7611bd492bb72af69722d8d536f72a30911e29d13f49267350685f2010a23887f711b2c30603a40e2bed10b72c4a5f351f9d15428cd43177f420e612341c1d1ad87373668149553f3952f662564a65ad05439a1c53aa5f30b622087f383b0e6d2c83da1c496e330e2b1c5b702ee3f1137eb957a9303ac6ef3fd54c4eb2ea503ee17bb1752648b41ef2561d90a245a6d42ae59b52589c6ef1f53f7288175102075d591187ba52aca9773e9e0296d82592ec4cff12404ce84aa5821e54e64f16c11a7e7903978f4e481731b8595884cc1b48275b63d05d787a35225e400c7e6c09d15bd5326bc4d3258ecb2e534c097a6c3b88206d285c2ca4f8337caa14b2a055853628f18655795d4af670234a8675e82678de6605528c7adf590f6e175bf3da459b7e628b345dbecb1a9e7b0006d96257c5574b3c69a8ef28983864b5fe7ef8f52a4e78120a230154a809b7ff435e87437ff85cd5b44dc04e4728af7f735d0c8d0d0f66d55a6d8061ab98185d96437f314682986629604bd57928de06465d8d49b0e309b4347c0db35a68b0409ba964d3d521762a658591246e3948c39b7bc7594f5859392db223092312eb67454df230c31c28542413232e7faf802dbfcb022a0b7e832c26587a6b3375095b766be1cc5e061e78e00d628c373da6044ae53c1f1d686330bb7361b85ea06c671ac7201fc65ba4ff60d77208f2016de024080e6d56038e6956881e6d3e2603bd6a9dfa07c0176dbfd474d0bd63e1e35195737ab60d2867ba2decd458018c3f4cf50b7009427e233cbd372733336739571a4b5d1969261ef20611c14e4c76c83cf42f7fb19a6af66c2e16693352d6034760085260741e782f63166f0a1107c0f1776d0b0d1ff03458240223d468c5595e88852faa3223fc655e694251e0ed65adb32ca5e679e1fe7b406435e1dd433aac464ade1cfe1473f1ce10170e74b6d7"; // ------------------------------------------------------------------ public interface /// @return true when sig is a valid ML-DSA-65 signature over message m with context ctx, for the key whose /// precomputed data is at dataA / dataB. function verify(bytes calldata m, bytes calldata ctx, bytes calldata sig, address dataA, address dataB) external view returns (bool) { if (sig.length != SIG_LEN || ctx.length > 255) return false; return _verify(m, ctx, sig, dataA, dataB); } function _verify(bytes memory m, bytes memory ctx, bytes memory sig, address dataA, address dataB) private view returns (bool) { bytes memory b = _read(dataB, B_LEN); if (b.length == 0) return false; bytes memory mu = _mu(b, ctx, m); bytes memory w1 = _w1all(sig, _read(dataA, A_LEN), b); if (w1.length == 0) return false; return keccak256(Shake.shake(136, abi.encodePacked(mu, w1), CT_LEN)) == keccak256(_slice(sig, 0, CT_LEN)); } /// Decodes z and h from the signature (empty result when either is malformed), then computes w'1. function _w1all(bytes memory sig, bytes memory a, bytes memory b) private pure returns (bytes memory w1) { if (a.length == 0) return w1; (uint256[][] memory zh, uint256[] memory zt, bool ok) = _zhat(sig); if (!ok) return w1; (uint256[6] memory h, bool hok) = _hints(sig); if (!hok) return w1; return _w1(a, b, zh, zt, h, _slice(sig, 0, CT_LEN)); } /// mu = H(tr || M', 64), M' = 0 || |ctx| || ctx || m function _mu(bytes memory b, bytes memory ctx, bytes memory m) private pure returns (bytes memory) { return Shake.shake(136, abi.encodePacked(_slice(b, 0, 64), uint8(0), uint8(ctx.length), ctx, m), 64); } /// z decoded, norm-checked and moved to the NTT domain function _zhat(bytes memory sig) private pure returns (uint256[][] memory zh, uint256[] memory zt, bool ok) { zt = _zetas(); zh = new uint256[][](L); for (uint256 s = 0; s < L; s++) { uint256[] memory z; (z, ok) = _decodeZ(sig, CT_LEN + 640 * s); if (!ok) return (zh, zt, false); _ntt(z, zt); zh[s] = z; } } /// w'1 = UseHint(h, NTT^-1(Â∘ẑ - ĉ∘t̂1)), encoded 4 bits per coefficient function _w1(bytes memory a, bytes memory b, uint256[][] memory zh, uint256[] memory zt, uint256[6] memory h, bytes memory ct) private pure returns (bytes memory w1) { uint256[] memory c = _sampleInBall(ct); _ntt(c, zt); w1 = new bytes(K * 128); uint256[] memory acc = new uint256[](256); for (uint256 r = 0; r < K; r++) { _row(acc, a, b, zh, c, r); _invntt(acc, zt); _useHintPack(acc, h[r], w1, r); } } /// @notice Recomputes Â, tr and NTT(t1·2^d) from a public key and compares them with the data contracts. /// Expensive (meant for eth_call): it is how anyone audits the data a vault verifies against. function checkExpansion(bytes calldata pk, address dataA, address dataB) external view returns (bool) { if (pk.length != PK_LEN) return false; bytes memory a = _read(dataA, A_LEN); bytes memory b = _read(dataB, B_LEN); if (a.length == 0 || b.length == 0) return false; uint256[] memory zt = _zetas(); for (uint256 r = 0; r < K; r++) for (uint256 s = 0; s < L; s++) { uint256[] memory p = _rejNttPoly(abi.encodePacked(pk[0:32], uint8(s), uint8(r))); for (uint256 i = 0; i < 256; i++) if (_coef(a, (r * L + s) * 256 + i, 0) != p[i]) return false; } if (keccak256(Shake.shake(136, pk, 64)) != keccak256(_slice(b, 0, 64))) return false; for (uint256 r = 0; r < K; r++) { uint256[] memory t = new uint256[](256); for (uint256 g = 0; g < 64; g++) { uint256 v; for (uint256 j = 0; j < 5; j++) v |= uint256(uint8(pk[32 + 320 * r + 5 * g + j])) << (8 * j); for (uint256 j = 0; j < 4; j++) t[4 * g + j] = mulmod((v >> (10 * j)) & 0x3ff, 8192, Q); } _ntt(t, zt); for (uint256 i = 0; i < 256; i++) if (_coef(b, r * 256 + i, 64) != t[i]) return false; } return true; } function algorithm() external pure returns (string memory) { return "ML-DSA-65 (FIPS 204)"; } // ------------------------------------------------------------------ internals function _read(address d, uint256 len) private view returns (bytes memory out) { if (d.code.length != len + 1) return out; out = new bytes(len); assembly ("memory-safe") { extcodecopy(d, add(out, 32), 1, len) } } function _slice(bytes memory b, uint256 from, uint256 len) private pure returns (bytes memory out) { out = new bytes(len); for (uint256 i = 0; i < len; i++) out[i] = b[from + i]; } function _coef(bytes memory buf, uint256 idx, uint256 base) private pure returns (uint256 v) { assembly ("memory-safe") { v := shr(232, mload(add(add(add(buf, 32), base), mul(idx, 3)))) } } function _zetas() private pure returns (uint256[] memory z) { z = new uint256[](256); bytes memory zs = ZETAS; assembly ("memory-safe") { let src := add(zs, 32) let dst := add(z, 32) for { let i := 0 } lt(i, 256) { i := add(i, 1) } { mstore(add(dst, mul(i, 32)), shr(232, mload(add(src, mul(i, 3))))) } } } function _decodeZ(bytes memory sig, uint256 off) private pure returns (uint256[] memory z, bool ok) { z = new uint256[](256); ok = true; assembly ("memory-safe") { let src := add(add(sig, 32), off) let dst := add(z, 32) for { let g := 0 } lt(g, 128) { g := add(g, 1) } { let w := mload(add(src, mul(g, 5))) let v := or(or(or(or(byte(0, w), shl(8, byte(1, w))), shl(16, byte(2, w))), shl(24, byte(3, w))), shl(32, byte(4, w))) for { let j := 0 } lt(j, 2) { j := add(j, 1) } { let c := and(shr(mul(j, 20), v), 0xfffff) if or(iszero(gt(c, 196)), iszero(lt(c, 1048380))) { ok := 0 } let zz := sub(GAMMA1, c) if gt(c, GAMMA1) { zz := sub(Q, sub(c, GAMMA1)) } mstore(add(dst, mul(add(mul(g, 2), j), 32)), zz) } } } } function _hints(bytes memory sig) private pure returns (uint256[6] memory h, bool ok) { bytes memory y = _slice(sig, SIG_LEN - OMEGA - K, OMEGA + K); uint256 index = 0; for (uint256 i = 0; i < K; i++) { uint256 end = uint8(y[OMEGA + i]); if (end < index || end > OMEGA) return (h, false); uint256 first = index; while (index < end) { if (index > first && uint8(y[index - 1]) >= uint8(y[index])) return (h, false); h[i] |= uint256(1) << uint8(y[index]); index++; } } for (uint256 i = index; i < OMEGA; i++) if (uint8(y[i]) != 0) return (h, false); return (h, true); } function _sampleInBall(bytes memory ct) private pure returns (uint256[] memory c) { uint256 outLen = 272; while (true) { bytes memory s = Shake.shake(136, ct, outLen); c = new uint256[](256); uint256 signs; for (uint256 k = 0; k < 8; k++) signs |= uint256(uint8(s[k])) << (8 * k); uint256 pos = 8; bool ok = true; for (uint256 i = 256 - TAU; i < 256 && ok; i++) { uint256 j = 256; while (pos < outLen) { j = uint8(s[pos++]); if (j <= i) break; j = 256; } if (j == 256) { ok = false; break; } c[i] = c[j]; c[j] = (signs & 1) == 1 ? Q - 1 : 1; signs >>= 1; } if (ok) return c; outLen *= 2; } } function _rejNttPoly(bytes memory seed) private pure returns (uint256[] memory p) { uint256 outLen = 840; while (true) { bytes memory s = Shake.shake(168, seed, outLen); p = new uint256[](256); uint256 n = 0; for (uint256 i = 0; i + 3 <= outLen && n < 256; i += 3) { uint256 v = uint256(uint8(s[i])) | (uint256(uint8(s[i + 1])) << 8) | (uint256(uint8(s[i + 2]) & 0x7f) << 16); if (v < Q) p[n++] = v; } if (n == 256) return p; outLen *= 2; } } /// acc = sum_s Â[r][s] ∘ ẑ_s - ĉ ∘ t̂1[r] function _row(uint256[] memory acc, bytes memory a, bytes memory b, uint256[][] memory zh, uint256[] memory c, uint256 r) private pure { uint256 z0; uint256 z1; uint256 z2; uint256 z3; uint256 z4; { uint256[] memory t0 = zh[0]; uint256[] memory t1 = zh[1]; uint256[] memory t2 = zh[2]; uint256[] memory t3 = zh[3]; uint256[] memory t4 = zh[4]; assembly ("memory-safe") { z0 := add(t0, 32) z1 := add(t1, 32) z2 := add(t2, 32) z3 := add(t3, 32) z4 := add(t4, 32) } } assembly ("memory-safe") { let ap := add(add(a, 32), mul(mul(r, 5), 768)) let tp := add(add(add(b, 32), 64), mul(r, 768)) let cp := add(c, 32) let out := add(acc, 32) for { let i := 0 } lt(i, 256) { i := add(i, 1) } { let o := mul(i, 32) let i3 := mul(i, 3) let v := mulmod(shr(232, mload(add(ap, i3))), mload(add(z0, o)), Q) v := addmod(v, mulmod(shr(232, mload(add(add(ap, 768), i3))), mload(add(z1, o)), Q), Q) v := addmod(v, mulmod(shr(232, mload(add(add(ap, 1536), i3))), mload(add(z2, o)), Q), Q) v := addmod(v, mulmod(shr(232, mload(add(add(ap, 2304), i3))), mload(add(z3, o)), Q), Q) v := addmod(v, mulmod(shr(232, mload(add(add(ap, 3072), i3))), mload(add(z4, o)), Q), Q) v := addmod(v, sub(Q, mulmod(mload(add(cp, o)), shr(232, mload(add(tp, i3))), Q)), Q) mstore(add(out, o), v) } } } /// UseHint (Algorithm 40) with gamma2 = (q-1)/32, then w1Encode: 4 bits per coefficient into w1[r*128 ..]. function _useHintPack(uint256[] memory w, uint256 hint, bytes memory w1, uint256 r) private pure { assembly ("memory-safe") { let src := add(w, 32) let dst := add(add(w1, 32), mul(r, 128)) for { let i := 0 } lt(i, 256) { i := add(i, 2) } { let packed := 0 for { let k := 0 } lt(k, 2) { k := add(k, 1) } { let idx := add(i, k) let x := mload(add(src, mul(idx, 32))) let r0 := mod(x, TWO_GAMMA2) let neg := gt(r0, GAMMA2) let diff := add(sub(x, r0), mul(neg, TWO_GAMMA2)) let r1 := 0 let pos := 0 if iszero(eq(diff, sub(Q, 1))) { r1 := div(diff, TWO_GAMMA2) pos := and(iszero(neg), gt(r0, 0)) } if and(shr(idx, hint), 1) { switch pos case 1 { r1 := mod(add(r1, 1), 16) } default { r1 := mod(add(r1, 15), 16) } } packed := or(packed, shl(mul(k, 4), r1)) } mstore8(add(dst, shr(1, i)), packed) } } } function _ntt(uint256[] memory w, uint256[] memory zt) private pure { assembly ("memory-safe") { let wp := add(w, 32) let zp := add(zt, 32) let m := 0 for { let len := 128 } gt(len, 0) { len := shr(1, len) } { let lb := mul(len, 32) for { let start := 0 } lt(start, 256) { start := add(start, shl(1, len)) } { m := add(m, 1) let zeta := mload(add(zp, mul(m, 32))) let endp := add(wp, mul(add(start, len), 32)) for { let a := add(wp, mul(start, 32)) } lt(a, endp) { a := add(a, 32) } { let b := add(a, lb) let t := mulmod(zeta, mload(b), Q) let x := mload(a) mstore(b, addmod(x, sub(Q, t), Q)) mstore(a, addmod(x, t, Q)) } } } } } function _invntt(uint256[] memory w, uint256[] memory zt) private pure { assembly ("memory-safe") { let wp := add(w, 32) let zp := add(zt, 32) let m := 256 for { let len := 1 } lt(len, 256) { len := shl(1, len) } { let lb := mul(len, 32) for { let start := 0 } lt(start, 256) { start := add(start, shl(1, len)) } { m := sub(m, 1) let zeta := sub(Q, mload(add(zp, mul(m, 32)))) let endp := add(wp, mul(add(start, len), 32)) for { let a := add(wp, mul(start, 32)) } lt(a, endp) { a := add(a, 32) } { let b := add(a, lb) let t := mload(a) let u := mload(b) mstore(a, addmod(t, u, Q)) mstore(b, mulmod(zeta, addmod(t, sub(Q, u), Q), Q)) } } } for { let i := 0 } lt(i, 256) { i := add(i, 1) } { let p := add(wp, mul(i, 32)) mstore(p, mulmod(mload(p), N_INV, Q)) } } } }