Skip to content

Commit

Permalink
Treat OP_1NEGATE as a small integer
Browse files Browse the repository at this point in the history
  • Loading branch information
petertodd committed Mar 12, 2024
1 parent 173a73e commit 6ad2414
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
24 changes: 13 additions & 11 deletions bitcoin/core/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,31 @@ def encode_op_pushdata(d):
@staticmethod
def encode_op_n(n):
"""Encode a small integer op, returning an opcode"""
if not (0 <= n <= 16):
raise ValueError('Integer must be in range 0 <= n <= 16, got %d' % n)
if not (-1 <= n <= 16):
raise ValueError('Integer must be in range -1 <= n <= 16, got %d' % n)

if n == 0:
return OP_0
elif n == -1:
return OP_1NEGATE
else:
return CScriptOp(OP_1 + n-1)

def decode_op_n(self):
"""Decode a small integer opcode, returning an integer"""
if self == OP_0:
return 0

if not (self == OP_0 or OP_1 <= self <= OP_16):
raise ValueError('op %r is not an OP_N' % self)

return int(self - OP_1+1)
elif self == OP_1NEGATE:
return -1
else:
if not (OP_1 <= self <= OP_16):
raise ValueError('op %r is not an OP_N' % self)
else:
return int(self - OP_1+1)

def is_small_int(self):
"""Return true if the op pushes a small integer to the stack"""
if 0x51 <= self <= 0x60 or self == 0:
if self == OP_0 or OP_1 <= self <= OP_16 or self == OP_1NEGATE:
return True
else:
return False
Expand Down Expand Up @@ -516,10 +520,8 @@ def __coerce_instance(cls, other):
if isinstance(other, CScriptOp):
other = bytes([other])
elif isinstance(other, int):
if 0 <= other <= 16:
if -1 <= other <= 16:
other = bytes([CScriptOp.encode_op_n(other)])
elif other == -1:
other = bytes([OP_1NEGATE])
else:
other = CScriptOp.encode_op_pushdata(bitcoin.core._bignum.bn2vch(other))
elif isinstance(other, (bytes, bytearray)):
Expand Down
3 changes: 2 additions & 1 deletion bitcoin/tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def t(n, op):
self.assertEqual(actual, n)
self.assertTrue(isinstance(actual, int))

t(-1, OP_1NEGATE)
t(0, OP_0)
t(1, OP_1)
t(2, OP_2)
Expand Down Expand Up @@ -108,7 +109,7 @@ def T(serialized_script, expected_tokens, test_roundtrip=True):

# numbers
T('00', [0x0])
T('4f', [OP_1NEGATE])
T('4f', [-1])
T('51', [0x1])
T('52', [0x2])
T('53', [0x3])
Expand Down

0 comments on commit 6ad2414

Please sign in to comment.