-
Notifications
You must be signed in to change notification settings - Fork 68
/
proof.go
316 lines (266 loc) · 7.48 KB
/
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package cell
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"fmt"
)
type cellHash = []byte
type ProofSkeleton struct {
recursive bool
branches [4]*ProofSkeleton
}
func CreateProofSkeleton() *ProofSkeleton {
return &ProofSkeleton{}
}
// ProofRef - include ref with index i to proof
func (s *ProofSkeleton) ProofRef(i int) *ProofSkeleton {
if s.branches[i] == nil {
s.branches[i] = &ProofSkeleton{}
}
return s.branches[i]
}
// SetRecursive - include all underlying refs recursively in ordinary form to proof
func (s *ProofSkeleton) SetRecursive() {
s.recursive = true
}
// AttachAt - attach skeleton chain at specific ref slot
func (s *ProofSkeleton) AttachAt(i int, sk *ProofSkeleton) {
s.branches[i] = sk
}
// Merge - merge 2 proof chains in a single proof tree
func (s *ProofSkeleton) Merge(sk *ProofSkeleton) {
for i, v := range sk.branches {
if v == nil {
continue
}
if s.branches[i] == nil {
s.branches[i] = v
continue
}
if v.recursive {
s.branches[i].SetRecursive()
continue
}
s.branches[i].Merge(v)
}
}
func (s *ProofSkeleton) Copy() *ProofSkeleton {
return &ProofSkeleton{
recursive: s.recursive,
branches: s.branches,
}
}
func (c *Cell) CreateProof(skeleton *ProofSkeleton) (*Cell, error) {
body, err := toProof(c, skeleton)
if err != nil {
return nil, fmt.Errorf("failed to build proof for cell: %w", err)
}
// build root Merkle Proof cell
data := make([]byte, 1+32+2)
data[0] = byte(MerkleProofCellType)
copy(data[1:], body.getHash(0))
binary.BigEndian.PutUint16(data[1+32:], body.getDepth(0))
proof := &Cell{
special: true,
levelMask: LevelMask{body.levelMask.Mask},
bitsSz: 8 + 256 + 16,
data: data,
refs: []*Cell{body},
}
if proof.levelMask.Mask > 0 {
proof.levelMask.Mask -= 1
}
proof.calculateHashes()
return proof, nil
}
func toProof(c *Cell, skeleton *ProofSkeleton) (*Cell, error) {
if skeleton.recursive {
return c, nil
}
cLvl := c.levelMask.Mask
c = c.copy()
for i := 0; i < len(skeleton.branches); i++ {
if skeleton.branches[i] != nil { // dive into branch
r, err := c.PeekRef(i)
if err != nil {
return nil, fmt.Errorf("failed to peek %d ref: %w", i, err)
}
r, err = toProof(r, skeleton.branches[i])
if err != nil {
return nil, fmt.Errorf("failed to proof %d ref: %w", i, err)
}
c.refs[i] = r
cLvl |= r.levelMask.Mask
} else if len(c.refs) > i && len(c.refs[i].refs) > 0 { // prune branch
r, err := c.PeekRef(i)
if err != nil {
return nil, fmt.Errorf("failed to peek %d ref: %w", i, err)
}
parentLvl := c.levelMask.GetLevel()
ourLvl := r.levelMask.GetLevel()
if parentLvl >= 3 || ourLvl >= 3 {
return nil, fmt.Errorf("level is to big to prune")
}
prunedData := make([]byte, 2+(ourLvl+1)*(32+2))
prunedData[0] = byte(PrunedCellType)
prunedData[1] = r.levelMask.Mask | (1 << parentLvl)
for lvl := 0; lvl <= ourLvl; lvl++ {
copy(prunedData[2+(lvl*32):], r.getHash(lvl))
binary.BigEndian.PutUint16(prunedData[2+((ourLvl+1)*32)+2*lvl:], r.getDepth(lvl))
}
r = &Cell{
special: true,
levelMask: LevelMask{prunedData[1]},
bitsSz: uint(len(prunedData) * 8),
data: prunedData,
}
r.calculateHashes()
c.refs[i] = r
cLvl |= r.levelMask.Mask
}
}
if c.special && c.GetType() == MerkleProofCellType {
// unset merkle level bit
m := LevelMask{cLvl}
mask := byte(^(1 << m.GetLevel()))
c.levelMask = LevelMask{m.Mask & mask}
} else {
c.levelMask = LevelMask{cLvl}
}
c.calculateHashes()
return c, nil
}
func CheckProof(proof *Cell, hash []byte) error {
_, err := UnwrapProof(proof, hash)
return err
}
func UnwrapProof(proof *Cell, hash []byte) (*Cell, error) {
if !proof.special || proof.RefsNum() != 1 || proof.BitsSize() != 280 ||
Type(proof.data[0]) != MerkleProofCellType {
return nil, fmt.Errorf("not a merkle proof cell")
}
if !bytes.Equal(hash, proof.data[1:33]) {
return nil, fmt.Errorf("incorrect proof hash")
}
calcDepth := proof.refs[0].getDepth(0)
if calcDepth != binary.BigEndian.Uint16(proof.data[33:]) {
return nil, fmt.Errorf("incorrect proof depth")
}
// we unwrap level by 1 to correctly check proofs on pruned cells
calcHash := proof.refs[0].getHash(0)
if !bytes.Equal(hash, calcHash) {
return nil, fmt.Errorf("incorrect proof")
}
return proof.refs[0], nil
}
func (c *Cell) getLevelMask() LevelMask {
return c.levelMask
}
func (c *Cell) getHash(level int) []byte {
hashIndex := c.getLevelMask().Apply(level).getHashIndex()
if c.GetType() == PrunedCellType {
prunedHashIndex := c.getLevelMask().getHashIndex()
if hashIndex != prunedHashIndex {
// return hash from data
return c.data[2+(hashIndex*32) : 2+((hashIndex+1)*32)]
}
hashIndex = 0
}
return c.hashes[hashIndex*32 : (hashIndex+1)*32]
}
// calculateHashes - we are precalculating cell hashes during creation for safe read parallel access later
func (c *Cell) calculateHashes() {
totalHashCount := c.levelMask.getHashIndex() + 1
c.hashes = make([]byte, 32*totalHashCount)
c.depthLevels = make([]uint16, totalHashCount)
hashCount := totalHashCount
typ := c.GetType()
if typ == PrunedCellType {
hashCount = 1
}
hashIndexOffset := totalHashCount - hashCount
hashIndex := 0
level := c.levelMask.GetLevel()
for levelIndex := 0; levelIndex <= level; levelIndex++ {
if !c.levelMask.IsSignificant(levelIndex) {
continue
}
func() {
defer func() {
hashIndex++
}()
if levelIndex < hashIndexOffset {
return
}
dsc := make([]byte, 2)
dsc[0], dsc[1] = c.descriptors(c.levelMask.Apply(levelIndex))
hash := sha256.New()
hash.Write(dsc)
if hashIndex == hashIndexOffset {
if levelIndex != 0 && typ != PrunedCellType {
// should never happen
panic("not pruned or 0")
}
data := c.BeginParse().MustLoadSlice(c.bitsSz)
unusedBits := 8 - (c.bitsSz % 8)
if unusedBits != 8 {
// we need to set bit at the end if not whole byte was used
data[len(data)-1] += 1 << (unusedBits - 1)
}
hash.Write(data)
} else {
if levelIndex == 0 || typ == PrunedCellType {
// should never happen
panic("pruned or 0")
}
off := hashIndex - hashIndexOffset - 1
hash.Write(c.hashes[off*32 : (off+1)*32])
}
var depth uint16
for i := 0; i < len(c.refs); i++ {
var childDepth uint16
if typ == MerkleProofCellType || typ == MerkleUpdateCellType {
childDepth = c.refs[i].getDepth(levelIndex + 1)
} else {
childDepth = c.refs[i].getDepth(levelIndex)
}
depthBytes := make([]byte, 2)
binary.BigEndian.PutUint16(depthBytes, childDepth)
hash.Write(depthBytes)
if childDepth > depth {
depth = childDepth
}
}
if len(c.refs) > 0 {
depth++
if depth >= maxDepth {
panic("depth is more than max depth")
}
}
for i := 0; i < len(c.refs); i++ {
if typ == MerkleProofCellType || typ == MerkleUpdateCellType {
hash.Write(c.refs[i].getHash(levelIndex + 1))
} else {
hash.Write(c.refs[i].getHash(levelIndex))
}
}
off := hashIndex - hashIndexOffset
c.depthLevels[off] = depth
copy(c.hashes[off*32:(off+1)*32], hash.Sum(nil))
}()
}
}
func (c *Cell) getDepth(level int) uint16 {
hashIndex := c.getLevelMask().Apply(level).getHashIndex()
if c.GetType() == PrunedCellType {
prunedHashIndex := c.getLevelMask().getHashIndex()
if hashIndex != prunedHashIndex {
// return depth from data
off := 2 + 32*prunedHashIndex + hashIndex*2
return binary.BigEndian.Uint16(c.data[off : off+2])
}
hashIndex = 0
}
return c.depthLevels[hashIndex]
}