Skip to main content

hypercall_state_commitment/
hasher.rs

1/// Keccak256 hasher for JMT.
2///
3/// `sha3::Keccak256` implements `digest::Digest`, and JMT provides a blanket
4/// `impl<T: Digest> SimpleHasher for T`. So this type alias is all we need --
5/// pass it as the generic `H` parameter to `JellyfishMerkleTree<R, H>`.
6pub type Keccak256Hasher = sha3::Keccak256;
7
8#[cfg(test)]
9mod tests {
10    use jmt::SimpleHasher;
11
12    use super::*;
13
14    #[test]
15    fn keccak256_produces_32_bytes() {
16        let hash = Keccak256Hasher::hash(b"hello");
17        assert_eq!(hash.len(), 32);
18    }
19
20    #[test]
21    fn keccak256_matches_known_vector() {
22        // keccak256("") = c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470
23        let hash = Keccak256Hasher::hash(b"");
24        assert_eq!(
25            hex::encode(hash),
26            "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
27        );
28    }
29
30    #[test]
31    fn keccak256_deterministic() {
32        let a = Keccak256Hasher::hash(b"test data");
33        let b = Keccak256Hasher::hash(b"test data");
34        assert_eq!(a, b);
35    }
36
37    #[test]
38    fn keccak256_collision_resistant() {
39        let a = Keccak256Hasher::hash(b"input A");
40        let b = Keccak256Hasher::hash(b"input B");
41        assert_ne!(a, b);
42    }
43}