From 6baf2396e09328eced29b416b7c622e918d07748 Mon Sep 17 00:00:00 2001 From: Ramona Lobont Date: Thu, 1 Feb 2024 14:52:01 +0200 Subject: [PATCH 1/6] Fix encode signed numbers. --- multiversx_sdk/core/codec.py | 3 ++- multiversx_sdk/core/codec_test.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 multiversx_sdk/core/codec_test.py diff --git a/multiversx_sdk/core/codec.py b/multiversx_sdk/core/codec.py index 28b3f80d..0f1c1926 100644 --- a/multiversx_sdk/core/codec.py +++ b/multiversx_sdk/core/codec.py @@ -7,7 +7,8 @@ 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])) + length = (arg.bit_length() + 7) // 8 + return arg.to_bytes(length, byteorder="big", signed=True) def decode_unsigned_number(arg: bytes) -> int: diff --git a/multiversx_sdk/core/codec_test.py b/multiversx_sdk/core/codec_test.py new file mode 100644 index 00000000..fc5901c4 --- /dev/null +++ b/multiversx_sdk/core/codec_test.py @@ -0,0 +1,10 @@ +from multiversx_sdk.core import codec + + +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]) From 10afc9392c761be4fd6d26fe4671b2d1fbac946b Mon Sep 17 00:00:00 2001 From: Ramona Lobont Date: Thu, 1 Feb 2024 17:21:33 +0200 Subject: [PATCH 2/6] Fix encode signed number function for positive numbers. --- multiversx_sdk/core/codec.py | 5 ++++- multiversx_sdk/core/codec_test.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/multiversx_sdk/core/codec.py b/multiversx_sdk/core/codec.py index 0f1c1926..169d6c15 100644 --- a/multiversx_sdk/core/codec.py +++ b/multiversx_sdk/core/codec.py @@ -8,7 +8,10 @@ def encode_unsigned_number(arg: int) -> bytes: def encode_signed_number(arg: int) -> bytes: length = (arg.bit_length() + 7) // 8 - return arg.to_bytes(length, byteorder="big", signed=True) + if arg >= 0: + return arg.to_bytes(length, byteorder="big") + else: + return arg.to_bytes(length, byteorder="big", signed=True) def decode_unsigned_number(arg: bytes) -> int: diff --git a/multiversx_sdk/core/codec_test.py b/multiversx_sdk/core/codec_test.py index fc5901c4..8b566f33 100644 --- a/multiversx_sdk/core/codec_test.py +++ b/multiversx_sdk/core/codec_test.py @@ -8,3 +8,7 @@ def test_encode_signed_number(): 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([0xFF]) From 63f84d3ef7253fccbc01419f796077a44c6dcc9f Mon Sep 17 00:00:00 2001 From: Ramona Lobont Date: Fri, 2 Feb 2024 13:48:23 +0200 Subject: [PATCH 3/6] Fix encode signed number + add some test vectors --- multiversx_sdk/core/codec.py | 6 ++-- multiversx_sdk/core/codec_test.py | 12 ++++++- multiversx_sdk/core/constants.py | 53 +++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/multiversx_sdk/core/codec.py b/multiversx_sdk/core/codec.py index 169d6c15..c7204601 100644 --- a/multiversx_sdk/core/codec.py +++ b/multiversx_sdk/core/codec.py @@ -7,8 +7,10 @@ def encode_unsigned_number(arg: int) -> bytes: def encode_signed_number(arg: int) -> bytes: - length = (arg.bit_length() + 7) // 8 - if arg >= 0: + 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) diff --git a/multiversx_sdk/core/codec_test.py b/multiversx_sdk/core/codec_test.py index 8b566f33..eaf22418 100644 --- a/multiversx_sdk/core/codec_test.py +++ b/multiversx_sdk/core/codec_test.py @@ -1,4 +1,6 @@ from multiversx_sdk.core import codec +from multiversx_sdk.core.constants import (testVectors1, testVectors2, + testVectors3, testVectors4) def test_encode_signed_number(): @@ -11,4 +13,12 @@ def test_encode_signed_number(): 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([0xFF]) + assert codec.encode_signed_number(255) == bytes([0x00, 0xFF]) + for [inputData, expectedData] in testVectors1: + 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]) diff --git a/multiversx_sdk/core/constants.py b/multiversx_sdk/core/constants.py index 175dc3e6..6f41da37 100644 --- a/multiversx_sdk/core/constants.py +++ b/multiversx_sdk/core/constants.py @@ -19,3 +19,56 @@ DIGEST_SIZE = 32 TOKEN_RANDOM_SEQUENCE_LENGTH = 6 + +# Sentinel: 2 (2 ^ 1) +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]], +] From 9cec5e52527a24475a5e5c5ad8eb526964b1883e Mon Sep 17 00:00:00 2001 From: Ramona Lobont Date: Mon, 5 Feb 2024 09:45:04 +0200 Subject: [PATCH 4/6] Fixes after reviews. --- multiversx_sdk/core/codec.py | 5 +-- multiversx_sdk/core/codec_test.py | 67 ++++++++++++++++++++++++++----- multiversx_sdk/core/constants.py | 53 ------------------------ 3 files changed, 58 insertions(+), 67 deletions(-) diff --git a/multiversx_sdk/core/codec.py b/multiversx_sdk/core/codec.py index c7204601..74791f0a 100644 --- a/multiversx_sdk/core/codec.py +++ b/multiversx_sdk/core/codec.py @@ -10,10 +10,7 @@ def encode_signed_number(arg: int) -> bytes: 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) + return arg.to_bytes(length, byteorder="big", signed=True) def decode_unsigned_number(arg: bytes) -> int: diff --git a/multiversx_sdk/core/codec_test.py b/multiversx_sdk/core/codec_test.py index eaf22418..efbfd68c 100644 --- a/multiversx_sdk/core/codec_test.py +++ b/multiversx_sdk/core/codec_test.py @@ -1,6 +1,53 @@ from multiversx_sdk.core import codec -from multiversx_sdk.core.constants import (testVectors1, testVectors2, - testVectors3, testVectors4) + +test_vectors_1 = [ + [-1, 0XFF], + [1, 0X01], + [2, 0X02], + [-2, 0XFE], + [3, 0X03], + [-3, 0XFD], + [4, 0X04], + [-4, 0XFC], +] + +test_vectors_2 = [ + [-1, 0XFF], + [1, 0X01], + [2, 0X02], + [-2, 0XFE], + [3, 0X03], + [-3, 0XFD], + [4, 0X04], + [-4, 0XFC], + [5, 0X05], + [-5, 0XFB], + [6, 0X06], + [-6, 0XFA], +] + +test_vectors_3 = [ + [125, 0X7D], + [-125, 0X83], + [126, 0X7E], + [-126, 0X82], + [127, 0X7F], + [-127, 0X81], + [-128, 0X80], +] + +test_vectors_4 = [ + [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]], +] def test_encode_signed_number(): @@ -14,11 +61,11 @@ def test_encode_signed_number(): 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: - 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]) + for [input_data, expected_data] in test_vectors_1: + assert codec.encode_signed_number(input_data) == bytes([expected_data]) + for [input_data, expected_data] in test_vectors_2: + assert codec.encode_signed_number(input_data) == bytes([expected_data]) + for [input_data, expected_data] in test_vectors_3: + assert codec.encode_signed_number(input_data) == bytes([expected_data]) + for [input_data, [expected_data_1, expected_data_2]] in test_vectors_4: + assert codec.encode_signed_number(input_data) == bytes([expected_data_1, expected_data_2]) diff --git a/multiversx_sdk/core/constants.py b/multiversx_sdk/core/constants.py index 6f41da37..175dc3e6 100644 --- a/multiversx_sdk/core/constants.py +++ b/multiversx_sdk/core/constants.py @@ -19,56 +19,3 @@ DIGEST_SIZE = 32 TOKEN_RANDOM_SEQUENCE_LENGTH = 6 - -# Sentinel: 2 (2 ^ 1) -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]], -] From 03506dd210720d97153b2d5fa588859216628292 Mon Sep 17 00:00:00 2001 From: Ramona Lobont Date: Mon, 5 Feb 2024 13:15:16 +0200 Subject: [PATCH 5/6] Readability fixes. --- multiversx_sdk/core/codec_test.py | 40 +++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/multiversx_sdk/core/codec_test.py b/multiversx_sdk/core/codec_test.py index efbfd68c..acee3b48 100644 --- a/multiversx_sdk/core/codec_test.py +++ b/multiversx_sdk/core/codec_test.py @@ -1,37 +1,37 @@ from multiversx_sdk.core import codec test_vectors_1 = [ - [-1, 0XFF], + [-1, 0Xff], [1, 0X01], [2, 0X02], - [-2, 0XFE], + [-2, 0Xfe], [3, 0X03], - [-3, 0XFD], + [-3, 0Xfd], [4, 0X04], - [-4, 0XFC], + [-4, 0Xfc], ] test_vectors_2 = [ - [-1, 0XFF], + [-1, 0Xff], [1, 0X01], [2, 0X02], - [-2, 0XFE], + [-2, 0Xfe], [3, 0X03], - [-3, 0XFD], + [-3, 0Xfd], [4, 0X04], - [-4, 0XFC], + [-4, 0Xfc], [5, 0X05], - [-5, 0XFB], + [-5, 0Xfb], [6, 0X06], - [-6, 0XFA], + [-6, 0Xfa], ] test_vectors_3 = [ - [125, 0X7D], + [125, 0X7d], [-125, 0X83], - [126, 0X7E], + [126, 0X7e], [-126, 0X82], - [127, 0X7F], + [127, 0X7f], [-127, 0X81], [-128, 0X80], ] @@ -39,13 +39,13 @@ test_vectors_4 = [ [128, [0x00, 0x80]], [129, [0x00, 0x81]], - [-129, [0xFF, 0x7F]], + [-129, [0xff, 0x7f]], [130, [0x00, 0x82]], - [-130, [0xFF, 0x7E]], - [253, [0x00, 0xFD]], + [-130, [0xff, 0x7e]], + [253, [0x00, 0xfd]], [256, [0x01, 0x00]], - [-256, [0xFF, 0x00]], - [-257, [0xFE, 0xFF]], + [-256, [0xff, 0x00]], + [-257, [0xfe, 0xff]], [258, [0x01, 0x02]], ] @@ -61,11 +61,15 @@ def test_encode_signed_number(): 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 [input_data, expected_data] in test_vectors_1: assert codec.encode_signed_number(input_data) == bytes([expected_data]) + for [input_data, expected_data] in test_vectors_2: assert codec.encode_signed_number(input_data) == bytes([expected_data]) + for [input_data, expected_data] in test_vectors_3: assert codec.encode_signed_number(input_data) == bytes([expected_data]) + for [input_data, [expected_data_1, expected_data_2]] in test_vectors_4: assert codec.encode_signed_number(input_data) == bytes([expected_data_1, expected_data_2]) From 7262515c8b2de7c93805cde1af18c2389fd7f84f Mon Sep 17 00:00:00 2001 From: Ramona Lobont Date: Mon, 5 Feb 2024 15:04:19 +0200 Subject: [PATCH 6/6] Readability fixes. --- multiversx_sdk/core/codec_test.py | 64 +++++++++++++++---------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/multiversx_sdk/core/codec_test.py b/multiversx_sdk/core/codec_test.py index acee3b48..4ade2d2a 100644 --- a/multiversx_sdk/core/codec_test.py +++ b/multiversx_sdk/core/codec_test.py @@ -1,51 +1,51 @@ from multiversx_sdk.core import codec test_vectors_1 = [ - [-1, 0Xff], - [1, 0X01], - [2, 0X02], - [-2, 0Xfe], - [3, 0X03], - [-3, 0Xfd], - [4, 0X04], - [-4, 0Xfc], + [-1, 0xFF], + [1, 0x01], + [2, 0x02], + [-2, 0xFE], + [3, 0x03], + [-3, 0xFD], + [4, 0x04], + [-4, 0xFC], ] test_vectors_2 = [ - [-1, 0Xff], - [1, 0X01], - [2, 0X02], - [-2, 0Xfe], - [3, 0X03], - [-3, 0Xfd], - [4, 0X04], - [-4, 0Xfc], - [5, 0X05], - [-5, 0Xfb], - [6, 0X06], - [-6, 0Xfa], + [-1, 0xFF], + [1, 0x01], + [2, 0x02], + [-2, 0xFE], + [3, 0x03], + [-3, 0xFD], + [4, 0x04], + [-4, 0xFC], + [5, 0x05], + [-5, 0xFB], + [6, 0x06], + [-6, 0xFA], ] test_vectors_3 = [ - [125, 0X7d], - [-125, 0X83], - [126, 0X7e], - [-126, 0X82], - [127, 0X7f], - [-127, 0X81], - [-128, 0X80], + [125, 0x7D], + [-125, 0x83], + [126, 0x7E], + [-126, 0x82], + [127, 0x7F], + [-127, 0x81], + [-128, 0x80], ] test_vectors_4 = [ [128, [0x00, 0x80]], [129, [0x00, 0x81]], - [-129, [0xff, 0x7f]], + [-129, [0xFF, 0x7F]], [130, [0x00, 0x82]], - [-130, [0xff, 0x7e]], - [253, [0x00, 0xfd]], + [-130, [0xFF, 0x7E]], + [253, [0x00, 0xFD]], [256, [0x01, 0x00]], - [-256, [0xff, 0x00]], - [-257, [0xfe, 0xff]], + [-256, [0xFF, 0x00]], + [-257, [0xFE, 0xFF]], [258, [0x01, 0x02]], ]