Skip to content
Verified contractExactUNLICENSED

Scrip

Address0x50f15a52d251dfa33862eee0b5bbb80f5283e9e9
Balance

0 ETH

 

Transactions

5

 

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
prague
License
UNLICENSED
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,465,859
Creation transaction
0xe33969cae6…87fc0182c4

3 files

src/Scrip.sol

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

import {ERC20} from "./ERC20.sol";
import {ScripLib} from "./ScripLib.sol";

/// @title Scrip
/// @notice Proof-of-work ERC20 with elastic supply. Every token is created by a
///         hash submitted to `mint`; there is no premine, no owner, no pause, no
///         upgrade and no parameter setter. The deployed bytecode is the final
///         protocol (architecture.md §1, §3).
/// @dev    Epochs are derived from `block.timestamp` and never stored, so the
///         contract holds no epoch state and needs no rotation (§3.5). Proofs are
///         bound to their epoch by `chal`, so a proof dies at the boundary with no
///         transaction required.
contract Scrip is ERC20 {
    // --- epoch (derived, never stored) ---
    /// @notice Set at construction; no epoch challenge is computable before deploy.
    bytes32 public immutable SALT;
    /// @notice Rarity of a 1-token proof. Sets token size, not emission rate (§3.4).
    uint256 public immutable BASE_DIFFICULTY;
    /// @notice Receives the 1/64 twin mint (§3.7).
    address public immutable DEV_ADDRESS;

    // --- nullifier set (rolling window) ---
    /// @notice nullifier => submission block (0 = unused)
    mapping(bytes32 => uint32) public nullifierBlock;
    /// @notice FIFO of nullifiers, indexed by `queueHead..queueTail`
    mapping(uint256 => bytes32) public nullifierQueue;
    uint128 public queueHead;
    uint128 public queueTail;

    event Proof(address indexed miner, uint256 zeros, uint8 floor, uint256 amount);
    event DevTwin(address indexed miner, uint256 amount);

    error EmptyBatch();
    error InvalidBaseDifficulty();
    error Rarity();
    error Spent();

    /// @param devAddress receives the probabilistic dev twin; immutable
    /// @param baseDifficulty leading-zero bits required for a 1-token proof
    constructor(address devAddress, uint8 baseDifficulty) {
        if (devAddress == address(0)) revert ZeroAddress();
        // z is capped at MAX_ZEROS, so this bounds the shift in `payout` to <= 127.
        if (baseDifficulty == 0 || baseDifficulty > ScripLib.MAX_ZEROS) {
            revert InvalidBaseDifficulty();
        }
        DEV_ADDRESS = devAddress;
        BASE_DIFFICULTY = baseDifficulty;
        // Drawn from `block.prevrandao` so it cannot be known before this block
        // executes. That is what keeps the launch epoch from being ground in
        // advance — a premine paid in electricity (§3.5). Not a PRNG: it only
        // needs to be unknowable until deploy, and a proposer who could bias it
        // would gain nothing but their own launch-epoch proofs.
        // forge-lint: disable-next-line(weak-prng)
        SALT = keccak256(abi.encodePacked(block.prevrandao, msg.sender, block.timestamp, address(this)));
    }

    // --- views for miners and third-party verifiers ---

    function currentEpoch() external view returns (uint256) {
        return ScripLib.epochAt(block.timestamp);
    }

    function currentChallenge() external view returns (bytes32) {
        return ScripLib.challengeAt(SALT, block.timestamp);
    }

    // --- mint ---

    /// @notice Submit one or more proofs. Self-submitters pass a single element;
    ///         aggregators pass many. A bad element reverts the whole call (§3.3).
    /// @dev    No ordering requirement: the digest is the nullifier, so a
    ///         duplicate reverts on `Spent()`.
    function mint(ScripLib.Submission[] calldata subs) external {
        uint256 n = subs.length;
        if (n == 0) revert EmptyBatch();

        bytes32 chal = ScripLib.challengeAt(SALT, block.timestamp);
        uint256 base = BASE_DIFFICULTY;

        for (uint256 i = 0; i < n;) {
            ScripLib.Submission calldata s = subs[i];
            bytes32 d = ScripLib.digest(chal, s.miner, s.floor, s.nonce);
            uint256 z = ScripLib.leadingZeros(d);

            // Batch atomicity is deliberate (§3.3): one bad element reverts all,
            // so submitters simulate first. Reverting here is the mechanism.
            // forge-lint: disable-next-line(require-revert-in-loop)
            if (!(z >= s.floor && s.floor >= base && z <= ScripLib.MAX_ZEROS)) revert Rarity();

            // digest is the nullifier: spent-flag and age in one 20k-gas write
            // forge-lint: disable-next-line(require-revert-in-loop)
            if (nullifierBlock[d] != 0) revert Spent();
            // uint32 is safe: 2^32 blocks is ~136 years at 12 s slots.
            // forge-lint: disable-next-line(unsafe-typecast)
            nullifierBlock[d] = uint32(block.number);
            nullifierQueue[queueTail] = d;
            unchecked {
                ++queueTail;
            }

            uint256 amount = ScripLib.payout(z, base);
            _mint(s.miner, amount);

            if (ScripLib.isDevTwin(d)) {
                _mint(DEV_ADDRESS, amount);
                emit DevTwin(s.miner, amount);
            }

            emit Proof(s.miner, z, s.floor, amount);

            unchecked {
                ++i;
            }
        }

        _evict(ScripLib.EVICT_PER_MINT * n);
    }

    // --- eviction ---

    /// @dev Walks the FIFO head and clears entries older than the TTL. Validity
    ///      windows are disjoint (§3.6): a proof dies at its epoch boundary, which
    ///      is <= 7,200 slots, and eviction waits 7,300 blocks, so an evicted
    ///      nullifier's proof was already dead. Self-healing: a fresh head stops
    ///      the pass and it resumes later.
    function _evict(uint256 budget) internal {
        uint128 head = queueHead;
        uint128 tail = queueTail;
        uint256 number = block.number;

        while (budget != 0 && head < tail) {
            bytes32 nul = nullifierQueue[head];
            uint32 submitted = nullifierBlock[nul];

            if (submitted != 0 && number - submitted <= ScripLib.NULLIFIER_TTL_BLOCKS) break;

            delete nullifierBlock[nul];
            delete nullifierQueue[head];
            unchecked {
                ++head;
                --budget;
            }
        }

        if (head != queueHead) queueHead = head;
    }
}

src/ERC20.sol

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

/// @title ERC20
/// @notice Minimal ERC20 with zero decimals. No owner, no hooks, no extensions.
/// @dev    Scrip has no decimals: `decimals()` is 0 and one token is the raw
///         uint256 unit. Nothing in this contract scales an amount (§2).
abstract contract ERC20 {
    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;
    uint256 public totalSupply;

    string public constant name = "Scrip";
    string public constant symbol = "SCRIP";
    uint8 public constant decimals = 0;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

    error ZeroAddress();
    error InsufficientBalance();
    error InsufficientAllowance();

    function transfer(address to, uint256 value) external returns (bool) {
        _transfer(msg.sender, to, value);
        return true;
    }

    function approve(address spender, uint256 value) external returns (bool) {
        if (spender == address(0)) revert ZeroAddress();
        allowance[msg.sender][spender] = value;
        emit Approval(msg.sender, spender, value);
        return true;
    }

    function transferFrom(address from, address to, uint256 value) external returns (bool) {
        uint256 allowed = allowance[from][msg.sender];
        if (allowed != type(uint256).max) {
            if (allowed < value) revert InsufficientAllowance();
            unchecked {
                allowance[from][msg.sender] = allowed - value;
            }
        }
        _transfer(from, to, value);
        return true;
    }

    function _transfer(address from, address to, uint256 value) internal {
        if (to == address(0)) revert ZeroAddress();
        uint256 fromBalance = balanceOf[from];
        if (fromBalance < value) revert InsufficientBalance();
        unchecked {
            balanceOf[from] = fromBalance - value;
        }
        balanceOf[to] += value;
        emit Transfer(from, to, value);
    }

    /// @dev Supply is created only here, and only Scrip.mint may call it.
    function _mint(address to, uint256 value) internal {
        // Called from the mint batch loop, so a zero recipient must revert there.
        // forge-lint: disable-next-line(require-revert-in-loop)
        if (to == address(0)) revert ZeroAddress();
        totalSupply += value;
        balanceOf[to] += value;
        emit Transfer(address(0), to, value);
    }
}

src/ScripLib.sol

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

/// @title ScripLib
/// @notice Shared constructs for the Scrip protocol: the fixed parameters and
///         the proof format. This library is the single source of truth for how
///         a proof is encoded, so the contract, the reference miner and any
///         third-party implementation cannot drift from each other.
/// @dev    Nothing here reads state or writes storage; every function is pure.
library ScripLib {
    // --- protocol constants (architecture.md §2) ---
    uint256 internal constant EPOCH_SECONDS = 86_400;
    uint256 internal constant MAX_ZEROS = 128;
    uint256 internal constant DEV_MASK = 0x3F;
    uint256 internal constant DEV_PATTERN = 0x2A;
    uint256 internal constant NULLIFIER_TTL_BLOCKS = 7_300;
    uint256 internal constant EVICT_PER_MINT = 3;

    /// @notice One submitted proof. `miner` receives the payout, `floor` is the
    ///         miner's declared minimum rarity, `nonce` is the grind solution.
    struct Submission {
        address miner;
        uint8 floor;
        bytes32 nonce;
    }

    /// @notice 24 h wall-clock bucket. Derived, never stored (§3.5).
    function epochAt(uint256 timestamp) internal pure returns (uint256) {
        return timestamp / EPOCH_SECONDS;
    }

    /// @notice Epoch binding: `chal` changes once per epoch and is never stored.
    function challenge(bytes32 salt, uint256 epoch) internal pure returns (bytes32) {
        return keccak256(abi.encodePacked(salt, epoch));
    }

    /// @notice Convenience: challenge for the epoch containing `timestamp`.
    function challengeAt(bytes32 salt, uint256 timestamp) internal pure returns (bytes32) {
        return challenge(salt, epochAt(timestamp));
    }

    /// @notice The proof digest. Every field is fixed-width, so the packed
    ///         encoding is unambiguous (§3.2).
    function digest(bytes32 chal, address miner, uint8 floor, bytes32 nonce)
        internal
        pure
        returns (bytes32)
    {
        return keccak256(abi.encodePacked(chal, miner, floor, nonce));
    }

    /// @notice Convenience: digest for the epoch containing `timestamp`.
    function digestAt(bytes32 salt, uint256 timestamp, address miner, uint8 floor, bytes32 nonce)
        internal
        pure
        returns (bytes32)
    {
        return digest(challengeAt(salt, timestamp), miner, floor, nonce);
    }

    /// @notice Number of leading zero bits in `h`, in [0, 256].
    /// @dev    Binary search, no assembly and no new opcodes, so it runs on any
    ///         EVM. Eight conditional steps, worst case ~8 shifts.
    function leadingZeros(bytes32 h) internal pure returns (uint256 z) {
        uint256 x = uint256(h);
        if (x == 0) return 256;
        unchecked {
            if (x >> 128 == 0) {
                z += 128;
                x <<= 128;
            }
            if (x >> 192 == 0) {
                z += 64;
                x <<= 64;
            }
            if (x >> 224 == 0) {
                z += 32;
                x <<= 32;
            }
            if (x >> 240 == 0) {
                z += 16;
                x <<= 16;
            }
            if (x >> 248 == 0) {
                z += 8;
                x <<= 8;
            }
            if (x >> 252 == 0) {
                z += 4;
                x <<= 4;
            }
            if (x >> 254 == 0) {
                z += 2;
                x <<= 2;
            }
            if (x >> 255 == 0) {
                z += 1;
            }
        }
    }

    /// @notice Integer payout for a proof with `z` leading zeros. One bit shift,
    ///         no division and no rounding, so balances are always whole tokens.
    function payout(uint256 z, uint256 baseDifficulty) internal pure returns (uint256) {
        return uint256(1) << (z - baseDifficulty);
    }

    /// @notice True when the digest triggers the dev twin (§3.7). Trailing bits
    ///         only, so conditional on `z` the rate stays exactly 1/64.
    function isDevTwin(bytes32 d) internal pure returns (bool) {
        return (uint256(d) & DEV_MASK) == DEV_PATTERN;
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "prague"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.