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

Binary encoding is not proper big-endian byte array even if binaryOrder = BigEndian #134

Closed
omerfirmak opened this issue Jul 28, 2023 · 1 comment

Comments

@omerfirmak
Copy link
Contributor

bitset/bitset.go

Lines 933 to 939 in 7e78d86

nWords := b.wordCount()
for i := range b.set[:nWords] {
binaryOrder.PutUint64(buf, b.set[i])
if nn, err := stream.Write(buf); err != nil {
return int64(i*int(wordBytes) + nn + n), err
}
}

WriteTo first writes the least significant word, so even if the individual words are in big-endian format entire bitset is not properly encoded as a big-endian byte array.

testcase:

func TestBitsetEncode(t *testing.T) {
	bs := bitset.New(128)
	bs.Set(64)
	bin, err := bs.MarshalBinary()
	require.NoError(t, err)

	assert.Equal(t, "000000000000008000000000000000010000000000000000", hex.EncodeToString(bin))
}

fails with:

--- FAIL: TestBitsetEncode (0.00s)
    state_test.go:680: 
                Error Trace:    /home/omer/Documents/juno/core/state_test.go:680
                Error:          Not equal: 
                                expected: "000000000000008000000000000000010000000000000000"
                                actual  : "000000000000008000000000000000000000000000000001"
                            
                                Diff:
                                --- Expected
                                +++ Actual
                                @@ -1 +1 @@
                                -000000000000008000000000000000010000000000000000
                                +000000000000008000000000000000000000000000000001
                Test:           TestBitsetEncode

expected big-endian encoding breakdown;

length           bits [128, 64]       bits [64, 0]
0000000000000080 0000000000000001 0000000000000000

current incorrect "big-endian" encoding generated by bitset.MarshalBinary

length           bits [64, 0]       bits [128, 64]
0000000000000080 0000000000000000 0000000000000001
@lemire
Copy link
Member

lemire commented Jul 28, 2023

A serialization bug would be an issue where you cannot recover the serialized bitset. Your example does not illustrate a bug.

@lemire lemire closed this as completed Jul 28, 2023
@bits-and-blooms bits-and-blooms locked as resolved and limited conversation to collaborators Jul 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants