Skip to content

consensus: Consistently encode and decode OP_1NEGATE similar to other small ints in Script - #29589

Closed
Christewart wants to merge 1 commit into
bitcoin:masterfrom
Christewart:2024-03-07-op1neg-py
Closed

Christewart wants to merge 1 commit into
bitcoin:masterfrom
Christewart:2024-03-07-op1neg-py

Conversation

@Christewart

@Christewart Christewart commented Mar 7, 2024 •

Copy link
Copy Markdown
Contributor

This PR fixes inconsistent behavior for encoding/decoding small integers in Script discovered when working on #29221.

OP_1NEGATE is the minimally encoded version of the number -1 in Script. All other minimally encoded small integers (OP_0, OP_1,...,OP_16) are handled correctly by DecodeOP_N() and EncodeOP_N().

This PR extends this functionality to handling OP_1NEGATE in the same way we treat other small integers in Script.

We treat all small integers consistently in EvalScript() - so we should treat their encoding and decoding helper functions consistently as well. It doesn't make sense to handle OP_1NEGATE separately imo.

This is useful for future extensions of the Script language - such as 64bit arithmetic. If this change isn't accepted, we need to adhoc handle OP_1NEGATE separately from other small integers when encoding/decoding OP_1NEGATE.

@DrahtBot

DrahtBot commented Mar 7, 2024 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/29589.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept NACK dgpv, theuni, darosior, ajtowns
Concept ACK theStack, petertodd
Stale ACK sipa, vostrnad, itornaza

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@DrahtBot DrahtBot added the Tests label Mar 7, 2024
@Christewart
Christewart marked this pull request as ready for review March 7, 2024 20:36
@Christewart
Christewart force-pushed the 2024-03-07-op1neg-py branch from 12cdde8 to fcf944b Compare March 8, 2024 10:49
@Christewart
Christewart requested a review from vostrnad March 8, 2024 10:50
@vostrnad

vostrnad commented Mar 8, 2024

Copy link
Copy Markdown

Should decode_op_n and is_small_int perhaps be fixed as well?

@Christewart

Copy link
Copy Markdown
Contributor Author

Should decode_op_n and is_small_int perhaps be fixed as well?

Done in 8bd0a8b18d68bbacee731f3e0583397f81ca078e , good catch 🙌

@sipa

sipa commented Mar 8, 2024

Copy link
Copy Markdown
Member

utACK 8bd0a8b18d68bbacee731f3e0583397f81ca078e

@DrahtBot
DrahtBot removed the request for review from vostrnad March 8, 2024 15:32
@Christewart
Christewart requested a review from vostrnad March 8, 2024 15:37
@Christewart

Christewart commented Mar 8, 2024 •

Copy link
Copy Markdown
Contributor Author

Thanks for the code review @vostrnad , I took your changes and added them to this PR. I also cherry-picked them and tested them on 64bit-cscriptnum and everything ✔️

@Christewart
Christewart requested a review from vostrnad March 8, 2024 20:29
@DrahtBot
DrahtBot requested review from sipa and removed request for vostrnad March 9, 2024 13:07

@vostrnad vostrnad left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 6cf08dd2d532766f1b7063f40e7ab25f8ee67e45 (one style nit not addressed)

PR should also be renamed, e.g. "tests: fix OP_1NEGATE handling in CScriptOp"

@Christewart Christewart changed the title tests: Fix bug CScriptOp.encode_op_n() where OP_1NEGATE was not handled correctly tests: fix OP_1NEGATE handling in CScriptOp Mar 9, 2024
@theStack

Copy link
Copy Markdown
Contributor

Concept ACK

Now that encode_op_n handles also -1, the following call-site in CScript.__coerce_instance can be simplified:

elif isinstance(other, int):
if 0 <= other <= 16:
other = bytes([CScriptOp.encode_op_n(other)])
elif other == -1:
other = bytes([OP_1NEGATE])

@petertodd

Copy link
Copy Markdown
Contributor

Concept ACK

I'm proposing this for python-bitcoinlib too, which is where all this code comes from originally: petertodd/python-bitcoinlib#299

petertodd added a commit to petertodd/python-bitcoinlib that referenced this pull request Mar 12, 2024
@Christewart

Copy link
Copy Markdown
Contributor Author

Concept ACK

Now that encode_op_n handles also -1, the following call-site in CScript.__coerce_instance can be simplified:

elif isinstance(other, int):
if 0 <= other <= 16:
other = bytes([CScriptOp.encode_op_n(other)])
elif other == -1:
other = bytes([OP_1NEGATE])

Done in f0c077a388b980566590d7887d8c6dd97cbaa20a

@dgpv

dgpv commented Mar 14, 2024 •

Copy link
Copy Markdown
Contributor

I'm not sure that it is wise to change the behavior of decode_op_n() and encode_op_n() to be different from CScript::DecodeOP_N() and CScript::EncodeOP_N():

bitcoin/src/script/script.h

Lines 506 to 519 in a85e5a7

static int DecodeOP_N(opcodetype opcode)
{
if (opcode == OP_0)
return 0;
assert(opcode >= OP_1 && opcode <= OP_16);
return (int)opcode - (int)(OP_1 - 1);
}
static opcodetype EncodeOP_N(int n)
{
assert(n >= 0 && n <= 16);
if (n == 0)
return OP_0;
return (opcodetype)(OP_1+n-1);
}

Having different behavior between code in C++ and python can will add confusion.

I would suggest to just handle -1/OP_1NEGATE in-place where they are relevant, rather than changing the assumption 'N is from 0 to 16' as it clearly is handled this way by EncodeOP_N/DecodeOP_N.

@rkrux rkrux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Make is successful
  2. All functional tests pass

Would it be possible to share where this error pops up in the branch specified in the description?

"When we don't encode -1 as OP_1NEGATE then we end up getting errors inside of CheckMinimalPush() saying -1 was not minimally encoded."

@itornaza

itornaza commented Apr 12, 2024 •

Copy link
Copy Markdown
Contributor

tested ACK f0c077a388b980566590d7887d8c6dd97cbaa20a

  • Performed a code review and everything lgtm
  • Configured with --with-incompatible-bdb and --enable-suppress-external-warnings
  • Built the PR, ran unit tests with make check and all tests pass
  • Ran all functional tests with no extra flags and all tests pass
  • Ran all functional tests with --extended and all tests pass

@DrahtBot
DrahtBot requested review from theStack and vostrnad April 12, 2024 17:13
@itornaza

Copy link
Copy Markdown
Contributor

Hey @Christewart! Do you plan to update for cmake?

@Christewart

Copy link
Copy Markdown
Contributor Author

Hey @Christewart! Do you plan to update for cmake?

Done, i squashed everything into 1 commit as well.

@theuni

theuni commented Oct 29, 2024 •

Copy link
Copy Markdown
Member

I'm ~0 on this leaning towards NACK. As far as I can tell, all code paths are currently gated by if (>= OP_1 && <= OP_16) when calling EncodeOP_N/DecodeOP_N.

So this is really only changing the intention of those functions. And since it would be unsafe (because of OP_RESERVED) to change those gates in the callers to >= OP_1NEGATE && <= OP_16, it seems like OP_1NEGATE is going to have to remain a special case for callers anyway.

@mzumsande mzumsande left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn’t see earlier versions of this PR, but It’s not clear to me what the goal of this PR is from the OP / Title. Should the title be updated, is this still supposed to fix a bug?

As mentioned above, DecodeOP_N / EncodeOP_N currently don't need to handle OP_1NEGATE in any of the current use cases - so I think that there should be some explanation why they should be changed to support it.

I noticed that EncodeOP_N is currently called from PushAll in sign.cpp with an unsigned char (valtype), where there is already special-casing for OP_1NEGATE as 0x81, which seems like a possible source of confusion if the function was extended to deal with OP_1NEGATE as -1.

Also, the commit message could be fixed / made more coherent, it reads like it contains multiple commit messages from before a squash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would mean that C++ (IsSmallInteger in solver.cpp) would have a different notion of a small interger.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was already the case before this PR? It seems that the currently implementation in solver.cpp doesn't handle OP_0 correctly, while the python codebase does?

if 0x51 <= self <= 0x60 or self == 0:

return opcode >= OP_1 && opcode <= OP_16;

This goes to the purpose of this PR, handle them consistently across codebases and remove special cases such as OP_1NEGATE.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In f2f8796 I modified IsSmallInteger() to handle OP_1NEGATE and OP_0 correctly. Previously only the python codebase handled OP_0 correctly.

@mzumsande mzumsande Nov 5, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that "correct" is the right term here, it's more about what these functions are intended to be used for. In this case, the comment Test for "small positive integer" indicates that the function wasn't designed to deal with OP_0, so if it's just the name could just rename it to IsSmallPositiveInteger to not introduce a change in behavior.

@Christewart Christewart Nov 5, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a reasonable thing to do. However it seems like the script.py implementation should still handle OP_1NEGATE imo.

IIUC the solver.cpp's IsSmallInteger() and script.py's is_small_int() serve 2 different purposes, and renaming the solver.cpp function name would help differentiate them.

However if we aren't going to change any functionality in this file, perhaps its best to not do anything and just revert it and move on? 🤷‍♂️

@Christewart Christewart changed the title consensus: fix OP_1NEGATE handling in CScriptOp consensus: Consistently encoded and decode OP_1NEGATE similar to other small ints in Script Nov 5, 2024
@Christewart Christewart changed the title consensus: Consistently encoded and decode OP_1NEGATE similar to other small ints in Script consensus: Consistently encode and decode OP_1NEGATE similar to other small ints in Script Nov 5, 2024
@Christewart

Copy link
Copy Markdown
Contributor Author

Thanks for looking at this @mzumsande !

I didn’t see earlier versions of this PR, but It’s not clear to me what the goal of this PR is from the OP / Title. Should the title be updated, is this still supposed to fix a bug?

Modified the title, hopefully its more clear.

As mentioned above, DecodeOP_N / EncodeOP_N currently don't need to handle OP_1NEGATE in any of the current use cases - so I think that there should be some explanation why they should be changed to support it.

I edited the original post to hopefully clarify why this change is useful.

Also, the commit message could be fixed / made more coherent, it reads like it contains multiple commit messages from before a squash.

done.

@darosior

darosior commented Nov 5, 2024

Copy link
Copy Markdown
Member

Concept NACK. This introduces dead code and i don't think the justification for this meets the bar for touching consensus code.

@Christewart

Copy link
Copy Markdown
Contributor Author

Rebased

@fanquake

Copy link
Copy Markdown
Member

@mzumsande do you want to circle back for another look here? Or @ajtowns?

@ajtowns

ajtowns commented Feb 20, 2025

Copy link
Copy Markdown
Contributor

Concept NACK. This introduces dead code and i don't think the justification for this meets the bar for touching consensus code.

I think it would make more sense to carry this patch as part of the 64 bit arithmetic patchset. If there's consensus to merge/activate 64 bit arithmetic, it could make sense at that point to split this into its own PR to make review easier, but without that motivation, I don't think this change makes much sense on its own. So Concept NACK from me too for now, unless there's some other tangible benefit now?

@fanquake

Copy link
Copy Markdown
Member

Closing for now.

@fanquake fanquake closed this Feb 20, 2025
Shailendra53 pushed a commit to CryptoTuraTeam/dash that referenced this pull request Apr 28, 2026
… add unit test

3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner)

Pull request description:

  In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method:
  https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593
  This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`.

  Note that this was unnoticed, as the code part was never hit so far in the test framework.

ACKs for top commit:
  achow101:
    ACK 3e9c736
  Christewart:
    ACK 3e9c736
  rkrux:
    tACK [3e9c736](bitcoin@3e9c736)
  hernanmarino:
    tACK 3e9c736

Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
Shailendra53 pushed a commit to CryptoTuraTeam/dash that referenced this pull request Apr 29, 2026
… add unit test

3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner)

Pull request description:

  In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method:
  https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593
  This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`.

  Note that this was unnoticed, as the code part was never hit so far in the test framework.

ACKs for top commit:
  achow101:
    ACK 3e9c736
  Christewart:
    ACK 3e9c736
  rkrux:
    tACK [3e9c736](bitcoin@3e9c736)
  hernanmarino:
    tACK 3e9c736

Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
Shailendra53 pushed a commit to CryptoTuraTeam/dash that referenced this pull request May 11, 2026
… add unit test

3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner)

Pull request description:

  In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method:
  https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593
  This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`.

  Note that this was unnoticed, as the code part was never hit so far in the test framework.

ACKs for top commit:
  achow101:
    ACK 3e9c736
  Christewart:
    ACK 3e9c736
  rkrux:
    tACK [3e9c736](bitcoin@3e9c736)
  hernanmarino:
    tACK 3e9c736

Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
vijaydasmp pushed a commit to vijaydasmp/dash that referenced this pull request Jun 21, 2026
… add unit test

3e9c736 test: fix accurate multisig sigop count (BIP16), add unit test (Sebastian Falbesoner)

Pull request description:

  In the course of reviewing bitcoin#29589 I noticed the following buggy call-site of `CScriptOp.decode_op_n` in the CScript's `GetSigOpCount` method:
  https://github.com/bitcoin/bitcoin/blob/4cc99df44aec4d104590aee46cf18318e22a8568/test/functional/test_framework/script.py#L591-L593
  This should be `lastOpcode` rather than `opcode`. The latter is either OP_CHECKMULTISIG or OP_CHECKMULTISIGVERIFY at this point, so `decode_op_n` would result in an error. Also, in `CScript.raw_iter`, we have to return the op as `CScriptOp` type instead of a bare integer, otherwise we can't call the decode method on it. To prevent this in the future, add some simple unit tests for `GetSigOpCount`.

  Note that this was unnoticed, as the code part was never hit so far in the test framework.

ACKs for top commit:
  achow101:
    ACK 3e9c736
  Christewart:
    ACK 3e9c736
  rkrux:
    tACK [3e9c736](bitcoin@3e9c736)
  hernanmarino:
    tACK 3e9c736

Tree-SHA512: 51647bb6d462fbd101effd851afdbd6ad198c0567888cd4fdcac389a9fb4bd3d7e648095c6944fd8875d36272107ebaabdc62d0e2423289055588c12294d05a7
@bitcoin bitcoin locked and limited conversation to collaborators Jul 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.