Skip to content

Detect key reuse in policy and Miniscript (was: Optimise policy compilation when keys are shared across branches) #115

@darosior

Description

@darosior

Currently, the policy needs to be tweaked to get the most optimized Bitcoin Script when thresh() that share some keys are used across or() branches.

For example:

or(thresh(4,pk(A),pk(B),pk(C),pk(D)), thresh(4,pk(A),pk(B),pk(E),pk(F)))

Currently returns (on http://bitcoin.sipa.be/miniscript/, but tested the same behaviour with rust-bitcoin locally)

OP_IF
  <A> OP_CHECKSIGVERIFY <B> OP_CHECKSIGVERIFY <C> OP_CHECKSIGVERIFY <D>
OP_ELSE
  <A> OP_CHECKSIGVERIFY <B> OP_CHECKSIGVERIFY <E> OP_CHECKSIGVERIFY <F>
OP_ENDIF
OP_CHECKSIG

    Script: 282 WU
    Input: 293.500000 WU
    Total: 575.500000 WU

And needs to be optimized by hand:

and(thresh(2,pk(A),pk(B)),or(thresh(2,pk(C),pk(D)),thresh(2,pk(E),pk(F))))

To return

<A> OP_CHECKSIGVERIFY <B> OP_CHECKSIGVERIFY OP_IF
  <C> OP_CHECKSIGVERIFY <D>
OP_ELSE
  <E> OP_CHECKSIGVERIFY <F>
OP_ENDIF
OP_CHECKSIG

    Script: 212 WU
    Input: 293.500000 WU
    Total: 505.500000 WU

Would a PR adding an analysis of duplicated keys to the policy compiler be welcome ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions