Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encode signed numbers. #6

Merged
merged 6 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion multiversx_sdk/core/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ def encode_unsigned_number(arg: int) -> bytes:


def encode_signed_number(arg: int) -> bytes:
return arg.to_bytes(INTEGER_MAX_NUM_BYTES, byteorder="big", signed=True).lstrip(bytes([255]))
if arg == 0:
return b''
length = ((arg + (arg < 0)).bit_length() + 7 + 1) // 8
if arg > 0:
return arg.to_bytes(length, byteorder="big")
else:
return arg.to_bytes(length, byteorder="big", signed=True)
ramonalobont marked this conversation as resolved.
Show resolved Hide resolved


def decode_unsigned_number(arg: bytes) -> int:
Expand Down
24 changes: 24 additions & 0 deletions multiversx_sdk/core/codec_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from multiversx_sdk.core import codec
from multiversx_sdk.core.constants import (testVectors1, testVectors2,
testVectors3, testVectors4)


def test_encode_signed_number():
assert codec.encode_signed_number(-256) == bytes([0xFF, 0x00])
assert codec.encode_signed_number(-0x11) == bytes([0xEF])
assert codec.encode_signed_number(-128) == bytes([0x80])
assert codec.encode_signed_number(-1) == bytes([0xFF])
assert codec.encode_signed_number(0) == bytes([])
assert codec.encode_signed_number(1) == bytes([0x01])
assert codec.encode_signed_number(256) == bytes([0x01, 0x00])
assert codec.encode_signed_number(127) == bytes([0x7F])
assert codec.encode_signed_number(0x11) == bytes([0x11])
assert codec.encode_signed_number(255) == bytes([0x00, 0xFF])
for [inputData, expectedData] in testVectors1:
ramonalobont marked this conversation as resolved.
Show resolved Hide resolved
assert codec.encode_signed_number(inputData) == bytes([expectedData])
for [inputData, expectedData] in testVectors2:
assert codec.encode_signed_number(inputData) == bytes([expectedData])
for [inputData, expectedData] in testVectors3:
assert codec.encode_signed_number(inputData) == bytes([expectedData])
for [inputData, [expectedData1, expectedData2]] in testVectors4:
assert codec.encode_signed_number(inputData) == bytes([expectedData1, expectedData2])
53 changes: 53 additions & 0 deletions multiversx_sdk/core/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,56 @@
DIGEST_SIZE = 32

TOKEN_RANDOM_SEQUENCE_LENGTH = 6

# Sentinel: 2 (2 ^ 1)
ramonalobont marked this conversation as resolved.
Show resolved Hide resolved
testVectors1 = [
[-1, 0XFF],
[1, 0X01],
[2, 0X02],
[-2, 0XFE],
[3, 0X03],
[-3, 0XFD],
[4, 0X04],
[-4, 0XFC],
]

# Sentinel: 4 (2 ^ 2)
testVectors2 = [
[-1, 0XFF],
[1, 0X01],
[2, 0X02],
[-2, 0XFE],
[3, 0X03],
[-3, 0XFD],
[4, 0X04],
[-4, 0XFC],
[5, 0X05],
[-5, 0XFB],
[6, 0X06],
[-6, 0XFA],
]

# Sentinel: 128 (2 ^ 7)
testVectors3 = [
[125, 0X7D],
[-125, 0X83],
[126, 0X7E],
[-126, 0X82],
[127, 0X7F],
[-127, 0X81],
[-128, 0X80],
]

# Sentinel: 256 (2 ^ 8)
testVectors4 = [
[128, [0x00, 0x80]],
[129, [0x00, 0x81]],
[-129, [0xFF, 0x7F]],
[130, [0x00, 0x82]],
[-130, [0xFF, 0x7E]],
[253, [0x00, 0xFD]],
[256, [0x01, 0x00]],
[-256, [0xFF, 0x00]],
[-257, [0xFE, 0xFF]],
[258, [0x01, 0x02]],
]
Loading