const MaxBuckets
MaxBuckets is the largest shard count Bucket can spread over evenly: the hash is 32 bits, so it names 2^32 slots and no more.
Package murmur3 implements MurmurHash3 x86\_32, Austin Appleby's non-cryptographic hash, as a pure, reusable package.
gno.land/p/moul/x/daily/murmur3/v0MurmurHash3 x86_32 - Sum32, Sum32Seed, SumHex, Hex, Bucket,
Verification.
Austin Appleby's non-cryptographic hash. Reads four bytes at a time, scrambles each block with two odd constants and a rotate, folds the length in, and finishes with an avalanche of shift-xor-multiply that spreads every input bit over every output bit.
1import "gno.land/p/moul/x/daily/murmur3/v0"
2
3murmur3.SumHex("abc") // "b3dd93fa"
4murmur3.Sum32("") // 0
5murmur3.Sum32Seed("abc", 1) // 0xaa75e9ff, unrelated to the seed-0 result
6murmur3.Bucket("moul", 16) // 3, a deterministic shard
7murmur3.Verification() // 0xb0f57ee3, the SMHasher self-test
What it is for: bucketing. Spreading keys over a fixed number of shards, sizing a sketch, picking a deterministic slot, keying one of the independent hash functions a Bloom filter or a count-min sketch needs (that is what the seed is for: it re-keys the whole function).
Bucket spreads evenly up to MaxBuckets, which is 1 << 32, and the ceiling
is the hash rather than the arithmetic: 32 bits of output name 2^32 slots and
no more. A larger n is accepted and answered in range, but every index from
2^32 up is unreachable, so the top half of a 2^33-way split would never see a
key. Nothing rejects it, because that n is a caller bug and a panic inside a
Render is a worse way to find out than a skewed table.
Verification is the reason this package can claim to be the same function as
everyone else's. It runs SMHasher's procedure, hashing 256 keys of growing
length under 256 seeds and hashing the results, and a correct MurmurHash3
x86_32 answers 0xb0f57ee3. One wrong constant, one mis-ordered tail byte or a
rotate in the wrong direction moves it, and no amount of reading the code finds
that. The test asserts it, and the demo realm recomputes it on chain.
⚠️ Not a cryptographic hash. MurmurHash3 is trivially invertible and collisions are cheap to generate, so anyone who chooses the input chooses the bucket. It is not a commitment, not a signature, and a table keyed by caller-supplied strings can be driven into its worst case on purpose.
Live demo: r/moul/x/daily/murmur3demo
· render it at /r/moul/x/daily/murmur3demo/v0.
Part of moul/gno-contracts — moul's versioned gno.land contracts. See the repository for the full catalog, build/test tooling, and usage.
🧪 Highly experimental — potentially vibe-coded. Not audited; may break, change, or be removed at any time. Do not use with anything of value. Full disclaimer: DISCLAIMER.
Package murmur3 implements MurmurHash3 x86_32, Austin Appleby's non-cryptographic hash, as a pure, reusable package.
The function reads the input four bytes at a time. Each block is scrambled by two odd constants and a rotate, mixed into the accumulator, and the accumulator is itself rotated and run through a multiply-add. The leftover one to three bytes take an abbreviated version of the same scramble, the length is folded in, and a final avalanche of shift-xor-multiply spreads every input bit over every output bit. That last stage is why flipping one letter changes the whole result rather than a corner of it.
What it is for on chain: bucketing. Spreading keys over a fixed number of shards, sizing a sketch, picking a deterministic slot. Bucket is that use spelled out.
What it is NOT for: anything an adversary chooses the input to. MurmurHash3 is trivially invertible and collisions can be generated on demand, so a hash table keyed by caller-supplied strings can be driven into its worst case by anyone who wants to. It is not a commitment, not a signature, and not a substitute for a cryptographic digest.
Correctness here rests on Verification, the SMHasher self-test: it hashes 256 keys of growing length under 256 different seeds and hashes the results, which pins every constant, every tail branch and the byte order in one 32-bit number.
A live demo of this package is at r/moul/x/daily/murmur3demo(/r/moul/x/daily/murmur3demo/v0).
MaxBuckets is the largest shard count Bucket can spread over evenly: the hash is 32 bits, so it names 2^32 slots and no more.
Bucket returns a shard index in [0, n) for s, or -1 when n is not positive.
This is the thing the hash is actually good for: a deterministic, evenly spread slot for a key, with no storage and no ordering. Do not use it where the key comes from someone with a reason to collide.
The spread is even only up to MaxBuckets shards, and that ceiling is the hash, not the arithmetic: 32 bits of output cannot name more than 2^32 distinct slots. An n above it is accepted and answered in range, but every index from 2^32 upwards is unreachable, so roughly the top half of a 2^33-way split would never receive a key. Nothing here rejects such an n, because a shard count that large is a caller bug and a panic deep inside a Render is a worse way to learn about it than a skewed table.
The remainder is taken in uint64 rather than uint32 on purpose: gno's int is 64-bit, so a caller may pass an n above 2^32, and narrowing the divisor would turn 1<<32 into a divide by zero on a value the first line of this comment promises to accept.
For an n below the ceiling that does not divide 2^32 the low buckets are very slightly favoured, by at most one key in 2^32. That bias is irrelevant for spreading and would matter for sampling; this is the spreading function.
Hex renders a hash as eight lowercase hex digits, zero-padded, which is how Murmur results are conventionally shown.
Sum32 returns the MurmurHash3 x86_32 hash of s with seed 0.
Sum32Seed returns the MurmurHash3 x86_32 hash of s under the given seed.
The seed re-keys the entire function: the same input under two seeds gives two unrelated results. That is what makes a family of independent hashes out of one function, which is what a Bloom filter or a count-min sketch needs.
SumHex is Sum32 rendered with Hex.
Verification returns the SMHasher verification value for this hash, which is 0xB0F57EE3 for a correct MurmurHash3 x86_32.
The procedure is SMHasher's: hash the keys {0}, {0,1}, {0,1,2}, ... up to 255 bytes, the key of length i under seed 256-i, write each 32-bit result little-endian into one buffer, then hash that buffer with seed 0. It is a self-test rather than a hash of anything useful, and it is the reason this package can claim to be the same function as everyone else's.