forked from txaty/go-merkletree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proof_gen.go
196 lines (159 loc) · 5.81 KB
/
proof_gen.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
// MIT License
//
// Copyright (c) 2023 Tommy TIAN
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package merkletree
import (
"fmt"
"sync"
"golang.org/x/sync/errgroup"
)
// proofGen constructs the Merkle Tree and generates the Merkle proofs for each leaf.
// It returns an error if there is an issue during the generation process.
func (m *MerkleTree) proofGen() (err error) {
m.initProofs()
buffer, bufferSize := initBuffer(m.Leaves)
for step := 0; step < m.Depth; step++ {
bufferSize = fixOddNumOfNodes(buffer, bufferSize, step)
m.updateProofs(buffer, bufferSize, step)
for idx := 0; idx < bufferSize; idx += 2 {
leftIdx := idx << step
rightIdx := min(leftIdx+(1<<step), len(buffer)-1)
buffer[leftIdx], err = m.HashFunc(m.concatHashFunc(buffer[leftIdx], buffer[rightIdx]))
if err != nil {
return
}
}
bufferSize >>= 1
}
m.Root = buffer[0]
return
}
// proofGenParallel generates proofs concurrently for the MerkleTree.
func (m *MerkleTree) proofGenParallel() error {
m.initProofs()
buffer, bufferSize := initBuffer(m.Leaves)
numRoutines := m.NumRoutines
for step := 0; step < m.Depth; step++ {
// Limit the number of workers to the previous level length.
numRoutines = min(numRoutines, bufferSize)
bufferSize = fixOddNumOfNodes(buffer, bufferSize, step)
m.updateProofsParallel(buffer, bufferSize, step)
eg := new(errgroup.Group)
for workerIdx := 0; workerIdx < numRoutines; workerIdx++ {
startIdx := workerIdx << 1
eg.Go(func() error {
var err error
for i := startIdx; i < bufferSize; i += numRoutines << 1 {
leftIdx := i << step
rightIdx := min(leftIdx+(1<<step), len(buffer)-1)
buffer[leftIdx], err = m.HashFunc(m.concatHashFunc(buffer[leftIdx], buffer[rightIdx]))
if err != nil {
return err
}
}
return nil
})
}
if err := eg.Wait(); err != nil {
return fmt.Errorf("proofGenParallel: %w", err)
}
bufferSize >>= 1
}
m.Root = buffer[0]
return nil
}
// initProofs initializes the MerkleTree's Proofs with the appropriate size and depth.
// This is to reduce overhead of slice resizing during the generation process.
func (m *MerkleTree) initProofs() {
m.Proofs = make([]*Proof, m.NumLeaves)
for i := 0; i < m.NumLeaves; i++ {
m.Proofs[i] = new(Proof)
m.Proofs[i].Siblings = make([][]byte, 0, m.Depth)
}
}
// initBuffer initializes the buffer with the leaves and returns the buffer size.
// If the number of leaves is odd, the buffer size is increased by 1.
func initBuffer(leaves [][]byte) (buffer [][]byte, numLeaves int) {
numLeaves = len(leaves)
// If the number of leaves is odd, make initial buffer size even by adding 1.
if numLeaves&1 == 1 {
buffer = make([][]byte, numLeaves+1)
} else {
buffer = make([][]byte, numLeaves)
}
copy(buffer, leaves)
return buffer, numLeaves
}
// fixOddNumOfNodes adjusts the buffer size if it has an odd number of nodes.
// It appends the last node to the buffer if the buffer length is odd.
func fixOddNumOfNodes(buffer [][]byte, bufferSize, step int) int {
// If the buffer length is even, no adjustment is needed.
if bufferSize&1 == 0 {
return bufferSize
}
// Determine the node to append.
appendNodeIndex := (bufferSize - 1) << step
// The appended node will be put at the end of the buffer.
buffer[len(buffer)-1] = buffer[appendNodeIndex]
bufferSize++
return bufferSize
}
// updateProofs updates the proofs for all the leaves while constructing the Merkle Tree.
func (m *MerkleTree) updateProofs(buffer [][]byte, bufferSize, step int) {
batch := 1 << step
for i := 0; i < bufferSize; i += 2 {
updateProofInTwoBatches(m.Proofs, buffer, i, batch, step)
}
}
// updateProofsParallel updates the proofs for all the leaves while constructing the Merkle Tree in parallel.
func (m *MerkleTree) updateProofsParallel(buffer [][]byte, bufferLength, step int) {
var (
batch = 1 << step
wg sync.WaitGroup
)
numRoutines := min(m.NumRoutines, bufferLength)
wg.Add(numRoutines)
for startIdx := 0; startIdx < numRoutines; startIdx++ {
go func(startIdx int) {
defer wg.Done()
for i := startIdx; i < bufferLength; i += numRoutines << 1 {
updateProofInTwoBatches(m.Proofs, buffer, i, batch, step)
}
}(startIdx << 1)
}
wg.Wait()
}
// updateProofInTwoBatches updates the path and the siblings of the proof in two batches.
func updateProofInTwoBatches(proofs []*Proof, buffer [][]byte, idx, batch, step int) {
start := idx * batch
end := min(start+batch, len(proofs))
siblingNodeIdx := min((idx+1)<<step, len(buffer)-1)
for i := start; i < end; i++ {
proofs[i].Path += 1 << step
proofs[i].Siblings = append(proofs[i].Siblings, buffer[siblingNodeIdx])
}
start += batch
end = min(start+batch, len(proofs))
siblingNodeIdx = min(idx<<step, len(buffer)-1)
for i := start; i < end; i++ {
proofs[i].Siblings = append(proofs[i].Siblings, buffer[siblingNodeIdx])
}
}