Skip to content

Commit bada963

Browse files
committed
Merge bitcoin/bitcoin#24043: Add (sorted)multi_a descriptor for k-of-n multisig inside tr
4828d53 Add (sorted)multi_a descriptors to doc/descriptors.md (Pieter Wuille) b5f33ac Simplify wallet_taproot.py functional test (Pieter Wuille) eb0667e Add tests for (sorted)multi_a derivation/signing (Pieter Wuille) c17c6aa Add signing support for (sorted)multi_a scripts (Pieter Wuille) 3eed6fc Add multi_a descriptor inference (Pieter Wuille) 79728c4 Add (sorted)multi_a descriptor and script derivation (Pieter Wuille) 25e95f9 Merge/generalize IsValidMultisigKeyCount/GetMultisigKeyCount (Pieter Wuille) Pull request description: This adds a new `multi_a(k,key_1,key_2,...,key_n)` (and corresponding `sortedmulti_a`) descriptor for k-of-n policies inside `tr()`. Semantically it is very similar to the existing `multi()` descriptor, but with the following changes: * The corresponding script is `<key1> OP_CHECKSIG <key2> OP_CHECKSIGADD <key3> OP_CHECKSIGADD ... <key_n> OP_CHECKSIGADD <k> OP_NUMEQUAL`, rather than the traditional `OP_CHECKMULTISIG`-based script, making it usable inside the `tr()` descriptor. * The keys can optionally be specified in x-only notation. * Both the number of keys and the threshold can be as high as 999; this is the limit due to the consensus stacksize=1000 limit I expect that this functionality will later be replaced with a miniscript-based implementation, but I don't think it's necessary to wait for that. Limitations: * The wallet code will for not estimate witness size incorrectly for script path spends, which may result in a (dramatic) fee underpayment with large multi_a scripts. * The multi_a script construction is (slightly) suboptimal for n-of-n (where a `<key1> OP_CHECKSIGVERIFY ... <key_n-1> OP_CHECKSIGVERIFY <key_n> OP_CHECKSIG` would be better). Such a construction is not included here. ACKs for top commit: achow101: ACK 4828d53 gruve-p: ACK bitcoin/bitcoin@4828d53 sanket1729: code review ACK 4828d53 darosior: Code review ACK 4828d53 Tree-SHA512: 5dcd434b79585f0ff830f7d501d27df5e346f5749f47a3109ec309ebf2cbbad0e1da541eec654026d911ab67fd7cf7793fab0f765628d68d81b96ef2a4d234ce
2 parents 4fae737 + 4828d53 commit bada963

File tree

8 files changed

+223
-47
lines changed

8 files changed

+223
-47
lines changed

doc/descriptors.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Output descriptors currently support:
3333
- Pay-to-taproot outputs (P2TR), through the `tr` function.
3434
- Multisig scripts, through the `multi` function.
3535
- Multisig scripts where the public keys are sorted lexicographically, through the `sortedmulti` function.
36+
- Multisig scripts inside taproot script trees, through the `multi_a` (and `sortedmulti_a`) function.
3637
- Any type of supported address through the `addr` function.
3738
- Raw hex scripts through the `raw` function.
3839
- Public keys (compressed and uncompressed) in hex notation, or BIP32 extended pubkeys with derivation paths.
@@ -56,6 +57,7 @@ Output descriptors currently support:
5657
- `wsh(multi(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*))` describes a set of *1-of-2* P2WSH multisig outputs where the first multisig key is the *1/0/`i`* child of the first specified xpub and the second multisig key is the *0/0/`i`* child of the second specified xpub, and `i` is any number in a configurable range (`0-1000` by default).
5758
- `wsh(sortedmulti(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*))` describes a set of *1-of-2* P2WSH multisig outputs where one multisig key is the *1/0/`i`* child of the first specified xpub and the other multisig key is the *0/0/`i`* child of the second specified xpub, and `i` is any number in a configurable range (`0-1000` by default). The order of public keys in the resulting witnessScripts is determined by the lexicographic order of the public keys at that index.
5859
- `tr(c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5,{pk(fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556),pk(e493dbf1c10d80f3581e4904930b1404cc6c13900ee0758474fa94abe8c4cd13)})` describes a P2TR output with the `c6...` x-only pubkey as internal key, and two script paths.
60+
- `tr(c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5,sortedmulti_a(2,2f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4,5cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc))` describes a P2TR output with the `c6...` x-only pubkey as internal key, and a single `multi_a` script that needs 2 signatures with 2 specified x-only keys, which will be sorted lexicographically.
5961

6062
## Reference
6163

@@ -68,8 +70,10 @@ Descriptors consist of several types of expressions. The top level expression is
6870
- `pkh(KEY)` (not inside `tr`): P2PKH output for the given public key (use `addr` if you only know the pubkey hash).
6971
- `wpkh(KEY)` (top level or inside `sh` only): P2WPKH output for the given compressed pubkey.
7072
- `combo(KEY)` (top level only): an alias for the collection of `pk(KEY)` and `pkh(KEY)`. If the key is compressed, it also includes `wpkh(KEY)` and `sh(wpkh(KEY))`.
71-
- `multi(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script.
73+
- `multi(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script using OP_CHECKMULTISIG.
7274
- `sortedmulti(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script with keys sorted lexicographically in the resulting script.
75+
- `multi_a(k,KEY_1,KEY_2,...,KEY_N)` (only inside `tr`): k-of-n multisig script using OP_CHECKSIG, OP_CHECKSIGADD, and OP_NUMEQUAL.
76+
- `sortedmulti_a(k,KEY_1,KEY_2,...,KEY_N)` (only inside `tr`): similar to `multi_a`, but the (x-only) public keys in it will be sorted lexicographically.
7377
- `tr(KEY)` or `tr(KEY,TREE)` (top level only): P2TR output with the specified key as internal key, and optionally a tree of script paths.
7478
- `addr(ADDR)` (top level only): the script which ADDR expands to.
7579
- `raw(HEX)` (top level only): the script whose hex encoding is HEX.

src/script/descriptor.cpp

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,30 @@ class MultisigDescriptor final : public DescriptorImpl
802802
bool IsSingleType() const final { return true; }
803803
};
804804

805+
/** A parsed (sorted)multi_a(...) descriptor. Always uses x-only pubkeys. */
806+
class MultiADescriptor final : public DescriptorImpl
807+
{
808+
const int m_threshold;
809+
const bool m_sorted;
810+
protected:
811+
std::string ToStringExtra() const override { return strprintf("%i", m_threshold); }
812+
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override {
813+
CScript ret;
814+
std::vector<XOnlyPubKey> xkeys;
815+
for (const auto& key : keys) xkeys.emplace_back(key);
816+
if (m_sorted) std::sort(xkeys.begin(), xkeys.end());
817+
ret << ToByteVector(xkeys[0]) << OP_CHECKSIG;
818+
for (size_t i = 1; i < keys.size(); ++i) {
819+
ret << ToByteVector(xkeys[i]) << OP_CHECKSIGADD;
820+
}
821+
ret << m_threshold << OP_NUMEQUAL;
822+
return Vector(std::move(ret));
823+
}
824+
public:
825+
MultiADescriptor(int threshold, std::vector<std::unique_ptr<PubkeyProvider>> providers, bool sorted = false) : DescriptorImpl(std::move(providers), sorted ? "sortedmulti_a" : "multi_a"), m_threshold(threshold), m_sorted(sorted) {}
826+
bool IsSingleType() const final { return true; }
827+
};
828+
805829
/** A parsed sh(...) descriptor. */
806830
class SHDescriptor final : public DescriptorImpl
807831
{
@@ -1040,7 +1064,6 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
10401064
using namespace spanparsing;
10411065

10421066
auto expr = Expr(sp);
1043-
bool sorted_multi = false;
10441067
if (Func("pk", expr)) {
10451068
auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
10461069
if (!pubkey) return nullptr;
@@ -1065,7 +1088,12 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
10651088
error = "Can only have combo() at top level";
10661089
return nullptr;
10671090
}
1068-
if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH) && ((sorted_multi = Func("sortedmulti", expr)) || Func("multi", expr))) {
1091+
const bool multi = Func("multi", expr);
1092+
const bool sortedmulti = !multi && Func("sortedmulti", expr);
1093+
const bool multi_a = !(multi || sortedmulti) && Func("multi_a", expr);
1094+
const bool sortedmulti_a = !(multi || sortedmulti || multi_a) && Func("sortedmulti_a", expr);
1095+
if (((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH) && (multi || sortedmulti)) ||
1096+
(ctx == ParseScriptContext::P2TR && (multi_a || sortedmulti_a))) {
10691097
auto threshold = Expr(expr);
10701098
uint32_t thres;
10711099
std::vector<std::unique_ptr<PubkeyProvider>> providers;
@@ -1086,9 +1114,12 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
10861114
providers.emplace_back(std::move(pk));
10871115
key_exp_index++;
10881116
}
1089-
if (providers.empty() || providers.size() > MAX_PUBKEYS_PER_MULTISIG) {
1117+
if ((multi || sortedmulti) && (providers.empty() || providers.size() > MAX_PUBKEYS_PER_MULTISIG)) {
10901118
error = strprintf("Cannot have %u keys in multisig; must have between 1 and %d keys, inclusive", providers.size(), MAX_PUBKEYS_PER_MULTISIG);
10911119
return nullptr;
1120+
} else if ((multi_a || sortedmulti_a) && (providers.empty() || providers.size() > MAX_PUBKEYS_PER_MULTI_A)) {
1121+
error = strprintf("Cannot have %u keys in multi_a; must have between 1 and %d keys, inclusive", providers.size(), MAX_PUBKEYS_PER_MULTI_A);
1122+
return nullptr;
10921123
} else if (thres < 1) {
10931124
error = strprintf("Multisig threshold cannot be %d, must be at least 1", thres);
10941125
return nullptr;
@@ -1109,10 +1140,17 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
11091140
return nullptr;
11101141
}
11111142
}
1112-
return std::make_unique<MultisigDescriptor>(thres, std::move(providers), sorted_multi);
1113-
} else if (Func("sortedmulti", expr) || Func("multi", expr)) {
1143+
if (multi || sortedmulti) {
1144+
return std::make_unique<MultisigDescriptor>(thres, std::move(providers), sortedmulti);
1145+
} else {
1146+
return std::make_unique<MultiADescriptor>(thres, std::move(providers), sortedmulti_a);
1147+
}
1148+
} else if (multi || sortedmulti) {
11141149
error = "Can only have multi/sortedmulti at top level, in sh(), or in wsh()";
11151150
return nullptr;
1151+
} else if (multi_a || sortedmulti_a) {
1152+
error = "Can only have multi_a/sortedmulti_a inside tr()";
1153+
return nullptr;
11161154
}
11171155
if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH) && Func("wpkh", expr)) {
11181156
auto pubkey = ParsePubkey(key_exp_index, expr, ParseScriptContext::P2WPKH, out, error);
@@ -1257,13 +1295,33 @@ std::unique_ptr<PubkeyProvider> InferXOnlyPubkey(const XOnlyPubKey& xkey, ParseS
12571295
return key_provider;
12581296
}
12591297

1298+
std::unique_ptr<DescriptorImpl> InferMultiA(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
1299+
{
1300+
auto match = MatchMultiA(script);
1301+
if (!match) return {};
1302+
std::vector<std::unique_ptr<PubkeyProvider>> keys;
1303+
keys.reserve(match->second.size());
1304+
for (const auto keyspan : match->second) {
1305+
if (keyspan.size() != 32) return {};
1306+
auto key = InferXOnlyPubkey(XOnlyPubKey{keyspan}, ctx, provider);
1307+
if (!key) return {};
1308+
keys.push_back(std::move(key));
1309+
}
1310+
return std::make_unique<MultiADescriptor>(match->first, std::move(keys));
1311+
}
1312+
12601313
std::unique_ptr<DescriptorImpl> InferScript(const CScript& script, ParseScriptContext ctx, const SigningProvider& provider)
12611314
{
12621315
if (ctx == ParseScriptContext::P2TR && script.size() == 34 && script[0] == 32 && script[33] == OP_CHECKSIG) {
12631316
XOnlyPubKey key{Span{script}.subspan(1, 32)};
12641317
return std::make_unique<PKDescriptor>(InferXOnlyPubkey(key, ctx, provider), true);
12651318
}
12661319

1320+
if (ctx == ParseScriptContext::P2TR) {
1321+
auto ret = InferMultiA(script, ctx, provider);
1322+
if (ret) return ret;
1323+
}
1324+
12671325
std::vector<std::vector<unsigned char>> data;
12681326
TxoutType txntype = Solver(script, data);
12691327

src/script/script.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ static const int MAX_OPS_PER_SCRIPT = 201;
2929
// Maximum number of public keys per multisig
3030
static const int MAX_PUBKEYS_PER_MULTISIG = 20;
3131

32+
/** The limit of keys in OP_CHECKSIGADD-based scripts. It is due to the stack limit in BIP342. */
33+
static constexpr unsigned int MAX_PUBKEYS_PER_MULTI_A = 999;
34+
3235
// Maximum script length in bytes
3336
static const int MAX_SCRIPT_SIZE = 10000;
3437

src/script/sign.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@ static bool SignTaprootScript(const SigningProvider& provider, const BaseSignatu
174174
result = Vector(std::move(sig));
175175
return true;
176176
}
177+
return false;
178+
}
179+
180+
// multi_a scripts (<key> OP_CHECKSIG <key> OP_CHECKSIGADD <key> OP_CHECKSIGADD <k> OP_NUMEQUAL)
181+
if (auto match = MatchMultiA(script)) {
182+
std::vector<std::vector<unsigned char>> sigs;
183+
int good_sigs = 0;
184+
for (size_t i = 0; i < match->second.size(); ++i) {
185+
XOnlyPubKey pubkey{*(match->second.rbegin() + i)};
186+
std::vector<unsigned char> sig;
187+
bool good_sig = CreateTaprootScriptSig(creator, sigdata, provider, sig, pubkey, leaf_hash, sigversion);
188+
if (good_sig && good_sigs < match->first) {
189+
++good_sigs;
190+
sigs.push_back(std::move(sig));
191+
} else {
192+
sigs.emplace_back();
193+
}
194+
}
195+
if (good_sigs == match->first) {
196+
result = std::move(sigs);
197+
return true;
198+
}
199+
return false;
177200
}
178201

179202
return false;

src/script/standard.cpp

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,51 +96,83 @@ static constexpr bool IsPushdataOp(opcodetype opcode)
9696
return opcode > OP_FALSE && opcode <= OP_PUSHDATA4;
9797
}
9898

99-
static constexpr bool IsValidMultisigKeyCount(int n_keys)
100-
{
101-
return n_keys > 0 && n_keys <= MAX_PUBKEYS_PER_MULTISIG;
102-
}
103-
104-
static bool GetMultisigKeyCount(opcodetype opcode, valtype data, int& count)
99+
/** Retrieve a minimally-encoded number in range [min,max] from an (opcode, data) pair,
100+
* whether it's OP_n or through a push. */
101+
static std::optional<int> GetScriptNumber(opcodetype opcode, valtype data, int min, int max)
105102
{
103+
int count;
106104
if (IsSmallInteger(opcode)) {
107105
count = CScript::DecodeOP_N(opcode);
108-
return IsValidMultisigKeyCount(count);
109-
}
110-
111-
if (IsPushdataOp(opcode)) {
112-
if (!CheckMinimalPush(data, opcode)) return false;
106+
} else if (IsPushdataOp(opcode)) {
107+
if (!CheckMinimalPush(data, opcode)) return {};
113108
try {
114109
count = CScriptNum(data, /* fRequireMinimal = */ true).getint();
115-
return IsValidMultisigKeyCount(count);
116110
} catch (const scriptnum_error&) {
117-
return false;
111+
return {};
118112
}
113+
} else {
114+
return {};
119115
}
120-
121-
return false;
116+
if (count < min || count > max) return {};
117+
return count;
122118
}
123119

124120
static bool MatchMultisig(const CScript& script, int& required_sigs, std::vector<valtype>& pubkeys)
125121
{
126122
opcodetype opcode;
127123
valtype data;
128-
int num_keys;
129124

130125
CScript::const_iterator it = script.begin();
131126
if (script.size() < 1 || script.back() != OP_CHECKMULTISIG) return false;
132127

133-
if (!script.GetOp(it, opcode, data) || !GetMultisigKeyCount(opcode, data, required_sigs)) return false;
128+
if (!script.GetOp(it, opcode, data)) return false;
129+
auto req_sigs = GetScriptNumber(opcode, data, 1, MAX_PUBKEYS_PER_MULTISIG);
130+
if (!req_sigs) return false;
131+
required_sigs = *req_sigs;
134132
while (script.GetOp(it, opcode, data) && CPubKey::ValidSize(data)) {
135133
pubkeys.emplace_back(std::move(data));
136134
}
137-
if (!GetMultisigKeyCount(opcode, data, num_keys)) return false;
138-
139-
if (pubkeys.size() != static_cast<unsigned long>(num_keys) || num_keys < required_sigs) return false;
135+
auto num_keys = GetScriptNumber(opcode, data, required_sigs, MAX_PUBKEYS_PER_MULTISIG);
136+
if (!num_keys) return false;
137+
if (pubkeys.size() != static_cast<unsigned long>(*num_keys)) return false;
140138

141139
return (it + 1 == script.end());
142140
}
143141

142+
std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script)
143+
{
144+
std::vector<Span<const unsigned char>> keyspans;
145+
146+
// Redundant, but very fast and selective test.
147+
if (script.size() == 0 || script[0] != 32 || script.back() != OP_NUMEQUAL) return {};
148+
149+
// Parse keys
150+
auto it = script.begin();
151+
while (script.end() - it >= 34) {
152+
if (*it != 32) return {};
153+
++it;
154+
keyspans.emplace_back(&*it, 32);
155+
it += 32;
156+
if (*it != (keyspans.size() == 1 ? OP_CHECKSIG : OP_CHECKSIGADD)) return {};
157+
++it;
158+
}
159+
if (keyspans.size() == 0 || keyspans.size() > MAX_PUBKEYS_PER_MULTI_A) return {};
160+
161+
// Parse threshold.
162+
opcodetype opcode;
163+
std::vector<unsigned char> data;
164+
if (!script.GetOp(it, opcode, data)) return {};
165+
if (it == script.end()) return {};
166+
if (*it != OP_NUMEQUAL) return {};
167+
++it;
168+
if (it != script.end()) return {};
169+
auto threshold = GetScriptNumber(opcode, data, 1, (int)keyspans.size());
170+
if (!threshold) return {};
171+
172+
// Construct result.
173+
return std::pair{*threshold, std::move(keyspans)};
174+
}
175+
144176
TxoutType Solver(const CScript& scriptPubKey, std::vector<std::vector<unsigned char>>& vSolutionsRet)
145177
{
146178
vSolutionsRet.clear();

src/script/standard.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ CScript GetScriptForDestination(const CTxDestination& dest);
191191
/** Generate a P2PK script for the given pubkey. */
192192
CScript GetScriptForRawPubKey(const CPubKey& pubkey);
193193

194+
/** Determine if script is a "multi_a" script. Returns (threshold, keyspans) if so, and nullopt otherwise.
195+
* The keyspans refer to bytes in the passed script. */
196+
std::optional<std::pair<int, std::vector<Span<const unsigned char>>>> MatchMultiA(const CScript& script LIFETIMEBOUND);
197+
194198
/** Generate a multisig script. */
195199
CScript GetScriptForMultisig(int nRequired, const std::vector<CPubKey>& keys);
196200

test/functional/test_framework/script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .ripemd160 import ripemd160
2828

2929
MAX_SCRIPT_ELEMENT_SIZE = 520
30+
MAX_PUBKEYS_PER_MULTI_A = 999
3031
LOCKTIME_THRESHOLD = 500000000
3132
ANNEX_TAG = 0x50
3233

0 commit comments

Comments
 (0)