bits-and-blooms / bitset

Go package implementing bitsets
BSD 3-Clause "New" or "Revised" License
1.32k stars 173 forks source link

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

Closed omerfirmak closed 1 year ago

omerfirmak commented 1 year ago

https://github.com/bits-and-blooms/bitset/blob/7e78d86f98bc571f7712a86620fe0ba2c79dedc6/bitset.go#L933-L939

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 commented 1 year ago

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