core/hash/xxhash

xxhash

Types

20

XXH3_accumulate_512_f

XXH3_accumulate_512_f :: proc(acc: []xxh_u64, input: []u8, secret: []u8)Source

XXH3_accumulate_512 is the tightest loop for long inputs, and it is the most optimized. *

  • It is a hardened version of UMAC, based off of FARSH's implementation.

*

  • This was chosen because it adapts quite well to 32-bit, 64-bit, and SIMD
  • implementations, and it is ridiculously fast.

*

  • We harden it by mixing the original input to the accumulators as well as the product.

*

  • This means that in the (relatively likely) case of a multiply by zero, the
  • original input is preserved.

*

  • On 128-bit inputs, we swap 64-bit pairs when we add the input to improve
  • cross-pollination, as otherwise the upper and lower halves would be
  • essentially independent.

*

  • This doesn't matter on 64-bit hashes since they all get merged together in
  • the end, so we skip the extra step.

*

  • Both XXH3_64bits and XXH3_128bits use this subroutine.

XXH3_state

XXH3_state :: struct { acc: [8]u64, custom_secret: [XXH_SECRET_DEFAULT_SIZE]u8, buffer: [256]u8, buffered_size: u32, reserved32: u32, stripes_so_far: uint, total_length: u64, stripes_per_block: uint, secret_limit: uint, seed: u64, reserved64: u64, external_secret: []u8, }Source

Streaming state.

IMPORTANT: This structure has a strict alignment requirement of 64 bytes!! **
	Default allocators will align it correctly if created via `new`, as will
	placing this struct on the stack, but if using a custom allocator make sure
	that it handles the alignment correctly!

xxh_u128

xxh_u128 :: u128Source

** XXH3 128-bit variant ** Stored in little endian order, although the fields themselves are in native endianness.

Constants

37

XXH_SECRET_DEFAULT_SIZE

XXH_SECRET_DEFAULT_SIZE :: max(XXH3_SECRET_SIZE_MIN, #config(XXH_SECRET_DEFAULT_SIZE, 192))Source

XXH3 New generation hash designed for speed on small keys and vectorization One goal of XXH3 is to make it fast on both 32-bit and 64-bit, while remaining a true 64-bit/128-bit hash function. ========================================== XXH3 default settings ========================================== Custom secrets have a default length of 192, but can be set to a different size.

The minimum secret size is 136 bytes. It must also be a multiple of 64.

Variables

1

Procedures

104

XXH32_canonical_from_hash

XXH32_canonical_from_hash :: proc(hash: XXH32_hash) -> (canonical: XXH32_canonical)Source

Canonical representation

The default return values from XXH functions are unsigned 32 and 64 bit integers.

	The canonical representation uses big endian convention,
	the same convention as human-readable numbers (large digits first).

	This way, hash values can be written into a file or buffer, remaining
	comparable across different systems.

	The following functions allow transformation of hash values to and from their
	canonical format.

XXH3_hashLong_64b_default

XXH3_hashLong_64b_default :: proc(input: []u8, seed64: xxh_u64, secret: []u8) -> (hash: xxh_u64)Source

It's important for performance that XXH3_hashLong is not inlined.

Since the function is not inlined, the compiler may not be able to understand that,
	in some scenarios, its `secret` argument is actually a compile time constant.
	This variant enforces that the compiler can detect that,
	and uses this opportunity to streamline the generated code for better performance.

XXH3_hashLong_64b_withSeed

XXH3_hashLong_64b_withSeed :: proc(input: []u8, seed: xxh_u64, secret: []u8) -> (hash: xxh_u64)Source

XXH3_hashLong_64b_withSeed():

Generate a custom key based on alteration of default XXH3_kSecret with the seed,
	and then use this key for long mode hashing.

	This operation is decently fast but nonetheless costs a little bit of time.
	Try to avoid it whenever possible (typically when seed==0).

	It's important for performance that XXH3_hashLong is not inlined. Not sure
	why (uop cache maybe?), but the difference is large and easily measurable.

XXH3_len_1to3_128b

XXH3_len_1to3_128b :: proc(input: []u8, secret: []u8, seed: xxh_u64) -> (res: xxh_u128)Source

==========================================

	   XXH3 128 bits (a.k.a XXH128)
	==========================================
	XXH3's 128-bit variant has better mixing and strength than the 64-bit variant,
	even without counting the significantly larger output size.

	For example, extra steps are taken to avoid the seed-dependent collisions
	in 17-240 byte inputs (See XXH3_mix16B and XXH128_mix32B).

	This strength naturally comes at the cost of some speed, especially on short
	lengths. Note that longer hashes are about as fast as the 64-bit version
	due to it using only a slight modification of the 64-bit loop.

	XXH128 is also more oriented towards 64-bit machines. It is still extremely
	fast for a _128-bit_ hash on 32-bit (it usually clears XXH64).

XXH3_len_1to3_64b

XXH3_len_1to3_64b :: proc(input: []u8, secret: []u8, seed: xxh_u64) -> (res: xxh_u64)Source

==========================================

Short keys
	==========================================
	One of the shortcomings of XXH32 and XXH64 was that their performance was
	sub-optimal on short lengths. It used an iterative algorithm which strongly
	favored lengths that were a multiple of 4 or 8.

	Instead of iterating over individual inputs, we use a set of single shot
	functions which piece together a range of lengths and operate in constant time.
	Additionally, the number of multiplies has been significantly reduced. This
	reduces latency, especially when emulating 64-bit multiplies on 32-bit.

	Depending on the platform, this may or may not be faster than XXH32, but it
	is almost guaranteed to be faster than XXH64.
At very short lengths, there isn't enough input to fully hide secrets, or use the entire secret.

	There is also only a limited amount of mixing we can do before significantly impacting performance.

	Therefore, we use different sections of the secret and always mix two secret samples with an XOR.
	This should have no effect on performance on the seedless or withSeed variants because everything
	_should_ be constant folded by modern compilers.

	The XOR mixing hides individual parts of the secret and increases entropy.
	This adds an extra layer of strength for custom secrets.

XXH3_mix16B

XXH3_mix16B :: proc(input: []u8, secret: []u8, seed: xxh_u64) -> (res: xxh_u64)Source

DISCLAIMER: There are known seed-dependent multicollisions here due to

multiplication by zero, affecting hashes of lengths 17 to 240.

	However, they are very unlikely.

	Keep this in mind when using the unseeded XXH3_64bits() variant: As with all
	unseeded non-cryptographic hashes, it does not attempt to defend itself
	against specially crafted inputs, only random inputs.

	Compared to classic UMAC where a 1 in 2^31 chance of 4 consecutive bytes
	cancelling out the secret is taken an arbitrary number of times (addressed
	in XXH3_accumulate_512), this collision is very unlikely with random inputs
	and/or proper seeding:

	This only has a 1 in 2^63 chance of 8 consecutive bytes cancelling out, in a
	function that is only called up to 16 times per hash with up to 240 bytes of
	input.

	This is not too bad for a non-cryptographic hash function, especially with
	only 64 bit outputs.

	The 128-bit variant (which trades some speed for strength) is NOT affected
	by this, although it is always a good idea to use a proper seed if you care
	about strength.

XXH64_canonical_from_hash

XXH64_canonical_from_hash :: proc(hash: XXH64_hash) -> (canonical: XXH64_canonical)Source

Canonical representation

The default return values from XXH functions are unsigned 32 and 64 bit integers.

	The canonical representation uses big endian convention,
	the same convention as human-readable numbers (large digits first).

	This way, hash values can be written into a file or buffer, remaining
	comparable across different systems.

	The following functions allow transformation of hash values to and from their
	canonical format.

Procedure Groups

2

Reference search

Find anything

Documentation preferences

Settings

System theme variants

Used only while Theme is set to System.