PQGrind Docs

Judgment pseudocode

Complete and self-contained; implements Valid mint with issuance Draft C and claim Format B, both pending freeze. Constants are symbols from Constants and difficulty.

constant ONE    = 2^16
constant MAX    = 2^256
constant R_HALF = 3b78ce563f89a0ed9414f5aa28ad0d96d6795f9c63      # 21 bytes
constant LEVELS = {80: LOCK_80, 88: LOCK_88}                       # template builders

function IS_CANDIDATE(tx):
    return PARSE_CLAIM_B(tx) ≠ none                  # first 6a5d output decodes and has tag 127
       and tx.vin[0].prevout.script starts with 0020

function JUDGE(tx, h, zeros, mints_before):        # zeros read once per block from m(h);
                                                   # mints_before includes earlier mints in this block
    wit ← tx.vin[0].witness
    spk ← tx.vin[0].prevout.script
    if len(wit) < 3 or len(spk) ≠ 34 or SHA256(wit[-1]) ≠ spk[2:]:
        return reject(E02)

    first_fail ← none
    for β in LEVELS from 80 to 88 step 8:            # β_min = 80 in every wave
        lock ← LEVELS[β]
        if not MATCH_TEMPLATE(wit[-1], lock):  first_fail ← first_fail or E03 ; continue
        if not WITNESS_SHAPE(wit, lock):       first_fail ← first_fail or E04 ; continue
        goto found
    return reject(first_fail)

found:
    c ← PARSE_CLAIM_B(tx)                            # see Claim output, Format B
    if c = none or c.op ≠ mint:                         return reject(E05)
    if c.target ≠ 0 or len(tx.vout) = 0 or tx.vout[0].script starts with 6a:
                                                       return reject(E06)
    if int(tx.txid) ≥ 16^(64 − zeros):                  return reject(E07)   # display order
    if tx.vin[0].prevout.value < R:                     return reject(E08)
    if mints_before ≥ M_max:                            return reject(E09)
    return accept(β, units = 1000 >> (WAVE(mints_before) − 1))

function MATCH_TEMPLATE(script, lock):
    # the 79 commitments and the final key are free; everything else is fixed
    if len(script) ≠ 1985: return false
    C ← [script[1 + 21·i : 21 + 21·i] for i in 0 … 78]        # each push is 0x14 ‖ 20 bytes
    pub ← script[-34 : -1]                                      # 0x21 ‖ 33 bytes ‖ OP_CHECKSIG
    return BUILD_TEMPLATE(C, pub, lock) = script

function WITNESS_SHAPE(wit, lock):                  # h = 9 positions, c = 4 pins
    if len(wit) ≠ 2 + 5·9 + 4: return false
    if wit[0] = empty or last(wit[0]) ≠ 0x01: return false
    end ← len(wit) − 1
    for pos in 0 … 8:                                # position 0 sits just below the script
        n ← 6 if pos < 4 else 5
        g ← wit[end − n : end] ; end ← end − n
        (tag?, T, A, Σ, B, j) ← g
        if not DER(T) or r(T) ≠ R_HALF or ht(T) ≠ 0x01 or len(T) ≠ lock.D[pos]: return false
        if not DER(Σ) or r(Σ) ≠ R_HALF or ht(Σ) ≠ 0x01:                          return false
        if len(A) ≠ 33 or len(B) ≠ 33 or len(j) > 1:                             return false
        if pos < 4 and len(tag) ≠ 32:                                             return false
    return end = 1

function DER(sig):                                   # 30 L 02 rl r 02 sl s ht
    return len(sig) ≥ 9 and sig[0] = 0x30 and sig[1] = len(sig) − 3 and sig[2] = 0x02
       and rl > 0 and sig[4 + rl] = 0x02 and sl > 0 and 6 + rl + sl = len(sig) − 1

BUILD_TEMPLATE emits exactly the byte sequence on Byte layouts, with D_ℓ taken from the level.

Notes

  • tx.txid may be taken from the node or recomputed from the non-witness serialization; they MUST agree.
  • The loop over levels tests 80 first. A transaction at 88 bits matches at 88 after failing at 80 (the scripts differ in one byte); under Draft C it gets no extra credit.
  • WAVE(m) is the smallest n with m < END[n] (Constants). Condition 8 checks the lock value against that same R.
  • Under the earlier drafts, condition 7 was SCORE_FP(β, txid) < ONE · D (Score).
  • If neither level matches, the reported code is the one from the lowest level (usually E03).