forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hasher.go
executable file
·439 lines (381 loc) · 9.07 KB
/
hasher.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package ssz
import (
"encoding/binary"
"errors"
"fmt"
"hash"
"math/bits"
"sync"
"github.com/minio/sha256-simd"
"github.com/prysmaticlabs/gohashtree"
)
var (
// ErrIncorrectByteSize means that the byte size is incorrect
ErrIncorrectByteSize = fmt.Errorf("incorrect byte size")
// ErrIncorrectListSize means that the size of the list is incorrect
ErrIncorrectListSize = fmt.Errorf("incorrect list size")
ErrRootSizeInvalid = errors.New("root must be 32 bytes")
)
var zeroHashes [65][32]byte
var zeroHashLevels map[string]int
var trueBytes, falseBytes []byte
const (
mask0 = ^uint64((1 << (1 << iota)) - 1)
mask1
mask2
mask3
mask4
mask5
)
const (
bit0 = uint8(1 << iota)
bit1
bit2
bit3
bit4
bit5
)
func init() {
falseBytes = make([]byte, 32)
trueBytes = make([]byte, 32)
trueBytes[0] = 1
zeroHashLevels = make(map[string]int)
zeroHashLevels[string(falseBytes)] = 0
tmp := [64]byte{}
for i := 0; i < 64; i++ {
copy(tmp[:32], zeroHashes[i][:])
copy(tmp[32:], zeroHashes[i][:])
zeroHashes[i+1] = sha256.Sum256(tmp[:])
zeroHashLevels[string(zeroHashes[i+1][:])] = i + 1
}
}
// HashWithDefaultHasher hashes a HashRoot object with a Hasher from
// the default HasherPool
func HashWithDefaultHasher(v HashRoot) ([32]byte, error) {
hh := DefaultHasherPool.Get()
if err := v.HashTreeRootWith(hh); err != nil {
DefaultHasherPool.Put(hh)
return [32]byte{}, err
}
root, err := hh.HashRoot()
DefaultHasherPool.Put(hh)
return root, err
}
var zeroBytes = make([]byte, 32)
// DefaultHasherPool is a default hasher pool
var DefaultHasherPool HasherPool
// Hasher is a utility tool to hash SSZ structs
type Hasher struct {
// buffer array to store hashing values
buf []byte
// tmp array used for uint64 and bitlist processing
tmp []byte
// tmp array used during the merkleize process
merkleizeTmp []byte
// sha256 hash function
hash hash.Hash
}
// NewHasher creates a new Hasher object
func NewHasher() *Hasher {
return &Hasher{
hash: sha256.New(),
tmp: make([]byte, 32),
}
}
// NewHasher creates a new Hasher object with a custom hash function
func NewHasherWithHash(hh hash.Hash) *Hasher {
return &Hasher{
hash: hh,
tmp: make([]byte, 32),
}
}
// Reset resets the Hasher obj
func (h *Hasher) Reset() {
h.buf = h.buf[:0]
h.hash.Reset()
}
func (h *Hasher) AppendBytes32(b []byte) {
h.buf = append(h.buf, b...)
if rest := len(b) % 32; rest != 0 {
// pad zero bytes to the left
h.buf = append(h.buf, zeroBytes[:32-rest]...)
}
}
// PutUint64 appends a uint64 in 32 bytes
func (h *Hasher) PutUint64(i uint64) {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, i)
h.AppendBytes32(buf)
}
// PutUint32 appends a uint32 in 32 bytes
func (h *Hasher) PutUint32(i uint32) {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, i)
h.AppendBytes32(buf)
}
// PutUint16 appends a uint16 in 32 bytes
func (h *Hasher) PutUint16(i uint16) {
buf := make([]byte, 2)
binary.LittleEndian.PutUint16(buf, i)
h.AppendBytes32(buf)
}
// PutUint16 appends a uint16 in 32 bytes
func (h *Hasher) PutUint8(i uint8) {
h.AppendBytes32([]byte{byte(i)})
}
func CalculateLimit(maxCapacity, numItems, size uint64) uint64 {
limit := (maxCapacity*size + 31) / 32
if limit != 0 {
return limit
}
if numItems == 0 {
return 1
}
return numItems
}
func (h *Hasher) FillUpTo32() {
// pad zero bytes to the left
if rest := len(h.buf) % 32; rest != 0 {
h.buf = append(h.buf, zeroBytes[:32-rest]...)
}
}
func (h *Hasher) AppendUint8(i uint8) {
h.buf = MarshalUint8(h.buf, i)
}
func (h *Hasher) AppendUint64(i uint64) {
h.buf = MarshalUint64(h.buf, i)
}
func (h *Hasher) Append(i []byte) {
h.buf = append(h.buf, i...)
}
// PutRootVector appends an array of roots
func (h *Hasher) PutRootVector(b [][]byte, maxCapacity ...uint64) error {
indx := h.Index()
for _, i := range b {
if len(i) != 32 {
return fmt.Errorf("bad root")
}
h.buf = append(h.buf, i...)
}
if len(maxCapacity) == 0 {
h.Merkleize(indx)
} else {
numItems := uint64(len(b))
limit := CalculateLimit(maxCapacity[0], numItems, 32)
h.MerkleizeWithMixin(indx, numItems, limit)
}
return nil
}
// PutUint64Array appends an array of uint64
func (h *Hasher) PutUint64Array(b []uint64, maxCapacity ...uint64) {
indx := h.Index()
for _, i := range b {
h.AppendUint64(i)
}
// pad zero bytes to the left
h.FillUpTo32()
if len(maxCapacity) == 0 {
// Array with fixed size
h.Merkleize(indx)
} else {
numItems := uint64(len(b))
limit := CalculateLimit(maxCapacity[0], numItems, 8)
h.MerkleizeWithMixin(indx, numItems, limit)
}
}
func parseBitlist(dst, buf []byte) ([]byte, uint64) {
msb := uint8(bits.Len8(buf[len(buf)-1])) - 1
size := uint64(8*(len(buf)-1) + int(msb))
dst = append(dst, buf...)
dst[len(dst)-1] &^= uint8(1 << msb)
newLen := len(dst)
for i := len(dst) - 1; i >= 0; i-- {
if dst[i] != 0x00 {
break
}
newLen = i
}
res := dst[:newLen]
return res, size
}
// PutBitlist appends a ssz bitlist
func (h *Hasher) PutBitlist(bb []byte, maxSize uint64) {
var size uint64
h.tmp, size = parseBitlist(h.tmp[:0], bb)
// merkleize the content with mix in length
indx := h.Index()
h.AppendBytes32(h.tmp)
h.MerkleizeWithMixin(indx, size, (maxSize+255)/256)
}
// PutBool appends a boolean
func (h *Hasher) PutBool(b bool) {
if b {
h.buf = append(h.buf, trueBytes...)
} else {
h.buf = append(h.buf, falseBytes...)
}
}
// PutBytes appends bytes
func (h *Hasher) PutBytes(b []byte) {
if len(b) <= 32 {
h.AppendBytes32(b)
return
}
// if the bytes are longer than 32 we have to
// merkleize the content
indx := h.Index()
h.AppendBytes32(b)
h.Merkleize(indx)
}
// Index marks the current buffer index
func (h *Hasher) Index() int {
return len(h.buf)
}
// Merkleize is used to merkleize the last group of the hasher
func (h *Hasher) Merkleize(indx int) {
h.buf = append(h.buf[:indx], merkleizeInput(h.buf[indx:], 0)...)
}
// MerkleizeWithMixin is used to merkleize the last group of the hasher
func (h *Hasher) MerkleizeWithMixin(indx int, num, limit uint64) {
input := merkleizeInput(h.buf[indx:], limit)
// mixin with the size
sizemix := h.tmp[:32]
for indx := range sizemix {
sizemix[indx] = 0
}
MarshalUint64(sizemix[:0], num)
h.buf = append(h.buf[:indx], h.doHash(input, input, sizemix)...)
}
// HashRoot creates the hash final hash root
func (h *Hasher) HashRoot() (res [32]byte, err error) {
if len(h.buf) != 32 {
err = ErrRootSizeInvalid
return
}
copy(res[:], h.buf)
return
}
// HasherPool may be used for pooling Hashers for similarly typed SSZs.
type HasherPool struct {
pool sync.Pool
}
// Get acquires a Hasher from the pool.
func (hh *HasherPool) Get() *Hasher {
h := hh.pool.Get()
if h == nil {
return NewHasher()
}
return h.(*Hasher)
}
// Put releases the Hasher to the pool.
func (hh *HasherPool) Put(h *Hasher) {
h.Reset()
hh.pool.Put(h)
}
func nextPowerOfTwo(v uint64) uint {
v--
v |= v >> 1
v |= v >> 2
v |= v >> 4
v |= v >> 8
v |= v >> 16
v++
return uint(v)
}
func getDepth(d uint64) uint8 {
if d == 0 {
return 0
}
if d == 1 {
return 1
}
i := nextPowerOfTwo(d)
return 64 - uint8(bits.LeadingZeros(i)) - 1
}
func (h *Hasher) doHash(dst []byte, a []byte, b []byte) []byte {
h.hash.Write(a)
h.hash.Write(b)
h.hash.Sum(dst[:0])
h.hash.Reset()
return dst
}
func merkleizeInput(input []byte, limit uint64) []byte {
chunkCount := (len(input) + 31) / 32
chunks := make([][32]byte, chunkCount)
for i, j := 0, 0; j < chunkCount; i, j = i+32, j+1 {
if j == chunkCount-1 {
copy(chunks[j][:], input[i:])
} else {
copy(chunks[j][:], input[i:i+32])
}
}
var result [32]byte
if limit == 0 {
result = merkleizeVector(chunks, uint64(chunkCount))
} else {
result = merkleizeVector(chunks, limit)
}
return result[:]
}
// MerkleizeVector uses our optimized routine to hash a list of 32-byte
// elements.
func merkleizeVector(elements [][32]byte, length uint64) [32]byte {
dep := depth(length)
// Return zerohash at depth
if len(elements) == 0 {
return zeroHashesRaw[dep]
}
for i := uint8(0); i < dep; i++ {
layerLen := len(elements)
oddNodeLength := layerLen%2 == 1
if oddNodeLength {
zerohash := zeroHashesRaw[i]
elements = append(elements, zerohash)
}
outputLen := len(elements) / 2
// gohashtree concurrently overwrites elements
err := gohashtree.Hash(elements, elements)
if err != nil {
panic(err)
}
elements = elements[:outputLen]
}
return elements[0]
}
// Depth retrieves the appropriate depth for the provided trie size.
func depth(v uint64) (out uint8) {
// bitmagic: binary search through a uint32, offset down by 1 to not round powers of 2 up.
// Then adding 1 to it to not get the index of the first bit, but the length of the bits (depth of tree)
// Zero is a special case, it has a 0 depth.
// Example:
// (in out): (0 0), (1 0), (2 1), (3 2), (4 2), (5 3), (6 3), (7 3), (8 3), (9 4)
if v <= 1 {
return 0
}
v--
if v&mask5 != 0 {
v >>= bit5
out |= bit5
}
if v&mask4 != 0 {
v >>= bit4
out |= bit4
}
if v&mask3 != 0 {
v >>= bit3
out |= bit3
}
if v&mask2 != 0 {
v >>= bit2
out |= bit2
}
if v&mask1 != 0 {
v >>= bit1
out |= bit1
}
if v&mask0 != 0 {
out |= bit0
}
out++
return
}