Repository navigation
Fix ASM ambiguity - #28824
Fix ASM ambiguity#28824willcl-ark wants to merge 7 commits 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/28824. ReviewsSee the guideline for information on the review process. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
There was a problem hiding this comment.
This seems much harder to understand to me -- you need to convert it to 0x07a120 then convert that to decimal to get back to the nice round number block height? In particular mistaking it for 0x20a107 (2,138,375) would be particularly misleading.
There was a problem hiding this comment.
This also seems confusing, particularly if it appears in something like DUP SIZE 20 EQUALVERIFY HASH160 <x> EQUAL -- whoops, that's actually saying the input should be 32 bytes, not 20 bytes.
|
In the dart library coinlib (https://pub.dev/packages/coinlib) I followed the same approach of encoding everything in HEX with integers encoded as LE. However -1 is encoded as -1. See the code here: https://github.com/peercoin/coinlib/blob/master/coinlib/lib/src/scripts/operations.dart I did this to avoid changing the ASM too much. However, I'm happy to move towards using |
|
@ajtowns @MatthewLM thank for your comments. I agree that after these changes some ASM fields will be less human-readable-friendly in LE hex. Prefixing with $ bitcoin-cli -regtest decodescript 0511121314150457c74942 | jq .asm
"1112131415 0d1112131415"
$ src/bitcoin-cli -regtest decodescript 0512345678900320A107b1 | jq .asm
"1234567890 0d500000 OP_CHECKLOCKTIMEVERIFY"
Thinking about this more, it feels like the question to be answered is perhaps "is the $ src/bitcoin-cli -regtest decodescript 0512345678900320A107b1 | jq .asm
"0x1234567890 500000 OP_CHECKLOCKTIMEVERIFY"This would create a moderately inconvenient interface in a few of the tests (and possibly some downstream users relying on this output) where hex prefixes, if present, have to be stripped from rpc output, but perhaps that's not too terrible. If we are designing the asm output to be a canonical, machine-readable format then my opinion remains that using hex-only makes the most sense. FWIW btcdeb interprets all values as decimal but prints a warning that they are ambiguous: $ btcc 1234567890 500000 OP_CHECKLOCKTIMEVERIFY
warning: ambiguous input 1234567890 is interpreted as a numeric value; use 0x1234567890 to force into hexadecimal interpretation
warning: ambiguous input 500000 is interpreted as a numeric value; use 0x500000 to force into hexadecimal interpretation
04d20296490320a107b1
$ btcc 0x1234567890 500000 OP_CHECKLOCKTIMEVERIFY
warning: ambiguous input 500000 is interpreted as a numeric value; use 0x500000 to force into hexadecimal interpretation
0512345678900320a107b1
And consequently generates a different script without manually specifying hex. I don't know what other tools do by default. |
|
I agree that |
I think it's human readable? For machines we could just give the raw hex script and not bother with an asm format. Anyway, how about going for something more left field and having Could perhaps also represent non-minimal pushes that way, eg the different encodings of -1 or |
This has always been my interpretation too.
Square brackets is something I could get behind; to me it is clear from your example that the brackets are indicating something, and just by looking at them I can see in most cases that they're hex values. To use the previous (less common?) example formatted with the brackets: $ bitcoin-cli -regtest decodescript 0511121314150457c74942 | jq .asm
"[1112131415] 1112131415"IMO it's again clear to me now that there's something different about these two values, and if I didn't know what it was then I should find out. One question I have is whether this will interfere with the scripthash types too badly? e.g. Is this nested bracket undesirable? $ src/bitcoin-cli -regtest decoderawtransaction 0100000001696a20784a2c70143f634e95227dbdfdf0ecd51647052e70854512235f5986ca010000008a47304402207174775824bec6c2700023309a168231ec80b82c6069282f5133e6f11cbb04460220570edc55c7c5da2ca687ebd0372d3546ebc3f810516a002350cac72dfe192dfb014104d3f898e6487787910a690410b7a917ef198905c27fb9d3b0a42da12aceae0544fc7088d239d9a48f2828a15a09e84043001f27cc80d162cb95404e1210161536ffffffff0100e1f505000000001976a914eb6c6e0cdb2d256a32d97b8df1fc75d1920d9bca88ac00000000 | jq .vin[].scriptSig.asm
"[304402207174775824bec6c2700023309a168231ec80b82c6069282f5133e6f11cbb04460220570edc55c7c5da2ca687ebd0372d3546ebc3f810516a002350cac72dfe192dfb[ALL]] [04d3f898e6487787910a690410b7a917ef198905c27fb9d3b0a42da12aceae0544fc7088d239d9a48f2828a15a09e84043001f27cc80d162cb95404e1210161536]"
specifically: [... 2dfb[ALL]]It might be preferable to have this format instead? [... 2dfb][ALL] |
|
|
It's pretty misleading though, since Note this applies to block hashes, as well, so if you provide block 816651's header and execute HASH256, you end up with |
I guess it being inside the brackets seems slightly clearer to me? Could have different brackets for the two things, eg |
Those smaller numbers will be shown in decimal still. For larger pushdata, hex is already shown in the little-endian encoded byte order. If |
|
Do we actually want to resolve all forms of ambiguity? E.g. what about the distinction between an OP_1 vs. a direct push of 0x01, vs. OP_PUSHDATA1 of 0x01? I think it'd be nice if there was a one-to-one correspondence between the disassembly and the actual script bytes. My contribution to this bikeshedding would be:
|
👍
Also
Provided you've already dealt with non-minimally encoded -1 and 1..16, using decimals seems nicer here? You can cope with negatives cleanly (
That would be nice. Probably requires remembering info from the scriptPubKey and using it when decoding the scriptSig or witness. |
I was thinking it would be nice to retain decimal but, due to ambiguity that could arise with CLTV and CSV, I'm not sure. Having embedded ASM for redeem scripts and Tapscripts would be very useful. |
What ambiguity? |
|
For 32-bit signed integers, the 32nd leftmost bit is negative, but that's not true for CLTV and CSV which uses 5 bytes. So a push data could represent a negative 32-bit signed integer, or a positive 40-bit signed integer if I understand the |
|
@MatthewLM I don't think that's correct. What's special in CSV and CLTV is that they permit numbers whose encoding is up to 5 bytes (rather than the math opcodes which only support encodings up to 4 bytes on input). But the encoding algorithm is the same: the top bit is the sign. |
|
@sipa I'll have to look at the code. I imagine it is two's complement? Then the negative bit will be different for each type of integer meaning that a 4 byte pushdata that would encode a negative 32-bit signed integer could also be interpreted as a positive 40-bit signed integer. |
|
@ajtowns Ok, iterating on that, new proposal:
There is no need for @MatthewLM I think you're missing something, but I don't see what. There is no ambiguity. Every byte encoding represents exactly one number, with a well-defined sign. |
|
@sipa Sorry, you are right. I looked at the code again. Bitcoin doesn't use two's complement for this (I have to keep in mind that Bitcoin has a lot of quirks). It uses a sign bit that negates the other bits, and this sign bit depends on the size of the push data (whatever the last byte is), so there is no ambiguity. Therefore I think minimally pushed numbers ought to be displayed as decimal for readability reasons as you now suggest.
Or any other decimal that has an even number of digits. Maybe use something like |
|
@MatthewLM Right, any single numeric push of even number of digits inside |
|
One more thought: if the format we end up with is unambiguous, there could also be a function/RPC/tool to convert asm back to script (and we can have fuzz tests that the roundtripping encoding+decoding is exact!). If so, we can probably permit a few things in the decoding that the encoder might not (yet) support:
|
|
I like where this is going now. Thanks for the conversation. I've written a branch implementing this previous version as I wanted to see what the changes would look like (although I'm not convinced I'm handling
I think this will make the representation even nicer to interpret and will gladly make this change.
I'll take a stab at implementing these nested scripts as well, as I think they'd be a nice improvement too... |
Per
Any push of a single positive number between 10-99; 1,000-9,999; 100,000-999,999; 10,000,000-99,999,999; 1,000,000,000-9,999,999,999; and 100,000,000,000-549,755,813,887 would be ambiguous, no? |
|
@ajtowns Right on all 3 counts. Instead of limiting the integer range, maybe just "all minimal pushes of up to 5 bytes, which push a minimally-encoded integer". |
|
One more question: what to do with unknown opcodes, and undecodable bytes? The current Suggestion: use |
|
Currently As for undecodable bytes, if we did want a keyword, perhaps something more explicit like
|
That's trivially ambiguous, as all unknown opcodes are mapped to the same thing.
Maybe, but I wouldn't use
No, any pushes become either decimal numbers, or EDIT: actually, I think all undecodable things (ignoring unknown opcodes) are pushes (but ones which straddle the end of the script boundary). |
We don't decode these on taproot signatures already, modify non-taproot signatures to match.
89547bb to
0a7c9d9
Compare
Strip unneccesary leading "OP_" prefix from opcodes when they are rendered as ASM.
0a7c9d9 to
71e6cf5
Compare
A simple helper function to angle bracket a hex string.
* Return minimal pushes < 5 bytes as decimal * Return non-minimal pushes < 5 bytes wrapped as OP_CODE<...> * Return pushes > 5 bytes as hex wrapped as <...>
71e6cf5 to
4890626
Compare
|
Attempt to revive the discussion: #27795 (comment) |
|
🐙 This pull request conflicts with the target branch and needs rebase. |
|
⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?
|
|
⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?
|
1 similar comment
|
⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?
|
|
ping @willcl-ark. There hasn't been any activity here since the revived discussion more than a year ago. |
|
for anyone following this pr but not the issue, #27795 (comment) |
|
⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?
|
|
Closing this as it has not had any activity in a while. If you are interested in continuing work on this, please leave a comment so that it can be reopened. cc @ajtowns |
|
Planning on rebasing/refactoring https://github.com/ajtowns/bitcoin/commits/202505-asmdecode/ to re-propose this work matching sipa's proposed spec. |
Closes: #27795
Closes: #7996
Previously
ScriptToAsmStrreturned hex-encoded values, except if data length was <= 4 bytes, in which case it displayed using decimal encoding.Fix the ASM representation by specifying an unambiguous encoding:
OP_prefix from all opcodesPUSHDATA1<0100><...>42<4200>UNDECODABLE(...)This should permit unambiguous decoding in the future.