Verified contractExactMIT
QdayVault
Address0xa414b5907633b951746dded7be86edfc7fb8dc89
- 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,350,297
- Creation transaction
- 0x8e4cb3e685…e2df626061
2 files
- QdayVault.solmain17.88 KB
- QdayTypes.sol2.75 KB
QdayVault.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
// QDAY PREPRINT 3 . A vault opened only by hash-based signatures
// web https://quantumday.fun . x https://x.com/qday_fun
//
// Result. Holds QDAY coins and ETH for vault keys. A vault key is an SLH-DSA-SHA2-128s public key (NIST
// FIPS 205, 32 bytes: PK.seed then PK.root). Its security rests on SHA-256 alone, so Shor's
// algorithm, which breaks the elliptic-curve keys of ordinary wallets, gives no way in.
// Deposits. Anyone may put coins or ETH into any key, as with sending to an address. buyInto buys on the curve
// or in the pool and lands the coins in a key; deposit moves coins from the caller's wallet (until
// Q-Day); addEth adds ETH.
// Withdrawals. Only an Instruction signed by the key: sell (curve or pool), send to another key, send to an
// address, buy with the key's ETH, or withdraw ETH. Each one names the key's next nonce and a
// deadline, and may tip whoever submits it, so a key never needs a wallet of its own. Recipients may
// not be address zero or this vault, so nothing is burned or stranded by mistake. The signed
// message is the 32-byte instruction digest under the context string "QDAY".
// Yield. The vault share of a coin's fee is credited to a key's ETH whenever that key's holding changes.
// Verification. FIPS 205 sections 5 to 10 for SLH-DSA-SHA2-128s (n 16, h 63, d 7, h' 9, a 12, k 14, w 16), with
// SHA-256 through the 0x02 precompile. It agrees with NIST's ACVP sigVer vectors (qday/spike).
// Measured. About 1M gas for a signed instruction: a 7,856-byte signature and about 2,140 SHA-256 calls.
// Limitations. New code, checked against NIST's vectors, not audited. A key's 24-word backup is the only way in.
import {PoolKey, SwapParams, IPoolManager, IUnlockCallback, IQdayPad, IQdayCoin} from "./QdayTypes.sol";
contract QdayVault is IUnlockCallback {
address internal constant POOL_MANAGER = 0x000000000004444c5dc75cB358380D2e3dE08A90;
uint160 internal constant MIN_SQRT = 4295128740;
uint160 internal constant MAX_SQRT = 1461446703485210103287273052203988822378723970341;
uint256 internal constant ACC = 1e27;
uint256 internal constant SIG_BYTES = 7856;
bytes32 public constant INSTRUCTION_TYPE = keccak256(
"Instruction(uint8 kind,address coin,uint256 amount,uint256 limit,address to,bytes32 toKey,uint256 tip,uint64 nonce,uint64 deadline)"
);
uint8 internal constant SELL = 1;
uint8 internal constant SEND_KEY = 2;
uint8 internal constant SEND_OUT = 3;
uint8 internal constant BUY = 4;
uint8 internal constant WITHDRAW = 5;
address public immutable PAD;
struct KeyFund {
uint128 eth;
uint64 nonce;
}
struct Hold {
uint128 bal;
uint128 accLast;
}
struct Instruction {
uint8 kind;
address coin;
uint256 amount;
uint256 limit;
address to;
bytes32 toKey;
uint256 tip;
uint64 nonce;
uint64 deadline;
}
mapping(bytes32 => KeyFund) public keyFunds;
mapping(bytes32 => mapping(address => Hold)) public holds;
event Vaulted(bytes32 indexed key, address indexed coin, uint256 amount, address indexed by);
event EthAdded(bytes32 indexed key, uint256 amount, address indexed by);
event Performed(
bytes32 indexed key, uint64 nonce, uint8 kind, address indexed coin, uint256 amount, uint256 result, bytes32 toKey, address to
);
event YieldCredited(bytes32 indexed key, address indexed coin, uint256 amount);
error NotQdayCoin();
error StaleNonce();
error Expired();
error BadSignature();
error UnknownInstruction();
error Slippage();
error NotPoolManager();
error Occupied();
error PayFailed();
error RecipientRefused();
error ShortOfEth();
error TooLarge();
constructor(address pad_) {
PAD = pad_;
}
receive() external payable {}
modifier exclusive() {
bool busy;
assembly {
busy := tload(0)
}
if (busy) revert Occupied();
assembly {
tstore(0, 1)
}
_;
assembly {
tstore(0, 0)
}
}
// ================================================================ deposits
function buyInto(address coin, bytes32 key, uint256 minOut) external payable exclusive returns (uint256 out) {
out = _spend(key, coin, msg.value, minOut);
_settle(key, coin, int256(out));
emit Vaulted(key, coin, out, msg.sender);
}
function deposit(address coin, bytes32 key, uint256 amount) external exclusive {
if (IQdayPad(PAD).coinPhase(coin) == 0) revert NotQdayCoin();
IQdayCoin(coin).drawFrom(msg.sender, amount);
_settle(key, coin, int256(amount));
emit Vaulted(key, coin, amount, msg.sender);
}
function addEth(bytes32 key) external payable {
keyFunds[key].eth += uint128(msg.value);
emit EthAdded(key, msg.value, msg.sender);
}
// ================================================================ signed instructions
function carryOut(bytes32 key, Instruction calldata a, bytes calldata sig) external exclusive returns (uint256 result) {
KeyFund storage k = keyFunds[key];
if (a.nonce != k.nonce) revert StaleNonce();
if (block.timestamp > a.deadline) revert Expired();
// cheap refusals first, so a doomed instruction never pays for its signature check
if (a.amount > type(uint128).max || a.tip > type(uint128).max) revert TooLarge();
if (a.kind == WITHDRAW || a.kind == SEND_OUT) {
if (a.to == address(0) || a.to == address(this)) revert RecipientRefused();
} else if (a.to == address(this)) {
revert RecipientRefused();
}
if (!(a.kind == SELL && a.to == address(0))) {
uint256 need = a.tip + ((a.kind == BUY || a.kind == WITHDRAW) ? a.amount : 0);
if (k.eth < need) revert ShortOfEth();
}
if (!_slhVerify(key, instructionDigest(key, a), sig)) revert BadSignature();
k.nonce = a.nonce + 1;
address coin = a.coin;
if (a.kind == SELL) {
_settle(key, coin, -int256(a.amount));
if (IQdayPad(PAD).promoted(coin)) result = _swap(coin, false, a.amount, a.limit);
else result = IQdayPad(PAD).sell(coin, a.amount, a.limit, address(this));
if (a.to == address(0)) k.eth += uint128(result);
} else if (a.kind == SEND_KEY) {
_settle(key, coin, -int256(a.amount));
_settle(a.toKey, coin, int256(a.amount));
result = a.amount;
} else if (a.kind == SEND_OUT) {
_settle(key, coin, -int256(a.amount));
IQdayCoin(coin).transfer(a.to, a.amount);
result = a.amount;
} else if (a.kind == BUY) {
k.eth -= uint128(a.amount);
result = _spend(key, coin, a.amount, a.limit);
_settle(key, coin, int256(result));
} else if (a.kind == WITHDRAW) {
k.eth -= uint128(a.amount);
result = a.amount;
} else {
revert UnknownInstruction();
}
if (a.tip != 0) {
if (k.eth < a.tip) revert ShortOfEth();
k.eth -= uint128(a.tip);
}
emit Performed(key, a.nonce, a.kind, coin, a.amount, result, a.toKey, a.to);
if (a.kind == WITHDRAW || (a.kind == SELL && a.to != address(0))) _send(a.to, result);
if (a.tip != 0) _send(msg.sender, a.tip);
}
function instructionDigest(bytes32 key, Instruction calldata a) public view returns (bytes32) {
return keccak256(
abi.encode(
INSTRUCTION_TYPE, block.chainid, address(this), key, a.kind, a.coin, a.amount, a.limit, a.to, a.toKey,
a.tip, a.nonce, a.deadline
)
);
}
function earned(bytes32 key, address coin) external view returns (uint256) {
Hold memory h = holds[key][coin];
return (uint256(h.bal) * (IQdayPad(PAD).accOf(coin) - h.accLast)) / ACC;
}
// ================================================================ the ledger
/// Credits the key's yield since its last change, then applies `delta` to its holding and to the pad's vault
/// total. A negative delta larger than the holding reverts.
function _settle(bytes32 key, address coin, int256 delta) private {
uint256 acc = IQdayPad(PAD).noteVault(coin, delta);
Hold memory h = holds[key][coin];
uint256 due = (uint256(h.bal) * (acc - h.accLast)) / ACC;
h.accLast = uint128(acc);
if (delta >= 0) h.bal += uint128(uint256(delta));
else h.bal -= uint128(uint256(-delta));
holds[key][coin] = h;
if (due != 0) {
keyFunds[key].eth += uint128(due);
IQdayPad(PAD).releaseYield(due);
emit YieldCredited(key, coin, due);
}
}
/// Spends `value` wei on `coin` and returns the coins bought, now held here. On the curve, the ETH the last step
/// did not need comes back and is credited to the key.
function _spend(bytes32 key, address coin, uint256 value, uint256 minOut) private returns (uint256 out) {
uint8 phase = IQdayPad(PAD).coinPhase(coin);
if (phase == 1) {
uint256 before = address(this).balance - value;
out = IQdayPad(PAD).buy{value: value}(coin, minOut, address(this));
uint256 back = address(this).balance - before;
if (back != 0) keyFunds[key].eth += uint128(back);
} else if (phase == 2) {
out = _swap(coin, true, value, minOut);
} else {
revert NotQdayCoin();
}
}
function _send(address to, uint256 amount) private {
(bool ok,) = to.call{value: amount}("");
if (!ok) revert PayFailed();
}
// ================================================================ the pool
function _swap(address coin, bool isBuy, uint256 amountIn, uint256 minOut) private returns (uint256 out) {
out = abi.decode(IPoolManager(POOL_MANAGER).unlock(abi.encode(coin, isBuy, amountIn, minOut)), (uint256));
}
function unlockCallback(bytes calldata data) external returns (bytes memory) {
if (msg.sender != POOL_MANAGER) revert NotPoolManager();
(address coin, bool isBuy, uint256 amountIn, uint256 minOut) = abi.decode(data, (address, bool, uint256, uint256));
PoolKey memory pk = PoolKey(address(0), coin, 0, 200, PAD);
int256 d = IPoolManager(POOL_MANAGER).swap(
pk, SwapParams(isBuy, -int256(amountIn), isBuy ? MIN_SQRT : MAX_SQRT), ""
);
int256 eth = d >> 128;
int256 coins = int256(int128(d));
uint256 out;
if (isBuy) {
IPoolManager(POOL_MANAGER).settle{value: uint256(-eth)}();
out = uint256(coins);
if (out < minOut) revert Slippage();
IPoolManager(POOL_MANAGER).take(coin, address(this), out);
} else {
IPoolManager(POOL_MANAGER).sync(coin);
IQdayCoin(coin).transfer(POOL_MANAGER, uint256(-coins));
IPoolManager(POOL_MANAGER).settle();
out = uint256(eth);
if (out < minOut) revert Slippage();
IPoolManager(POOL_MANAGER).take(address(0), address(this), out);
}
return abi.encode(out);
}
// ================================================================ SLH-DSA-SHA2-128s verification (FIPS 205)
/// True only for a valid signature under `key` on the 32-byte message `m` with context "QDAY".
function _slhVerify(bytes32 key, bytes32 m, bytes calldata sig) private view returns (bool ok) {
if (sig.length != SIG_BYTES) return false;
assembly ("memory-safe") {
// Frames sit past the free memory pointer and are dropped on return. A frame is
// PK.seed || 0^48 || ADRSc (22 bytes) || data; the ADRSc word written at +64 is layer(1) tree(8)
// type(1) keypair(4) chain-or-height(4) hash-or-index(4), and data follows at +86.
let A := mload(0x40)
let B := add(A, 0x100)
let P := add(A, 0x400)
let HI := not(0xffffffffffffffffffffffffffffffff)
let seed := and(key, HI)
let root := shl(128, key)
mstore(A, seed)
mstore(add(A, 32), 0)
mstore(B, seed)
mstore(add(B, 32), 0)
let s := sig.offset
let good := 1
// H_msg = MGF1-SHA-256(R || PK.seed || SHA-256(R || PK.seed || PK.root || 0x00 0x04 "QDAY" || m), 30)
let R := and(calldataload(s), HI)
mstore(P, R)
mstore(add(P, 16), seed)
mstore(add(P, 32), root)
mstore(add(P, 48), shl(208, 0x000451444159)) // 0x00, 0x04, "QDAY"
mstore(add(P, 54), m)
good := and(good, staticcall(gas(), 2, P, 86, 0, 32))
let inner := mload(0)
mstore(P, R)
mstore(add(P, 16), seed)
mstore(add(P, 32), inner)
mstore(add(P, 64), 0)
good := and(good, staticcall(gas(), 2, P, 68, 0, 32))
let dg := mload(0)
let tree := and(shr(32, dg), 0x3fffffffffffff) // digest bytes 21..28, mod 2^54
let leaf := and(shr(16, dg), 0x1ff) // digest bytes 28..30, mod 2^9
// FORS: 14 trees of height 12, roots gathered in frame B behind the FORS_ROOTS address
let fbase := or(shl(184, tree), or(shl(176, 3), shl(144, leaf)))
mstore(add(B, 64), or(shl(184, tree), or(shl(176, 4), shl(144, leaf))))
let sp := add(s, 16)
for { let i := 0 } lt(i, 14) { i := add(i, 1) } {
let index := add(shl(12, i), and(shr(sub(244, mul(12, i)), dg), 0xfff))
mstore(add(A, 64), or(fbase, shl(80, index)))
mstore(add(A, 86), calldataload(sp))
good := and(good, staticcall(gas(), 2, A, 102, 0, 32))
let node := and(mload(0), HI)
sp := add(sp, 16)
for { let j := 1 } lt(j, 13) { j := add(j, 1) } {
let auth := and(calldataload(sp), HI)
let side := and(index, 1)
index := shr(1, index)
mstore(add(A, 64), or(fbase, or(shl(112, j), shl(80, index))))
switch side
case 0 { mstore(add(A, 86), or(node, shr(128, auth))) }
default { mstore(add(A, 86), or(auth, shr(128, node))) }
good := and(good, staticcall(gas(), 2, A, 118, 0, 32))
node := and(mload(0), HI)
sp := add(sp, 16)
}
mstore(add(add(B, 86), shl(4, i)), node)
}
good := and(good, staticcall(gas(), 2, B, 310, 0, 32))
let msgNode := and(mload(0), HI)
// hypertree: 7 XMSS layers of height 9 over WOTS+ (w 16, len 35)
for { let layer := 0 } lt(layer, 7) { layer := add(layer, 1) } {
if layer {
leaf := and(tree, 0x1ff)
tree := shr(9, tree)
}
let wbase := or(shl(248, layer), or(shl(184, tree), shl(144, leaf)))
mstore(add(B, 64), or(wbase, shl(176, 1)))
let csum := 0
for { let c := 0 } lt(c, 32) { c := add(c, 1) } {
csum := add(csum, sub(15, and(shr(sub(252, shl(2, c)), msgNode), 0xf)))
}
csum := shl(4, csum)
for { let c := 0 } lt(c, 35) { c := add(c, 1) } {
let dgt
switch lt(c, 32)
case 1 { dgt := and(shr(sub(252, shl(2, c)), msgNode), 0xf) }
default { dgt := and(shr(sub(12, shl(2, sub(c, 32))), csum), 0xf) }
let v := and(calldataload(sp), HI)
sp := add(sp, 16)
if lt(dgt, 15) {
mstore(add(A, 64), or(wbase, shl(112, c)))
for { let h := dgt } lt(h, 15) { h := add(h, 1) } {
// the low half of v is ignored: it falls past the 102 bytes hashed
mstore(add(A, 82), or(shl(224, h), shr(32, v)))
good := and(good, staticcall(gas(), 2, A, 102, 0, 32))
v := mload(0)
}
v := and(v, HI)
}
mstore(add(add(B, 86), shl(4, c)), v)
}
good := and(good, staticcall(gas(), 2, B, 646, 0, 32))
let node := and(mload(0), HI)
let index := leaf
let tbase := or(shl(248, layer), or(shl(184, tree), shl(176, 2)))
for { let q := 1 } lt(q, 10) { q := add(q, 1) } {
let auth := and(calldataload(sp), HI)
let side := and(index, 1)
index := shr(1, index)
mstore(add(A, 64), or(tbase, or(shl(112, q), shl(80, index))))
switch side
case 0 { mstore(add(A, 86), or(node, shr(128, auth))) }
default { mstore(add(A, 86), or(auth, shr(128, node))) }
good := and(good, staticcall(gas(), 2, A, 118, 0, 32))
node := and(mload(0), HI)
sp := add(sp, 16)
}
msgNode := node
}
ok := and(good, eq(msgNode, root))
}
}
}
QdayTypes.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
// QDAY PREPRINT 0 . Interfaces
// web https://quantumday.fun . x https://x.com/qday_fun
//
// Result. The few Uniswap v4 shapes the pad, the vault and the siren use, declared here so that no QDAY
// source imports anything from outside this folder.
// Construction. Field order and types follow v4-core, so the ABI encodings match the deployed PoolManager,
// PositionManager and Permit2 exactly; only the ABI matters to them, not these names.
struct PoolKey {
address currency0;
address currency1;
uint24 fee;
int24 tickSpacing;
address hooks;
}
struct SwapParams {
bool zeroForOne;
int256 amountSpecified;
uint160 sqrtPriceLimitX96;
}
struct ModifyLiquidityParams {
int24 tickLower;
int24 tickUpper;
int256 liquidityDelta;
bytes32 salt;
}
interface IPoolManager {
function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);
function unlock(bytes calldata data) external returns (bytes memory);
function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData) external returns (int256 delta);
function sync(address currency) external;
function settle() external payable returns (uint256 paid);
function take(address currency, address to, uint256 amount) external;
}
interface IUnlockCallback {
function unlockCallback(bytes calldata data) external returns (bytes memory);
}
interface IPositionManager {
function modifyLiquidities(bytes calldata unlockData, uint256 deadline) external payable;
function nextTokenId() external view returns (uint256);
}
interface IAllowanceTransfer {
function approve(address token, address spender, uint160 amount, uint48 expiration) external;
}
/// The pad, as the coins and the vault reach it.
interface IQdayPad {
function buy(address coin, uint256 minOut, address to) external payable returns (uint256 out);
function sell(address coin, uint256 amount, uint256 minEth, address to) external returns (uint256 net);
function promoted(address coin) external view returns (bool);
function coinPhase(address coin) external view returns (uint8);
function noteVault(address coin, int256 delta) external returns (uint256 acc);
function accOf(address coin) external view returns (uint256);
function releaseYield(uint256 amount) external;
}
interface IQdayCoin {
function drawFrom(address from, uint256 amount) external;
function transfer(address to, uint256 amount) external returns (bool);
function approve(address spender, uint256 amount) external returns (bool);
function balanceOf(address who) external view returns (uint256);
}
Compiler settingsShow
{
"viaIR": true,
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.