4

Testnet transaction c474c67cabc13f74c65e7cf503db971d5ba176794bff7de36f46d51ca675cf3a locked 0.0005BTC with a simple math script:

OP_2DUP
OP_ADD
5102
OP_EQUALVERIFY
OP_SUB
230d
OP_EQUAL

Transaction b6714e05dd014cc92de09271886e62d85e7d41dd44fad417c520eb51d57f94c0 spent that output with the sigscript:

ba07
6985

I can't see how (ba07 + 6985) == 5102 or (ba07 - 6985) == 230d.

What am I missing about Bitcoin script, integers, and math?

1 Answer 1

6

For mathematical operations, Bitcoin treats byte vectors as signed (signed magnitude) little endian values. So let's look what the numbers are then:

  • The byte vector ba07 is the hexadecimal number (0)7ba.
  • The byte vector 6985 is 8569 in big endian. Since the highest bit is set the number is negative. So the hexadecimal number is -(0)569.
  • The byte vector 5102 is the hexadecimal number (0)251.
  • The byte vector 0d23 is the hexadecimal number (0)23d.

Therefore we end up with the two statements 7ba+(-569)=251 and 7ba-(-569)=d23 which are true.

1
  • Ah, got it, thanks! I had tried playing with the sign bits, but I didn't realize it was little-endian on top of all that.
    – Dave M.
    May 1 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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