This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
siphash.go
258 lines (213 loc) · 4.99 KB
/
siphash.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package siphash
import (
"encoding/binary"
)
type SipHash struct {
k0, k1 uint64 // two parts of key
V [4]uint64 // v is the current internal state.
}
/*
siphashKey[:] = [196 107 38 219 80 75 209 213 243 49 219 252 101 35 20 105]
siphashKey[8:] = [243 49 219 252 101 35 20 105]
k0 = 15407178610857372612
k1 = 7571715794457539059
before s.V = [0 0 0 0]
after s.V = [12015082867820662449 971459224712208030 13377991302290020773 2121554997101417600]
*/
func Newsip(siphashKey []byte) *SipHash {
s := &SipHash{
k0: binary.LittleEndian.Uint64(siphashKey[:]), // 17624113405423192833
k1: binary.LittleEndian.Uint64(siphashKey[8:]), // 16633022555945065022
}
//fmt.Printf("before s.V = %v\n",s.V)
s.V[0] = s.k0 ^ 0x736f6d6570736575 //9798144632717078132, 0x87fa00a173006e74
s.V[1] = s.k1 ^ 0x646f72616e646f6d //9420175285032990035, 0x82bb2f82f24e3553
s.V[2] = s.k0 ^ 0x6c7967656e657261 //11019194076704962912,0x98ec0aa16d167960
s.V[3] = s.k1 ^ 0x7465646279746573 //10570293030476988237,0x92b13981e55e3f4d
return s
}
func Siphash(k0, k1, b uint64) uint64 {
// Initialization.
var v [4]uint64
v[0] = k0 ^ 0x736f6d6570736575
v[1] = k1 ^ 0x646f72616e646f6d
v[2] = k0 ^ 0x6c7967656e657261
v[3] = k1 ^ 0x7465646279746573
return SiphashPRF(&v, b)
}
func SiphashPRF(v *[4]uint64, b uint64) uint64 {
v0 := v[0]
v1 := v[1]
v2 := v[2]
v3 := v[3]
// Initialization.
// Compression.
v3 ^= b
// Round 1.
v0 += v1
v1 = v1<<13 | v1>>(64-13)
v1 ^= v0
v0 = v0<<32 | v0>>(64-32)
v2 += v3
v3 = v3<<16 | v3>>(64-16)
v3 ^= v2
v0 += v3
v3 = v3<<21 | v3>>(64-21)
v3 ^= v0
v2 += v1
v1 = v1<<17 | v1>>(64-17)
v1 ^= v2
v2 = v2<<32 | v2>>(64-32)
// Round 2.
v0 += v1
v1 = v1<<13 | v1>>(64-13)
v1 ^= v0
v0 = v0<<32 | v0>>(64-32)
v2 += v3
v3 = v3<<16 | v3>>(64-16)
v3 ^= v2
v0 += v3
v3 = v3<<21 | v3>>(64-21)
v3 ^= v0
v2 += v1
v1 = v1<<17 | v1>>(64-17)
v1 ^= v2
v2 = v2<<32 | v2>>(64-32)
v0 ^= b
// Finalization.
v2 ^= 0xff
// Round 1.
v0 += v1
v1 = v1<<13 | v1>>(64-13)
v1 ^= v0
v0 = v0<<32 | v0>>(64-32)
v2 += v3
v3 = v3<<16 | v3>>(64-16)
v3 ^= v2
v0 += v3
v3 = v3<<21 | v3>>(64-21)
v3 ^= v0
v2 += v1
v1 = v1<<17 | v1>>(64-17)
v1 ^= v2
v2 = v2<<32 | v2>>(64-32)
// Round 2.
v0 += v1
v1 = v1<<13 | v1>>(64-13)
v1 ^= v0
v0 = v0<<32 | v0>>(64-32)
v2 += v3
v3 = v3<<16 | v3>>(64-16)
v3 ^= v2
v0 += v3
v3 = v3<<21 | v3>>(64-21)
v3 ^= v0
v2 += v1
v1 = v1<<17 | v1>>(64-17)
v1 ^= v2
v2 = v2<<32 | v2>>(64-32)
// Round 3.
v0 += v1
v1 = v1<<13 | v1>>(64-13)
v1 ^= v0
v0 = v0<<32 | v0>>(64-32)
v2 += v3
v3 = v3<<16 | v3>>(64-16)
v3 ^= v2
v0 += v3
v3 = v3<<21 | v3>>(64-21)
v3 ^= v0
v2 += v1
v1 = v1<<17 | v1>>(64-17)
v1 ^= v2
v2 = v2<<32 | v2>>(64-32)
// Round 4.
v0 += v1
v1 = v1<<13 | v1>>(64-13)
v1 ^= v0
v0 = v0<<32 | v0>>(64-32)
v2 += v3
v3 = v3<<16 | v3>>(64-16)
v3 ^= v2
v0 += v3
v3 = v3<<21 | v3>>(64-21)
v3 ^= v0
v2 += v1
v1 = v1<<17 | v1>>(64-17)
v1 ^= v2
v2 = v2<<32 | v2>>(64-32)
return v0 ^ v1 ^ v2 ^ v3
}
const sipHashBlockBits uint64 = 6
const sipHashBlockSize uint64 = 1 << sipHashBlockBits
const sipHashBlockMask uint64 = sipHashBlockSize - 1
// SipHashBlock builds a block of siphash values by repeatedly hashing from the
// nonce truncated to its closest block start, up to the end of the block.
// Returns the resulting hash at the nonce's position.
func SipHashBlock(v [4]uint64, nonce uint64, rotE uint8, xorAll bool) uint64 {
// beginning of the block of hashes
nonce0 := nonce & ^sipHashBlockMask
nonceI := nonce & sipHashBlockMask
nonceHash := make([]uint64, sipHashBlockSize)
// repeated hashing over the whole block
s := new(sipHash24)
siphash := s.new(v)
var i uint64
for i = 0; i < sipHashBlockSize; i++ {
siphash.hash(nonce0+i, rotE)
nonceHash[i] = siphash.digest()
}
// xor the hash at nonce_i < SIPHASH_BLOCK_MASK with some or all later hashes to force hashing the whole block
var xor uint64 = nonceHash[nonceI]
var xorFrom uint64
if xorAll || nonceI == sipHashBlockMask {
xorFrom = nonceI + 1
} else {
xorFrom = sipHashBlockMask
}
for i := xorFrom; i < sipHashBlockSize; i++ {
xor ^= nonceHash[i]
}
return xor
}
type sipHash24 struct {
v0, v1, v2, v3 uint64
}
func (s *sipHash24) new(v [4]uint64) sipHash24 {
return sipHash24{v[0], v[1], v[2], v[3]}
}
// One siphash24 hashing, consisting of 2 and then 4 rounds
func (s *sipHash24) hash(nonce uint64, rotE uint8) {
s.v3 ^= nonce
s.round(rotE)
s.round(rotE)
s.v0 ^= nonce
s.v2 ^= 0xff
for i := 0; i < 4; i++ {
s.round(rotE)
}
}
// Resulting hash digest
func (s *sipHash24) digest() uint64 {
return (s.v0 ^ s.v1) ^ (s.v2 ^ s.v3)
}
func (s *sipHash24) round(rotE uint8) {
s.v0 = s.v0 + s.v1
s.v2 = s.v2 + s.v3
s.v1 = rotl(s.v1, 13)
s.v3 = rotl(s.v3, 16)
s.v1 ^= s.v0
s.v3 ^= s.v2
s.v0 = rotl(s.v0, 32)
s.v2 = s.v2 + s.v1
s.v0 = s.v0 + s.v3
s.v1 = rotl(s.v1, 17)
s.v3 = rotl(s.v3, rotE)
s.v1 ^= s.v2
s.v3 ^= s.v0
s.v2 = rotl(s.v2, 32)
}
func rotl(val uint64, shift uint8) uint64 {
num := (val << shift) | (val >> (64 - shift))
return num
}