simple.go 3.04 KB
Newer Older
Jakub Sztandera's avatar
Jakub Sztandera committed
1
2
//+build cgo

3
4
5
6
7
8
9
10
11
12
13
14
package sectorbuilder

import (
	"context"
	"io"

	sectorbuilder "github.com/filecoin-project/filecoin-ffi"
	"go.opencensus.io/trace"

	"github.com/filecoin-project/go-address"
)

Jakub Sztandera's avatar
Jakub Sztandera committed
15
16
var _ Verifier = ProofVerifier

17
18
19
20
func (sb *SectorBuilder) SectorSize() uint64 {
	return sb.ssize
}

Jakub Sztandera's avatar
Jakub Sztandera committed
21
22
type proofVerifier struct{}

Łukasz Magiera's avatar
Łukasz Magiera committed
23
24
var ProofVerifier = proofVerifier{}

25
26
var UserBytesForSectorSize = sectorbuilder.GetMaxUserBytesPerStagedSector

Łukasz Magiera's avatar
Łukasz Magiera committed
27
func (proofVerifier) VerifySeal(sectorSize uint64, commR, commD []byte, proverID address.Address, ticket []byte, seed []byte, sectorID uint64, proof []byte) (bool, error) {
28
29
30
31
32
33
34
35
36
37
	var commRa, commDa, ticketa, seeda [32]byte
	copy(commRa[:], commR)
	copy(commDa[:], commD)
	copy(ticketa[:], ticket)
	copy(seeda[:], seed)
	proverIDa := addressToProverID(proverID)

	return sectorbuilder.VerifySeal(sectorSize, commRa, commDa, proverIDa, ticketa, seeda, sectorID, proof)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
38
func (proofVerifier) VerifyElectionPost(ctx context.Context, sectorSize uint64, sectorInfo SortedPublicSectorInfo, challengeSeed []byte, proof []byte, candidates []EPostCandidate, proverID address.Address) (bool, error) {
39
40
41
42
	challengeCount := ElectionPostChallengeCount(uint64(len(sectorInfo.Values())), 0)
	return verifyPost(ctx, sectorSize, sectorInfo, challengeCount, challengeSeed, proof, candidates, proverID)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
43
func (proofVerifier) VerifyFallbackPost(ctx context.Context, sectorSize uint64, sectorInfo SortedPublicSectorInfo, challengeSeed []byte, proof []byte, candidates []EPostCandidate, proverID address.Address, faults uint64) (bool, error) {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
	challengeCount := fallbackPostChallengeCount(uint64(len(sectorInfo.Values())), faults)
	return verifyPost(ctx, sectorSize, sectorInfo, challengeCount, challengeSeed, proof, candidates, proverID)
}

func verifyPost(ctx context.Context, sectorSize uint64, sectorInfo SortedPublicSectorInfo, challengeCount uint64, challengeSeed []byte, proof []byte, candidates []EPostCandidate, proverID address.Address) (bool, error) {
	var challengeSeeda [CommLen]byte
	copy(challengeSeeda[:], challengeSeed)

	_, span := trace.StartSpan(ctx, "VerifyPoSt")
	defer span.End()
	prover := addressToProverID(proverID)
	return sectorbuilder.VerifyPoSt(sectorSize, sectorInfo, challengeSeeda, challengeCount, proof, candidates, prover)
}

Łukasz Magiera's avatar
Łukasz Magiera committed
58
59
60
61
62
63
64
65
func newSortedPrivateSectorInfo(sectors []sectorbuilder.PrivateSectorInfo) SortedPrivateSectorInfo {
	return sectorbuilder.NewSortedPrivateSectorInfo(sectors...)
}

func NewSortedPublicSectorInfo(sectors []sectorbuilder.PublicSectorInfo) SortedPublicSectorInfo {
	return sectorbuilder.NewSortedPublicSectorInfo(sectors...)
}

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
func GeneratePieceCommitment(piece io.Reader, pieceSize uint64) (commP [CommLen]byte, err error) {
	f, werr, err := toReadableFile(piece, int64(pieceSize))
	if err != nil {
		return [32]byte{}, err
	}

	commP, err = sectorbuilder.GeneratePieceCommitmentFromFile(f, pieceSize)
	if err != nil {
		return [32]byte{}, err
	}

	return commP, werr()
}

func GenerateDataCommitment(ssize uint64, pieces []sectorbuilder.PublicPieceInfo) ([CommLen]byte, error) {
	return sectorbuilder.GenerateDataCommitment(ssize, pieces)
}