-
Notifications
You must be signed in to change notification settings - Fork 1
/
murmur3.go
132 lines (110 loc) · 2.57 KB
/
murmur3.go
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
// Copyright 2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"encoding/binary"
"hash"
"math/bits"
)
const (
murmurC1 = 0xCC9E2D51
murmurC2 = 0x1B873593
)
type MurmurHash3 struct {
rem, len int
k1, h1 uint32
}
func NewMurmurHash3(seed uint32) hash.Hash32 {
return &MurmurHash3{h1: seed}
}
func MurmurHash3Sum(data []byte, seed uint32) uint32 {
h := MurmurHash3{h1: seed}
_, _ = h.Write(data)
return h.Sum32()
}
//nolint:varnamelen // names match reference implementation for clarity
func (h *MurmurHash3) Write(data []byte) (int, error) {
dataLen := len(data)
h.len += dataLen
k1, h1 := h.k1, h.h1
if h.rem != 0 {
i, j := h.rem, 0
for ; i < 4 && j < dataLen; i++ {
k1 |= uint32(data[j]) << (i << 3)
j++
}
data = data[j:]
h.rem = i
h.k1 = k1
if h.rem < 4 {
return dataLen, nil
}
h1 = round(h1, k1)
}
bodyLen := len(data) &^ 3
for i, l := 0, bodyLen; i < l; i += 4 {
k1 := uint32(data[i+3])<<24 |
uint32(data[i+2])<<16 |
uint32(data[i+1])<<8 |
uint32(data[i])
h1 = round(h1, k1)
}
data = data[bodyLen:]
h.rem = len(data)
k1 = 0
for i, l := 0, h.rem; i < l; i++ {
k1 |= uint32(data[i]) << (i << 3)
}
h.k1 = k1
h.h1 = h1
return dataLen, nil
}
//nolint:varnamelen // names match reference implementation for clarity
func (h *MurmurHash3) Sum32() uint32 {
k1 := h.k1
h1 := h.h1
k1 *= murmurC1
k1 = bits.RotateLeft32(k1, 15)
k1 *= murmurC2
h1 ^= k1
h1 ^= uint32(h.len)
h1 ^= h1 >> 16
h1 *= 0x85EBCA6B
h1 ^= h1 >> 13
h1 *= 0xC2B2AE35
h1 ^= h1 >> 16
return h1
}
//nolint:varnamelen // names match reference implementation for clarity
func round(h1, k1 uint32) uint32 {
k1 *= murmurC1
k1 = bits.RotateLeft32(k1, 15)
k1 *= murmurC2
h1 ^= k1
h1 = bits.RotateLeft32(h1, 13)
h1 = h1*4 + h1 + 0xE6546B64
return h1
}
func (h *MurmurHash3) Sum(b []byte) []byte {
return binary.LittleEndian.AppendUint32(b, h.Sum32())
}
func (h *MurmurHash3) Reset() {
*h = MurmurHash3{}
}
func (h *MurmurHash3) Size() int {
return 4
}
func (h *MurmurHash3) BlockSize() int {
return 4
}