Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compiler: improve logic when deciding between conjunctions and multi/multi_a #657

Merged
merged 5 commits into from
Mar 12, 2024

Conversation

apoelstra
Copy link
Member

The compiler logic when encountering thresholds of pks is currently a bit confused, and therefore chooses multi/multi_a in cases where this is not the most efficient compilation.

This PR fixes that, and also brings in a few other cleanup commits that I had laying around.

This does not fix #656, which I didn't know how to approach.

There is more to come, but do a bunch now because it's cathartic.
When you grep the codebase for FIXME it's nice if the output isn't
overwhelmed by the same message repeated 20 times.
This matches the same variant in Terminal, matches the string
serialization "thresh", and will avoid weird compiler errors related to
collisions with the upcoming `Threshold` type.

This will be an annoying API-breaking change, but fortunately(?) this PR
is already breaking the API for thresholds in an annoying way.
In our threshold logic we have some special cases to handle multi,
multi_a, and conjunctions. However, we were not choosing between
these special cases correctly. Our logic was roughly that we would
try to use multi_a if all children were pk()s, OR try to use multi
if all children were pk() and there weren't too many, OR try to
use a conjunction if k == n.

The correct logic is: if all children are keys, and there aren't
too many, try to use multi or multi_a. ALSO, if k == n, try to
use a conjunction.

With this fix, the compiler correctly finds that conjunctions are more
efficient than CHECKMULTISIG when k == n and n < 3. When n == 3 the
two cases have equal cost, but it seems to prefer the conjunction. It
also correctly finds that when k == n, it is always more efficient to
use a conjunction than to use CHECKSIGADD.

This change necessitates changing some tests.
Copy link
Member

@sanket1729 sanket1729 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 200991d

@apoelstra apoelstra merged commit f6ffc3e into rust-bitcoin:master Mar 12, 2024
16 checks passed
@apoelstra apoelstra deleted the 2024-03--and-multi branch March 12, 2024 23:23
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.

Can trigger assertation failure in experimental compiler
2 participants