Skip to content
Verified contractExactMIT

QairosVault

Address0xd207cf263bf288add199ca6777b413a68494961a
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,162,612
Creation transaction
0x3e9b82a3cd…c5211f564a

src/QairosVault.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {IQairosOracle} from "./interfaces/IQairosOracle.sol";
import {QairosGuard} from "./QairosGuard.sol";
import {QairosArk} from "./QairosArk.sol";

/// @title QairosVault
/// @notice Reference integration of QairosGuard + QairosArk. Normal ECDSA withdrawals work while the breaker is
///         open. Once tripped, funds move only through Ark recovery: a two-step commit-reveal of the pre-Moment
///         post-quantum preimage. A quantum attacker who derived the ECDSA key still cannot know that preimage.
contract QairosVault is QairosGuard {
    QairosArk public immutable ark;
    mapping(address => uint256) public balanceOf;

    /// @dev recovery commitment => block number
    mapping(bytes32 => uint256) public recoveryCommitBlock;

    uint256 private _lock = 1;

    event Deposited(address indexed account, uint256 amount);
    event Withdrawn(address indexed account, uint256 amount);
    event RecoveryCommitted(bytes32 commitment);
    event Recovered(address indexed account, address indexed to, uint256 amount);

    error Reentrancy();
    error Insufficient();
    error NoRecoveryCommit();
    error RecoveryTooEarly();
    error ArkMismatch();
    error NotPreMoment();
    error TransferFailed();

    modifier nonReentrant() {
        if (_lock != 1) revert Reentrancy();
        _lock = 2;
        _;
        _lock = 1;
    }

    constructor(IQairosOracle oracle_, QairosArk ark_, uint8 tripLevel_) QairosGuard(oracle_, tripLevel_) {
        ark = ark_;
    }

    function deposit() external payable {
        balanceOf[msg.sender] += msg.value;
        emit Deposited(msg.sender, msg.value);
    }

    function withdraw(uint256 amount) external quantumSafe nonReentrant {
        if (balanceOf[msg.sender] < amount) revert Insufficient();
        balanceOf[msg.sender] -= amount;
        _send(msg.sender, amount);
        emit Withdrawn(msg.sender, amount);
    }

    function recoveryCommitment(address account, address to, bytes calldata pqPublicKey, bytes32 salt)
        public
        pure
        returns (bytes32)
    {
        return keccak256(abi.encode(account, to, keccak256(pqPublicKey), salt));
    }

    /// @notice Step 1 of recovery. Anyone may submit; the commitment hides the preimage and destination.
    function commitRecovery(bytes32 commitment) external onlyWhenTripped {
        if (recoveryCommitBlock[commitment] == 0) recoveryCommitBlock[commitment] = block.number;
        emit RecoveryCommitted(commitment);
    }

    /// @notice Step 2 of recovery, in a later block. Moves the whole balance of `account` to `to`.
    function recover(address account, address to, bytes calldata pqPublicKey, bytes32 salt)
        external
        onlyWhenTripped
        nonReentrant
    {
        bytes32 c = recoveryCommitment(account, to, pqPublicKey, salt);
        uint256 cb = recoveryCommitBlock[c];
        if (cb == 0) revert NoRecoveryCommit();
        if (cb >= block.number) revert RecoveryTooEarly();
        if (!ark.verify(account, pqPublicKey, salt)) revert ArkMismatch();
        if (!ark.isPreMoment(account)) revert NotPreMoment();

        uint256 amount = balanceOf[account];
        balanceOf[account] = 0;
        delete recoveryCommitBlock[c];
        _send(to, amount);
        emit Recovered(account, to, amount);
    }

    function _send(address to, uint256 amount) private {
        (bool ok,) = to.call{value: amount}("");
        if (!ok) revert TransferFailed();
    }
}

src/QairosArk.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {IQairosOracle} from "./interfaces/IQairosOracle.sol";

/// @title QairosArk
/// @notice Pre-registration of post-quantum ownership. An account stores keccak256(pqPublicKey || salt || account)
///         today. If ECDSA falls, revealing a preimage that was committed before the first armed QairosCanary break
///         proves ownership in a way a quantum attacker cannot forge after the fact.
contract QairosArk {
    struct Entry {
        bytes32 commitment;
        uint64 committedAt;
        uint64 blockNumber;
    }

    IQairosOracle public immutable oracle;
    mapping(address => Entry) public entries;
    uint256 public totalEntries;

    event Committed(address indexed account, bytes32 commitment, uint64 timestamp, bool preQairosMoment);

    error MomentPassed();

    constructor(IQairosOracle oracle_) {
        oracle = oracle_;
    }

    /// @notice Store or rotate a commitment. Rotation is closed once the Qairos Moment is recorded by the oracle,
    ///         so a quantum attacker holding the ECDSA key cannot overwrite a pre-Moment entry.
    function commit(bytes32 commitment) external {
        bool pre = oracle.firstBreakTime() == 0;
        Entry storage e = entries[msg.sender];
        if (!pre && e.commitment != bytes32(0)) revert MomentPassed();
        if (e.commitment == bytes32(0)) totalEntries += 1;
        entries[msg.sender] = Entry(commitment, uint64(block.timestamp), uint64(block.number));
        emit Committed(msg.sender, commitment, uint64(block.timestamp), pre);
    }

    function commitmentFor(bytes calldata pqPublicKey, bytes32 salt, address account) public pure returns (bytes32) {
        return keccak256(abi.encodePacked(pqPublicKey, salt, account));
    }

    /// @notice True when the preimage matches the stored commitment for `account`.
    function verify(address account, bytes calldata pqPublicKey, bytes32 salt) public view returns (bool) {
        bytes32 c = entries[account].commitment;
        return c != bytes32(0) && c == commitmentFor(pqPublicKey, salt, account);
    }

    /// @notice True when the account's entry predates the first armed canary break (or no break has happened).
    function isPreMoment(address account) public view returns (bool) {
        Entry memory e = entries[account];
        if (e.commitment == bytes32(0)) return false;
        uint64 q = oracle.firstBreakTime();
        return q == 0 || e.committedAt < q;
    }
}

src/QairosGuard.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {IQairosOracle} from "./interfaces/IQairosOracle.sol";

/// @title QairosGuard (Qairos Breaker)
/// @notice Drop-in circuit breaker. Inherit it, set a threshold, and tag functions with `quantumSafe`.
///         Once QairosOracle.threatLevel() reaches the threshold, tagged functions stop accepting ECDSA-only callers.
abstract contract QairosGuard {
    IQairosOracle public immutable qOracle;
    uint8 public immutable tripLevel;

    error QuantumBreakerTripped(uint8 level, uint8 threshold);

    constructor(IQairosOracle oracle_, uint8 tripLevel_) {
        require(tripLevel_ >= 1, "QairosGuard: threshold >= 1");
        qOracle = oracle_;
        tripLevel = tripLevel_;
    }

    function breakerTripped() public view returns (bool) {
        return qOracle.threatLevel() >= tripLevel;
    }

    modifier quantumSafe() {
        uint8 l = qOracle.threatLevel();
        if (l >= tripLevel) revert QuantumBreakerTripped(l, tripLevel);
        _;
    }

    modifier onlyWhenTripped() {
        require(breakerTripped(), "QairosGuard: not tripped");
        _;
    }
}

src/interfaces/IQairosOracle.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

interface IQairosOracle {
    event ThreatRaised(uint8 level, uint16 bits, address solver);

    /// @notice Called by QairosCanary when a rung is broken.
    function report(uint16 bits, bool armed, address solver) external;

    /// @notice 0 = nominal. +1 per broken armed rung (128-bit and up). Max 5.
    function threatLevel() external view returns (uint8);

    function rungBroken(uint16 bits) external view returns (bool);

    function lastBreak() external view returns (uint16 bits, uint64 ts);

    /// @notice Timestamp of the first armed break, or 0 if none. Ark commitments before this time are pre-Moment.
    function firstBreakTime() external view returns (uint64);
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "bytecodeHash": "ipfs"
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.