Skip to content

Improve pruning process - #402

Closed
stringhandler wants to merge 5 commits into
BlockstreamResearch:masterfrom
stringhandler:st-prune-merge-cases
Closed

stringhandler wants to merge 5 commits into
BlockstreamResearch:masterfrom
stringhandler:st-prune-merge-cases

Conversation

@stringhandler

Copy link
Copy Markdown
Contributor

No description provided.

Pruning built types for every node of the program, including the nodes
in the branches that it then hid. A node which a hidden branch shares
with the rest of the program kept that branch's type constraints, so
the types of the pruned program were narrower than those a decoder
infers from it. Witness values were pruned to those narrower types, so
the encoded program failed to decode with trailing witness bytes, or
decoded to a different IHR.

First walk the program from the root, descending only into branches
which were used, to find the nodes to keep. Then build the pruned
program from those nodes alone, children first, so that nodes in hidden
branches add no type constraints, and fix the program type to 1 -> 1 as
a decoder does. This replaces the Pruner converter.

The bug predates BlockstreamResearch#379, which made it much more likely by sharing nodes
with equal IHRs: the same program with a pointer-shared node fails
without BlockstreamResearch#379 too. The regression test covers both kinds of sharing, and
also checks the pruned program against libsimplicity.

Fixes BlockstreamResearch#391.

Assisted-by: Claude
b841d93 upgraded the nightly in .github/workflows/fuzz.yml by hand, but
not in the template in fuzz/generate-files.sh, so running the script, as
is required when adding a fuzz target, downgraded it to
nightly-2024-07-01.

Assisted-by: Claude
The existing generator picks a combinator and then type checks it, so
few of its programs are well-typed, and those are tiny. Add a generator
which picks a type first, and then only combinators which can have that
type, so every program is well-typed. It never generates fail,
assertions, disconnect or jets which can fail, so every program also
runs. It reuses nodes, so that pruning sees nodes shared between hidden
and kept code. It uses Elements jets, because libsimplicity decodes
Elements jet codes.

Add the prune_generated target, which prunes each program and checks
that:

* the pruned program keeps the CMR and runs,
* it decodes to the same IHR and encodes back to the same bytes,
* libsimplicity accepts it and computes the same CMR, AMR and IHR,
* libsimplicity rejects the unpruned program as anti-DOS exactly when
  pruning hid a branch.

Without the previous fix, this target finds BlockstreamResearch#391 within two minutes. It
also finds the bug that the next commit fixes.

Also build ElementsEnv::dummy with the test-utils feature, so that the
fuzz crate can run Elements jets.

Assisted-by: Claude
The tracker records which branches each case node used by IHR. Two case
nodes with different types have different IHRs, so pruning can hide a
different branch in each. Pruning can then make their types equal, and
the IHR of a case node does not show which branch it hides, if that
branch holds no witness. An encoder then gives both case nodes the same
node, which hides a branch that the other one uses, and the encoded
program reaches a pruned branch.

Prune in rounds: after rebuilding, merge the case nodes whose IHRs in
the pruned program are equal, let them share the branches that any of
them used, and rebuild. Every round that does not finish merges two
groups of case nodes, so there are at most as many rounds as case nodes.

Found by the prune_generated fuzz target.

Assisted-by: Claude
Two case unit unit nodes with different types, one of which only takes
its right branch and the other only its left. Without the previous
commit, the encoded pruned program reaches a pruned branch, in Rust and
in libsimplicity.

The program is built with the constructor API, because human encoding
merges nodes with equal CMRs and so cannot express it.

Assisted-by: Claude
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.

1 participant