Skip to content

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

Merged
apoelstra merged 5 commits intorust-bitcoin:masterfrom
apoelstra:2024-03--and-multi
Mar 12, 2024
Merged

compiler: improve logic when deciding between conjunctions and multi/multi_a#657
apoelstra merged 5 commits intorust-bitcoin:masterfrom
apoelstra:2024-03--and-multi

Conversation

@apoelstra
Copy link
Copy Markdown
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.
@apoelstra apoelstra force-pushed the 2024-03--and-multi branch from 489c851 to 200991d Compare March 12, 2024 15:09
Copy link
Copy Markdown
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
@apoelstra apoelstra deleted the 2024-03--and-multi branch March 12, 2024 23:23
heap-coder added a commit to heap-coder/rust-miniscript that referenced this pull request Sep 27, 2025
…deciding between conjunctions and multi/multi_a

200991dbed0411bcad36990b366feddc7580ae0b compiler: improve logic for deciding between thresholds and ands (Andrew Poelstra)
bc3b8dc2ec549d3b8e98c54f3347bac4a18b87b8 policy: rename Threshold variant to Thresh (Andrew Poelstra)
78db616daa4335f211493436d8aa27613cda4bf4 consolidate some FIXMEs (Andrew Poelstra)
0666aef121424357b7f4e254f3ac950894ab8236 error: remove some unused variants (Andrew Poelstra)
b2ec4a878d66ad660948c081c266c46a6b9ffc2a clippy: fix some nits introduced by #651 (Andrew Poelstra)

Pull request description:

  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.

ACKs for top commit:
  sanket1729:
    ACK 200991dbed0411bcad36990b366feddc7580ae0b

Tree-SHA512: 252a60891cf1c1d1cd3ded88d97122fd1e76bd25807770f4843ae68bd2d854fc617518f26be86dcb57cd7fc369e1a4be81daa42ee1a6d4bc976dbad6dc1150f6
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