Skip to content
Verified contractPartialMIT

Verifier

Address0xe22d5c511ac5365807a8145d3b7a66e604cbafb4
Balance

0 ETH

 

Transactions

1

 

Compiler

solc 0.8.26

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 1,000 runs
EVM version
cancun
Via IR
yes
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,226,591
Creation transaction
0x9c35fef8b8…7c237f9dcc

src/Verifier.sol

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

// RETICLE · DATASHEET · VERIFIER (part RT-V1)
// https://reticlefab.xyz   https://x.com/reticlefab
//
// FUNCTION
//   Stateless. Flattens a design source (its own NAND gates plus instances of proven Die circuits) into one flat
//   netlist, measures it (gates, logic depth), and proves it equal to a task's reference netlist on every input,
//   256 input combinations per EVM word ("bit-sliced"): one AND and one NOT simulate a gate on 256 inputs at once.
//
// FLAT NETLIST (bytes)
//   nIn u8 | nOut u8 | nGates u16 | nGates x (a u16, b u16) | nOut x (wire u16)
//   wires 0..nIn-1 are inputs; wire nIn+k is gate k = NAND(wire a, wire b) with a, b < nIn+k (strictly earlier).
//
// DESIGN SOURCE (bytes)
//   nIn u8 | nOut u8 | nInst u8 | nGates u16 | instances | gates (a u16, b u16) | outputs (u16)
//   instance: dieId u32 | k u8 | k x (source wire u16)        k must equal the die's own input count
//   source wires: inputs, then each instance's outputs in order, then own gates. Every reference points earlier.
//
// MEASURED (forge, solc 0.8.26 viaIR, 1000 runs)
//   109 gas per gate per 256-input pass. 16 inputs exhaustively (256 passes): 8-bit adder of 68 gates 5.9M gas
//   with its reference; 8x8 multiplier of 600 gates 33.6M gas, proven over several transactions.
//
// ABSOLUTE MAXIMUM RATINGS
//   64 inputs, 64 outputs, 8,192 gates per flat netlist, 32 instances per source.

contract Verifier {
    uint256 public constant MAX_IO = 64;
    uint256 public constant MAX_GATES = 8192;
    uint256 public constant MAX_INSTANCES = 32;
    uint256 public constant EXHAUSTIVE_INPUTS = 16; // up to this many inputs, every combination is proven
    uint256 public constant RANDOM_PASSES = 32; // wider tasks: 32 x 256 = 8,192 vectors from a committed seed

    error Malformed();
    error TooBig();
    error Interface();

    // ------------------------------------------------------------------------------------------------ reading

    /// @notice Bytes of a netlist stored as contract code (the first code byte is a STOP guard).
    function load(address pointer) public view returns (bytes memory n) {
        uint256 size = pointer.code.length;
        if (size < 5) revert Malformed();
        n = new bytes(size - 1);
        assembly {
            extcodecopy(pointer, add(n, 32), 1, sub(size, 1))
        }
    }

    /// @notice A flat netlist's inputs, outputs and gate count, with its length checked against them.
    function header(bytes memory n) public pure returns (uint256 nIn, uint256 nOut, uint256 nGates) {
        if (n.length < 4) revert Malformed();
        nIn = uint8(n[0]);
        nOut = uint8(n[1]);
        nGates = (uint256(uint8(n[2])) << 8) | uint8(n[3]);
        if (nIn == 0 || nOut == 0 || nIn > MAX_IO || nOut > MAX_IO) revert Malformed();
        if (nGates > MAX_GATES) revert TooBig();
        if (n.length != 4 + nGates * 4 + nOut * 2) revert Malformed();
    }

    /// @notice A flat netlist is well formed: every gate reads strictly earlier wires, every output names a wire.
    function check(bytes memory n) public pure returns (uint256 nIn, uint256 nOut, uint256 nGates) {
        (nIn, nOut, nGates) = header(n);
        for (uint256 k; k < nGates; ++k) {
            uint256 o = 4 + k * 4;
            if (_u16(n, o) >= nIn + k || _u16(n, o + 2) >= nIn + k) revert Malformed();
        }
        uint256 base = 4 + nGates * 4;
        for (uint256 j; j < nOut; ++j) if (_u16(n, base + j * 2) >= nIn + nGates) revert Malformed();
    }

    /// @notice Gate count and logic depth (the longest NAND path from an input to an output).
    function measure(bytes memory n) public pure returns (uint256 gates, uint256 depth) {
        (uint256 nIn, uint256 nOut, uint256 nGates) = header(n);
        uint256[] memory level = new uint256[](nIn + nGates);
        for (uint256 k; k < nGates; ++k) {
            uint256 o = 4 + k * 4;
            uint256 la = level[_u16(n, o)];
            uint256 lb = level[_u16(n, o + 2)];
            level[nIn + k] = (la > lb ? la : lb) + 1;
        }
        uint256 base = 4 + nGates * 4;
        for (uint256 j; j < nOut; ++j) {
            uint256 l = level[_u16(n, base + j * 2)];
            if (l > depth) depth = l;
        }
        gates = nGates;
    }

    /// @notice The cost a task scores: objective 0 = gates, 1 = gates x depth, 2 = gates x depth^2 (depth >= 1).
    function cost(uint256 gates, uint256 depth, uint8 objective) public pure returns (uint256) {
        uint256 d = depth == 0 ? 1 : depth;
        if (objective == 0) return gates;
        if (objective == 1) return gates * d;
        return gates * d * d;
    }

    // ------------------------------------------------------------------------------------------------ flattening

    /// @notice The dies a source instantiates, in order.
    function instancesOf(bytes memory src) public pure returns (uint32[] memory ids) {
        (,, uint256 nInst,) = _srcHeader(src);
        ids = new uint32[](nInst);
        uint256 o = 5;
        for (uint256 k; k < nInst; ++k) {
            ids[k] = uint32(_u32(src, o));
            o += 5 + 2 * _u8(src, o + 4);
        }
    }

    /// @dev The flattening state shared by its helpers (one memory struct keeps the stack shallow).
    struct Ctx {
        bytes src;
        bytes flat;
        uint256[] map; // source wire -> flat wire
        uint256 nIn;
        uint256 s; // next source wire
        uint256 f; // next flat wire
        uint256 o; // read offset in src
    }

    /// @notice Inline every instance (given as the dies' netlist pointers, in order) and append the source's own
    ///         gates: one flat netlist. `inlined[k]` is how many gates instance k contributed (for royalties).
    function flatten(bytes memory src, address[] memory dies)
        public
        view
        returns (bytes memory flat, uint256[] memory inlined)
    {
        (uint256 nIn, uint256 nOut, uint256 nInst, uint256 ownGates) = _srcHeader(src);
        if (dies.length != nInst) revert Malformed();
        bytes[] memory subs = new bytes[](nInst);
        uint256 total = ownGates;
        uint256 srcWires = nIn + ownGates;
        for (uint256 k; k < nInst; ++k) {
            subs[k] = load(dies[k]);
            (, uint256 sOut, uint256 sGates) = header(subs[k]);
            total += sGates;
            srcWires += sOut;
        }
        if (total > MAX_GATES || nIn + total > type(uint16).max) revert TooBig();
        flat = new bytes(4 + total * 4 + nOut * 2);
        flat[0] = bytes1(uint8(nIn));
        flat[1] = bytes1(uint8(nOut));
        flat[2] = bytes1(uint8(total >> 8));
        flat[3] = bytes1(uint8(total));
        Ctx memory x = Ctx(src, flat, new uint256[](srcWires), nIn, nIn, nIn, 5);
        for (uint256 i; i < nIn; ++i) x.map[i] = i;
        inlined = new uint256[](nInst);
        for (uint256 k; k < nInst; ++k) inlined[k] = _inline(x, subs[k]);
        for (uint256 g; g < ownGates; ++g) _ownGate(x);
        _outputs(x, nOut, total);
    }

    /// @dev One instance: map its inputs, copy its gates renumbered, map its outputs. Returns its gate count.
    function _inline(Ctx memory x, bytes memory sub) private pure returns (uint256 sGates) {
        uint256 sIn;
        uint256 sOut;
        (sIn, sOut, sGates) = header(sub);
        if (_u8(x.src, x.o + 4) != sIn) revert Interface();
        uint256[] memory m = new uint256[](sIn + sGates); // die wire -> flat wire
        for (uint256 j; j < sIn; ++j) {
            uint256 w = _u16(x.src, x.o + 5 + 2 * j);
            if (w >= x.s) revert Malformed();
            m[j] = x.map[w];
        }
        x.o += 5 + 2 * sIn;
        for (uint256 g; g < sGates; ++g) {
            _gate(x.flat, x.f - x.nIn, m[_u16(sub, 4 + g * 4)], m[_u16(sub, 6 + g * 4)]);
            m[sIn + g] = x.f++;
        }
        uint256 base = 4 + sGates * 4;
        for (uint256 j; j < sOut; ++j) x.map[x.s++] = m[_u16(sub, base + j * 2)];
    }

    function _ownGate(Ctx memory x) private pure {
        uint256 a = _u16(x.src, x.o);
        uint256 b = _u16(x.src, x.o + 2);
        if (a >= x.s || b >= x.s) revert Malformed();
        _gate(x.flat, x.f - x.nIn, x.map[a], x.map[b]);
        x.map[x.s++] = x.f++;
        x.o += 4;
    }

    function _outputs(Ctx memory x, uint256 nOut, uint256 total) private pure {
        uint256 ob = 4 + total * 4;
        for (uint256 j; j < nOut; ++j) {
            uint256 w = _u16(x.src, x.o + 2 * j);
            if (w >= x.s) revert Malformed();
            uint256 fw = x.map[w];
            x.flat[ob + 2 * j] = bytes1(uint8(fw >> 8));
            x.flat[ob + 2 * j + 1] = bytes1(uint8(fw));
        }
        if (x.src.length != x.o + 2 * nOut) revert Malformed();
    }

    // ------------------------------------------------------------------------------------------------ proving

    /// @notice How many 256-input passes prove a task with `nIn` inputs.
    function passesFor(uint256 nIn) public pure returns (uint256) {
        if (nIn > EXHAUSTIVE_INPUTS) return RANDOM_PASSES;
        return nIn <= 8 ? 1 : 1 << (nIn - 8);
    }

    /// @notice Run passes [from, to) of the equivalence proof of `cand` against `ref`. For a task of more than 16
    ///         inputs, pass p feeds inputs drawn from keccak(seed, p, i); otherwise pass p is combinations
    ///         p*256 .. p*256+255. Returns false at the first output that differs.
    function proveRange(address cand, address ref, uint256 from, uint256 to, uint256 seed) external view returns (bool) {
        bytes memory c = load(cand);
        bytes memory r = load(ref);
        (uint256 nIn, uint256 nOut, uint256 cG) = header(c);
        (uint256 rIn, uint256 rOut, uint256 rG) = header(r);
        if (nIn != rIn || nOut != rOut) revert Interface();
        bool wide = nIn > EXHAUSTIVE_INPUTS;
        uint256 live = nIn >= 8 ? type(uint256).max : (uint256(1) << (1 << nIn)) - 1;
        uint256[] memory wa = new uint256[](nIn + cG);
        uint256[] memory wb = new uint256[](nIn + rG);
        uint256[] memory ca = _decode(c, wa, cG);
        uint256[] memory cb = _decode(r, wb, rG);
        uint256[] memory oa = _outs(c, nIn, cG, nOut);
        uint256[] memory ob = _outs(r, nIn, rG, nOut);
        uint256 da;
        uint256 db;
        assembly {
            da := add(add(wa, 32), shl(5, nIn))
            db := add(add(wb, 32), shl(5, nIn))
        }
        for (uint256 p = from; p < to; ++p) {
            for (uint256 i; i < nIn; ++i) {
                uint256 v = wide ? uint256(keccak256(abi.encode(seed, p, i))) : _lane(i, p);
                wa[i] = v;
                wb[i] = v;
            }
            _run(ca, da);
            _run(cb, db);
            for (uint256 j; j < nOut; ++j) if ((wa[oa[j]] ^ wb[ob[j]]) & live != 0) return false;
        }
        return true;
    }

    /// @notice One input vector through a netlist (bit i of `input` drives input i): the output bits.
    function evalOne(address pointer, uint256 input) public view returns (uint256 out) {
        bytes memory n = load(pointer);
        (uint256 nIn, uint256 nOut, uint256 nG) = header(n);
        uint256[] memory w = new uint256[](nIn + nG);
        for (uint256 i; i < nIn; ++i) w[i] = (input >> i) & 1;
        uint256[] memory code = _decode(n, w, nG);
        uint256 d;
        assembly {
            d := add(add(w, 32), shl(5, nIn))
        }
        _run(code, d);
        uint256 base = 4 + nG * 4;
        for (uint256 j; j < nOut; ++j) out |= (w[_u16(n, base + 2 * j)] & 1) << j;
    }

    // ------------------------------------------------------------------------------------------------ internals

    /// @dev Pass p, input i (< 8: a fixed lane pattern; >= 8: all ones or all zeros by bit i-8 of p).
    function _lane(uint256 i, uint256 p) private pure returns (uint256 w) {
        if (i >= 8) return ((p >> (i - 8)) & 1) == 1 ? type(uint256).max : 0;
        if (i == 0) return 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
        if (i == 1) return 0xcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc;
        if (i == 2) return 0xf0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0;
        if (i == 3) return 0xff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00;
        if (i == 4) return 0xffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000ffff0000;
        if (i == 5) return 0xffffffff00000000ffffffff00000000ffffffff00000000ffffffff00000000;
        if (i == 6) return 0xffffffffffffffff0000000000000000ffffffffffffffff0000000000000000;
        return 0xffffffffffffffffffffffffffffffff00000000000000000000000000000000;
    }

    /// @dev Each gate once into one word: memory address of input a << 128 | memory address of input b.
    function _decode(bytes memory n, uint256[] memory wires, uint256 nGates) private pure returns (uint256[] memory code) {
        code = new uint256[](nGates);
        assembly {
            let w := add(wires, 32)
            let g := add(n, 36)
            let c := add(code, 32)
            let end := add(c, shl(5, nGates))
            for {} lt(c, end) {
                c := add(c, 32)
                g := add(g, 4)
            } {
                let x := mload(g)
                mstore(c, or(shl(128, add(w, shl(5, shr(240, x)))), add(w, shl(5, and(shr(224, x), 0xffff)))))
            }
        }
    }

    /// @dev The gate loop: writes gate outputs from `dst` on (the netlist's own order is topological).
    function _run(uint256[] memory code, uint256 dst) private pure {
        assembly {
            let c := add(code, 32)
            let end := add(c, shl(5, mload(code)))
            let m := 0xffffffffffffffffffffffffffffffff
            for {} lt(c, end) {
                c := add(c, 32)
                dst := add(dst, 32)
            } {
                let x := mload(c)
                mstore(dst, not(and(mload(shr(128, x)), mload(and(x, m)))))
            }
        }
    }

    function _outs(bytes memory n, uint256 nIn, uint256 nGates, uint256 nOut) private pure returns (uint256[] memory o) {
        o = new uint256[](nOut);
        uint256 base = 4 + nGates * 4;
        for (uint256 j; j < nOut; ++j) o[j] = _u16(n, base + 2 * j);
        nIn;
    }

    function _gate(bytes memory flat, uint256 k, uint256 a, uint256 b) private pure {
        uint256 o = 4 + k * 4;
        flat[o] = bytes1(uint8(a >> 8));
        flat[o + 1] = bytes1(uint8(a));
        flat[o + 2] = bytes1(uint8(b >> 8));
        flat[o + 3] = bytes1(uint8(b));
    }

    function _srcHeader(bytes memory src) private pure returns (uint256 nIn, uint256 nOut, uint256 nInst, uint256 nGates) {
        if (src.length < 5) revert Malformed();
        nIn = uint8(src[0]);
        nOut = uint8(src[1]);
        nInst = uint8(src[2]);
        nGates = (uint256(uint8(src[3])) << 8) | uint8(src[4]);
        if (nIn == 0 || nOut == 0 || nIn > MAX_IO || nOut > MAX_IO) revert Malformed();
        if (nInst > MAX_INSTANCES) revert TooBig();
        if (nGates > MAX_GATES) revert TooBig();
    }

    function _u8(bytes memory b, uint256 o) private pure returns (uint256) {
        if (o >= b.length) revert Malformed();
        return uint8(b[o]);
    }

    function _u16(bytes memory b, uint256 o) private pure returns (uint256) {
        if (o + 2 > b.length) revert Malformed();
        return (uint256(uint8(b[o])) << 8) | uint8(b[o + 1]);
    }

    function _u32(bytes memory b, uint256 o) private pure returns (uint256) {
        return (_u16(b, o) << 16) | _u16(b, o + 2);
    }
}
Compiler settingsShow
{
  "viaIR": true,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 1000,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "solmate/=lib/solmate/",
    "v4-core/=lib/v4-core/",
    "@uniswap/v4-core/=lib/v4-core/",
    "v4-periphery/=lib/v4-periphery/",
    "permit2/=lib/permit2/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.