Skip to content

Template hash hardening (breaking change) - #143

Merged
someone235 merged 4 commits into
kaspanet:masterfrom
michaelsutton:template-hash-hardening
Jul 12, 2026
Merged

Template hash hardening (breaking change)#143
someone235 merged 4 commits into
kaspanet:masterfrom
michaelsutton:template-hash-hardening

Conversation

@michaelsutton

Copy link
Copy Markdown
Contributor

This PR changes the canonical template hash from:

H(prefix || suffix)

to:

H(i64le(prefix.length) || prefix || i64le(suffix.length) || suffix)

Lengths are encoded as fixed-width 8-byte little-endian integers.

This is a breaking change for contracts using readInputStateWithTemplate or validateOutputStateWithTemplate. Existing template hashes must be recalculated.

The new templateHash(prefix, suffix) builtin exposes the canonical calculation to Silverscript contracts. Rust callers can use template::template_hash(prefix, suffix) or CompiledContract::template_hash().

Include fixed-width prefix and suffix lengths when validating template hashes in readInputStateWithTemplate and validateOutputStateWithTemplate.

This changes the template hash format and breaks existing tests until Rust-side hash calculations and fixtures are updated.
Exposes templateHash(prefix, suffix), allowing contracts to reconstruct the canonical length-bound template hash from revealed template parts.

Supports cases where a template hash is not committed directly, but is contained in a higher-level commitment such as a route table or a hash over several template hashes. The contract can open that commitment, reconstruct the individual template hash, and use it for state validation.

Documents the builtin, updates template-related examples to use the canonical hash and camelCase naming, and adds an execution test proving that the prefix/suffix boundary affects the result.
- introduced a shared length-bound template hash helper
- exposed the canonical hash through CompiledContract
- migrated chess contracts and tests to templateHash
- refreshed chess size snapshots and editor highlighting
- locked Rust and Sil hashing to shared canonical vectors
- verified templateHash against both *WithTemplate builtins at runtime
@someone235
someone235 merged commit 956868e into kaspanet:master Jul 12, 2026
4 checks passed
@michaelsutton
michaelsutton deleted the template-hash-hardening branch July 12, 2026 09:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants