Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Merge 07095e8 into 69a1ef5
Browse files Browse the repository at this point in the history
  • Loading branch information
ixje committed Jun 10, 2019
2 parents 69a1ef5 + 07095e8 commit 30caf2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ All notable changes to this project are documented in this file.
- Fix error while parsing list arguments from prompt for smart contract test invocations
- Fix ``Runtime.GetTrigger`` and ``Transaction.GetType`` syscalls pushing wrong StackItem type
- Update handling of default cause for ``PICKITEM`` instruction
- Fix ``BigInteger.ToByteArray()`` for some negative values to return too many bytes


[0.8.4] 2019-02-14
Expand Down
15 changes: 14 additions & 1 deletion neo/Core/BigInteger.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ def ToByteArray(self, signed=True):
return b'\x00'

if self < 0:
return self.to_bytes(1 + ((self.bit_length() + 7) // 8), byteorder='little', signed=True)
highbyte = 0xff
data = self.to_bytes(1 + ((self.bit_length() + 7) // 8), byteorder='little', signed=signed)

msb = len(data) - 1
for i, b in enumerate(data[::-1]):
if b != highbyte:
msb = i
break

needExtraByte = (data[msb] & 0x80) == (highbyte & 0x80)
if needExtraByte:
return data
else:
return data[:-1]

try:
return self.to_bytes((self.bit_length() + 7) // 8, byteorder='little', signed=signed)
Expand Down
8 changes: 8 additions & 0 deletions neo/Core/tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ def test_big_integer_to_ba(self):
b4ba = b4.ToByteArray()
self.assertEqual(b4ba, b'\x00')

b5 = BigInteger(-146)
b5ba = b5.ToByteArray()
self.assertEqual(b'\x6e\xff', b5ba)

b6 = BigInteger(-48335248028225339427907476932896373492484053930)
b6ba = b6.ToByteArray()
self.assertEqual(20, len(b6ba))

def test_big_integer_frombytes(self):
b1 = BigInteger(8972340892734890723)
ba = b1.ToByteArray()
Expand Down

0 comments on commit 30caf2c

Please sign in to comment.