-
Notifications
You must be signed in to change notification settings - Fork 2
/
t1ha.go
181 lines (160 loc) · 3.1 KB
/
t1ha.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Package t1ha implements the t1ha hash function
/*
https://github.com/leo-yuriev/t1ha
*/
package t1ha
import "encoding/binary"
const (
// magic primes
p0 = 17048867929148541611
p1 = 9386433910765580089
p2 = 15343884574428479051
p3 = 13662985319504319857
p4 = 11242949449147999147
p5 = 13862205317416547141
p6 = 14653293970879851569
/* rotations */
s0 = 41
s1 = 17
s2 = 31
)
func rot64(v uint64, s uint) uint64 {
return (v >> s) | (v << (64 - s))
}
func fetch64(v []byte, idx int) uint64 {
return binary.LittleEndian.Uint64(v[idx*8:])
}
func fetch_tail(v []byte) uint64 {
switch len(v) {
case 1:
return uint64(v[0])
case 2:
return uint64(binary.LittleEndian.Uint16(v))
case 3:
return uint64(binary.LittleEndian.Uint16(v)) | uint64(v[2])<<16
case 4:
return uint64(binary.LittleEndian.Uint32(v))
case 5:
return uint64(binary.LittleEndian.Uint32(v)) | uint64(v[4])<<32
case 6:
return uint64(binary.LittleEndian.Uint32(v)) | uint64(binary.LittleEndian.Uint16(v[4:]))<<32
case 7:
return uint64(binary.LittleEndian.Uint32(v)) | uint64(binary.LittleEndian.Uint16(v[4:]))<<32 | uint64(v[6])<<48
case 8:
return binary.LittleEndian.Uint64(v)
}
panic("not reached")
}
func mux64(a, b uint64) uint64 {
lo, hi := mullu(a, b)
return lo ^ hi
}
// xor-mul-xor mixer
func mix(v, p uint64) uint64 {
v *= p
return v ^ rot64(v, s0)
}
func Sum64(data []byte, seed uint64) uint64 {
var a uint64 = seed
var b uint64 = uint64(len(data))
if len(data) > 32 {
c := rot64(uint64(len(data)), s1) + seed
d := uint64(len(data)) ^ rot64(seed, s1)
for len(data) >= 32 {
v := data
w0 := fetch64(v, 0)
w1 := fetch64(v, 1)
w2 := fetch64(v, 2)
w3 := fetch64(v, 3)
d02 := w0 ^ rot64(w2+d, s1)
c13 := w1 ^ rot64(w3+c, s1)
c += a ^ rot64(w0, s0)
d -= b ^ rot64(w1, s2)
a ^= p1 * (d02 + w3)
b ^= p0 * (c13 + w2)
data = data[8*4:]
}
a ^= p6 * (rot64(c, s1) + d)
b ^= p5 * (c + rot64(d, s1))
}
v := data
switch len(v) {
default:
b += mux64(fetch64(v, 0), p4)
v = v[8:]
fallthrough
case 24:
fallthrough
case 23:
fallthrough
case 22:
fallthrough
case 21:
fallthrough
case 20:
fallthrough
case 19:
fallthrough
case 18:
fallthrough
case 17:
a += mux64(fetch64(v, 0), p3)
v = v[8:]
fallthrough
case 16:
fallthrough
case 15:
fallthrough
case 14:
fallthrough
case 13:
fallthrough
case 12:
fallthrough
case 11:
fallthrough
case 10:
fallthrough
case 9:
b += mux64(fetch64(v, 0), p2)
v = v[8:]
fallthrough
case 8:
fallthrough
case 7:
fallthrough
case 6:
fallthrough
case 5:
fallthrough
case 4:
fallthrough
case 3:
fallthrough
case 2:
fallthrough
case 1:
a += mux64(fetch_tail(v), p1)
case 0:
}
return mux64(rot64(a+b, s1), p4) + mix(a^b, p0)
}
// From https://golang.org/src/runtime/softfloat64.go
// 64x64 -> 128 multiply.
// adapted from hacker's delight.
func mullu(u, v uint64) (lo, hi uint64) {
const (
s = 32
mask = 1<<s - 1
)
u0 := u & mask
u1 := u >> s
v0 := v & mask
v1 := v >> s
w0 := u0 * v0
t := u1*v0 + w0>>s
w1 := t & mask
w2 := t >> s
w1 += u0 * v1
return u * v, u1*v1 + w2 + w1>>s
}