aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2021-09-15 23:32:48 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2021-09-15 23:32:48 +0200
commitb6d0a8fe0cb1a7e1ea3a858e5102b1b2acdbc2c0 (patch)
tree521b0a002808efc1aa3382481745191de023545f
parent662d27b79650176d27cdc5589a0862a33dbcc313 (diff)
xxhash: Add tests for streaming input.
-rw-r--r--core/hash/xxhash/streaming.odin24
-rw-r--r--tests/core/hash/test_core_hash.odin30
2 files changed, 39 insertions, 15 deletions
diff --git a/core/hash/xxhash/streaming.odin b/core/hash/xxhash/streaming.odin
index 95ff38f78..737e37eae 100644
--- a/core/hash/xxhash/streaming.odin
+++ b/core/hash/xxhash/streaming.odin
@@ -22,7 +22,7 @@ XXH3_128_reset :: proc(state: ^XXH3_state) -> (err: Error) {
if state == nil {
return .Error
}
- XXH3_reset_internal(state, 0, XXH3_kSecret[:])
+ XXH3_reset_internal(state, 0, XXH3_kSecret[:], len(XXH3_kSecret))
return .None
}
XXH3_64_reset :: XXH3_128_reset
@@ -34,7 +34,7 @@ XXH3_128_reset_with_secret :: proc(state: ^XXH3_state, secret: []u8) -> (err: Er
if secret == nil || len(secret) < XXH3_SECRET_SIZE_MIN {
return .Error
}
- XXH3_reset_internal(state, 0, secret)
+ XXH3_reset_internal(state, 0, secret, len(secret))
return .None
}
XXH3_64_reset_with_secret :: XXH3_128_reset_with_secret
@@ -46,15 +46,18 @@ XXH3_128_reset_with_seed :: proc(state: ^XXH3_state, seed: XXH64_hash) -> (err:
if seed != state.seed {
XXH3_init_custom_secret(state.custom_secret[:], seed)
}
-
- XXH3_reset_internal(state, seed, nil)
+ XXH3_reset_internal(state, seed, nil, XXH_SECRET_DEFAULT_SIZE)
return .None
}
XXH3_64_reset_with_seed :: XXH3_128_reset_with_seed
XXH3_128_update :: proc(state: ^XXH3_state, input: []u8) -> (err: Error) {
+ if len(input) < XXH3_MIDSIZE_MAX {
+ return .Error
+ }
return XXH3_update(state, input, XXH3_accumulate_512, XXH3_scramble_accumulator)
}
+XXH3_64_update :: XXH3_128_update
XXH3_128_digest :: proc(state: ^XXH3_state) -> (hash: XXH3_128_hash) {
secret := state.custom_secret[:] if len(state.external_secret) == 0 else state.external_secret[:]
@@ -137,7 +140,7 @@ XXH3_copy_state :: proc(dest, src: ^XXH3_state) {
mem_copy(dest, src, size_of(XXH3_state))
}
-XXH3_reset_internal :: proc(state: ^XXH3_state, seed: XXH64_hash, secret: []u8) {
+XXH3_reset_internal :: proc(state: ^XXH3_state, seed: XXH64_hash, secret: []u8, secret_size: uint) {
assert(state != nil)
init_start := offset_of(XXH3_state, buffered_size)
@@ -162,10 +165,9 @@ XXH3_reset_internal :: proc(state: ^XXH3_state, seed: XXH64_hash, secret: []u8)
state.seed = seed
state.external_secret = secret
- secret_length := uint(len(secret))
- assert(secret_length > XXH3_SECRET_SIZE_MIN)
+ assert(secret_size >= XXH3_SECRET_SIZE_MIN)
- state.secret_limit = secret_length - XXH_STRIPE_LEN
+ state.secret_limit = secret_size - XXH_STRIPE_LEN
state.stripes_per_block = state.secret_limit / XXH_SECRET_CONSUME_RATE
}
@@ -211,6 +213,8 @@ XXH3_update :: #force_inline proc(
length := len(input)
secret := state.custom_secret[:] if len(state.external_secret) == 0 else state.external_secret[:]
+ assert(len(input) > 0)
+
state.total_length += u64(length)
assert(state.buffered_size <= XXH3_INTERNAL_BUFFER_SIZE)
@@ -265,10 +269,6 @@ XXH3_update :: #force_inline proc(
return .None
}
-XXH3_64_update :: proc(state: ^XXH3_state, input: []u8) -> (err: Error) {
- return XXH3_update(state, input, XXH3_accumulate_512, XXH3_scramble_accumulator)
-}
-
XXH3_digest_long :: #force_inline proc(acc: []u64, state: ^XXH3_state, secret: []u8) {
/*
Digest on a local copy. This way, the state remains unaltered, and it can
diff --git a/tests/core/hash/test_core_hash.odin b/tests/core/hash/test_core_hash.odin
index 74884dd68..34d42cd9b 100644
--- a/tests/core/hash/test_core_hash.odin
+++ b/tests/core/hash/test_core_hash.odin
@@ -202,15 +202,39 @@ test_xxhash_vectors :: proc(t: ^testing.T) {
xxh32 := xxhash.XXH32(b, u32(seed))
xxh64 := xxhash.XXH64(b, seed)
+ xxh3_64 := xxhash.XXH3_64(b, seed)
xxh3_128 := xxhash.XXH3_128(b, seed)
- xxh32_error := fmt.tprintf("[ XXH32(%03d)] Expected: %08x. Got: %08x.", i, v.xxh_32, xxh32)
- xxh64_error := fmt.tprintf("[ XXH64(%03d)] Expected: %16x. Got: %16x.", i, v.xxh_64, xxh64)
- xxh3_128_error := fmt.tprintf("[XXH3_128(%03d)] Expected: %32x. Got: %32x.", i, v.xxh3_128, xxh3_128)
+ xxh32_error := fmt.tprintf("[ XXH32(%03d) ] Expected: %08x. Got: %08x.", i, v.xxh_32, xxh32)
+ xxh64_error := fmt.tprintf("[ XXH64(%03d) ] Expected: %16x. Got: %16x.", i, v.xxh_64, xxh64)
+
+ xxh3_64_error := fmt.tprintf("[XXH3_64(%03d) ] Expected: %16x. Got: %16x.", i, v.xxh3_64, xxh3_64)
+ xxh3_128_error := fmt.tprintf("[XXH3_128(%03d) ] Expected: %32x. Got: %32x.", i, v.xxh3_128, xxh3_128)
expect(t, xxh32 == v.xxh_32, xxh32_error)
expect(t, xxh64 == v.xxh_64, xxh64_error)
+ expect(t, xxh3_64 == v.xxh3_64, xxh3_64_error)
expect(t, xxh3_128 == v.xxh3_128, xxh3_128_error)
+
+ if len(b) > xxhash.XXH3_MIDSIZE_MAX {
+ fmt.printf("XXH3 - size: %v\n", len(b))
+
+ xxh3_state, _ := xxhash.XXH3_create_state()
+ xxhash.XXH3_64_reset_with_seed(xxh3_state, seed)
+ xxhash.XXH3_64_update(xxh3_state, b)
+ xxh3_64_streamed := xxhash.XXH3_64_digest(xxh3_state)
+ xxhash.XXH3_destroy_state(xxh3_state)
+ xxh3_64s_error := fmt.tprintf("[XXH3_64s(%03d) ] Expected: %16x. Got: %16x.", i, v.xxh3_64, xxh3_64_streamed)
+ expect(t, xxh3_64_streamed == v.xxh3_64, xxh3_64s_error)
+
+ xxh3_state2, _ := xxhash.XXH3_create_state()
+ xxhash.XXH3_128_reset_with_seed(xxh3_state2, seed)
+ xxhash.XXH3_128_update(xxh3_state2, b)
+ xxh3_128_streamed := xxhash.XXH3_128_digest(xxh3_state2)
+ xxhash.XXH3_destroy_state(xxh3_state2)
+ xxh3_128s_error := fmt.tprintf("[XXH3_128s(%03d) ] Expected: %32x. Got: %32x.", i, v.xxh3_128, xxh3_128_streamed)
+ expect(t, xxh3_128_streamed == v.xxh3_128, xxh3_128s_error)
+ }
}
}