Reuse input templates for output-state validation#144
Open
michaelsutton wants to merge 6 commits into
Open
Conversation
Adds validateOutputStateWithInputTemplate for validating an output with a template already present in another input's redeem script. The new builtin extracts only the required prefix and suffix through sigscript substring introspection, binds each slice once on the stack, and lowers validation through the existing validateOutputStateWithTemplate path. Adds builtin documentation, editor highlighting, and runtime tests covering canonical template hashes, invalid lengths, nested state layouts, and shared lowering semantics.
Adds expected sigscript totals to the chess size snapshots and checks them against the complete P2SH sigscripts of the transactions exercised by the runtime tests. This establishes a witness-size baseline. A follow-up will replace eligible validateOutputStateWithTemplate calls with validateOutputStateWithInputTemplate and record the resulting reductions.
Updates ChessSettle to reconstruct the shared Player template from an existing Player input instead of receiving its full prefix and suffix in the settle sigscript. The settlement transaction now supplies only the template lengths. Total sigscript size falls from 13,407 to 10,424 bytes, a 2,983-byte reduction, while the settlement contract grows by 209 bytes.
Updates KCC20Minter to validate both asset outputs using the template already present in the sole KCC20 input. Removes the full template prefix and suffix from the minter constructor while retaining their lengths and the committed template hash. The same input index is reused for prior-state reading and both output validations. Synchronizes the KCC20 book source, scenario, and template-validation explanations with the optimized flow.
Contributor
Author
|
Noticed after the fact that this addresses and closes #140 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
validateOutputStateWithTemplaterequires the caller to provide the targettemplate prefix and suffix. In many multi-input covenant transactions, those
exact bytes are already present at the end of another input's sigscript as its
P2SH redeem script.
Passing or embedding them again duplicates potentially large contract
templates:
This PR adds:
The builtin:
templateInputIndex.OpTxInputScriptSigSubstr, the provided lengths, and the encoded state sizederived by the compiler from
nextState's layout.validateOutputStateWithTemplatepath,which authenticates their length-delimited template hash against
expectedTemplateHashand validates the output state.The referenced input supplies the template bytes, but it is not the trust
anchor. Its existing UTXO commits to the full redeem script, while
expectedTemplateHashremains the protocol commitment that authenticates theextracted template.
Chess settlement
Chess settlement spends one settlement input and two Player inputs, then
recreates both Player outputs. Previously the settlement sigscript carried a
full Player template again:
It now carries only the lengths and reuses the Player template from the first
Player input:
The chess tests now track complete P2SH sigscript bytes across the actual
transactions they execute.
The additional extraction logic costs 209 bytes in the settlement redeem
script, but removes 3,192 bytes of duplicated template arguments, producing a
net reduction of 2,983 bytes.
KCC20 minter
The same reuse applies to KCC20 minting. The minter validates two KCC20 outputs whose template is already present in the co-spent KCC20 input, so the new builtin avoids carrying the same prefix and suffix again in the minter sigscript.
Applying this also cleaned up an inefficiency in the original KCC20 sketch. Its prefix and suffix were constructor constants, although only
expectedTemplateHashneeds to be committed. The template bytes themselves may remain untrusted as long as output validation checks them against that hash.Embedding them as constants was also costly. Each
validateOutputStateWithTemplatecall uses the prefix and suffix three times: for their encoded lengths, the template-hash preimage, and the expected output script. Since minting validates two outputs, the complete template pair appeared six times in the minter redeem script.Moving the template bytes out of the constructor accounts for the larger initial reduction. The new builtin then removes the remaining sigscript copy by extracting those bytes from the KCC20 input.
expectedTemplateHashremains committed in the minter contract and authenticates the reused template.Tests and tooling
templateHash,readInputStateWithTemplate,validateOutputStateWithTemplate, and the newbuiltin.
highlighting files.