3

From my interpretation of the bitcoin Script Wiki and the rust-bitcoin codebase, two items on the stack get interpreted by bitcoin Script as false:

  1. The empty array []
  2. The single byte 0x00

Q1: Is it correct to think that a 2-byte long or more array of 0s (even though as a number would get interpreted as 0) would not be interpreted by bitcoin Script as false? For example the script OP_PUSHBYTES_4 <0x00000000> would put a byte array of 0s on the stack but still equate to true? On the other hand the script OP_PUSHBYTES_1 <0x00> would interpret as false?

Q2: Will an opcode like OP_NOT work with/interpret both of the above as false? (the docs on rust-bitcoin, Push 1 if the top item is zero; otherwise, push 0. are not very explicit in that sense, and the bitcoin wiki is even worse).

1
  • 1
    For readers coming to this post: note that my assumption on Q1 was wrong, and in fact the script OP_PUSHBYTES_4 00000000 would evaluate to false. Commented Nov 6 at 16:46

1 Answer 1

5

For the purposes of determining whether a Script stack element is true/false in boolean context:

  • The empty array is false (the canonical encoding of the number 0).
  • Any array consisting of just 0x00 bytes is false (an overly long, but still valid encoding of the number 0).
  • Any array consisting of just 0x00 bytes, followed by a single 0x80 at the end, is false (encodings of "negative 0").
  • Everything else is true.

This boolean context is relevant for OP_IF, OP_NOTIF, OP_VERIFY, and OP_IFDUP, as well as for determining final success of Script evaluation. Opcodes like OP_BOOLOR and OP_BOOLAND actually interpret their arguments as integers, though the only practical difference is that integer arguments are limited to 4 bytes (violating this limit makes the entire script invalid).

3
  • Thanks for the answer @pieter-wuille. Here is a follow-up question I'd like to confirm with you. Are you saying that the script <ababababababababab> <01> OP_BOOLAND is invalid instead of true (because the first argument is 9 bytes long instead of 8 or less? Commented yesterday
  • Yes, <ababababab> OP_1 OP_BOOLAND would also be invalid (5 bytes is more than 4). Commented yesterday
  • Oh shoot yes 4 bytes is what I meant haha. Perfect thanks! I was working on this page. Cheers. Commented yesterday

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.