/
proof.go
672 lines (551 loc) · 19.7 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package metaservice
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"math"
"math/bits"
"os"
"path"
"reflect"
"sort"
"sync"
"github.com/dataswap/go-metadata/utils"
commcid "github.com/filecoin-project/go-fil-commcid"
sha256simd "github.com/minio/sha256-simd"
"github.com/opentracing/opentracing-go/log"
"golang.org/x/xerrors"
mt "github.com/txaty/go-merkletree"
)
const (
SOURCE_CHUNK_SIZE = 127
SLAB_CHUNK_SIZE = 128
NODE_SIZE = 32
CHUNK_NODES_NUM = 4
CAR_32GIB_SIZE = uint64(1 << 35)
CAR_2MIB_CHUNK_SIZE = uint64(SOURCE_CHUNK_SIZE * CAR_2MIB_NODE_NUM / CHUNK_NODES_NUM) // source data node = 127, no padding
CAR_512B_CHUNK_SIZE = uint64(SOURCE_CHUNK_SIZE * CAR_512B_NODE_NUM / CHUNK_NODES_NUM) // source data node = 127, no padding
CAR_2MIB_NODE_NUM = uint64(1 << 20 * 2 / NODE_SIZE) // = 2MIB / NODE_SIZE
CAR_512B_NODE_NUM = uint64(1 << 9 / NODE_SIZE) // = 512B / NODE_SIZE
DATASET_RULE_CHALLENGE_POINTS_PER_AUDITOR = 5 // 5 point per auditor
CAR_2MIB_CACHE_LAYER_START = 16
CAR_512B_CACHE_LAYER_START = 4
CACHE_SUFFIX = ".cache"
CACHE_DATASET_PROOF_PATH = "dataset.proof"
CACHE_CHALLENGE_PROOFS_PATH = "challenges.proofs"
PROOFS_PATH = "proofs"
// MaxLayers is the current maximum height of the rust-fil-proofs proving tree.
MaxLayers = uint(31) // result of log2( 64 GiB / 32 )
// MaxPieceSize is the current maximum size of the rust-fil-proofs proving tree.
MaxPieceSize = uint64(1 << (MaxLayers + 5))
)
var (
StackedNulPadding [MaxLayers][]byte
SumChunkCount uint64
CommpHashConfig = &mt.Config{
HashFunc: NewHashFunc,
DisableLeafHashing: true,
Mode: mt.ModeTreeBuild,
RunInParallel: true,
}
Once sync.Once
)
// ### export functions
// PadCommP is experimental, do not use it.
func PadCommP(sourceCommP []byte, sourcePaddedSize, targetPaddedSize uint64) ([]byte, error) {
if len(sourceCommP) != 32 {
return nil, xerrors.Errorf("provided commP must be exactly 32 bytes long, got %d bytes instead", len(sourceCommP))
}
if bits.OnesCount64(sourcePaddedSize) != 1 {
return nil, xerrors.Errorf("source padded size %d is not a power of 2", sourcePaddedSize)
}
if bits.OnesCount64(targetPaddedSize) != 1 {
return nil, xerrors.Errorf("target padded size %d is not a power of 2", targetPaddedSize)
}
if sourcePaddedSize > targetPaddedSize {
return nil, xerrors.Errorf("source padded size %d larger than target padded size %d", sourcePaddedSize, targetPaddedSize)
}
if sourcePaddedSize < 128 {
return nil, xerrors.Errorf("source padded size %d smaller than the minimum of 128 bytes", sourcePaddedSize)
}
if targetPaddedSize > MaxPieceSize {
return nil, xerrors.Errorf("target padded size %d larger than Filecoin maximum of %d bytes", targetPaddedSize, MaxPieceSize)
}
// noop
if sourcePaddedSize == targetPaddedSize {
return sourceCommP, nil
}
out := make([]byte, 32)
copy(out, sourceCommP)
s := bits.TrailingZeros64(sourcePaddedSize)
t := bits.TrailingZeros64(targetPaddedSize)
sha256Func := sha256simd.New()
for ; s < t; s++ {
sha256Func.Reset()
sha256Func.Write(out)
sha256Func.Write(StackedNulPadding[s-5]) // account for 32byte chunks + off-by-one padding tower offset
out = sha256Func.Sum(out[:0])
out[31] &= 0x3F
}
return out, nil
}
// SaveCommP append. struct format: map{commmp:carSize}
func SaveCommP(rawCommP []byte, carSize uint64, cachePath string) error {
cPath := createPath(cachePath, "rawCommP"+CACHE_SUFFIX)
lock, err := utils.NewFileLock(cachePath)
if err != nil {
fmt.Println("Lock File Error:", err)
return err
}
defer lock.Close()
if err := lock.Lock(); err != nil {
fmt.Println("Lock Error:", err)
return err
}
defer lock.Unlock()
commp, _ := loadCommP(cPath)
if commp == nil { // first is nil
commp = &map[string]uint64{}
}
(*commp)[string(rawCommP)] = carSize
utils.WriteGob(commp, cPath)
return nil
}
// GenCommP is the commP generate. targetPaddedSize = 0 is use default padded size
func GenCommP(buf bytes.Buffer, cachePath string, targetPaddedSize uint64) ([]byte, uint64, error) {
blocks, paddedPieceSize, err := NewPaddedDataBlocksFromBuffer(buf, targetPaddedSize)
if err != nil {
return nil, 0, err
}
tree, _ := mt.NewWithPadding(CommpHashConfig, blocks, StackedNulPadding)
// paddedPieceSize := SumChunkCount * SLAB_CHUNK_SIZE
// hacky round-up-to-next-pow2
if bits.OnesCount64(paddedPieceSize) != 1 {
paddedPieceSize = 1 << uint(64-bits.LeadingZeros64(paddedPieceSize))
}
cacheStart := CarCacheLayerStart(uint64(buf.Len()))
lc, err := mt.NewLevelCache(tree, cacheStart, tree.Depth-cacheStart)
if err != nil {
log.Error(err)
return nil, 0, err
}
cPath := createPath(cachePath, hex.EncodeToString(tree.Root)+CACHE_SUFFIX)
if err = lc.StoreToFile(cPath); err != nil {
log.Error(err)
return nil, 0, err
}
return tree.Root, paddedPieceSize, nil
}
// Generate commPs Merkle-Tree root to .tcache, proofs{rootHash, leafHashes[]}
// cachePath: store to file path
func GenDatasetProof(cachePath string) ([]byte, error) {
commPs, sizes := LoadSortCommp(cachePath)
Leaves := NewDataBlocksFromBytes(commPs)
cache := DatasetMerkletree{}
if err := errors.New("the number of leaves must be greater than 0"); len(Leaves) < 1 {
log.Error(err)
return nil, err
} else if len(Leaves) == 1 {
if leave, err := Leaves[0].Serialize(); err != nil {
return nil, err
} else {
cache = DatasetMerkletree{Root: leave, Leaves: [][]byte{leave}}
}
} else {
tree, err := mt.New(CommpHashConfig, Leaves)
if err != nil {
log.Error(err)
return nil, err
}
cache = DatasetMerkletree{Root: tree.Root, Leaves: tree.Leaves}
}
cPath := createPath(cachePath, CACHE_DATASET_PROOF_PATH)
err := NewDatasetProof(cache, sizes).save(cPath)
if err != nil {
log.Error(err)
return nil, err
}
return cache.Root, nil
}
// Verify commPs Merkle-Tree proof
func VerifyDatasetProof(cachePath string, randomness uint64) (bool, *mt.Proof, error) {
cPath := createPath(cachePath, CACHE_DATASET_PROOF_PATH)
datasetProof, err := NewDatasetProofFromFile(cPath)
if err != nil {
return false, nil, err
}
cache := datasetProof.proof()
Leaves := NewDataBlocksFromBytes(cache.Leaves)
if err := errors.New("the number of leaves must be greater than 0"); len(Leaves) < 1 {
log.Error(err)
return false, nil, err
} else if len(Leaves) == 1 {
if bytes.Equal(cache.Root, cache.Leaves[0]) {
return true, nil, nil
} else {
return false, nil, nil
}
} else {
tree, err := mt.New(CommpHashConfig, Leaves)
if err != nil {
log.Error(err)
return false, nil, err
}
if !reflect.DeepEqual(tree.Root, cache.Root) {
proof, err := tree.Proof(Leaves[randomness%uint64(len(Leaves))])
if err != nil {
return false, nil, err
}
return false, proof, nil
}
return true, nil, nil
}
}
// Generate challenge nodes Proofs
func GenChallengeProof(randomness uint64, cachePath string) (*Proofs, error) {
// 1. Generate challenge nodes
commPs, carSize := LoadSortCommp(cachePath)
carChallenges, err := GenChallenges(randomness, uint64(len(commPs)), carSize)
if err != nil {
return nil, err
}
// 2. Get challenge chunk data
var proofs Proofs
for _, challenge := range carChallenges {
carChunkSize, carChunkNodes := CarChunkParams(carSize[challenge.CarIndex])
for _, leafIndex := range challenge.Leaves {
commCid, err := commcid.DataCommitmentV1ToCID(commPs[challenge.CarIndex])
if err != nil {
return nil, err
}
offset := uint64((leafIndex / carChunkNodes) * carChunkSize)
buf, err := GetChallengeChunk(commCid, offset, carChunkSize)
if err != nil {
return nil, err
}
// 3. Generate a car chunk proof
blocks, _, err := NewPaddedDataBlocksFromBuffer(*bytes.NewBuffer(buf), 0)
if err != nil {
return nil, err
}
proof, root, err := GenProof(blocks, blocks[leafIndex%carChunkNodes])
if err != nil {
return nil, err
}
// 4. Generate a car cache proof
cPath := createPath(cachePath, commCid.String()+CACHE_SUFFIX)
cacheProof, _, err := GenProofFromCache(NewDataBlockFromBytes(root), cPath)
if err != nil {
return nil, err
}
// 5. Concat proofs
proof, err = AppendProof(proof, *cacheProof)
if err != nil {
return nil, err
}
if proof == nil {
return nil, errors.New("proof is nil")
}
leaf, err := blocks[leafIndex%carChunkNodes].Serialize()
if err != nil {
return nil, err
}
// fmt.Print("\r\nleaf", leaf, "root:", root, "proof:", proof, "\r\n")
proofs.append(utils.ConvertToHexPrefix(leaf), *proof)
}
}
// 6. Store to cache file
cPath := createPath(cachePath, CACHE_CHALLENGE_PROOFS_PATH)
NewChallengeProofs(randomness, proofs).save(cPath)
return &proofs, nil
}
// Verify challenge nodes Proof
func VerifyChallengeProof(cachePath string) (bool, error) {
// 1. Load proofs
cPath := path.Join(cachePath, CACHE_CHALLENGE_PROOFS_PATH)
challengeProofs, err := NewChallengeProofsFromFile(cPath)
if err != nil {
return false, err
}
proofs := challengeProofs.proof()
// 2. Generate challenge nodes
commPs, carSize := LoadSortCommp(cachePath)
if commPs == nil {
return false, errors.New("commPs is nil")
}
carChallenges, err := GenChallenges(challengeProofs.RandomSeed, uint64(len(commPs)), carSize)
if err != nil {
return false, err
}
// 3. Verify proofs
var idx []uint64
i := 0
for _, challenge := range carChallenges {
for range challenge.Leaves {
idx = append(idx, challenge.CarIndex)
}
}
for index, leaf := range proofs.Leaves {
leaf, _ := utils.ParseHexWithPrefix(leaf)
// fmt.Print("\r\nleaf", leaf, "root:", commPs[idx[i]], "proof:", proofs.Proofs[index], "\r\n")
rst, err := mt.Verify(&DataBlock{Data: leaf}, &proofs.Proofs[index], commPs[idx[i]], CommpHashConfig)
if err != nil || !rst {
return false, err
}
i++
}
return true, nil
}
//### public functions
// SHA256 hash generate function for commp
func NewHashFunc(data []byte) ([]byte, error) {
sha256Func := sha256simd.New()
sha256Func.Write(data)
rst := sha256Func.Sum(nil)
rst[31] &= 0x3F
return rst, nil
}
// SHA256 DataPadding function for commp
func DataPadding(inSlab []byte) []byte {
chunkCount := len(inSlab) / SOURCE_CHUNK_SIZE
SumChunkCount += uint64(chunkCount)
outSlab := make([]byte, chunkCount*SLAB_CHUNK_SIZE)
for j := 0; j < chunkCount; j++ {
// Cycle over four(4) 31-byte groups, leaving 1 byte in between:
// 31 + 1 + 31 + 1 + 31 + 1 + 31 = 127
input := inSlab[j*SOURCE_CHUNK_SIZE : (j+1)*SOURCE_CHUNK_SIZE]
expander := outSlab[j*SLAB_CHUNK_SIZE : (j+1)*SLAB_CHUNK_SIZE]
inputPlus1, expanderPlus1 := input[1:], expander[1:]
// First 31 bytes + 6 bits are taken as-is (trimmed later)
// Note that copying them into the expansion buffer is mandatory:
// we will be feeding it to the workers which reuse the bottom half
// of the chunk for the result
copy(expander[:], input[:32])
// first 2-bit "shim" forced into the otherwise identical bitstream
expander[31] &= 0x3F
// In: {{ C[7] C[6] }} X[7] X[6] X[5] X[4] X[3] X[2] X[1] X[0] Y[7] Y[6] Y[5] Y[4] Y[3] Y[2] Y[1] Y[0] Z[7] Z[6] Z[5]...
// Out: X[5] X[4] X[3] X[2] X[1] X[0] C[7] C[6] Y[5] Y[4] Y[3] Y[2] Y[1] Y[0] X[7] X[6] Z[5] Z[4] Z[3]...
for i := 31; i < 63; i++ {
expanderPlus1[i] = inputPlus1[i]<<2 | input[i]>>6
}
// next 2-bit shim
expander[63] &= 0x3F
// In: {{ C[7] C[6] C[5] C[4] }} X[7] X[6] X[5] X[4] X[3] X[2] X[1] X[0] Y[7] Y[6] Y[5] Y[4] Y[3] Y[2] Y[1] Y[0] Z[7] Z[6] Z[5]...
// Out: X[3] X[2] X[1] X[0] C[7] C[6] C[5] C[4] Y[3] Y[2] Y[1] Y[0] X[7] X[6] X[5] X[4] Z[3] Z[2] Z[1]...
for i := 63; i < 95; i++ {
expanderPlus1[i] = inputPlus1[i]<<4 | input[i]>>4
}
// next 2-bit shim
expander[95] &= 0x3F
// In: {{ C[7] C[6] C[5] C[4] C[3] C[2] }} X[7] X[6] X[5] X[4] X[3] X[2] X[1] X[0] Y[7] Y[6] Y[5] Y[4] Y[3] Y[2] Y[1] Y[0] Z[7] Z[6] Z[5]...
// Out: X[1] X[0] C[7] C[6] C[5] C[4] C[3] C[2] Y[1] Y[0] X[7] X[6] X[5] X[4] X[3] X[2] Z[1] Z[0] Y[7]...
for i := 95; i < 126; i++ {
expanderPlus1[i] = inputPlus1[i]<<6 | input[i]>>2
}
// the final 6 bit remainder is exactly the value of the last expanded byte
expander[127] = input[126] >> 2
}
return outSlab
}
// LoadSortCommp loads and sorts the CommP values from the cache file.
// It takes the cache file path as input.
// It returns a slice of CommP values and a slice of their corresponding indices.
func LoadSortCommp(cachePath string) ([][]byte, []uint64) {
cPath := createPath(cachePath, "rawCommP"+CACHE_SUFFIX)
c, err := loadCommP(cPath)
if err != nil {
fmt.Println("loadCommP err: ", err)
return nil, nil
}
return sortCommPSlices(*c)
}
// Car leaf challenge count.
func LeafChallengeCount(carChallengesCount uint64) []uint64 {
leafChallengeCount := make([]uint64, carChallengesCount)
equalDistribution := DATASET_RULE_CHALLENGE_POINTS_PER_AUDITOR / carChallengesCount
remainder := DATASET_RULE_CHALLENGE_POINTS_PER_AUDITOR % carChallengesCount
for i := range leafChallengeCount {
leafChallengeCount[i] = equalDistribution
}
for i := uint64(0); i < remainder; i++ {
leafChallengeCount[i]++
}
return leafChallengeCount
}
// Car challenge count
func CarChallengeCount(carNum uint64) uint64 {
return uint64(math.Min(float64(carNum), float64(DATASET_RULE_CHALLENGE_POINTS_PER_AUDITOR)))
}
// CarChunkParams returns the chunk size and node number for a given CAR size.
// It takes the size of the CAR as input.
// It returns the chunk size and the number of nodes.
func CarChunkParams(carSize uint64) (uint64, uint64) {
if carSize < CAR_2MIB_CHUNK_SIZE {
return CAR_512B_CHUNK_SIZE, CAR_512B_NODE_NUM
} else {
return CAR_2MIB_CHUNK_SIZE, CAR_2MIB_NODE_NUM
}
}
// CarCacheLayerStart returns the start index of the cache layer for a given CAR size.
// It takes the size of the CAR as input.
// It returns the start index of the cache layer.
func CarCacheLayerStart(carSize uint64) int {
if carSize < CAR_2MIB_CHUNK_SIZE {
return CAR_512B_CACHE_LAYER_START
} else {
return CAR_2MIB_CACHE_LAYER_START
}
}
// GenChallenges function generates challenge information for cars and returns a sequentially traversable structure.
func GenChallenges(randomness uint64, carNum uint64, carSize []uint64) ([]CarChallenge, error) {
// Calculate the number of car challenges and leaf challenges per car
carChallengesCount := CarChallengeCount(carNum)
leafChallengeCount := LeafChallengeCount(carChallengesCount)
// Initialize a slice to store car challenge information
carChallenges := make([]CarChallenge, carChallengesCount)
// Generate challenges for each car
for i := uint64(0); i < carChallengesCount; i++ {
carIndex := GenCarChallenge(randomness, i, carChallengesCount, carNum)
leaves, err := GenLeafChallenge(randomness, carIndex, leafChallengeCount[i], carSize[carIndex])
if err != nil {
return nil, err
}
// Add car challenge information to the slice
carChallenges[i] = CarChallenge{
CarIndex: carIndex,
Leaves: leaves,
}
}
return carChallenges, nil
}
// GenCarChallenge generates a car challenge index using randomness, the car challenge index, and the total number of car challenges.
// It returns the generated car challenge index.
func GenCarChallenge(randomness uint64, carChallengeIndex uint64, carChallengesCount uint64, carNum uint64) uint64 {
if carChallengesCount == carNum {
return carChallengeIndex
} else {
sha256Func := sha256simd.New()
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes[:8], randomness)
sha256Func.Write(bytes)
binary.LittleEndian.PutUint64(bytes[:8], carChallengeIndex)
sha256Func.Write(bytes)
hash := sha256Func.Sum(nil)
carChallenge := binary.LittleEndian.Uint64(hash[:8])
return carChallenge % carNum
}
}
// GenLeafChallenge generates a leaf challenge index using randomness, the car index, the leaf challenge index, and the size of the car.
// It returns the generated leaf challenge index.
func GenLeafChallenge(randomness uint64, carIndex uint64, leafChallengeCount uint64, carSize uint64) ([]uint64, error) {
points := carSize / NODE_SIZE
leaves := make([]uint64, 0, leafChallengeCount)
if leafChallengeCount > points {
return nil, errors.New("car points is less leafChallengeCount")
} else if leafChallengeCount == points {
for i := uint64(0); i < leafChallengeCount; i++ {
leaves = append(leaves, i)
}
} else {
uniqueLeaves := make(map[uint64]bool)
for len(uniqueLeaves) < int(leafChallengeCount) {
sha256Func := sha256.New()
bytes := make([]byte, 8)
binary.LittleEndian.PutUint64(bytes[:8], randomness)
sha256Func.Write(bytes)
binary.LittleEndian.PutUint64(bytes[:8], carIndex)
sha256Func.Write(bytes)
hash := sha256Func.Sum(nil)
leaf := binary.LittleEndian.Uint64(hash[:8]) % points
uniqueLeaves[leaf] = true
randomness++
}
for leaf := range uniqueLeaves {
leaves = append(leaves, leaf)
}
}
return leaves, nil
}
// GenProof generates a Merkle tree proof for the specified leaf block.
// It takes a slice of data blocks and the leaf block as input.
// It returns the proof, the root hash of the Merkle tree, and any error encountered.
func GenProof(blocks []mt.DataBlock, leaf mt.DataBlock) (*mt.Proof, []byte, error) {
tree, err := mt.NewWithPadding(CommpHashConfig, blocks, StackedNulPadding)
if err != nil {
log.Error(err)
return nil, nil, err
}
proof, err := tree.Proof(leaf)
if err != nil {
return nil, nil, err
}
return proof, tree.Root, nil
}
// GenProofFromCache generates a Merkle tree proof for the specified leaf block using a level cache.
// It takes the leaf block and the cache file path as input.
// It returns the proof, the root hash of the Merkle tree, and any error encountered.
func GenProofFromCache(leaf mt.DataBlock, file string) (*mt.Proof, []byte, error) {
lc, err := mt.NewLevelCacheFromFile(file)
if err != nil {
fmt.Println("NewLevelCacheFromFile error: ", err)
return nil, nil, err
}
return lc.Prove(leaf, CommpHashConfig)
}
// Append base and sub proof
func AppendProof(base *mt.Proof, sub mt.Proof) (*mt.Proof, error) {
if base == nil {
return nil, errors.New("AppendProof base proof is nil")
}
return mt.AppendProof(base, sub)
}
//### internal functions
// initialize the nul padding stack
func initStackedNulPadding() {
digest := sha256.New()
StackedNulPadding[0] = make([]byte, sha256.Size)
for i := uint(1); i < MaxLayers; i++ {
digest.Reset()
digest.Write(StackedNulPadding[i-1]) // yes, got to...
digest.Write(StackedNulPadding[i-1]) // ...do it twice
StackedNulPadding[i] = digest.Sum(make([]byte, 0, sha256.Size))
StackedNulPadding[i][31] &= 0x3F
}
}
// createPath creates a directory path and returns the full file path by joining the directory path with the file name.
// It takes the directory path and the file name as input.
// It returns the full file path.
func createPath(filePath string, fileName string) string {
os.MkdirAll(filePath, 0o775)
return path.Join(filePath, fileName)
}
// loadCommP loads CommP values from a cache file.
// It takes the cache file path as input.
// It returns a map containing CommP values and an error if any.
func loadCommP(cachePath string) (*map[string]uint64, error) {
commp := map[string]uint64{}
if err := utils.ReadGob(cachePath, &commp); err != nil {
return nil, err
}
return &commp, nil
}
// sortCommPSlices sorts CommP slices and returns them along with their corresponding sizes.
// It takes a map containing CommP values as input.
// It returns slices of CommP values and their corresponding sizes.
func sortCommPSlices(c map[string]uint64) ([][]byte, []uint64) {
commp := make([][]byte, 0, len(c))
for k := range c {
commp = append(commp, []byte(k))
}
sort.Slice(commp, func(i, j int) bool {
return bytes.Compare(commp[i], commp[j]) < 0
})
size := make([]uint64, 0, len(commp))
for _, v := range commp {
size = append(size, c[string(v)])
}
return commp, size
}