Repository navigation
Design hashing
STATUS: accepted. The implementation on a branch has not landed. It lands once it meets rulings 4, 5a, 7 and 8a. Ruling 9, where the table mixes in a second secret, is not started (maxon-lang/maxon#79).
The rulings are on the design issue.
A hash table needs a hash an attacker cannot predict. A cache key, a generated name or a shard assignment needs a digest that is the same everywhere. These are different functions:
| Need | Function | Lives |
|---|---|---|
A type's hash(), hash-table keys |
Hasher, from the process seed, 32-bit result |
inside one process |
| In-process or stored keys under a seed the program owns |
KeyedHasher, from a HashSeed, 64-bit digest |
beside its seed, on one tier |
| Anything that crosses a process, host or file | SHA-256, truncated where short | anywhere |
There is no fixed-function non-cryptographic hash in the standard library.
A process draws 64 bits from the operating system before main. Hasher folds from it, and with it String.hash(), Character.hash() and Array.hash(). A process fold has no observable state. Hasher finalizes to a 32-bit HashValue, and it has no empty state, no resume and no 64-bit digest. No public API returns the process seed, or anything a process fold can be continued from (ruling 8a).
KeyedHasher folds from a HashSeed the program draws with HashSeed.random() or reads with fromBytes. Its states and 64-bit digests are the program's to store beside that seed; the compiler's library cache keeps its seed this way. MAXON_HASH_SEED pins the process seed only, and HashSeed.random() is always fresh.
Map and Set mix a second secret into the slot spread, so integer, float and user-hashed keys are covered too. This is not started; until it lands, collision resistance covers byte-hashed keys only.
| Tier | Where | Function |
|---|---|---|
aes |
a CPU with AES instructions | AES rounds |
software |
every host; the only tier on wasm32-wasi
|
a multiply fold, the same digest on every target |
A digest depends on the seed, the steps and the tier. A third tier needs a supported class of hosts that has its instructions and lacks AES.
Map and Set iterate in table order, which is unspecified and differs between runs. A program that needs an order sorts.
| Variable | Value | A bad value |
|---|---|---|
MAXON_HASH_SEED |
16 hexadecimal digits | ends the program before main with exit 1, naming the value |
MAXON_HASH_TIER |
aes or software
|
the same, and also for a tier the host lacks |
A run is reproduced by its seed and its tier together.
Both variables are read by the hash runtime, which a program contains only when it hashes. Such a program validates both before main; a program that hashes nothing does not read them and runs whatever they hold (ruling 5a).
A pinned seed is not a secret: it stays in the environment, the program can read it, and child processes inherit it. A process running under a pin has no collision resistance against anyone who knows the pin, so a deployed program is not pinned. How a pin maps to the process fold is unspecified beyond "same seed, tier and program, same hashes" (ruling 8b).
A suite run is never pinned. spec-test and maxon test draw one seed per run, pass it to every program that does not pin its own, and print the seed and the tier with every failure.
A pin belongs to:
- a spec case:
<!-- env: MAXON_HASH_SEED=... -->; - a test under
tests/, for a process the test starts; - a measurement (
scale-test, a benchmark); - reproducing a reported failure.
A case whose expected output depends on a hash value or an iteration order also sets MAXON_HASH_TIER=software, the one tier every lane has. A case whose subject is not hashing sorts instead of pinning.
Compiler output is byte-identical under every seed, both the executable and the debug file. Emitters walk registries in name order, never table order, and a tests/cli gate builds one program under two seeds and compares the output. Names that reach output (generic instance names, tuple and type-parameter tokens, the build-id) and --shard are truncated SHA-256.
Insertion-ordered Map and Set; opt-in seeding; FNV-1a or djb2 under any spelling; a hash value in a file, a name or a protocol; a suite-wide seed pin.