-
Notifications
You must be signed in to change notification settings - Fork 2
/
circlehash64.go
108 lines (86 loc) · 2.77 KB
/
circlehash64.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
// Copyright 2021 Faye Amacker
//
// 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
//
// https://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 circlehash
import (
"unsafe"
)
// Hash64 returns a 64-bit digest of data.
func Hash64(b []byte, seed uint64) uint64 {
return uint64(circle64f(*(*ptr)(ptr(&b)), seed, uint64(len(b))))
}
// HashString64 returns a 64-bit digest of string s.
func HashString64(s string, seed uint64) uint64 {
return uint64(circle64f(*(*ptr)(ptr(&s)), seed, uint64(len(s))))
}
func circle64f(p unsafe.Pointer, seed uint64, dlen uint64) uint64 {
startingLength := dlen
currentState := seed ^ pi0
if dlen > 64 {
// Process chunks of 64 bytes.
duplicatedState := currentState
for ; dlen > 64; dlen -= 64 {
a := readUnaligned64(p)
b := readUnaligned64(add(p, 8))
c := readUnaligned64(add(p, 16))
d := readUnaligned64(add(p, 24))
e := readUnaligned64(add(p, 32))
f := readUnaligned64(add(p, 40))
g := readUnaligned64(add(p, 48))
h := readUnaligned64(add(p, 56))
cs0 := mix64(a^pi1, b^currentState)
cs1 := mix64(c^pi2, d^currentState)
currentState = (cs0 ^ cs1)
ds0 := mix64(e^pi3, f^duplicatedState)
ds1 := mix64(g^pi4, h^duplicatedState)
duplicatedState = (ds0 ^ ds1)
p = add(p, 64)
}
currentState = currentState ^ duplicatedState
}
// We have at most 64 bytes to process.
// Process chunks of 16 bytes
for ; dlen > 16; dlen -= 16 {
a := readUnaligned64(p)
b := readUnaligned64(add(p, 8))
currentState = mix64(a^pi1, b^currentState)
p = add(p, 16)
}
// We have at most 16 bytes to process.
a := uint64(0)
b := uint64(0)
switch dlen {
case 9, 10, 11, 12, 13, 14, 15, 16:
// We have 9-16 bytes to process.
// a and b might overlap.
a = readUnaligned64(p)
b = readUnaligned64(add(p, uintptr(dlen-8)))
case 4, 5, 6, 7, 8:
// We have 4-8 bytes to process.
// a and b might overlap.
a = uint64(readUnaligned32(p))
b = uint64(readUnaligned32(add(p, uintptr(dlen-4))))
case 1, 2, 3:
// We have 1-3 bytes to process.
a = uint64(*(*byte)(p)) << 16
a |= uint64(*(*byte)(add(p, uintptr(dlen>>1)))) << 8
a |= uint64(*(*byte)(add(p, uintptr(dlen-1))))
b = 0
case 0:
a = 0
b = 0
}
w := mix64(a^pi1, b^currentState)
z := pi4 ^ startingLength // wyhash variants use salt1 here, except Go 1.17 uses salt4 here (better)
return mix64(w, z)
}