PQGuard — require PQ authorisation
A guarded operation carries an ML-DSA-87 signature over a digest that binds the domain, chain ID, contract address, signer, operation class, payload, a per-(signer, operation) nonce and a deadline. The contract reads the signer’s key live from the chain registry via pqcKeyStatus (0x…0A02) and verifies the signature via pqcVerify (0x…0A01). The key is never stored in the contract, so rotating it on-chain rotates it for every guarded contract at once.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import {IPQC} from "../pqc/IPQC.sol";
/// @notice PQGuard — the Fix Kit authorisation base contract (SPEC §5.6).
///
/// Authorisation model: a privileged operation executes only when it
/// carries an ML-DSA-87 signature, by the signer's *currently
/// registered* on-chain PQ key, over a digest binding:
/// domain separator, chain ID, this contract, signer,
/// operation class, per-(signer, operation) nonce, deadline, payload.
/// The key is read from the chain registry (via IPQC.pqcKeyStatus) on
/// every call and is never stored in the contract, so an on-chain key
/// rotation (MsgRotatePQCKey) takes effect immediately and stale-key
/// signatures stop working (attack A7 defence).
///
/// Fail-closed: IPQC implementations revert on empty precompile
/// returns (production adapter) and this contract reverts on
/// unregistered keys and invalid signatures — there is no code path
/// that proceeds without a positive verification (attack A8 defence).
/// Separate nonce spaces per (signer, operation class) make a
/// signature for one operation useless for any other, and the nonce
/// makes every signature single-use (attack A6 defence).
abstract contract PQGuard {
IPQC public immutable pqc;
address public pqController;
bytes32 public constant GUARD_DOMAIN = keccak256("QoreGauntlet.PQGuard.v1");
/// @notice Per-(signer, operation class) nonce spaces.
mapping(address => mapping(bytes32 => uint256)) public pqcNonces;
error PQDeadlineExpired();
error PQKeyNotRegistered();
error PQInvalidSignature();
error PQZeroAddress();
event PQControllerRotated(address indexed previous, address indexed next);
constructor(address pqc_, address pqController_) {
if (pqc_ == address(0) || pqController_ == address(0)) revert PQZeroAddress();
pqc = IPQC(pqc_);
pqController = pqController_;
}
/// @notice The exact digest a PQ signature must cover. Public so off-chain
/// signers (and the attack engine, as a control) can reproduce it.
function pqcDigest(address signer, bytes32 opClass, bytes memory payload, uint256 nonce, uint256 deadline)
public
view
returns (bytes32)
{
return keccak256(abi.encode(GUARD_DOMAIN, block.chainid, address(this), signer, opClass, nonce, deadline, payload));
}
/// @notice Authorise one operation for the PQ controller.
function _pqcAuthorize(bytes32 opClass, bytes memory payload, uint256 deadline, bytes memory signature)
internal
returns (bytes32 digest)
{
return _pqcAuthorizeFor(pqController, opClass, payload, deadline, signature);
}
/// @notice Authorise one operation for an arbitrary signer (e.g. a
/// guardian) against that signer's registered PQ key.
function _pqcAuthorizeFor(
address signer,
bytes32 opClass,
bytes memory payload,
uint256 deadline,
bytes memory signature
) internal returns (bytes32 digest) {
if (block.timestamp > deadline) revert PQDeadlineExpired();
(bool registered,, bytes memory pubkey) = pqc.pqcKeyStatus(signer);
if (!registered || pubkey.length == 0) revert PQKeyNotRegistered();
uint256 nonce = pqcNonces[signer][opClass];
digest = pqcDigest(signer, opClass, payload, nonce, deadline);
if (!pqc.pqcVerify(pubkey, signature, abi.encodePacked(digest))) {
revert PQInvalidSignature();
}
pqcNonces[signer][opClass] = nonce + 1;
}
/// @notice Rotate the PQ controller. Only callable from a function that
/// has already authorised the rotation with the current
/// controller's PQ signature.
function _rotatePQController(address next) internal {
if (next == address(0)) revert PQZeroAddress();
address previous = pqController;
pqController = next;
emit PQControllerRotated(previous, next);
}
}
QoreChain adapter — fail closed
Where the precompiles are inactive they return empty data. The production adapter checks return-data length and the ML-DSA-87 sizes (2,592-byte public key, 4,627-byte signature — the chain-canonical size) and reverts on anything short. An inactive precompile stops the call; it never waves it through.
Finding — the 4,595-byte lesson
The first version of this adapter enforced 4,595 bytes — the textbook FIPS 204 signature size — and reverted every genuine signature on qorechain-diana, because the chain’s canonical ML-DSA-87 signature unit is the SDK’s 4,627-byte encoding, and neither 4,595-byte slice of a real signature verifies. The shipped adapter takes the length from the SDK constant (4,627) and enforces exactly that. The full finding is recorded in the deployment notes in the repository.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import {IPQC, IPQCVerifier, IPQCKeyStatus} from "./IPQC.sol";
/// @notice Production adapter: forwards IPQC calls to the real QoreChain
/// precompiles on diana/mainnet and FAILS CLOSED — any unsuccessful
/// call or empty/short return data reverts instead of being treated
/// as a negative or positive answer (SPEC §5.6, §9.6).
/// @dev ML-DSA-87 size checks are enforced here, at the production
/// boundary, so the Fix Kit logic above it stays verifier-agnostic.
/// The lengths are the CHAIN's canonical lengths, taken from the
/// installed qorechain SDK 0.14.0 (`ML_DSA_87_PUBLIC_KEY_LENGTH`
/// 2592, `ML_DSA_87_SIGNATURE_LENGTH` 4627) — NOT the bare FIPS 204
/// signature size (4595): QoreChain's verifiable signature unit is
/// the SDK's 4627-byte encoding, and neither 4595-byte slice of it
/// verifies. The first version of this adapter enforced 4595 and
/// reverted every real signature; the diana testnet campaign
/// caught it (see docs/VERIFICATION.md). Take lengths from the
/// SDK, never from the textbook.
contract QoreChainPQCAdapter is IPQC {
address public constant PQC_VERIFY_PRECOMPILE = 0x0000000000000000000000000000000000000a01;
address public constant PQC_KEY_STATUS_PRECOMPILE = 0x0000000000000000000000000000000000000a02;
uint256 public constant MLDSA87_PUBKEY_LENGTH = 2592;
uint256 public constant MLDSA87_SIGNATURE_LENGTH = 4627;
error PrecompileUnavailable();
error InvalidPubkeyLength();
error InvalidSignatureLength();
function pqcVerify(bytes calldata pubkey, bytes calldata signature, bytes calldata message)
external
view
override
returns (bool valid)
{
if (pubkey.length != MLDSA87_PUBKEY_LENGTH) revert InvalidPubkeyLength();
if (signature.length != MLDSA87_SIGNATURE_LENGTH) revert InvalidSignatureLength();
(bool ok, bytes memory ret) =
PQC_VERIFY_PRECOMPILE.staticcall(abi.encodeCall(IPQCVerifier.pqcVerify, (pubkey, signature, message)));
if (!ok || ret.length < 32) revert PrecompileUnavailable();
return abi.decode(ret, (bool));
}
function pqcKeyStatus(address account)
external
view
override
returns (bool registered, uint8 algorithmId, bytes memory pubkey)
{
(bool ok, bytes memory ret) =
PQC_KEY_STATUS_PRECOMPILE.staticcall(abi.encodeCall(IPQCKeyStatus.pqcKeyStatus, (account)));
// (bool, uint8, bytes) head is 96 bytes plus the dynamic tail offset.
if (!ok || ret.length < 96) revert PrecompileUnavailable();
return abi.decode(ret, (bool, uint8, bytes));
}
}
PQVault — reference implementation
PQVault.sol applies PQGuard to withdraw, owner rotation, guardian rotation and freeze/unfreeze, and adds a second line of defence: instant withdrawals are bounded by a daily budget, and anything above it waits out a timelock. Even a stolen post-quantum key is bounded by that budget until the timelock expires.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import {PQGuard} from "./PQGuard.sol";
/// @notice PQVault — reference vault template built on PQGuard (SPEC §5.6).
/// Every value-moving or authority-changing operation requires an
/// ML-DSA-87 signature verified on-chain; `msg.sender` alone can do
/// nothing except deposit. As a second line of defence (against even
/// a stolen PQ key), instant withdrawals are bounded by a daily
/// budget and larger amounts must pass a timelock queue.
contract PQVault is PQGuard {
bytes32 public constant OP_WITHDRAW = keccak256("WITHDRAW");
bytes32 public constant OP_QUEUE_WITHDRAWAL = keccak256("QUEUE_WITHDRAWAL");
bytes32 public constant OP_EXECUTE_WITHDRAWAL = keccak256("EXECUTE_WITHDRAWAL");
bytes32 public constant OP_SET_OWNER = keccak256("SET_OWNER");
bytes32 public constant OP_SET_GUARDIAN = keccak256("SET_GUARDIAN");
bytes32 public constant OP_FREEZE = keccak256("FREEZE");
bytes32 public constant OP_UNFREEZE = keccak256("UNFREEZE");
address public owner;
address public guardian;
bool public frozen;
uint256 public immutable dailyBudget;
uint256 public immutable timelockDelay;
mapping(uint256 => uint256) public instantSpentOnDay;
uint256 public nextWithdrawalId;
mapping(uint256 => QueuedWithdrawal) public queuedWithdrawals;
struct QueuedWithdrawal {
address to;
uint256 amount;
uint256 eta;
bool executed;
bool cancelled;
}
error VaultFrozen();
error ExceedsInstantBudget();
error WithdrawalNotReady();
error WithdrawalAlreadyFinal();
error ZeroAmount();
event Deposited(address indexed from, uint256 amount);
event Withdrawn(address indexed to, uint256 amount);
event WithdrawalQueued(uint256 indexed id, address indexed to, uint256 amount, uint256 eta);
event WithdrawalExecuted(uint256 indexed id, address indexed to, uint256 amount);
event OwnerRotated(address indexed previous, address indexed next);
event GuardianRotated(address indexed previous, address indexed next);
event Frozen(address indexed by);
event Unfrozen(address indexed by);
constructor(
address pqc_,
address owner_,
address guardian_,
uint256 dailyBudget_,
uint256 timelockDelay_
) PQGuard(pqc_, owner_) {
if (owner_ == address(0)) revert PQZeroAddress();
owner = owner_;
guardian = guardian_;
dailyBudget = dailyBudget_;
timelockDelay = timelockDelay_;
}
receive() external payable {}
function deposit() external payable {
if (msg.value == 0) revert ZeroAmount();
emit Deposited(msg.sender, msg.value);
}
function instantRemainingToday() public view returns (uint256) {
uint256 spent = instantSpentOnDay[block.timestamp / 1 days];
if (spent >= dailyBudget) return 0;
return dailyBudget - spent;
}
/// @notice Instant withdrawal, bounded by the daily budget. PQ-signed.
function withdraw(address to, uint256 amount, uint256 deadline, bytes calldata signature) external {
if (frozen) revert VaultFrozen();
if (amount == 0) revert ZeroAmount();
if (amount > instantRemainingToday()) revert ExceedsInstantBudget();
_pqcAuthorize(OP_WITHDRAW, abi.encode(to, amount), deadline, signature);
instantSpentOnDay[block.timestamp / 1 days] += amount;
(bool ok,) = payable(to).call{value: amount}("");
require(ok, "transfer failed");
emit Withdrawn(to, amount);
}
/// @notice Queue a withdrawal above the instant budget. PQ-signed.
function queueWithdrawal(address to, uint256 amount, uint256 deadline, bytes calldata signature)
external
returns (uint256 id)
{
if (frozen) revert VaultFrozen();
if (amount == 0) revert ZeroAmount();
_pqcAuthorize(OP_QUEUE_WITHDRAWAL, abi.encode(to, amount), deadline, signature);
id = nextWithdrawalId++;
queuedWithdrawals[id] =
QueuedWithdrawal({to: to, amount: amount, eta: block.timestamp + timelockDelay, executed: false, cancelled: false});
emit WithdrawalQueued(id, to, amount, block.timestamp + timelockDelay);
}
/// @notice Execute a queued withdrawal after its timelock. PQ-signed.
function executeWithdrawal(uint256 id, uint256 deadline, bytes calldata signature) external {
if (frozen) revert VaultFrozen();
QueuedWithdrawal storage w = queuedWithdrawals[id];
if (w.executed || w.cancelled) revert WithdrawalAlreadyFinal();
if (block.timestamp < w.eta) revert WithdrawalNotReady();
_pqcAuthorize(OP_EXECUTE_WITHDRAWAL, abi.encode(id, w.to, w.amount), deadline, signature);
w.executed = true;
(bool ok,) = payable(w.to).call{value: w.amount}("");
require(ok, "transfer failed");
emit WithdrawalExecuted(id, w.to, w.amount);
}
/// @notice Rotate the owner (and with it the PQ controller). PQ-signed
/// by the current controller.
function setOwner(address next, uint256 deadline, bytes calldata signature) external {
_pqcAuthorize(OP_SET_OWNER, abi.encode(next), deadline, signature);
address previous = owner;
owner = next;
_rotatePQController(next);
emit OwnerRotated(previous, next);
}
/// @notice Rotate the guardian. PQ-signed by the controller.
function setGuardian(address next, uint256 deadline, bytes calldata signature) external {
_pqcAuthorize(OP_SET_GUARDIAN, abi.encode(next), deadline, signature);
address previous = guardian;
guardian = next;
emit GuardianRotated(previous, next);
}
/// @notice Freeze the vault. PQ-signed by the guardian (or controller).
function freeze(uint256 deadline, bytes calldata signature) external {
address signer = guardian != address(0) ? guardian : pqController;
_pqcAuthorizeFor(signer, OP_FREEZE, abi.encode(address(this)), deadline, signature);
frozen = true;
emit Frozen(signer);
}
/// @notice Unfreeze the vault. PQ-signed by the controller only.
function unfreeze(uint256 deadline, bytes calldata signature) external {
_pqcAuthorize(OP_UNFREEZE, abi.encode(address(this)), deadline, signature);
frozen = false;
emit Unfrozen(pqController);
}
}