/
merkle_proof.go
295 lines (268 loc) · 8.23 KB
/
merkle_proof.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
package smt
import (
"bytes"
"fmt"
"github.com/dotbitHQ/das-lib/common"
)
type MerkleProof struct {
LeavesBitmap []H256
MerklePath []MergeValue
}
func (m *MerkleProof) compile(keys, values []H256) (*CompiledMerkleProof, error) {
if len(keys) == 0 {
return nil, fmt.Errorf("keys is nil")
} else if len(keys) != len(values) {
return nil, fmt.Errorf("len(keys) != len(values)")
} else if len(keys) != len(m.LeavesBitmap) {
return nil, fmt.Errorf("len(keys) != LeavesCount")
}
// sort keys leaves.sort_unstable_by_key(|(k, _v)| *k);
// sort.Sort(SortH256(keys))
var proof []byte
stackForkHeight := make([]byte, MaxStackSize)
stackTop := 0
leafIndex := 0
merklePathIndex := 0
for leafIndex < len(keys) {
leafKey, _ := keys[leafIndex], values[leafIndex]
forkHeight := byte(MaxU8)
if leafIndex+1 < len(keys) {
forkHeight = leafKey.ForkHeight(&keys[leafIndex+1])
}
proof = append(proof, common.Hex2Bytes("0x4C")...)
zeroCount := uint16(0)
for i := 0; i <= int(forkHeight); i++ {
height := byte(i)
if height == forkHeight && leafIndex+1 < len(keys) {
// If it's not final round, we don't need to merge to root (height=255)
break
}
var opCodeOpt string
var siblingDataOpt []byte
if stackTop > 0 && stackForkHeight[stackTop-1] == height {
stackTop -= 1
opCodeOpt = "0x48"
} else if m.LeavesBitmap[leafIndex].GetBit(height) {
if merklePathIndex >= len(m.MerklePath) {
return nil, fmt.Errorf("CorruptedProof")
}
node := m.MerklePath[merklePathIndex]
merklePathIndex += 1
if node.Value != nil {
opCodeOpt = "0x50"
siblingDataOpt = node.Hash()
} else {
var buffer []byte
buffer = append(buffer, node.ZeroCount)
buffer = append(buffer, node.BaseNode...)
buffer = append(buffer, node.ZeroBits...)
opCodeOpt = "0x51"
siblingDataOpt = buffer
}
} else {
zeroCount += 1
if zeroCount > 256 {
return nil, fmt.Errorf("CorruptedProof")
}
}
if opCode := opCodeOpt; opCode != "" {
if zeroCount > 0 {
n := byte(0)
if zeroCount != 256 {
n = byte(zeroCount)
}
proof = append(proof, common.Hex2Bytes("0x4F")...)
proof = append(proof, n)
zeroCount = 0
}
// note: opCode to []byte
proof = append(proof, common.Hex2Bytes(opCode)...)
}
data := siblingDataOpt
proof = append(proof, data...)
}
if zeroCount > 0 {
n := byte(0)
if zeroCount != 256 {
n = byte(zeroCount)
}
proof = append(proof, common.Hex2Bytes("0x4F")...)
proof = append(proof, n)
}
stackForkHeight[stackTop] = forkHeight
stackTop += 1
leafIndex += 1
}
if stackTop != 1 {
return nil, fmt.Errorf("CorruptedProof")
}
if leafIndex != len(keys) {
return nil, fmt.Errorf("CorruptedProof")
}
if merklePathIndex != len(m.MerklePath) {
return nil, fmt.Errorf("CorruptedProof")
}
res := CompiledMerkleProof(proof)
return &res, nil
}
type CompiledMerkleProof []byte
func (c *CompiledMerkleProof) computeRoot(keys, values []H256) (*H256, error) {
// leaves.sort_unstable_by_key(|(k, _v)| *k);
// sort.Sort(SortH256(keys))
var stackHeights = make([]uint16, MaxStackSize)
var stackKeys = make([]H256, MaxStackSize)
var stackValues = make([]MergeValue, MaxStackSize)
var proofIndex, proofLen, leaveIndex, stackTop = 0, len(*c), 0, 0
for proofIndex < proofLen {
code := (*c)[proofIndex]
proofIndex++
switch code {
case 0x4C: // L : push leaf value
if stackTop >= MaxStackSize {
return nil, fmt.Errorf("CorruptedStack")
}
if leaveIndex >= len(keys) {
return nil, fmt.Errorf("CorruptedStack")
}
k, v := keys[leaveIndex], values[leaveIndex]
stackHeights[stackTop] = 0
stackKeys[stackTop] = k
stackValues[stackTop] = MergeValueFromH256(v)
leaveIndex++
stackTop++
case 0x50: // P : hash stack top item with sibling node in proof
if stackTop == 0 {
return nil, fmt.Errorf("CorruptedStack")
}
if proofIndex+32 > proofLen {
return nil, fmt.Errorf("CorruptedProof")
}
proofTmp := (*c)[proofIndex : proofIndex+32]
siblingNode := MergeValueFromH256(H256(proofTmp))
proofIndex += 32
height, key, value := stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1]
if height > 255 {
return nil, fmt.Errorf("CorruptedProof")
}
heightPtr := byte(height) // u16 to u8
parentKey := key.ParentPath(heightPtr)
var parent MergeValue
if key.GetBit(heightPtr) {
parent = Merge(heightPtr, *parentKey, siblingNode, value)
} else {
parent = Merge(heightPtr, *parentKey, value, siblingNode)
}
stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1] = height+1, *parentKey, parent
case 0x51:
// Q : hash stack top item with sibling node in proof,
// this is similar to P except that proof comes in using
// MergeWithZero format.
if stackTop == 0 {
return nil, fmt.Errorf("CorruptedStack")
}
if proofIndex+65 > proofLen {
return nil, fmt.Errorf("CorruptedProof")
}
zeroCount := (*c)[proofIndex]
baseNode := H256((*c)[proofIndex+1 : proofIndex+33])
zeroBits := H256((*c)[proofIndex+33 : proofIndex+65])
proofIndex += 65
siblingNode := MergeValue{
Value: nil,
BaseNode: baseNode,
ZeroBits: zeroBits,
ZeroCount: zeroCount,
}
heightU16, key, value := stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1]
if heightU16 > 255 {
return nil, fmt.Errorf("CorruptedProof")
}
height := byte(heightU16) // u16 to u8
parentKey := key.ParentPath(height)
var parent MergeValue
if key.GetBit(height) {
parent = Merge(height, *parentKey, siblingNode, value)
} else {
parent = Merge(height, *parentKey, value, siblingNode)
}
stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1] = heightU16+1, *parentKey, parent
case 0x48:
// H : pop 2 items in stack hash them then push the result
if stackTop < 2 {
return nil, fmt.Errorf("CorruptedStack")
}
heightA, keyA, valueA := stackHeights[stackTop-2], stackKeys[stackTop-2], stackValues[stackTop-2]
heightB, keyB, valueB := stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1]
stackTop--
if heightA != heightB {
return nil, fmt.Errorf("CorruptedProof")
}
if heightA > 255 {
return nil, fmt.Errorf("CorruptedProof")
}
heightPtr := byte(heightA)
parentKeyA := keyA.ParentPath(heightPtr)
parentKeyB := keyB.ParentPath(heightPtr)
if bytes.Compare(*parentKeyA, *parentKeyB) != 0 {
return nil, fmt.Errorf("CorruptedProof")
}
var parent MergeValue
if keyA.GetBit(heightPtr) {
parent = Merge(heightPtr, *parentKeyA, valueB, valueA)
} else {
parent = Merge(heightPtr, *parentKeyA, valueA, valueB)
}
stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1] = heightA+1, *parentKeyA, parent
case 0x4F:
// O : hash stack top item with n zero values
if stackTop < 1 {
return nil, fmt.Errorf("CorruptedStack")
}
if proofIndex >= proofLen {
return nil, fmt.Errorf("CorruptedProof")
}
n := (*c)[proofIndex]
proofIndex++
zeroCount := uint16(256)
if n != 0 {
zeroCount = uint16(n)
}
baseHeight, key, value := stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1]
if baseHeight > 255 {
return nil, fmt.Errorf("CorruptedProof")
}
var parentKey *H256
heightU16 := baseHeight
for idx := uint16(0); idx < zeroCount; idx++ {
if baseHeight+idx > 255 {
return nil, fmt.Errorf("CorruptedProof")
}
heightU16 = baseHeight + idx
height := byte(heightU16)
parentKey = key.ParentPath(height)
if key.GetBit(height) {
value = Merge(height, *parentKey, MergeValueFromZero(), value)
} else {
value = Merge(height, *parentKey, value, MergeValueFromZero())
}
}
stackHeights[stackTop-1], stackKeys[stackTop-1], stackValues[stackTop-1] = heightU16+1, *parentKey, value
default:
return nil, fmt.Errorf("CorruptedProof")
}
}
if stackTop != 1 {
return nil, fmt.Errorf("CorruptedStack")
}
if stackHeights[0] != 256 {
return nil, fmt.Errorf("CorruptedProof")
}
if leaveIndex != len(keys) {
return nil, fmt.Errorf("CorruptedProof")
}
root := stackValues[0].Hash()
return &root, nil
}
func (c *CompiledMerkleProof) String() string {
return common.Bytes2Hex(*c)
}