diff --git a/pkg/vm/emit/bigint.go b/pkg/vm/emit/bigint.go index 8c14a377c..fb555036c 100644 --- a/pkg/vm/emit/bigint.go +++ b/pkg/vm/emit/bigint.go @@ -80,10 +80,13 @@ func getEffectiveSize(buf []byte, isNeg bool) int { } // IntToBytes converts integer to a slice in little-endian format. +// Note: NEO3 serialization differs from default C# BigInteger.ToByteArray() +// when n == 0. For zero is equal to empty slice in NEO3. +// https://github.com/neo-project/neo-vm/blob/master/src/neo-vm/Types/Integer.cs#L16 func IntToBytes(n *big.Int) []byte { sign := n.Sign() if sign == 0 { - return []byte{0} + return []byte{} } var ws []big.Word diff --git a/pkg/vm/emit/bigint_test.go b/pkg/vm/emit/bigint_test.go index 2195ac3dc..71ec2326a 100644 --- a/pkg/vm/emit/bigint_test.go +++ b/pkg/vm/emit/bigint_test.go @@ -16,7 +16,7 @@ var testCases = []struct { number int64 buf []byte }{ - {0, []byte{0}}, + {0, []byte{}}, {1, []byte{1}}, {-1, []byte{255}}, {2, []byte{2}}, @@ -52,7 +52,7 @@ var testCases = []struct { {-9187484529235886209, []byte{127, 127, 127, 127, 127, 127, 127, 128}}, // https://github.com/dotnet/runtime/blob/master/src/libraries/System.Runtime.Numerics/tests/BigInteger/ToByteArray.cs#L14 - {0, []byte{0x00}}, + // {0, []byte{0x00}}, commented because this test check default `BigInteger` serialization, not NEO3 serialization. {3, []byte{0x03}}, {128, []byte{0x80, 0x00}}, {200, []byte{0xc8, 0x00}}, diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 4f655870c..afe15ce62 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -2213,7 +2213,7 @@ func TestCATInt0ByteArray(t *testing.T) { vm.estack.PushVal([]byte{}) runVM(t, vm) assert.Equal(t, 1, vm.estack.Len()) - assert.Equal(t, &ByteArrayItem{[]byte{0}}, vm.estack.Pop().value) + assert.Equal(t, &ByteArrayItem{[]byte{}}, vm.estack.Pop().value) } func TestCATByteArrayInt1(t *testing.T) {