Debian Codegraph @

hasher.go

raw
1 package xxh3
2
3 import (
4 "encoding/binary"
5 "hash"
6 )
7
8 // Hasher implements the hash.Hash interface
9 type Hasher struct {
10 acc [8]u64
11 blk u64
12 len u64
13 key ptr
14 buf [_block + _stripe]byte
15 seed u64
16 }
17
18 var (
19 _ hash.Hash = (*Hasher)(nil)
20 _ hash.Hash64 = (*Hasher)(nil)
21 )
22
23 // New returns a new Hasher that implements the hash.Hash interface.
24 func New() *Hasher {
25 return new(Hasher)
26 }
27
28 // NewSeed returns a new Hasher that implements the hash.Hash interface.
29 func NewSeed(seed uint64) *Hasher {
30 var h Hasher
31 h.Reset()
32 h.seed = seed
33 h.key = key
34
35 // Only initiate once, not on reset.
36 if seed != 0 {
37 h.key = ptr(&[secretSize]byte{})
38 initSecret(h.key, seed)
39 }
40 return &h
41 }
42
43 // Reset resets the Hash to its initial state.
44 func (h *Hasher) Reset() {
45 h.acc = [8]u64{
46 prime32_3, prime64_1, prime64_2, prime64_3,
47 prime64_4, prime32_2, prime64_5, prime32_1,
48 }
49 h.blk = 0
50 h.len = 0
51 }
52
53 // ResetSeed will reset the hash and set a new seed.
54 // This will change the original state used by Reset.
55 func (h *Hasher) ResetSeed(seed uint64) {
56 h.Reset()
57 // Set key if not set before.
58 if h.seed == 0 && seed != 0 {
59 h.key = ptr(&[secretSize]byte{})
60 }
61 // Re-init seed.
62 if seed == 0 {
63 h.key = nil
64 } else if seed != h.seed {
65 initSecret(h.key, seed)
66 }
67 h.seed = seed
68 }
69
70 // BlockSize returns the hash's underlying block size.
71 // The Write method will accept any amount of data, but
72 // it may operate more efficiently if all writes are a
73 // multiple of the block size.
74 func (h *Hasher) BlockSize() int { return _stripe }
75
76 // Size returns the number of bytes Sum will return.
77 func (h *Hasher) Size() int { return 8 }
78
79 // Sum appends the current hash to b and returns the resulting slice.
80 // It does not change the underlying hash state.
81 func (h *Hasher) Sum(b []byte) []byte {
82 var tmp [8]byte
83 binary.BigEndian.PutUint64(tmp[:], h.Sum64())
84 return append(b, tmp[:]...)
85 }
86
87 // Write adds more data to the running hash.
88 // It never returns an error.
89 func (h *Hasher) Write(buf []byte) (int, error) {
90 h.update(buf)
91 return len(buf), nil
92 }
93
94 // WriteString adds more data to the running hash.
95 // It never returns an error.
96 func (h *Hasher) WriteString(buf string) (int, error) {
97 h.updateString(buf)
98 return len(buf), nil
99 }
100
101 func (h *Hasher) update(buf []byte) {
102 // relies on the data pointer being the first word in the string header
103 h.updateString(*(*string)(ptr(&buf)))
104 }
105
106 func (h *Hasher) updateString(buf string) {
107 if h.key == nil {
108 h.key = key
109 h.Reset()
110 }
111
112 // On first write, if more than 1 block, process without copy.
113 for h.len == 0 && len(buf) > len(h.buf) {
114 if hasAVX512 {
115 accumBlockAVX512(&h.acc, *(*ptr)(ptr(&buf)), h.key)
116 } else if hasAVX2 {
117 accumBlockAVX2(&h.acc, *(*ptr)(ptr(&buf)), h.key)
118 } else if hasSSE2 {
119 accumBlockSSE(&h.acc, *(*ptr)(ptr(&buf)), h.key)
120 } else if hasNEON {
121 accumBlockNEON(&h.acc, *(*ptr)(ptr(&buf)), h.key)
122 } else {
123 accumBlockScalar(&h.acc, *(*ptr)(ptr(&buf)), h.key)
124 }
125 buf = buf[_block:]
126 h.blk++
127 }
128
129 for len(buf) > 0 {
130 if h.len < u64(len(h.buf)) {
131 n := copy(h.buf[h.len:], buf)
132 h.len += u64(n)
133 buf = buf[n:]
134 continue
135 }
136
137 if hasAVX512 {
138 accumBlockAVX512(&h.acc, ptr(&h.buf), h.key)
139 } else if hasAVX2 {
140 accumBlockAVX2(&h.acc, ptr(&h.buf), h.key)
141 } else if hasSSE2 {
142 accumBlockSSE(&h.acc, ptr(&h.buf), h.key)
143 } else if hasNEON {
144 accumBlockNEON(&h.acc, ptr(&h.buf), h.key)
145 } else {
146 accumBlockScalar(&h.acc, ptr(&h.buf), h.key)
147 }
148
149 h.blk++
150 h.len = _stripe
151 copy(h.buf[:_stripe], h.buf[_block:])
152 }
153 }
154
155 // Sum64 returns the 64-bit hash of the written data.
156 func (h *Hasher) Sum64() uint64 {
157 if h.key == nil {
158 h.key = key
159 h.Reset()
160 }
161
162 if h.blk == 0 {
163 if h.seed == 0 {
164 return Hash(h.buf[:h.len])
165 }
166 return HashSeed(h.buf[:h.len], h.seed)
167 }
168
169 l := h.blk*_block + h.len
170 acc := l * prime64_1
171 accs := h.acc
172
173 if h.len > 0 {
174 // We are only ever doing 1 block here, so no avx512.
175 if hasAVX2 {
176 accumAVX2(&accs, ptr(&h.buf[0]), h.key, h.len)
177 } else if hasSSE2 {
178 accumSSE(&accs, ptr(&h.buf[0]), h.key, h.len)
179 } else if hasNEON {
180 accumNEON(&accs, ptr(&h.buf[0]), h.key, h.len)
181 } else {
182 accumScalar(&accs, ptr(&h.buf[0]), h.key, h.len)
183 }
184 }
185
186 if h.seed == 0 {
187 acc += mulFold64(accs[0]^key64_011, accs[1]^key64_019)
188 acc += mulFold64(accs[2]^key64_027, accs[3]^key64_035)
189 acc += mulFold64(accs[4]^key64_043, accs[5]^key64_051)
190 acc += mulFold64(accs[6]^key64_059, accs[7]^key64_067)
191 } else {
192 secret := h.key
193 acc += mulFold64(accs[0]^readU64(secret, 11), accs[1]^readU64(secret, 19))
194 acc += mulFold64(accs[2]^readU64(secret, 27), accs[3]^readU64(secret, 35))
195 acc += mulFold64(accs[4]^readU64(secret, 43), accs[5]^readU64(secret, 51))
196 acc += mulFold64(accs[6]^readU64(secret, 59), accs[7]^readU64(secret, 67))
197 }
198
199 acc = xxh3Avalanche(acc)
200
201 return acc
202 }
203
204 // Sum128 returns the 128-bit hash of the written data.
205 func (h *Hasher) Sum128() Uint128 {
206 if h.key == nil {
207 h.key = key
208 h.Reset()
209 }
210
211 if h.blk == 0 {
212 if h.seed == 0 {
213 return Hash128(h.buf[:h.len])
214 }
215 return Hash128Seed(h.buf[:h.len], h.seed)
216 }
217
218 l := h.blk*_block + h.len
219 acc := Uint128{Lo: l * prime64_1, Hi: ^(l * prime64_2)}
220 accs := h.acc
221
222 if h.len > 0 {
223 // We are only ever doing 1 block here, so no avx512.
224 if hasAVX2 {
225 accumAVX2(&accs, ptr(&h.buf[0]), h.key, h.len)
226 } else if hasSSE2 {
227 accumSSE(&accs, ptr(&h.buf[0]), h.key, h.len)
228 } else if hasNEON {
229 accumNEON(&accs, ptr(&h.buf[0]), h.key, h.len)
230 } else {
231 accumScalar(&accs, ptr(&h.buf[0]), h.key, h.len)
232 }
233 }
234
235 if h.seed == 0 {
236 acc.Lo += mulFold64(accs[0]^key64_011, accs[1]^key64_019)
237 acc.Hi += mulFold64(accs[0]^key64_117, accs[1]^key64_125)
238
239 acc.Lo += mulFold64(accs[2]^key64_027, accs[3]^key64_035)
240 acc.Hi += mulFold64(accs[2]^key64_133, accs[3]^key64_141)
241
242 acc.Lo += mulFold64(accs[4]^key64_043, accs[5]^key64_051)
243 acc.Hi += mulFold64(accs[4]^key64_149, accs[5]^key64_157)
244
245 acc.Lo += mulFold64(accs[6]^key64_059, accs[7]^key64_067)
246 acc.Hi += mulFold64(accs[6]^key64_165, accs[7]^key64_173)
247 } else {
248 secret := h.key
249 const hi_off = 117 - 11
250
251 acc.Lo += mulFold64(accs[0]^readU64(secret, 11), accs[1]^readU64(secret, 19))
252 acc.Hi += mulFold64(accs[0]^readU64(secret, 11+hi_off), accs[1]^readU64(secret, 19+hi_off))
253
254 acc.Lo += mulFold64(accs[2]^readU64(secret, 27), accs[3]^readU64(secret, 35))
255 acc.Hi += mulFold64(accs[2]^readU64(secret, 27+hi_off), accs[3]^readU64(secret, 35+hi_off))
256
257 acc.Lo += mulFold64(accs[4]^readU64(secret, 43), accs[5]^readU64(secret, 51))
258 acc.Hi += mulFold64(accs[4]^readU64(secret, 43+hi_off), accs[5]^readU64(secret, 51+hi_off))
259
260 acc.Lo += mulFold64(accs[6]^readU64(secret, 59), accs[7]^readU64(secret, 67))
261 acc.Hi += mulFold64(accs[6]^readU64(secret, 59+hi_off), accs[7]^readU64(secret, 67+hi_off))
262 }
263
264 acc.Lo = xxh3Avalanche(acc.Lo)
265 acc.Hi = xxh3Avalanche(acc.Hi)
266
267 return acc
268 }