Skip to content

@smartledger/scriptmin 0.1.0

Choose a tag to compare

@codenlighten codenlighten released this 17 Sep 12:37
· 14 commits to main since this release

A Bitcoin Script minimizer. It takes a working locking script, returns a smaller one that does exactly the same thing, and proves the two are equivalent.

What's in it

  • scriptmin <script>: minimizes a script and reports where the savings came from. It uses:
    • a liveness-driven stack scheduler, with an optional beam search over its choices;
    • an exhaustive table plus A* superoptimizer for short stack sequences;
    • peephole rules and constant folding.
  • scriptmin --profile: shows where a script's bytes go (stack moves, arithmetic, constants, hashing), how deep its PICKs and ROLLs reach, and its most expensive repeated patterns.
  • scriptmin compile: compiles a prime-field circuit (JSON) to Script with lazy modular reduction.
  • scriptmin relax: lifts existing field-arithmetic Script back to its circuit and re-decides where it reduces.

Correctness

  • Symbolic proof: every result is proven equivalent region by region. It fails on exactly the stacks the original fails on, leaves identical main and alt stacks, and evaluates the same set of checks.
  • Interpreter tests: results are also run against the original on @smartledger/bsv's interpreter.
  • relax: its results are checked on the interpreter against the original, on canonical inputs and out-of-range ones.

Measured on real code

On script-high-level-modules, a library of BSV Script modules up to a full BLS12-381 pairing:

  • Miller loop: the 333 KB loop that ran on mainnet drops 40%, to 199 KB, and every test case of the library still passes.
  • Mainnet redeployment: the library's two-transaction pairing chain was redeployed to mainnet with minimized stage scripts.

Details are in docs/real-world.md.

Install

npm install @smartledger/scriptmin @smartledger/bsv

Requires Node 20.19+.