zeebo / xxh3

XXH3 algorithm in Go
BSD 2-Clause "Simplified" License
403 stars 20 forks source link

Hasher: Incorrect Hash len(payload) > 1089 #14

Closed klauspost closed 3 years ago

klauspost commented 3 years ago

Changing the TestCompatWithC_XXH3_64bits to the following produces mismatch after 1088 bytes:

func TestCompatWithC_XXH3_64bits(t *testing.T) {
    buf := make([]byte, len(testVecs64))
    for i, exp := range testVecs64 {
        buf[i] = byte((i + 1) % 251)
        if got := Hash(buf[:i]); got != exp {
            t.Fatalf("% -4d: %016x != %016x", i, got, exp)
        }
        if got := HashString(string(buf[:i])); got != exp {
            t.Fatalf("% -4d: %016x != %016x", i, got, exp)
        }

        // NEW:
        h := New()
        h.Write(buf[:i])
        if got := h.Sum64(); got != exp {
            t.Fatalf("% -4d: %016x != %016x", i, got, exp)
        }
        h.Reset()
        h.WriteString(string(buf[:i]))
        if got := h.Sum64(); got != exp {
            t.Fatalf("% -4d: %016x != %016x", i, got, exp)
        }
    }
}

Produces output:

=== RUN   TestCompatWithC_XXH3_64bits
    compat_test.go:20:  1089: 9ac8e74b6ae33cfe != e6b8f8f8df938429
--- FAIL: TestCompatWithC_XXH3_64bits (0.00s)
klauspost commented 3 years ago

TestHasherCompat also fails if you change the input to be buf[i] = byte((i + 1) % 251). Strange.