Repository navigation
Relax standardness rules regarding CHECKMULTISIG - #33755
roconnor-blockstream wants to merge 1 commit into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code Coverage & BenchmarksFor details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33755. ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsNo conflicts as of last run. |
|
If anyone has documentation showing the format (pre-OP_RETURN) this would be useful. |
https://docs.counterparty.io/docs/advanced/protocol/#transactions and https://github.com/CounterpartyXCP/counterparty-core/blob/master/counterparty-core/counterpartycore/lib/api/composer.py#L304 should give an idea. AFAICT the real pubkey goes last. |
a94f8f8 to
bbfaed4
Compare
|
This change affects enforcement of |
|
@ajtowns I did consider that. Firstly we should note that Regarding as to whether it should be weakened: Currently Bitcoin Core's implementation choice for CHECKMULTISIG is such that once enough signatures match the pubkeys, no further pubkeys in the CHECKMULTISIG stack are inspected for well-formedness policy constraints. That is to say, current policy lets you have uncompressed pubkeys (or at least data that looks like uncompressed pubkeys) on one side of the stack of pubkeys but not the other. This change in policy makes it (more) symmetric so that you can have uncompressed pubkeys (or data that looks like uncompressed pubkeys) on either end of the stack of pubkeys, but those uncompressed pubkeys cannot be used in passing signature checks. Now the Counterparty UTXO predates Segwit V0, so we could refactor the code to keep the current asymmetric policy rules regarding Segwit V0 script, and I would be fine with that. However, my current judgement is that, since data-that-looks-like-uncompressed-pubkeys is already allowed on one side of the pubkey stack in Segwit V0 script, it isn't worth the code complexity to maintain this policy for the sake of Segwit V0 script. There is no real advantage in doing that extra work. If other folks do think it is worthwhile for reasons I cannot imagine, I'm fine with that too. |
|
If you're deliberately changing the standardness rules specified in BIP143, that should be in the PR description, and possibly proposed as an update to the BIP. If you're not deliberately changing them, they shouldn't be changed by accident. Personally, I don't see the justification for changing them here, though I don't really understand why it was specced as a policy rule rather than a consensus one at the time, either. Personally, I don't really understand the motivation for reconsidering a policy rule that's been in place for 11 years, either, and for which new adoption would only be less fee efficient than what's already allowed. If there were real coins locked due to this policy, it seems like they should have been already recovered via getting a non-standard spend mined directly. |
|
I guess we could change BIP143. Currently the text reads:
Note that is not how Bitcoin Core implements So the current status is that the Edit: Or we could change how |
I don't know why you would think that they should have already been recovered. You think anyone can just call up a mining pool and get their non-standard transactions mined? How much money needs to be at stake before you can even get a pool's attention? Or how small in value do UTXOs have to be so that it is okay to soft-confiscate it since it isn't worth fixing for such a little amount? Anyhow, AFAIU Counterparty was using bare multisigs, so in theory we don't have to speculate and we can just look for such UTXOs that are waiting to be freed. Someone please correct me if I'm wrong and Counterparty was using p2sh multisigs. I haven't checked my IRC logs, but my recollection is the person I was chatting with who had their UTXO's soft-confiscated by Bitcoin Core's policy change decided the amount of funds were small and they were not going to pursue it. However what was a small amount of Bitcoin 11 years ago may not be a small amount today. Again, this soft-confiscation was inadvertent. PR #5247 was trying to address hybrid keys, not Counterparty UTXOs. An error was made and the result was arguably unjust towards those folks holding those UTXOs. Recent posts on the mailing list made me aware that people incorrectly believe that Bitcoin Core has never soft-confiscated folk's UTXOs before, but I know that isn't true. I'm not sure what my thinking was at the time, but had I thought there it was a deliberate policy decision against Counterparty transactions. However, upon rereviewing PR #5247 last night I came to understand the policy was never intended to soft-confiscate Counterparty UTXOs. And this morning I realized we can fix this injustice. Better late than never. Soft-confiscating these transactions was wrong, and we should fix mistakes if we can when we realize we have made one, even if the mistake was made 11 years ago. This code is the way PR #5247 ought to have been written in the first place. |
That's what things like slipstream allow today, so, yes? Template building by hashers also make that more accessible today than previously. In any event, if it's not worth calling up a single miner and getting it mined, it also doesn't seem worth changing the default policy for every miner that follows our defaults.
I don't think chastising the reader about confiscating coins is constructive. If you want to make those coins easily accessible now, that's a fine thing to argue for, but it should be done on its merits, not by attempting to guilt trip people. This PR isn't passing CI, for instance.
There seem to be six utxos from prior to block 400,000 that currently have a value of more than $100 USD that fit the description here:
The 3.8M sat output was worth about $11 at the time, so I could quite easily see it having been written off as a loss and access to the corresponding private keys not having been maintained in the meantime. There are an additional 15,133 outputs from prior to block 400k that look like they might fit this case, all with between 780 and 30,919 sats, totalling 1.0148,8939 BTC, which had a total value of about $472 if I pick the highest BTC price in the above timeframe. (Or at least there were as at block 897666 which is the utxo data I have handy for analysing. This likely includes some false positives as well, though I think I eliminated most things) For comparison, it looks to me like there was 0.9068,0410 BTC (so $422 at the time by the same logic) across perhaps 11k bare multisig addresses that can only be spent via uncompressed pubkeys (eg this looks like ~10M sats worth ~$10k USD today, sent to a 2/3 uncompressed bare multisig, from the same multisig, so at one point certainly spendable).
Soft-confiscating is always a risk with policy restrictions, but there is also already a straight-forward solution to all problems in that class: talk to a miner directly. |
|
Those outputs look like multisigs that are composed out of valid compressed or uncompressed pubkeys, and I believe spending them would pass standardness rules (except for 3482ea1d219120b13f3be49f63fd7079136785dff2bf7487726b640da5876e33:0, which appears to simply be a broken script). The UTXO's I'm looking for would have pubkeys with an invalid prefix, or an invalid length for their prefix. Based on the code prior to CounterpartyXCP/counterparty-core@f1a8c82 I would specifically expect a 1-of-3 multisig where at least 1 of the keys is 33 bytes but does not begin with 0x02 or 0x03. |
|
Bitcoin Core relay policy is largely motivated by not relaying third party malleable transactions. There is no reason to relay transactions if they are just going to be modified before they get mined. Given the, unfortunately extensive, amount of malleability generally afford by Bitcoin Script's design, sometimes policy just limits third party malleable transactions to some canonical form, one that minimizes transaction size so that miners are not incentivized to reduce the size further. Hence the policy around canonical pushes and IF arguments, cleanstack, etc. Closely related is policy around fee bumping, where policy is addressing abuse from first party malleable transactions to prevent excessively relaying transactions that aren't going to be mined due to replacement. There are a couple of relay policies around soft-fork points. The use of NOPn transactions are banned by policy. Again these forms of script are unused by folks, who have a perfectly good normal NOP operation that they could have used instead. Using OP_NOP7 is either carelessness or maliciousness. There is some other policy around OP_RETURN which is about limiting the damage of externalities caused by users who are motivated to store data on the blockchain. This policy was recently improved, so that's good. Then we get to hybrid keys policy... Arguably hybrid keys don't even deserve to be banned by policy to begin with. They are do not cause malleability issues in most practical cases. In particular, their malleability problems are no worse than malleability problems induced by uncompressed pubkeys, which are not banned. The main problem with hybrid keys is that they are weird, and that implementers of the Bitcoin protocol are likely to mess it up. Hybrid key policy is there just in case maybe we want to soft-fork them out one day. In my opinion the Bitcoin protocol already has a lot of weirdness in it and is hard to reimplement correctly anyways. Hybrid keys are no more abusive than uncompressed pubkeys and for that reason we are probably never going to soft-fork them out. Folks already are motivated to avoid them for the same reason they avoid uncompressed pubkeys. Similar to using NOP7, users of hybrid keys would just be doing so to be malicious. But unlike NOP7 hybrid keys don't exist to soft-fork in new behaviour. But fine, this PR isn't proposing the removal of hybrid key policy. Transactions using hybrid keys remain disallowed in this PR. The problem is that PR #5247 doesn't just ban the use of hybrid keys. The implementation goes way overboard and disallows all sorts of data passed to CHECKMULTISIG that isn't even hybrid keys. Okay, but maybe the policy implemented #5247 just happens to be good for other reasons beyond its primary purpose? Isn't banning arbitrary data in CHECKMULTISIG good? I don't know if it is good or not, but #5247 doesn't ban arbitrary data in CHECKMULTISIG. #5247 still allows arbitrary data pushes in CHECKMULTISIG so long as they occur after all the signatures are consumed. So this policy implementation isn't providing any additional benefits. Okay, but at least the policy implemented #5247 isn't harming anyone right? This is the crux of the problem with #5247: it has harmed Bitcoin users. I cannot think of any other relay policy implemented in Bitcoin Core that has ever disallowed actively used non-malicious transactions before. Are the Counterparty transactions that were banned #5247 malicious? Maybe, but I don't think so. The blockspace used by them are identical to a normal 1-of-3 multisig transaction. In fact, in response to #5247 Counterparty simply reversed the order of their "pubkey" data in their protocol in order to satisfy the restrictions imposed by #5247. (They also did a better job hiding their data pushes look more like pubkeys to prevent further policy interference). So however malicious their transactions are, their replacement is equally malicious and it isn't banned. Because disallowing "bad data" was never the purpose of #5247, it isn't surprising that the policy doesn't stop it. But Counterparty adjusting their protocol to comply with #5247 does nothing to help the folks that were already using their older protocol, and is the big problem, and why I think this is, in fact, a big deal. Again, I cannot think of any other relay policy implemented in Bitcoin Core that has ever banned actively used non-malicious, non-malleable transactions before or since. It undermines Bitcoin's premise as permissionless money if every time a user comes up with an novel, safe, secure, non-malicious use of Bitcoin as it is today, they run the risk of their UTXOs being thoughtlessly banned without consideration of their use case. And yes #5247 did ban these Counterparty UTXOs without consideration. When petertodd correctly noted "Equally this change may render some users' funds unspendable, potentially even funds locked in P2SH outputs." sipa replied "I don't believe it should affect any system in production, as hybrid pubkeys have been non-standard since v0.8." No one at the time seemed to be aware that #5247 was affecting in use Counterparty transactions! The PR didn't just maybe render some users's funds unspendable, it actually did! sipa also said "AFAIK does not change anything for tx acceptance except in weird checksig+not cases", but this isn't a checksig+not case. I'm am quite certain that #5247 would have never been accepted in its form had the authors been aware that they were banning actively used non-malicious transaction protocols. As far as guilt tripping goes, I personally feel bad that long ago when someone came to me asking for my help in spending their transaction, I had to tell them something like "SFYL, redeeming your UTXO is now banned from the Bitcoin network. You will require miner assistance". And today I feel much worse now that I understand that the banning of their UTXO was actually unintentional. Had I just dug into the issue a little bit more and seen the orginal purpose of the code, we could have perhaps fixed this policy implementation a long time ago. So in summary:
|
|
@TheCharlatan I touch on this in the above, but I do want to explicitly note that the order of public keys that Counterparty uses changed in response to #5247 and the the keys used to be in the other order. |
I think the process was:
Also, the latter keys should be the ones that are invalid, as they will be popped off the stack first. For that, it looks to me like there are 147,470 such utxos from prior to block 400k, containing a total of 9.2591,3010 BTC, though only four utxos are individually worth more than $20 today:
That still feels a bit borderline to me: if they actually get reclaimed, that's great; but when they're all that old and that low individual value, the chances that anything much will happen still seems low to me. For context, 0.8.0 came out around block 222,000, the counterparty commit cited above was made around block 332,500,
I'm sorry you feel bad about this, but I think emotional arguments are more destructive than constructive in regards to technical issues. Maybe that's okay in moderation, but it's definitely been overused over the past 6+ months. I think the minimally intrusive change here would be replace --- a/src/script/interpreter.cpp
+++ b/src/script/interpreter.cpp
@@ -61,26 +61,15 @@ static inline void popstack(std::vector<valtype>& stack)
stack.pop_back();
}
-bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) {
- if (vchPubKey.size() < CPubKey::COMPRESSED_SIZE) {
- // Non-canonical public key: too short
- return false;
- }
- if (vchPubKey[0] == 0x04) {
- if (vchPubKey.size() != CPubKey::SIZE) {
- // Non-canonical public key: invalid length for uncompressed key
- return false;
- }
- } else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) {
- if (vchPubKey.size() != CPubKey::COMPRESSED_SIZE) {
- // Non-canonical public key: invalid length for compressed key
- return false;
+static bool IsEmptyOrHybridPubKey(const valtype &vchPubKey)
+{
+ if (vchPubKey.size() == 0) return true;
+ if (vchPubKey.size() == CPubKey::SIZE) {
+ if (vchPubKey[0] == 0x06 || vchPubKey[0] == 0x07) {
+ return true;
}
- } else {
- // Non-canonical public key: neither compressed nor uncompressed
- return false;
}
- return true;
+ return false;
}
bool static IsCompressedPubKey(const valtype &vchPubKey) {
@@ -216,7 +205,7 @@ bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, script_ver
}
bool static CheckPubKeyEncoding(const valtype &vchPubKey, script_verify_flags flags, const SigVersion &sigversion, ScriptError* serror) {
- if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && !IsCompressedOrUncompressedPubKey(vchPubKey)) {
+ if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && IsEmptyOrHybridPubKey(vchPubKey)) {
return set_error(serror, SCRIPT_ERR_PUBKEYTYPE);
}
// Only compressed keys are accepted in segwit |
In PR bitcoin#5247, the STRICTENC standardness rules were tightened with regards to CHECKMULTISIG so that unparsable public keys fail the script when they are encountered. The overall purpose here was to disallow the use of confusing hybrid public keys by policy while keeping policy compatible (i.e., strictly stronger) with consensus rules. Comments in PR bitcoin#5247 note that "I don't believe it should affect any system in production"; however, this belief is/was false. Counterparty was stuffing data blobs into multisig pubkey lists, but these UTXOs were meant to be spendable because, although some pubkeys were unparsable, other keys were parsable, and the UTXOs were meant to be spent by those valid keys. But in tackling the hybrid key issue, PR bitcoin#5247 disallowed any unparsable keys in multisigs, whether or not they were hybrid, and whether or not the signature was meant to satisfy a hybrid key. In production, Counterparty UTXOs were inadvertently caught up in this standardness rule change and became "soft confiscated." That is, they were no longer spendable by policy but still recoverable if users are able to somehow bypass standardness by mining their transactions themselves, or getting out-of-band assistance from some other miner. I understand that Bitcoin Core never intended to "soft confiscate" any UTXOs by policy changes. This change addresses the problem. With this change, only hybrid formatted keys and empty keys will cause early abort in CHECKMULTISIG operations. Otherwise, failing signature checks revert to their consensus behaviour of testing subsequent public keys. Counterparty UTXOs were never intended to make use of hybrid keys and thus shouldn't have any passing signatures using hybrid keys.
bbfaed4 to
4013f4d
Compare
|
At the risk of opening the bikeshed, another reasonable option I was thinking of today is allowing arbitrary data up to 33 bytes, or an uncompressed pubkey: I'm not sure if any of the 147,470 UTXO would pass one condition but not the other. For the moment I've pushed @ajtowns's suggested policy. I'm fine with that one or the one above or anything in between. |
|
Concept NACK, they "soft confiscated" their own coins. This isn't a good excuse to enable spam. |
I see two misses for this approach (though I'm still not looking at any utxos after block 400k):
(EDIT: also, keep |
|
It would be good to add a test case that demonstrates scriptPubKeys of this form (baremultisig and possible multisig in p2sh, with valid key push prior to invalid key) are spendable. I think creating these outputs became non-standard with #12460 (2018-04-05 merge date), which changed the checks for pubkeys in a bare multisig from a size between 33 and 65 bytes (inclusive) to an explicit check of 02/03-and-33-bytes or 04/06/07-and-65-bytes. In trying to create a functional test to validate these become spendable, I'm getting Fix and test case at https://github.com/ajtowns/bitcoin/commits/202511-cparty-spends/ Not really convinced adding params to Solver is sensible, but also pulling the pubkey size checks out also seems silly. |
|
Doe this mean that, due to |
I think that's pretty likely, but haven't confirmed. Should mean that bare multisigs are only spendable if any data is pushed as a fake un/compressed pubkey. |
|
I tried cargo culting @ajtowns 's testing code to check that currently one can create (multisig) UTXOs with hybrid keys, but cannot spend them: Hopefully I got that test case right. It means our current policy surrounding bare multisig is incoherent. I'll open an issue listing all the problems I have with the current policy since this seems to have grown larger than a single PR. P.S. another worm crawling out of this can that I opened: Why doesn't |
The vsize that |
a4ee513 to
4013f4d
Compare
Hi AJ, are these the test vectors you had in mind? You can run them here with |
I was thinking functional tests, to also make sure there isn't some obscure policy rule that the unit tests miss that nevertheless ends up blocking successful spends of things that should be spendable. Unit tests are good too though. "CMS NOT" isn't super interesting though, since it's easily satisfiable with empty sigs (apart from mempool/consensus inconsistency bugs). The things that (per this PR's arguments) should be spendable are |
👍
I took a closer look through We have the following permutations in
I believe we are missing |
|
@pinheadmz do you want to take a look here? |
|
I spent a few days reviewing all the commit history and discussion here and in #33882. I've been able to contextualize just about all of it, but I need to revisit the effect it has on bip143. Anyway, I took a utxo snapshot and analyzed it with claude to determine how much utxo could be rescued by the policy change: https://gist.github.com/pinheadmz/e599afb5f926131ae5d9c67ef68ce09b Seems like the bulk is around 80,000 UTXOs worth $1-10 each and consuming maybe around 10 MB of utxo set space total. So it's certainly not an urgent issue. I even have a 2015-era counterparty wallet around here somewhere... but even if I did dig it up I don't think I'd personally bother scraping the loose change. I generally agree with @roconnor-blockstream that #5247 was overkill and its something we should fix. Probably the most important motivation is in #33882 just clarifying and sane-ifying the multisig policy (making spend match create, etc). As a PR, this could use tests before and after the change which I think @151henry151 might have already written, but it would definitely help understand all the implications. |
|
@pinheadmz The tests I wrote are in #33926 (closed) — |
|
Closing this as won't fix / limited interest for now |
In PR #5247, the STRICTENC standardness rules were tightened with regards to CHECKMULTISIG so that unparsable public keys fail the script when they are encountered. The overall purpose here was to disallow the use of confusing hybrid public keys by policy while keeping policy compatible (i.e., strictly stronger) with consensus rules.
Comments in PR #5247 note that "I don't believe it should affect any system in production"; however, this belief is/was false. Counterparty was stuffing data blobs into multisig pubkey lists, but these UTXOs were meant to be spendable because, although some pubkeys were unparsable, other keys were parsable, and the UTXOs were meant to be spent by those valid keys.
But in tackling the hybrid key issue, PR #5247 disallowed any unparsable keys in multisigs, whether or not they were hybrid, and whether or not the signature was meant to satisfy a hybrid key.
In production, Counterparty UTXOs were inadvertently caught up in this standardness rule change and became "soft confiscated." That is, they were no longer spendable by policy but still recoverable if users are able to somehow bypass standardness by mining their transactions themselves, or getting out-of-band assistance from some other miner.
I understand that Bitcoin Core never intended to "soft confiscate" any UTXOs by policy changes. This change addresses the problem.
With this change, only hybrid formatted keys and empty keys will cause early abort in CHECKMULTISIG operations. Otherwise, failing signature checks revert to their consensus behaviour of testing subsequent public keys.
Counterparty UTXOs were never intended to make use of hybrid keys and thus shouldn't have any passing signatures using hybrid keys.