Consider use of non cryptographic hash for function hashing
- Dominant language
- Rust
- Stars
- 61.4k
- Forks
- 5.4k
- Avg merge
- 3h 33m
- Merged PRs (30d)
- 4
Description
* FxHasher is currently used for function [hashing](https://github.com/FuelLabs/sway/blob/master/sway-ir/src/optimize/fn_dedup.rs#L55), which is not a cryptographically secure hash according to the [document](https://docs.rs/fxhash/latest/fxhash/). Diving deeper into the hasher [implementation](https://docs.rs/fxhash/latest/src/fxhash/lib.rs.html#64) reveals that there are no collision / pre-image protections. The potential impacts of this are
* If two function hashes collide, it would be catastrophically for the contract being compiled
* A more obscure effect is if malicious actors may intentionally craft contracts that have different functions deduped, which could potentially lead to confusion / losses for contract users
* Function hashing iterates over fields that are of dynamic length, which means it might be susceptible to malleability issues. To be more precise, the result of a.hash(hasher); b.hash(hasher) could be identical to c.hash(hasher); d.hash(hasher); e.hash(hasher) if a || b == c || d || e. The current usage of [discriminator](https://github.com/FuelLabs/sway/blob/master/sway-ir/src/optimize/fn_dedup.rs#L80) within hash function doesn't really mitigate the problem since misaligned structures would always provide chances for discriminators to match with some different fields / structures. Malleability has been one of the major concerns for hashing in blockchain (e.g. merkle tree leaves / nodes), and we believe it would be better to redesign the hashing scheme to alleviate the risk.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in sway-ir/src/optimize/fn_dedup.rs around the function hashing at lines 55 and 80, then review the linked FxHasher implementation and documentation. Determine how the current dynamic-length inputs and discriminator affect collision and malleability risks; done means function deduplication uses a hashing scheme that addresses those risks without incorrectly merging distinct functions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers, security
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100