forked from kevinko/rabin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabin.go
290 lines (241 loc) · 7.01 KB
/
rabin.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright 2012, Kevin Ko <kevin@faveset.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This implements Rabin fingerprinting using a fixed irreducible 64-bit
// polynomial.
package rabin
import (
"fmt"
"hash"
)
type RollingHash interface {
hash.Hash64
// Drains the oldest bytes oldData from the hash and appends newData.
// This is used for rolling windows.
//
// len(oldData) MUST equal len(newData). This may not be checked,
// and errant behavior may result if this does not hold.
Roll(oldData, newData []byte) (int, error)
}
const (
// 64-bit fingerprints
kNumBytes = 8
)
// x^64 + x^62 + x^60 + x^59 + x^56 + x^55 + x^54 + x^51
// + x^50 + x^48 + x^47 + x^43 + x^34 + x^33 + x^32 + x^31
// + x^29 + x^27 + x^26 + x^21 + x^20 + x^19 + x^18 + x^17
// + x^14 + x^4 + x^2 + x^1 + 1
//
// This holds the coefficents of degree < 64.
const (
kIrreduciblePolyCoeffs = 0x59cd8807ac3e4017
kIrreduciblePolyDegree = 64
)
// These are tables for the 32-bit approach.
var kTables *rabinTables32
type digest struct {
// The fingerprint is (f1 f2) = (f1 << 32) | f2
f1 uint32
f2 uint32
// The following are only defined if a rolling window is specified.
windowSize int
rollingTables *rabinRollingTables32
}
func init() {
kTables = makeRabinTables32()
}
func New() hash.Hash64 {
hash := new(digest)
return hash
}
// windowSize in bytes. A table will be pre-computed, so a non-negligible setup
// cost occurs for each rolling hash construction.
func NewRolling(windowSize int) RollingHash {
hash := new(digest)
hash.windowSize = windowSize
hash.rollingTables = makeRabinRollingTables32(windowSize)
return hash
}
func (d *digest) BlockSize() int {
return 4
}
func (d *digest) Reset() {
d.f1 = 0
d.f2 = 0
}
// Rolling is similar to writing new bytes. For each step, we need only
// subtract out a corresponding amount of oldData. (See rabin.tex.)
func (d *digest) Roll(oldData, newData []byte) (int, error) {
if len(oldData) != len(newData) {
panic("len(oldData) != len(newData)")
}
// Number of 32-bit words
numWords := len(newData) >> 2
// f = f1 f2 // f1 is the high word
f1 := d.f1
f2 := d.f2
for ii := 0; ii < numWords; ii++ {
offset := 4 * ii
inWord := (uint32(newData[offset]) << 24) |
(uint32(newData[offset+1]) << 16) |
(uint32(newData[offset+2]) << 8) |
(uint32(newData[offset+3]))
ta := kTables.t88[uint8(f1>>24)]
tb := kTables.t80[uint8(f1>>16)]
tc := kTables.t72[uint8(f1>>8)]
td := kTables.t64[uint8(f1)]
f1 = uint32(ta>>32) ^ uint32(tb>>32) ^
uint32(tc>>32) ^ uint32(td>>32) ^ f2
f2 = uint32(ta) ^ uint32(tb) ^
uint32(tc) ^ uint32(td) ^ inWord
// Subtract the old data. Maintain big-endian order.
t8m0 := d.rollingTables.t8m0[oldData[offset+3]]
t8m8 := d.rollingTables.t8m8[oldData[offset+2]]
t8m16 := d.rollingTables.t8m16[oldData[offset+1]]
t8m24 := d.rollingTables.t8m24[oldData[offset]]
f1 ^= uint32(t8m0>>32) ^ uint32(t8m8>>32) ^
uint32(t8m16>>32) ^ uint32(t8m24>>32)
f2 ^= uint32(t8m0) ^ uint32(t8m8) ^
uint32(t8m16) ^ uint32(t8m24)
}
// Process the remainder.
offset := numWords * 4
f1, f2 = updateSubword(f1, f2, newData[offset:])
// Fix up the remainder.
switch len(oldData) - offset {
case 3:
t8m0 := d.rollingTables.t8m0[oldData[offset+2]]
t8m8 := d.rollingTables.t8m8[oldData[offset+1]]
t8m16 := d.rollingTables.t8m16[oldData[offset]]
f1 ^= uint32(t8m0>>32) ^ uint32(t8m8>>32) ^ uint32(t8m16>>32)
f2 ^= uint32(t8m0) ^ uint32(t8m8) ^ uint32(t8m16)
break
case 2:
t8m0 := d.rollingTables.t8m0[oldData[offset+1]]
t8m8 := d.rollingTables.t8m8[oldData[offset]]
f1 ^= uint32(t8m0>>32) ^ uint32(t8m8>>32)
f2 ^= uint32(t8m0) ^ uint32(t8m8)
break
case 1:
t8m0 := d.rollingTables.t8m0[oldData[offset]]
f1 ^= uint32(t8m0 >> 32)
f2 ^= uint32(t8m0)
break
case 0:
break
}
// Store the updated fingerprints.
d.f1, d.f2 = f1, f2
return len(newData), nil
}
func (d *digest) Size() int {
return kNumBytes
}
func (d *digest) Sum(b []byte) []byte {
// Maintain little-endian order.
b = append(b, byte(d.f1>>24))
b = append(b, byte(d.f1>>16))
b = append(b, byte(d.f1>>8))
b = append(b, byte(d.f1))
b = append(b, byte(d.f2>>24))
b = append(b, byte(d.f2>>16))
b = append(b, byte(d.f2>>8))
b = append(b, byte(d.f2))
return b
}
func (d *digest) Sum64() uint64 {
return (uint64(d.f1) << 32) | uint64(d.f2)
}
// len(p) is a multiple of 4 (32-bit words) = numWords * 32
func update32Generic(f1, f2 uint32, rawTables *[4][256]uint64, p []byte, numWords int) (newF1, newF2 uint32) {
t64 := &rawTables[0]
t72 := &rawTables[1]
t80 := &rawTables[2]
t88 := &rawTables[3]
for ii := 0; ii < numWords; ii++ {
offset := ii << 2
inWord := (uint32(p[offset]) << 24) |
(uint32(p[offset+1]) << 16) |
(uint32(p[offset+2]) << 8) |
(uint32(p[offset+3]))
ta := t88[uint8(f1>>24)]
tb := t80[uint8(f1>>16)]
tc := t72[uint8(f1>>8)]
td := t64[uint8(f1)]
f1 = uint32(ta>>32) ^ uint32(tb>>32) ^
uint32(tc>>32) ^ uint32(td>>32) ^ f2
f2 = uint32(ta) ^ uint32(tb) ^
uint32(tc) ^ uint32(td) ^ inWord
}
newF1 = f1
newF2 = f2
return
}
// len(p) must be < 4. This updates the fingerprint based on p. It is used
// to finish up processing when word-sized updates can no longer be performed.
// Returns (f1, f2)
func updateSubword(f1, f2 uint32, p []byte) (uint32, uint32) {
switch len(p) {
case 3:
j1 := (f1 << 24) | (f2 >> 8)
j2 := f2 << 24
bytes := (uint32(p[0]) << 16) |
(uint32(p[1]) << 8) |
uint32(p[2])
tb := kTables.t80[uint8(f1>>24)]
tc := kTables.t72[uint8(f1>>16)]
td := kTables.t64[uint8(f1>>8)]
f1 = uint32(tb>>32) ^ uint32(tc>>32) ^
uint32(td>>32) ^ j1
f2 = uint32(tb) ^ uint32(tc) ^ uint32(td) ^ j2 ^ bytes
break
case 2:
j1 := (f1 << 16) | (f2 >> 16)
j2 := f2 << 16
bytes := (uint32(p[0]) << 8) | uint32(p[1])
tc := kTables.t72[uint8(f1>>24)]
td := kTables.t64[uint8(f1>>16)]
f1 = uint32(tc>>32) ^ uint32(td>>32) ^ j1
f2 = uint32(tc) ^ uint32(td) ^ j2 ^ bytes
break
case 1:
j1 := (f1 << 8) | (f2 >> 24)
j2 := f2 << 8
td := kTables.t64[uint8(f1>>24)]
f1 = uint32(td>>32) ^ j1
f2 = uint32(td) ^ j2 ^ uint32(p[0])
break
case 0:
break
default:
panic(fmt.Sprint("unexpected remainder ", len(p)))
}
return f1, f2
}
func (d *digest) Write(p []byte) (n int, err error) {
// Number of 32-bit words
numWords := len(p) >> 2
// f = f1 f2 // f1 is the high word
f1 := d.f1
f2 := d.f2
f1, f2 = update32(f1, f2, kTables.raw, p, numWords)
// Process the remainder.
offset := numWords * 4
// Store the result.
d.f1, d.f2 = updateSubword(f1, f2, p[offset:])
return len(p), nil
}
// This always uses native go code for benchmarking purposes.
func (d *digest) writeGeneric(p []byte) (n int, err error) {
// Number of 32-bit words
numWords := len(p) >> 2
// f = f1 f2 // f1 is the high word
f1 := d.f1
f2 := d.f2
f1, f2 = update32Generic(f1, f2, kTables.raw, p, numWords)
// Process the remainder.
offset := numWords * 4
// Store the result.
d.f1, d.f2 = updateSubword(f1, f2, p[offset:])
return len(p), nil
}