FoldHash Explained: The Rust Hash That Replaced aHash

FoldHash explained in plain terms means understanding the hash that quietly became Rust’s default, a design from 2024 that turns any key into a bucket number with a single folded multiply. The central question is why a hash table would swap a proven hasher for a new one, and the answer is that tables spend most of their time hashing short keys, so FoldHash gives hashbrown both speed and simplicity in a handful of instructions, and that economy is the whole point of the design.

FoldHash is a fast, non-cryptographic hash function created by Orson Peters, the Rust programmer behind the orlp crates, and it is the default hasher in the hashbrown crate from version 0.15.0. It turns a key into a 64-bit value by multiplying 64-bit words and folding the 128-bit result back on itself, and it is built on the idea that a hash can be both fast and good enough when its job is only to fill a table, which means the design trades attack resistance for speed, and that trade is what makes it unlike the hashes of its era.

This article explains what FoldHash is, how it works, and where it runs in the real world.

Key takeaways

  • FoldHash is a fast Rust hasher from 2024, by Orson Peters.
  • It is the default hasher in hashbrown from version 0.15.0.
  • It mixes with a folded multiply, not with AES rounds.
  • It comes in fast and quality variants for different jobs.
  • It is minimally DoS-resistant, never cryptographic.

What FoldHash is

FoldHash is a hash that turns a key into a 64-bit value with one core operation, and that smallness is what makes it unusual among hashes. Each word of input passes through a folded multiply, which means the hash spreads bits with a single multiplication, and because a multiply mixes more bit pairs than an xor, the result is well spread after very few steps.

Its signature feature is the fold. Most hashes mix with a chain of shifts and xors, but FoldHash multiplies two 64-bit words into a 128-bit product and xors the two halves together, which means every output bit depends on many input bits, and because the fold happens on every step, the mixing builds up as the key flows in.

The design was a reaction to the cost of the old defaults. Rust’s standard HashMap used SipHash, which is safe against attackers but slow, so the hashbrown project wanted a hasher that was fast for the common case, because a table that hashes every key on every lookup pays for its hasher thousands of times a second, and that practicality is what made FoldHash worth adopting.

It became one of the best-known fast hashes in the Rust world, and it is named in every discussion of the field. FoldHash ships as the default in hashbrown, it runs in Rust programs that use the crate directly, and it is still the first example engineers meet when they learn that a multiply can hash, which means the design survives as a tool even though it is not the standard library default.

Its most famous property is its two variants. The fast variant is tuned for hash tables and bloom filters, while the quality variant adds extra folding for statistics work, which means the same core serves two different jobs, and because the choice is explicit, users pick speed or quality on purpose rather than by accident.

Our guide to MurmurHash explains the earlier multiply-and-rotate hash it is measured against, and our guide to xxHash explains the speed champion that sets the pace for fast hashing.

How FoldHash works

Everything starts with the seed, which sets the initial state. FoldHash takes a 128-bit secret seed and splits it into the starting values, which means the hash depends on the seed, and because the seed is fixed by default in hashbrown, the same key always gives the same hash in one program.

Each step begins with a word of input, which is mixed by the folded multiply. FoldHash takes the next 64-bit word, multiplies it with the running state into a 128-bit product, which means fresh data arrives at every step, and because the multiply is wide, the mixing spreads across all 64 output bits at once.

The fold is what makes the design work as a whole. After the multiply, FoldHash xors the high and low halves of the 128-bit product together, which means the value is scrambled thoroughly, and because the fold is a single xor, the mixing costs almost nothing.

Short inputs take a special path. FoldHash hashes keys of up to a few words with a dedicated short routine, which means small keys skip the long loop entirely, and because most hash table keys are short, that shortcut is where the speed is won.

Long inputs are processed in blocks. FoldHash feeds 64-bit words through the same folded multiply again and again, which means the state keeps mixing as data flows in, and because each step depends on the last, the final value reflects the whole input.

The result comes from the folded state, read out as 64 bits. FoldHash outputs the running value directly as the hash, which means the result is a fixed size whatever the input, and that size is exactly what a hash table needs for its bucket index.

A dark office desk with a laptop showing a glowing green cipher diagram, a brass key and a notebook beside it
A dark office desk with a laptop showing a glowing green cipher diagram, a brass key and a notebook beside it, shows how FoldHash works: words take turns being folded through the multiply, chunk after chunk, until a hash emerges.

Comparing FoldHash with other table hashes

FoldHash is one of several fast hashes built for tables, and each was built with a different emphasis. aHash is the AES-based hash that hashbrown used before, SipHash-1-3 is the attack-resistant hash Rust’s standard library still uses, and FoldHash is the one built on a folded multiply, while each approach trades something for something else.

The table below sets out the main contrasts between the leading approaches.

Aspect FoldHash aHash SipHash-1-3
Output size 64 bits 64 bits 64 bits
Core step Folded multiply AES rounds SipHash rounds
DoS resistance Minimal Low Strong
Default in hashbrown 0.15.0 hashbrown before 0.15 Rust std HashMap
Best for Hash tables General hashing Adversarial input

The practical result is that FoldHash is the right choice for hash tables, aHash suits general hashing, and SipHash-1-3 fits adversarial input instead.

Where FoldHash is used

Rust hash tables use it as their default. hashbrown’s HashMap and HashSet hash every key with FoldHash, because it is fast and simple, and that adoption is the reason it runs in real programs rather than only in benchmarks.

Bloom filters use it to test membership quickly. A filter that checks millions of items needs a hash that is both fast and well spread, because a slow hash becomes the bottleneck, and that speed is the reason FoldHash earns its keep in probabilistic data structures.

Count sketching uses it to estimate frequencies. A sketch that counts heavy hitters hashes every event, because the estimate needs many independent hashes, and that cheap mixing is the reason FoldHash appears in streaming code.

Statistics tools use the quality variant. HyperLogLog and MinHash need hashes with proper avalanche behaviour, because a biased hash skews the estimate, and that quality is the reason the second variant exists at all.

Teaching uses it as the folded-multiply example. Courses introduce FoldHash when they explain that a multiply can hash, because it is the clearest case of the idea in a real design, and a student who understands FoldHash understands why one multiply beats a chain of xors.

Strengths and limits

The greatest strength of FoldHash is its speed on short keys. A hash that mixes with one multiply gives tables faster lookups for free, and that strength is real, measured in published benchmarks and confirmed by the crate that adopted it.

It is also a strength that the design has no dependencies. A hash with no runtime dependencies and no_std support fits anywhere, and that portability is worth more than cleverness in a field where a hash that drags in a crate is a hash nobody adopts.

The limits are real, though, and the biggest is the DoS resistance. FoldHash is only minimally resistant to hash-flooding attacks, which means it must never hash untrusted input in a network service, and a system that faces attackers must use SipHash or a randomised hasher instead.

FoldHash checklist

  • Use FoldHash for hash tables, never for security or passwords.
  • Remember it outputs 64 bits from a folded multiply.
  • Note the default seed is fixed in hashbrown.
  • Compare it with aHash to see two kinds of fast hash.
  • Study the folded multiply, which is what mixes the bits.
  • Check the variant, since fast and quality serve different jobs.
  • Keep real secrets behind cryptographic hashes.
  • Read the foldhash crate docs to see how the seed works.
  • Choose SipHash when untrusted input must be hashed.

Frequently asked questions

What is FoldHash in simple terms?

It is a 2024 fast hash that turns any key into a 64-bit value by multiplying words and folding the 128-bit product back on itself.

Who created FoldHash?

Orson Peters, the Rust programmer behind the orlp crates, who built it for the hashbrown project.

How is FoldHash different from aHash?

Both are fast Rust hashes, but FoldHash mixes with a folded multiply while aHash mixes with AES rounds.

Is FoldHash still secure?

It is fast and well tested, but it is only minimally DoS-resistant, so it must never guard secrets or hash untrusted input.

Where is FoldHash used?

In hashbrown’s HashMap and HashSet, in bloom filters, in count sketching, and in teaching about multiply-based hashing.

When should I use FoldHash?

When you need the fastest hash for a table, when your keys are trusted, or when you study folded-multiply design.

The bottom line

FoldHash explained in one line is this: the folded-multiply hash, a 2024 design of one multiply and a fold that became Rust’s table hasher and taught the field that a multiply can hash.

It is proof that one multiply is enough, and the reason it still matters is that every fast hash designed since has been judged partly by the lesson FoldHash taught.

This article is general information about how FoldHash works, not advice about choosing encryption for a particular device or system.

Latest articles

Scroll to Top