// HashSig, client side: key trees, signing and the vault-operation digest. Mirrors contracts/HashSig.sol exactly. // Only keccak256 is used, so the same code runs in Node and in the browser. Secrets never leave this module's caller. import { keccak_256 } from "@noble/hashes/sha3"; export const CHAINS = 67; const TOP = 15; // w = 16, chain steps 0..15 const enc = new TextEncoder(); export const hex = (b) => "0x" + Array.from(b, (x) => x.toString(16).padStart(2, "0")).join(""); export const unhex = (s) => { s = s.startsWith("0x") ? s.slice(2) : s; const b = new Uint8Array(s.length / 2); for (let i = 0; i < b.length; i++) b[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16); return b; }; const u256 = (n, out = new Uint8Array(32), off = 0) => { let x = BigInt(n); for (let i = 31; i >= 0; i--) { out[off + i] = Number(x & 255n); x >>= 8n; } return out; }; const H = (...parts) => { const len = parts.reduce((a, p) => a + p.length, 0), b = new Uint8Array(len); let o = 0; for (const p of parts) { b.set(p, o); o += p.length; } return keccak_256(b); }; /** Digits signed for a 32-byte digest: 64 nibbles (most significant first) and a 3-nibble checksum. */ export function digits(digest) { const d = []; let csum = 0; for (let i = 0; i < 32; i++) { d.push(digest[i] >> 4, digest[i] & 15); } for (const a of d) csum += TOP - a; d.push((csum >> 8) & 15, (csum >> 4) & 15, csum & 15); return d; } // one chain step: keccak(pubSeed | tweak(leaf, chain, step) | x), using a reused 96-byte buffer function stepper(pubSeed) { const buf = new Uint8Array(96); buf.set(pubSeed, 0); return (leaf, i, j, x) => { u256((BigInt(leaf) << 64n) | (BigInt(i) << 16n) | BigInt(j), buf, 32); buf.set(x, 64); return keccak_256(buf); }; } /** Derive one key tree from a 32-byte master seed. treeNo selects the tree (a new one per rotation). */ export function deriveTree(seed, treeNo, height) { const pubSeed = H(enc.encode("pqvault:pub"), seed, u256(treeNo)); const skSeed = H(enc.encode("pqvault:sk"), seed, u256(treeNo)); const step = stepper(pubSeed); const n = 1 << height, leaves = new Array(n); for (let l = 0; l < n; l++) { const pre = new Uint8Array(64 + CHAINS * 32); pre.set(pubSeed, 0); u256(l, pre, 32); for (let i = 0; i < CHAINS; i++) { let x = H(skSeed, u256(l), u256(i)); for (let j = 0; j < TOP; j++) x = step(l, i, j, x); pre.set(x, 64 + i * 32); } leaves[l] = keccak_256(pre); } const levels = [leaves]; for (let h = 0; h < height; h++) { const prev = levels[h], next = []; for (let p = 0; p < prev.length / 2; p++) { const tweak = (1n << 255n) | (BigInt(h + 1) << 32n) | BigInt(p); next.push(H(pubSeed, u256(tweak), prev[2 * p], prev[2 * p + 1])); } levels.push(next); } return { treeNo, height, pubSeed, skSeed, levels, root: levels[height][0] }; } /** Sign a digest with one leaf. The caller must never use the same leaf twice. */ export function sign(tree, leaf, digest) { if (leaf < 0 || leaf >= 1 << tree.height) throw new Error("leaf out of range"); const step = stepper(tree.pubSeed), d = digits(digest), sig = []; for (let i = 0; i < CHAINS; i++) { let x = H(tree.skSeed, u256(leaf), u256(i)); for (let j = 0; j < d[i]; j++) x = step(leaf, i, j, x); sig.push(hex(x)); } const path = []; for (let h = 0, idx = leaf; h < tree.height; h++, idx >>= 1) path.push(hex(tree.levels[h][idx ^ 1])); return { sig, path, leaf }; } /** Recompute the root a signature commits to (what the contract does). */ export function rootOf(pubSeed, leaf, digest, sig, path) { const step = stepper(pubSeed), d = digits(digest); const pre = new Uint8Array(64 + CHAINS * 32); pre.set(pubSeed, 0); u256(leaf, pre, 32); for (let i = 0; i < CHAINS; i++) { let x = unhex(sig[i]); for (let j = d[i]; j < TOP; j++) x = step(leaf, i, j, x); pre.set(x, 64 + i * 32); } let node = keccak_256(pre), idx = leaf; path.forEach((sib, h) => { const tweak = (1n << 255n) | (BigInt(h + 1) << 32n) | BigInt(idx >> 1); node = idx & 1 ? H(pubSeed, u256(tweak), unhex(sib), node) : H(pubSeed, u256(tweak), node, unhex(sib)); idx >>= 1; }); return node; } /** The digest QuantumVault.opDigest computes. `abi` is an ethers-compatible AbiCoder. */ export function opDigest(abi, { chainId, vault, policyVersion, leaf, deadline, calls }) { const k = (b) => keccak_256(unhex(b)); const callsHash = k(abi.encode(["tuple(address target,uint256 value,bytes data)[]"], [calls.map((c) => [c.target, c.value, c.data])])); return k(abi.encode( ["bytes32", "uint256", "address", "uint32", "uint32", "uint64", "bytes32"], [hex(keccak_256(enc.encode("PQVAULT_OP_V1"))), chainId, vault, policyVersion, leaf, deadline, hex(callsHash)], )); }