Skip to content
Verified contractPartialMIT

LatticeKeys

Address0x93c28b6b28f89720c94bcd3f70242da60f958a47
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
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,392,237
Creation transaction
0x4e50c5eeb3…6cf821e156

lattice/src/LatticeKeys.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

// web: https://latticepq.xyz   x: https://x.com/lattice_quantum
//
// LATTICE . CRYSTAL REPORT . plane (0 1 0): the keys.
//
// Sample:      an ML-DSA-65 public key (FIPS 204, 1952 bytes). Its lattice id is keccak256 of the key.
// Growth:      the key is expanded on chain (tr, the 6 x 5 matrix A-hat from SHAKE128, NTT(t1 * 2^13)) in three parts, and
//              each part is written as the code of a new contract this registry creates. Anyone may write any part of
//              any key: the bytes come only from MLDSA65.expandPart run on the key itself, so a part is right or absent.
// Measured:    see lattice/measured_lattice.json: expandPart 4.7M to 5.8M gas a part, plus 200 gas a byte of code
//              (10,305 / 10,241 / 16,385 bytes). Each part fits one transaction under the 16.7M cap; all three together
//              do not, which is why there are three.
// Reading:     check() verifies a FIPS 204 signature, context "LATTICE", against the stored parts.
// Limits:      a key with a part missing verifies nothing. A written part can never change or be removed.

import {IMLDSA65} from "./LatticeShapes.sol";

contract LatticeKeys {
    address public immutable verifier;

    mapping(bytes32 => address[3]) internal _parts;

    event PartWritten(bytes32 indexed id, uint256 part, address at);

    error WrongKeyLength();
    error WrongPart();
    error AlreadyWritten();
    error WriteFailed();

    constructor(address verifier_) {
        verifier = verifier_;
    }

    /// @notice Expand part 0, 1 or 2 of an ML-DSA-65 public key and store it as code.
    function write(bytes calldata pk, uint256 part) external returns (address at) {
        if (pk.length != 1952) revert WrongKeyLength();
        if (part > 2) revert WrongPart();
        bytes32 id = keccak256(pk);
        if (_parts[id][part] != address(0)) revert AlreadyWritten();
        bytes memory data = IMLDSA65(verifier).expandPart(pk, part);
        // creation code: PUSH2 n, DUP1, PUSH1 0x0a, PUSH0, CODECOPY, PUSH0, RETURN, then a 0x00 byte (so the stored
        // code stops at once if anyone calls it) and the part
        bytes memory init = abi.encodePacked(hex"61", uint16(data.length + 1), hex"80600a5f395ff300", data);
        assembly ("memory-safe") {
            at := create(0, add(init, 32), mload(init))
        }
        if (at == address(0)) revert WriteFailed();
        _parts[id][part] = at;
        emit PartWritten(id, part, at);
    }

    function parts(bytes32 id) external view returns (address[3] memory) {
        return _parts[id];
    }

    function ready(bytes32 id) public view returns (bool) {
        address[3] storage p = _parts[id];
        return p[0] != address(0) && p[1] != address(0) && p[2] != address(0);
    }

    function check(bytes32 id, bytes calldata message, bytes calldata sig) external view returns (bool) {
        address[3] storage p = _parts[id];
        if (p[0] == address(0) || p[1] == address(0) || p[2] == address(0)) return false;
        return IMLDSA65(verifier).verifyStored(p[0], p[1], p[2], message, "LATTICE", sig);
    }
}

lattice/src/LatticeShapes.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

// web: https://latticepq.xyz   x: https://x.com/lattice_quantum
//
// LATTICE . CRYSTAL REPORT . plane (0 0 1): the ground the crystal grows on.
//
// Sample:      Ethereum mainnet. Uniswap v4 PoolManager and PositionManager, Permit2, nothing else from outside.
// Habit:       every outside address is a constant here; no constructor argument can point the crystal elsewhere.
// The verifier is not a constant: LatticeKeys is told it once at deployment, and the launch desk reads it back.

struct PoolKey {
    address currency0;
    address currency1;
    uint24 fee;
    int24 tickSpacing;
    address hooks;
}

struct SwapParams {
    bool zeroForOne;
    int256 amountSpecified;
    uint160 sqrtPriceLimitX96;
}

/// @dev One signed instruction for a lattice account. Which fields count depends on `kind` (see Lattice.sol).
struct Order {
    uint8 kind;
    uint64 nonce;
    uint64 deadline;
    bytes32 target; // an address in the low 160 bits, or a lattice id
    uint256 amount;
    uint256 minOut;
    uint256 tip; // LATTICE to whoever relays the order
    uint32 delay;
    bytes32 recovery;
}

interface IPoolManager {
    function unlock(bytes calldata data) external returns (bytes memory);
    function initialize(PoolKey memory key, uint160 sqrtPriceX96) external returns (int24 tick);
    function swap(PoolKey memory key, SwapParams memory params, bytes calldata hookData) external returns (int256);
    function settle() external payable returns (uint256);
    function sync(address currency) external;
    function take(address currency, address to, uint256 amount) external;
    function extsload(bytes32 slot) external view returns (bytes32);
}

interface IPositionManager {
    function modifyLiquidities(bytes calldata unlockData, uint256 deadline) external payable;
    function nextTokenId() external view returns (uint256);
}

interface IPermit2 {
    function approve(address token, address spender, uint160 amount, uint48 expiration) external;
}

/// @dev MLDSA65.yul: FIPS 204 ML-DSA-65 verification against a key expanded into three code contracts.
interface IMLDSA65 {
    function verifyStored(address part0, address part1, address part2, bytes calldata message, bytes calldata context, bytes calldata sig)
        external
        view
        returns (bool);
    function expandPart(bytes calldata pk, uint256 part) external view returns (bytes memory);
}

interface ILatticeKeys {
    function check(bytes32 id, bytes calldata message, bytes calldata sig) external view returns (bool);
}

interface ILatticeToken {
    function pourYield() external payable;
    function latticeTotal() external view returns (uint256);
    function lock(bytes32 id, uint256 amount) external;
    function bindDoor(address door) external;
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function balanceOf(address a) external view returns (uint256);
}

interface ILatticeDoor {
    function sellForOrder(uint256 amount, uint256 minOut, address to) external returns (uint256);
}

library Mainnet {
    address internal constant POOL_MANAGER = 0x000000000004444c5dc75cB358380D2e3dE08A90;
    address internal constant POSITION_MANAGER = 0xbD216513d74C8cf14cf4747E6AaA6420FF64ee9e;
    address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
}

/// @dev The one pool: ETH / LATTICE, LP fee 0 (the hook's 1% is the only fee), tick spacing 200.
library Cell {
    uint24 internal constant LP_FEE = 0;
    int24 internal constant SPACING = 200;
    int24 internal constant TICK_BASE = -887200;
    int24 internal constant TICK_TOP = 207200; // 1.0001^207200 LATTICE per ETH: the whole supply at a 1.0043 ETH FDV
    uint160 internal constant SQRT_BASE = 4310618292; // TickMath at -887200, read off a chain
    uint160 internal constant SQRT_TOP = 2500031419217008302293562112940196; // TickMath at 207200, read off a chain

    function key(address token, address hook) internal pure returns (PoolKey memory) {
        return PoolKey(address(0), token, LP_FEE, SPACING, hook);
    }

    /// @dev StateLibrary: pools live at mapping slot 6; slot0's low 160 bits are the sqrt price.
    function sqrtPrice(address token, address hook) internal view returns (uint160) {
        bytes32 s = keccak256(abi.encodePacked(keccak256(abi.encode(key(token, hook))), uint256(6)));
        return uint160(uint256(IPoolManager(Mainnet.POOL_MANAGER).extsload(s)));
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none"
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.