PQGrind Docs

State transition

State

Name Type Meaning
U map outpoint → list of [a, b) marked ranges per unspent output, offsets relative to that output's first satoshi; sorted, disjoint, non-adjacent
mints integer valid mints so far
burned integer units sent to OP_RETURN outputs or to fees
tip (height, block hash) last applied block

Derived: circulating = mints · R − burned (see the open issue below).

Block

procedure APPLY_BLOCK(block, h):
    tip ← (h, block.hash)
    if h < H_start: return
    D ← difficulty_for(mints)                  # once per block
    for tx in block.tx[1:]:                    # coinbase skipped
        APPLY_TX(tx, h, D)

Transaction

procedure APPLY_TX(tx, h, D):
    spent[i] ← U.remove(tx.vin[i].outpoint) or []        for every input i
    minted   ← IS_CANDIDATE(tx) and VALID_MINT(tx, h, D)
    if minted: mints ← mints + 1
    if not minted and every spent[i] = []: return

    # 1. line up inputs
    line ← [] ; off ← 0
    for i in 0 … len(tx.vin) − 1:
        line += [(off + a, off + b) for (a, b) in spent[i]]
        if i = 0 and minted: line += [(off, off + R)]
        off += value(prevout of tx.vin[i])
    line ← MERGE(line)                                    # sort, unite overlapping or touching ranges

    # 2. cut into outputs
    off ← 0 ; given ← 0
    for k in 0 … len(tx.vout) − 1:
        lo ← off ; hi ← off + tx.vout[k].value ; off ← hi
        got ← MERGE([(max(a, lo) − lo, min(b, hi) − lo) for (a, b) in line if a < hi and b > lo])
        if got = []: continue
        n ← Σ (b − a) over got
        if tx.vout[k] is OP_RETURN: burned += n
        else: U[(txid, k)] ← got
        given += n
    burned += |line| − given                              # the part that fell into the fee

|line| is the total length of the merged ranges.

Worked cases

Cases 1, 2 and 4 are mints whose input 0 is worth 5,000 sat; case 3 is a transfer. Values in sat; R as on the constants page.

# Inputs (value, marked) Outputs (value) Result
1 in0 (5,000, new [0, R)) [R, claim 0, change] out0 gets [0, R)
2 in0 (5,000, new [0, R)) [R/2, claim 0, change ≥ R/2] out0 [0, R/2); out1 (OP_RETURN, value 0) covers nothing; out2 [0, R/2)
3 in0 (1,000, [0, 1,000)), in1 (600, [0, 600)) [1,200, 400] line [0, 1,600); out0 [0, 1,200); out1 [0, 400)
4 in0 (R + 100, new [0, R)) [R/2, claim 0] out0 [0, R/2); the other R/2 fall into the fee and are burned

Byte-level example of case 1: Byte layouts.

Open issue (pending freeze)

If input 0 of a valid mint already carries marked ranges inside [0, R), MERGE unites them with the new range. mints grows by 1 while the ledger grows by less than R, so mints · R − burned overstates the circulating count. The frozen rule must either reject such mints (a new code) or define the overlap. Until then, minters SHOULD fund input 0 with unmarked satoshis.

Invariants (for testing)

  1. Σ_{o ∈ U} |U[o]| + burned = mints · R when no overlap case has occurred.
  2. For each o ∈ U, every range lies inside [0, value(o)).
  3. Replaying the same blocks gives the same U, mints, burned (Replay and compare).