Skip to content

@smartledger/scriptmin 0.1.3

Latest

Choose a tag to compare

@codenlighten codenlighten released this 18 Sep 00:30

Data and standard templates are kept

A script can be shorter without being the same output. Earlier versions made that mistake on real mainnet scripts, so both protections below are on by default:

  • Data that nothing uses is kept byte for byte. A push no operation takes as an operand, and that is not left on the stack, is a data carrier: <document> OP_DROP <pubkey> OP_CHECKSIG (121 B → 35 B in 0.1.2), signed protocol fields dropped in pairs, a Boost puzzle's content hash. Removing them leaves the script doing the same thing, but the output no longer carries what it was made to carry. Where a region carries data, its one-byte fields and empty pushes are kept too. --drop-unused-data (keepData: false) allows the old behaviour.
  • Standard output templates come back unchanged: P2PKH, P2PK, P2SH, bare multisig and OP_RETURN data outputs. A bare multisig listing the same key twice could reuse it with OP_OVER and save 65 bytes, and stop being a bare multisig, which wallets, indexers and relay policy recognise by its exact bytes. --rewrite-templates (templates: false) optimizes them anyway.

Generated contract code is unaffected: across 72 mainnet script templates the savings are the same as with the protections off, and the Miller loop, the final exponentiation and 19 other module scripts come out byte for byte as before.

--effort high no longer wastes minutes

The superoptimizer's A* search costs more the deeper the stack a window starts from, while its wins do not: on real scripts every replacement it found started from a stack of at most 15 items, and the searches on stacks of 20 or more found nothing while taking most of the time. Those windows now go to the lookup table.

script bytes before after
field-300.hex 2,607 45 s 8.9 s
a 934 B mainnet contract 694 155 s 51 s

Output is byte for byte what it was.

Also

  • The reason ASM output is refused now names the real cause for data after a top-level OP_RETURN too.
  • New: docs/mainnet-survey.md and examples/mainnet-survey.js — what mainnet locking scripts are made of, and what this saves. Half of outputs are P2PKH holding under 3% of the bytes; most bytes are data; the saving is concentrated in contracts that check their own transaction (a fifth to a third of compiled contract bytes, under 1% of all locking-script bytes).

Install

npm install @smartledger/scriptmin @smartledger/bsv