-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.go
60 lines (51 loc) · 1022 Bytes
/
util.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package trie
import (
"bytes"
"crypto/sha256"
)
var (
// Trie default value : hash of 0x0
DefaultLeaf = Hasher([]byte{0x0})
)
const (
HashLength = 32
maxPastTries = 300
)
type Hash [HashLength]byte
func bitIsSet(bits []byte, i int) bool {
return bits[i/8]&(1<<uint(7-i%8)) != 0
}
func bitSet(bits []byte, i int) {
bits[i/8] |= 1 << uint(7-i%8)
}
func bitUnSet(bits []byte, i int) {
bits[i/8] = bits[i/8] &^ (1 << uint(7-i%8))
}
func bitSplit(bits []byte, i int) (split []byte) {
split = make([]byte, len(bits))
copy(split, bits)
bitSet(split, i)
return
}
func Hasher(data ...[]byte) []byte {
hasher := sha256.New()
for i := 0; i < len(data); i++ {
hasher.Write(data[i])
}
return hasher.Sum(nil)
}
// for sorting
type DataArray [][]byte
func (d DataArray) Len() int {
return len(d)
}
func (d DataArray) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
func (d DataArray) Less(i, j int) bool {
return bytes.Compare(d[i], d[j]) == -1
}