-
Notifications
You must be signed in to change notification settings - Fork 4
/
root.go
95 lines (77 loc) · 2.58 KB
/
root.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
package smt
import (
"encoding/binary"
"fmt"
)
// MustSum returns the uint64 sum of the merkle root, it checks the length of the
// merkle root and if it is no the same as the size of the SMST's expected
// root hash it will panic.
func (root MerkleSumRoot) MustSum() uint64 {
sum, err := root.Sum()
if err != nil {
panic(err)
}
return sum
}
// Sum returns the uint64 sum of the merkle root, it checks the length of the
// merkle root and if it is no the same as the size of the SMST's expected
// root hash it will return an error.
func (root MerkleSumRoot) Sum() (uint64, error) {
if err := root.validateBasic(); err != nil {
return 0, err
}
return root.sum(), nil
}
// MustCount returns the uint64 count of the merkle root, a cryptographically secure
// count of the number of non-empty leafs in the tree. It panics if the root length
// is invalid.
func (root MerkleSumRoot) MustCount() uint64 {
count, err := root.Count()
if err != nil {
panic(err)
}
return count
}
// Count returns the uint64 count of the merkle root, a cryptographically secure
// count of the number of non-empty leafs in the tree. It returns an error if the
// root length is invalid.
func (root MerkleSumRoot) Count() (uint64, error) {
if err := root.validateBasic(); err != nil {
return 0, err
}
return root.count(), nil
}
// DigestSize returns the length of the digest portion of the root.
func (root MerkleSumRoot) DigestSize() int {
return len(root) - countSizeBytes - sumSizeBytes
}
// HasDigestSize returns true if the root digest size is the same as
// that of the size of the given hasher.
func (root MerkleSumRoot) HasDigestSize(size int) bool {
return root.DigestSize() == size
}
// validateBasic returns an error if the root digest size is not a power of two.
func (root MerkleSumRoot) validateBasic() error {
if !isPowerOfTwo(root.DigestSize()) {
return fmt.Errorf("MerkleSumRoot#validateBasic: invalid root length")
}
return nil
}
// sum returns the sum of the node stored in the root.
func (root MerkleSumRoot) sum() uint64 {
firstSumByteIdx, firstCountByteIdx := getFirstMetaByteIdx(root)
return binary.BigEndian.Uint64(root[firstSumByteIdx:firstCountByteIdx])
}
// count returns the count of the node stored in the root.
func (root MerkleSumRoot) count() uint64 {
_, firstCountByteIdx := getFirstMetaByteIdx(root)
return binary.BigEndian.Uint64(root[firstCountByteIdx:])
}
// isPowerOfTwo function returns true if the input n is a power of 2
func isPowerOfTwo(n int) bool {
// A power of 2 has only one bit set in its binary representation
if n <= 0 {
return false
}
return (n & (n - 1)) == 0
}