loom_evm_db/
fast_hasher.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use std::hash::{BuildHasher, Hash, Hasher};

use alloy::primitives::{Address, U256};

#[derive(Clone, Eq, PartialEq)]
pub struct HashedAddress(Address);

#[derive(Clone, Eq, PartialEq)]
pub struct HashedAddressCell(pub Address, pub U256);

impl From<Address> for HashedAddress {
    fn from(value: Address) -> Self {
        HashedAddress(value)
    }
}

impl Hash for HashedAddress {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        let mut array = [0u8; 8];
        array.copy_from_slice(&self.0[0..8]);
        let mut value = u64::from_ne_bytes(array);
        array.copy_from_slice(&self.0[8..16]);
        value ^= u64::from_ne_bytes(array);
        let mut array = [0u8; 4];
        array.copy_from_slice(&self.0[16..20]);
        value += u32::from_ne_bytes(array) as u64;

        state.write_u64(value);
    }
}

impl Hash for HashedAddressCell {
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        let mut value = 0x12345678;
        let mut array = [0u8; 8];
        array.copy_from_slice(&self.0[0..8]);
        value += u64::from_ne_bytes(array);
        array.copy_from_slice(&self.0[8..16]);
        value ^= u64::from_ne_bytes(array);

        let u_slice = self.1.as_le_slice();

        array.copy_from_slice(&u_slice[0..8]);
        value += u64::from_ne_bytes(array);
        array.copy_from_slice(&u_slice[8..16]);
        value ^= u64::from_ne_bytes(array);
        array.copy_from_slice(&u_slice[16..24]);
        value += u64::from_ne_bytes(array);
        array.copy_from_slice(&u_slice[24..32]);
        value ^= u64::from_ne_bytes(array);

        let mut array = [0u8; 4];
        array.copy_from_slice(&self.0[16..20]);
        value += u32::from_ne_bytes(array) as u64;

        state.write_u64(value);
    }
}

#[derive(Default)]
pub struct SimpleHasher {
    state: u64,
}

impl SimpleHasher {
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }
}

impl Hasher for SimpleHasher {
    #[inline]
    fn finish(&self) -> u64 {
        self.state
    }

    #[inline]
    fn write(&mut self, bytes_buf: &[u8]) {
        if self.state == 0 {
            self.state = 1;
        } else {
            let l = bytes_buf.len() / 8;
            let r = bytes_buf.len() % 8;

            let mut state: u64 = 0x12345678;
            let mut array = [0u8; 8];
            for i in 0..l {
                array.copy_from_slice(&bytes_buf[i * 8..i * 8 + 8]);
                let value = u64::from_ne_bytes(array);
                if i % 2 == 0 {
                    (state, _) = state.overflowing_add(value);
                } else {
                    state ^= value;
                }
            }
            if r != 0 {
                let mut array = [0u8; 8];
                for i in 0..r {
                    array[i] = bytes_buf[l * 8 + i]
                }
                let value = u64::from_ne_bytes(array);
                state += value;
            }

            // let mut array = [0u8; 8];
            // let mut state = 0x12345678;
            // array.copy_from_slice(&bytes_buf[0..8]);
            // let value = u64::from_ne_bytes(array);
            // state += value;
            // array.copy_from_slice(&bytes_buf[8..16]);
            // let value = u64::from_ne_bytes(array);
            // state ^= value;
            // let mut array = [0u8; 4];
            // array.copy_from_slice(&bytes_buf[16..20]);
            // let value = u32::from_ne_bytes(array);
            // state += value as u64;

            self.state = state
        }
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        self.state = i;
    }
}

#[derive(Default, Clone)]
pub struct SimpleBuildHasher {}

impl BuildHasher for SimpleBuildHasher {
    type Hasher = SimpleHasher;

    fn build_hasher(&self) -> Self::Hasher {
        SimpleHasher::new()
    }
}

#[cfg(test)]
mod test {
    use std::hash::Hash;

    use alloy::primitives::Address;

    use super::*;

    #[test]
    fn test_hasher() {
        let addr = Address::random();

        let mut hasher = SimpleHasher::new();
        addr.hash(&mut hasher);
    }

    #[test]
    fn test_hashed_address() {
        let addr = HashedAddress::from(Address::random());

        let mut hasher = SimpleBuildHasher {}.build_hasher();

        addr.hash(&mut hasher);
    }
}