Verified contractPartialMIT
HashmindRegistry
Address0xe9433f8ac94cabc0de2b3d0758de3bf928ec7842
- Balance
0 ETH
- Transactions
14
- Compiler
solc 0.8.26
- Same code
Only this contract
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- Yes, 400 runs
- EVM version
- cancun
- Via IR
- yes
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,156,034
- Creation transaction
- 0x7f50e8543a…53142b65b9
2 files
- src/HashmindRegistry.solmain16.48 KB
- src/pq/HMS.sol5.75 KB
src/HashmindRegistry.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {HMS} from "./pq/HMS.sol";
/// @title HashmindRegistry
/// @notice The keyring of Hashmind: every hash-based (post-quantum) identity, its trees, and the ledger of the
/// one-time keys it has spent. Three kinds of identities live here:
///
/// Root (the platform) height 16. Attests every agent key Hashmind issues, at launch.
/// creators height 10, derived in the creator's browser. Certify their agent at launch, pause or
/// resume it, and can replace its key after a public delay if it is ever lost.
/// agents height 16, generated and sealed in Hashmind's signer service. Authorize every
/// batch of actions their coin's vault executes (hook settings, buybacks, claims,
/// credits, notes).
///
/// Every authorization is an HMS-1 signature (contracts/src/pq/HMS.sol) of a message this contract rebuilds
/// from the call itself: keccak256(abi.encode(DOMAIN, chainid, this, identity, purpose, keccak256(body))). A
/// signature therefore cannot be replayed on another chain, another contract, another identity or for another
/// purpose. One-time keys are never accepted twice: an agent's leaf index must strictly increase (one storage
/// slot), creators and Root keep a bitmap of spent leaves (so two launches can confirm in any order).
///
/// There is no owner. Nothing here can be changed by an ECDSA key alone.
contract HashmindRegistry {
uint8 public constant KIND_ROOT = 1;
uint8 public constant KIND_CREATOR = 2;
uint8 public constant KIND_AGENT = 3;
uint8 public constant AGENT_HEIGHT = 16;
uint8 public constant CREATOR_HEIGHT = 10;
uint8 public constant ROOT_HEIGHT = 16;
uint64 public constant REKEY_DELAY = 3 days;
uint64 public constant MAX_DEADLINE = 7 days;
bytes32 public constant DOMAIN = keccak256("Hashmind HMS-1");
bytes32 public constant P_BATCH = keccak256("hashmind.batch");
bytes32 public constant P_ATTEST = keccak256("hashmind.attest");
bytes32 public constant P_CERTIFY = keccak256("hashmind.certify");
bytes32 public constant P_PAUSE = keccak256("hashmind.pause");
bytes32 public constant P_ROTATE = keccak256("hashmind.rotate");
bytes32 public constant P_REKEY = keccak256("hashmind.rekey");
bytes32 public constant P_CANCEL_REKEY = keccak256("hashmind.cancel-rekey");
bytes32 public constant P_BIND = keccak256("hashmind.bind");
bytes32 public constant P_JOURNAL = keccak256("hashmind.journal");
struct Identity {
uint32 gen; // current tree (0 at registration, +1 per rotation or rekey)
uint32 nextLeaf; // agents: lowest leaf the next signature may use
uint8 height;
uint8 kind;
bool paused; // agents: set by their creator
uint64 signed; // signatures verified on-chain, all trees
}
struct Tree {
bytes32 pubSeed;
bytes32 root;
}
struct Agent {
address vault;
address token;
bytes32 creator;
}
struct Rekey {
bytes32 pubSeed;
bytes32 root;
uint64 at;
uint32 fromGen; // the tree it replaces: a rotation in between voids the proposal
}
/// What the factory hands over at launch (the creator's tree is only read the first time that wallet launches).
struct AgentLaunch {
bytes32 agentPubSeed;
bytes32 agentRoot;
bytes32 creatorPubSeed;
bytes32 creatorRoot;
uint64 attestDeadline;
bytes creatorSig;
bytes attestSig;
}
address public immutable factory;
bytes32 public immutable rootId;
mapping(bytes32 id => Identity) public identities;
mapping(bytes32 id => mapping(uint256 gen => Tree)) public trees;
mapping(bytes32 id => mapping(uint256 gen => mapping(uint256 word => uint256))) private _spent;
mapping(bytes32 agentId => Agent) public agents;
mapping(address vault => bytes32) public agentOfVault;
mapping(address wallet => bytes32) public creatorOfWallet;
mapping(bytes32 creatorId => address) public walletOfCreator;
mapping(bytes32 agentId => Rekey) public rekeys;
uint256 public agentCount;
uint256 public creatorCount;
event IdentityRegistered(bytes32 indexed id, uint8 kind, bytes32 pubSeed, bytes32 root, uint8 height);
event Signed(bytes32 indexed id, uint32 gen, uint32 leaf, bytes32 indexed purpose, bytes32 msgHash);
event Rotated(bytes32 indexed id, uint32 gen, bytes32 pubSeed, bytes32 root);
event AgentRegistered(bytes32 indexed agentId, address indexed vault, address indexed token, bytes32 creator);
event CreatorBound(address indexed wallet, bytes32 indexed creatorId);
event AgentPaused(bytes32 indexed agentId, bool paused);
event RekeyProposed(bytes32 indexed agentId, bytes32 pubSeed, bytes32 root, uint64 at);
event RekeyCancelled(bytes32 indexed agentId);
event RekeyApplied(bytes32 indexed agentId, uint32 gen);
error NotFactory();
error NotAgentVault();
error UnknownIdentity();
error AlreadyRegistered();
error BadSignature();
error LeafSpent(uint32 leaf);
error Paused();
error Expired();
error BadDeadline();
error NoRekey();
error TooEarly();
error StaleRekey();
constructor(address factory_, bytes32 rootPubSeed, bytes32 rootRoot) {
factory = factory_;
bytes32 id = HMS.identityId(rootPubSeed, rootRoot);
rootId = id;
_create(id, KIND_ROOT, ROOT_HEIGHT, rootPubSeed, rootRoot);
}
// ================================================================== launch (factory)
/// @notice Registers a new coin's agent: Root must attest the key, the creator must certify it. Returns its id.
/// `config` is the hash of the launch's exact name, ticker, metadata, agent file and hook settings
/// (computed by the factory from the call): Root's attestation covers it, so an approved draft cannot be
/// sent with another name or ticker.
function registerAgent(AgentLaunch calldata x, address vault, address token, address creatorWallet, bytes32 config)
external
returns (bytes32 agentId, bytes32 creatorId)
{
if (msg.sender != factory) revert NotFactory();
agentId = HMS.identityId(x.agentPubSeed, x.agentRoot);
if (identities[agentId].kind != 0) revert AlreadyRegistered();
creatorId = creatorOfWallet[creatorWallet];
if (creatorId == bytes32(0)) {
// first launch from this wallet: the certification itself proves the wallet holds this tree
creatorId = HMS.identityId(x.creatorPubSeed, x.creatorRoot);
if (identities[creatorId].kind == 0) _create(creatorId, KIND_CREATOR, CREATOR_HEIGHT, x.creatorPubSeed, x.creatorRoot);
else if (identities[creatorId].kind != KIND_CREATOR) revert AlreadyRegistered();
_bind(creatorWallet, creatorId);
}
_use(creatorId, P_CERTIFY, keccak256(abi.encode(agentId, token, creatorWallet)), x.creatorSig);
if (block.timestamp > x.attestDeadline) revert Expired();
if (x.attestDeadline > block.timestamp + MAX_DEADLINE) revert BadDeadline();
_use(rootId, P_ATTEST, keccak256(abi.encode(agentId, token, creatorWallet, creatorId, x.attestDeadline, config)), x.attestSig);
_create(agentId, KIND_AGENT, AGENT_HEIGHT, x.agentPubSeed, x.agentRoot);
agents[agentId] = Agent(vault, token, creatorId);
agentOfVault[vault] = agentId;
agentCount++;
emit AgentRegistered(agentId, vault, token, creatorId);
}
// ================================================================== agents (their vault)
/// @notice Spends one of the calling vault's agent keys on a batch. Only the agent's own vault, never while its
/// creator has paused it. Returns the leaf used.
function consume(bytes32 bodyHash, bytes calldata sig) external returns (uint32 leaf) {
bytes32 id = agentOfVault[msg.sender];
if (id == bytes32(0)) revert NotAgentVault();
if (identities[id].paused) revert Paused();
return _use(id, P_BATCH, bodyHash, sig);
}
// ================================================================== creators
/// @notice Binds a creator identity to the calling wallet (new identity, or re-binding after a lost passphrase).
/// The tree signs the wallet address: only its holder can bind it. Agents already launched keep the
/// creator identity that certified them.
function registerCreator(bytes32 pubSeed, bytes32 root, bytes calldata sig) external returns (bytes32 id) {
id = HMS.identityId(pubSeed, root);
uint8 kind = identities[id].kind;
if (kind == 0) _create(id, KIND_CREATOR, CREATOR_HEIGHT, pubSeed, root);
else if (kind != KIND_CREATOR) revert AlreadyRegistered();
_use(id, P_BIND, keccak256(abi.encode(msg.sender)), sig);
_bind(msg.sender, id);
}
/// @notice The creator pauses or resumes its agent (its vault then refuses every batch). Anyone can relay.
function setPaused(bytes32 agentId, bool paused, uint64 deadline, bytes calldata creatorSig) external {
Agent memory a = agents[agentId];
if (a.vault == address(0)) revert UnknownIdentity();
if (block.timestamp > deadline) revert Expired();
if (deadline > block.timestamp + MAX_DEADLINE) revert BadDeadline();
_use(a.creator, P_PAUSE, keccak256(abi.encode(agentId, paused, deadline)), creatorSig);
identities[agentId].paused = paused;
emit AgentPaused(agentId, paused);
}
/// @notice The creator proposes a new key for its agent (recovery if the old one is lost). It applies after
/// REKEY_DELAY unless the agent's current key refuses it. Anyone can relay.
function proposeRekey(bytes32 agentId, bytes32 pubSeed, bytes32 root, uint64 deadline, bytes calldata creatorSig)
external
{
Agent memory a = agents[agentId];
if (a.vault == address(0)) revert UnknownIdentity();
if (block.timestamp > deadline) revert Expired();
if (deadline > block.timestamp + MAX_DEADLINE) revert BadDeadline();
uint32 gen = identities[agentId].gen;
// the replacement tree is built for generation gen + 1, and the proposal says so
_use(a.creator, P_REKEY, keccak256(abi.encode(agentId, pubSeed, root, uint256(AGENT_HEIGHT), deadline, uint256(gen))), creatorSig);
uint64 at = uint64(block.timestamp) + REKEY_DELAY;
rekeys[agentId] = Rekey(pubSeed, root, at, gen);
emit RekeyProposed(agentId, pubSeed, root, at);
}
/// @notice The agent's current key refuses a pending rekey.
function cancelRekey(bytes32 agentId, bytes calldata agentSig) external {
Rekey memory r = rekeys[agentId];
if (r.at == 0) revert NoRekey();
_use(agentId, P_CANCEL_REKEY, keccak256(abi.encode(keccak256(abi.encode(r.pubSeed, r.root, r.at)))), agentSig);
delete rekeys[agentId];
emit RekeyCancelled(agentId);
}
/// @notice Applies a rekey whose delay has run. Anyone.
function applyRekey(bytes32 agentId) external {
Rekey memory r = rekeys[agentId];
if (r.at == 0) revert NoRekey();
if (block.timestamp < r.at) revert TooEarly();
delete rekeys[agentId];
if (identities[agentId].gen != r.fromGen) revert StaleRekey();
uint32 gen = _nextTree(agentId, r.pubSeed, r.root);
emit RekeyApplied(agentId, gen);
}
// ================================================================== every identity
/// @notice Moves an identity to its next tree, certified by a one-time key of the current one. Anyone can relay.
function rotate(bytes32 id, bytes32 pubSeed, bytes32 root, bytes calldata sig) external {
Identity memory s = identities[id];
if (s.kind == 0) revert UnknownIdentity();
_use(id, P_ROTATE, keccak256(abi.encode(pubSeed, root, uint256(s.height))), sig);
_nextTree(id, pubSeed, root);
if (rekeys[id].at != 0) {
// a pending rekey was built for the tree that just retired: it can no longer apply
delete rekeys[id];
emit RekeyCancelled(id);
}
}
// ================================================================== views
function msgHash(bytes32 id, bytes32 purpose, bytes32 bodyHash) public view returns (bytes32) {
return keccak256(abi.encode(DOMAIN, block.chainid, address(this), id, purpose, bodyHash));
}
/// @notice Checks a signature against any tree an identity ever had, without spending anything. The site's
/// Verify button and the public journal use it (journal entries are signed off-chain, purpose JOURNAL).
function check(bytes32 id, uint32 gen, bytes32 purpose, bytes32 bodyHash, bytes calldata sig)
external
view
returns (bool ok, uint32 leaf)
{
Identity memory s = identities[id];
Tree memory t = trees[id][gen];
if (s.kind == 0 || gen > s.gen) return (false, 0);
uint256 l;
(ok, l) = HMS.verify(t.pubSeed, t.root, gen, s.height, msgHash(id, purpose, bodyHash), sig);
leaf = uint32(l);
}
function isSpent(bytes32 id, uint32 gen, uint32 leaf) external view returns (bool) {
Identity memory s = identities[id];
if (s.kind == KIND_AGENT) return gen < s.gen || leaf < s.nextLeaf;
return _spent[id][gen][leaf >> 8] & (1 << (leaf & 0xff)) != 0;
}
/// One-time keys an agent can still use on its current tree.
function leavesLeft(bytes32 id) external view returns (uint256) {
Identity memory s = identities[id];
if (s.kind == 0) return 0;
return (uint256(1) << s.height) - s.nextLeaf;
}
// ================================================================== internals
function _create(bytes32 id, uint8 kind, uint8 height, bytes32 pubSeed, bytes32 root) private {
identities[id] = Identity(0, 0, height, kind, false, 0);
trees[id][0] = Tree(pubSeed, root);
if (kind == KIND_CREATOR) creatorCount++;
emit IdentityRegistered(id, kind, pubSeed, root, height);
}
function _bind(address wallet, bytes32 id) private {
address previous = walletOfCreator[id];
if (previous != address(0) && previous != wallet) delete creatorOfWallet[previous];
bytes32 old = creatorOfWallet[wallet];
if (old != bytes32(0) && old != id) delete walletOfCreator[old];
creatorOfWallet[wallet] = id;
walletOfCreator[id] = wallet;
emit CreatorBound(wallet, id);
}
function _nextTree(bytes32 id, bytes32 pubSeed, bytes32 root) private returns (uint32 gen) {
Identity storage s = identities[id];
gen = s.gen + 1;
s.gen = gen;
s.nextLeaf = 0;
trees[id][gen] = Tree(pubSeed, root);
emit Rotated(id, gen, pubSeed, root);
}
/// Verifies `sig` for (id, purpose, body) on the identity's current tree and spends its leaf.
function _use(bytes32 id, bytes32 purpose, bytes32 bodyHash, bytes calldata sig) private returns (uint32 leaf) {
Identity storage s = identities[id];
uint8 kind = s.kind;
if (kind == 0) revert UnknownIdentity();
uint32 gen = s.gen;
Tree storage t = trees[id][gen];
bytes32 m = msgHash(id, purpose, bodyHash);
(bool ok, uint256 l) = HMS.verify(t.pubSeed, t.root, gen, s.height, m, sig);
if (!ok) revert BadSignature();
leaf = uint32(l);
if (kind == KIND_AGENT) {
if (leaf < s.nextLeaf) revert LeafSpent(leaf);
s.nextLeaf = leaf + 1; // at most 2^16: the tree is then exhausted and must have rotated before
} else {
mapping(uint256 => uint256) storage bits = _spent[id][gen];
uint256 word = bits[leaf >> 8];
uint256 bit = 1 << (leaf & 0xff);
if (word & bit != 0) revert LeafSpent(leaf);
bits[leaf >> 8] = word | bit;
}
unchecked {
s.signed += 1; // same slot as the leaf index: no extra storage write
}
emit Signed(id, gen, leaf, purpose, m);
}
}
src/pq/HMS.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
/// @title HMS
/// @notice On-chain verifier for HMS-1, the Hashmind Merkle Signature scheme (docs/SPEC-PQ.md): Winternitz one-time
/// signatures (w = 16, n = 32, 67 chains) under a Merkle tree of height h, where every hash is a
/// domain-separated Keccak-256: Th(P, ADRS, M) = keccak256(P ‖ ADRS ‖ M). It uses the KECCAK256 opcode only:
/// no precompile, no elliptic curve, nothing a quantum computer is known to break.
///
/// Signature: leaf index (uint32, big-endian) ‖ 67 chain values ‖ h authentication-path nodes
/// (4 + 2,144 + 32·h bytes: 2,468 for creators, h = 10; 2,660 for agents, h = 16).
///
/// This library only checks that a signature of `msgHash` leads to `root`. Spending each leaf at most once
/// is the caller's job (HashmindRegistry keeps that ledger).
library HMS {
uint256 internal constant LEN = 67;
uint256 internal constant MAX_HEIGHT = 20;
uint256 private constant MAGIC = 0x484D5331; // "HMS1"
uint256 private constant T_CHAIN = 0;
uint256 private constant T_LEAF = 1;
uint256 private constant T_NODE = 2;
uint256 private constant T_MESSAGE = 4;
uint256 private constant T_IDENTITY = 5;
error BadSignatureLength();
error BadLeaf();
error BadHeight();
error BadGeneration();
function sigBytes(uint256 height) internal pure returns (uint256) {
return 4 + LEN * 32 + 32 * height;
}
/// @dev ADRS as one word: magic ‖ gen ‖ type ‖ leaf ‖ chain ‖ pos|height ‖ index ‖ 0 (big-endian uint32s).
function _adrs(uint256 gen, uint256 typ, uint256 leaf) private pure returns (uint256) {
return (MAGIC << 224) | (gen << 192) | (typ << 160) | (leaf << 128);
}
/// @notice The leaf index a signature claims, after checking its length for `height`.
function leafOf(bytes calldata sig, uint256 height) internal pure returns (uint256 leaf) {
if (height == 0 || height > MAX_HEIGHT) revert BadHeight();
if (sig.length != sigBytes(height)) revert BadSignatureLength();
leaf = uint32(bytes4(sig[0:4]));
if (leaf >= (1 << height)) revert BadLeaf();
}
/// @notice Identity id of a first tree: keccak256(pubSeed ‖ ADRS(IDENTITY, gen 0) ‖ root). Fixed for life.
function identityId(bytes32 pubSeed, bytes32 root) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(pubSeed, _adrs(0, T_IDENTITY, 0), root));
}
/// @notice True iff `sig` is a valid HMS-1 signature of `msgHash` under the tree (pubSeed, root, gen, height).
function verify(bytes32 pubSeed, bytes32 root, uint256 gen, uint256 height, bytes32 msgHash, bytes calldata sig)
internal
pure
returns (bool ok, uint256 leaf)
{
if (gen > type(uint32).max) revert BadGeneration();
leaf = leafOf(sig, height);
ok = rootOf(pubSeed, root, gen, height, leaf, msgHash, sig) == root;
}
/// @notice The root a (length-checked) signature leads to.
function rootOf(
bytes32 pubSeed,
bytes32 root,
uint256 gen,
uint256 height,
uint256 leaf,
bytes32 msgHash,
bytes calldata sig
) internal pure returns (bytes32 out) {
uint256 msgAdrs = _adrs(gen, T_MESSAGE, leaf);
uint256 chainAdrs = _adrs(gen, T_CHAIN, leaf);
uint256 leafAdrs = _adrs(gen, T_LEAF, leaf);
uint256 nodeAdrs = _adrs(gen, T_NODE, 0);
assembly ("memory-safe") {
// m: pubSeed ‖ ADRS ‖ x ‖ y (96 bytes per chain step, 128 for the digest and the tree nodes)
// m+128: pubSeed ‖ ADRS(LEAF) ‖ the 67 chain ends (2,208 bytes, compressed into the leaf)
let m := mload(0x40)
let l := add(m, 128)
mstore(m, pubSeed)
// D = Th(P, ADRS(MESSAGE, gen, leaf), root ‖ msgHash)
mstore(add(m, 32), msgAdrs)
mstore(add(m, 64), root)
mstore(add(m, 96), msgHash)
let d := keccak256(m, 128)
mstore(l, pubSeed)
mstore(add(l, 32), leafAdrs)
let csum := 0
let base := add(sig.offset, 4)
for { let j := 0 } lt(j, 67) { j := add(j, 1) } {
let dj
switch lt(j, 64)
case 1 {
dj := and(shr(sub(252, shl(2, j)), d), 15)
csum := add(csum, sub(15, dj))
}
default { dj := and(shr(shl(2, sub(66, j)), csum), 15) }
let x := calldataload(add(base, shl(5, j)))
let a := or(chainAdrs, shl(96, j))
for { let p := dj } lt(p, 15) { p := add(p, 1) } {
mstore(add(m, 32), or(a, shl(64, p)))
mstore(add(m, 64), x)
x := keccak256(m, 96)
}
mstore(add(add(l, 64), shl(5, j)), x)
}
let cur := keccak256(l, 2208)
let auth := add(base, 2144) // 4 + 67 * 32
for { let h := 1 } iszero(gt(h, height)) { h := add(h, 1) } {
let sib := calldataload(add(auth, shl(5, sub(h, 1))))
mstore(add(m, 32), or(nodeAdrs, or(shl(64, h), shl(32, shr(h, leaf)))))
switch and(shr(sub(h, 1), leaf), 1)
case 0 {
mstore(add(m, 64), cur)
mstore(add(m, 96), sib)
}
default {
mstore(add(m, 64), sib)
mstore(add(m, 96), cur)
}
cur := keccak256(m, 128)
}
out := cur
}
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "none",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 400,
"enabled": true
},
"evmVersion": "cancun",
"remappings": [
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"forge-std/=lib/forge-std/src/",
"@uniswap/v4-core/=lib/v4-periphery/lib/v4-core/",
"v4-core/=lib/v4-periphery/lib/v4-core/src/",
"v4-periphery/=lib/v4-periphery/",
"permit2/=lib/v4-periphery/lib/permit2/",
"solmate/=lib/v4-periphery/lib/v4-core/lib/solmate/",
"lib/v4-periphery/lib/v4-core/:openzeppelin-contracts/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
"lib/v4-periphery/lib/v4-core/:openzeppelin-contracts/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
"@ensdomains/=lib/v4-periphery/lib/v4-core/node_modules/@ensdomains/",
"@openzeppelin/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
"ds-test/=lib/v4-periphery/lib/v4-core/lib/forge-std/lib/ds-test/src/",
"hardhat/=lib/v4-periphery/lib/v4-core/node_modules/hardhat/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.