How would I push a negative number with OPCODES onto the stack?

0

If the user wants to push -10 onto the stack.

Would I do "10 OP_NEGATE" ?

Or could I convert it to the hex value by doing 0x80 + 10 = 0X8A ?

Kyle Graham

Posted 2018-05-05T23:20:36.630

Reputation: 472

Answers

1

Both will work. OP_NEGATE simply XORs with 0x80, so you can also directly push the result.

Raghav Sood

Posted 2018-05-05T23:20:36.630

Reputation: 10 897

And we take the absolute value of a signed integer right? In this case 10 could be written with one byte so I think that using two bytes was overkill?Kyle Graham 2018-05-06T09:30:40.390

Not necessarily. Some operations, like hashing, will produce different results for positive or negative numbers. Some operations, like signatures, will fail if the R value is negative. It depends on what you're doing.Raghav Sood 2018-05-06T14:08:55.297

1Take into account that for numbers between -1 and 16 inclusive, short 1-byte opcodes exist. Pushing any other number (like -10) requires an explicit push operation of the bytes encoding of the number. Both "10 NEGATE" and "-10" are therefore 2-byte scripts.Pieter Wuille 2018-05-06T23:45:48.093