Skip to main content

Module seeded_hash

Module seeded_hash 

Source
Expand description

A seeded hasher for maps whose keys a block can choose.

rustc-hash 2.x is unseeded and hashes a key of 17 to 32 bytes as multiply_mix(SEED2 ^ word(bytes[len - 16..len - 8]), ..) ^ len, where SEED2 is a public constant and multiply_mix(0, _) == 0. Keys carrying SEED2 in that word all hash to len and share one hashbrown bucket, so storage slots, touched addresses or calldata crafted that way turn every lookup into a scan, at flat opcode gas. SeededState hashes with foldhash instead, seeded per process and per map, with one twist: a 32- or 20-byte key (H256, U256, Address) is fed to foldhash as words rather than as a byte string. foldhash’s byte path spends four folded multiplies on 32 bytes where its integer path spends two, which shows on storage-heavy code; the word path resists the same attacks, since every multiply still mixes in a seed the key does not know.

A keccak output is no safer as a key than the preimage behind it. Whoever picks the preimage (a CREATE2 salt, the trailing bytes of deployed code) can grind it until the hash lands in a chosen bucket, at about one keccak per bucket of the table for each key. What keeps such a map on FxHash safe is the gas a block pays for each key it adds or looks up. Code hashes are seeded: a contract deployed once can be called again for 100 gas, and every call looks its code up by hash. Created and self-destructed accounts stay on FxHash, since each entry costs the block a CREATE or a SELFDESTRUCT.

A seeded map iterates in a different order in every process and every map. Whatever carries that order out of the process (a list another program replays, a report, the offender an error names) has to be sorted, as the prover’s host and the guest sort a block’s account updates before applying them to the state trie.

Inside a zkVM guest foldhash finds no clock and no address randomization, so its seeds follow from the guest program and its input. Crafting keys against a guest then takes replaying it up to the map under attack, which is harder than against FxHash but possible.

Structs§

SeededHasher
The hasher built by SeededState.
SeededState
A foldhash RandomState, seeded per process and per map, whose hasher takes fixed-size keys through foldhash’s integer path. See the module docs.

Type Aliases§

SeededHashMap
A std::collections::HashMap hashed with SeededState.
SeededHashSet
A std::collections::HashSet hashed with SeededState.