-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ffi: s/verifier/proofs, incorporate GenerateUnsealedCID
- Loading branch information
Showing
24 changed files
with
258 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package proofsffi | ||
|
||
import ( | ||
"math/bits" | ||
|
||
"github.com/ipfs/go-cid" | ||
|
||
ffi "github.com/filecoin-project/filecoin-ffi" | ||
"github.com/filecoin-project/go-commp-utils/zerocomm" | ||
"github.com/filecoin-project/go-state-types/abi" | ||
) | ||
|
||
func GenerateUnsealedCID(proofType abi.RegisteredSealProof, pieces []abi.PieceInfo) (cid.Cid, error) { | ||
ssize, err := proofType.SectorSize() | ||
if err != nil { | ||
return cid.Undef, err | ||
} | ||
|
||
pssize := abi.PaddedPieceSize(ssize) | ||
allPieces := make([]abi.PieceInfo, 0, len(pieces)) | ||
if len(pieces) == 0 { | ||
allPieces = append(allPieces, abi.PieceInfo{ | ||
Size: pssize, | ||
PieceCID: zerocomm.ZeroPieceCommitment(pssize.Unpadded()), | ||
}) | ||
} else { | ||
var sum abi.PaddedPieceSize | ||
|
||
padTo := func(pads []abi.PaddedPieceSize) { | ||
for _, p := range pads { | ||
allPieces = append(allPieces, abi.PieceInfo{ | ||
Size: p, | ||
PieceCID: zerocomm.ZeroPieceCommitment(p.Unpadded()), | ||
}) | ||
|
||
sum += p | ||
} | ||
} | ||
|
||
for _, p := range pieces { | ||
ps, _ := GetRequiredPadding(sum, p.Size) | ||
padTo(ps) | ||
|
||
allPieces = append(allPieces, p) | ||
sum += p.Size | ||
} | ||
|
||
ps, _ := GetRequiredPadding(sum, pssize) | ||
padTo(ps) | ||
} | ||
|
||
return ffi.GenerateUnsealedCID(proofType, allPieces) | ||
} | ||
|
||
func GetRequiredPadding(oldLength abi.PaddedPieceSize, newPieceLength abi.PaddedPieceSize) ([]abi.PaddedPieceSize, abi.PaddedPieceSize) { | ||
padPieces := make([]abi.PaddedPieceSize, 0) | ||
toFill := uint64(-oldLength % newPieceLength) | ||
|
||
n := bits.OnesCount64(toFill) | ||
var sum abi.PaddedPieceSize | ||
for i := 0; i < n; i++ { | ||
next := bits.TrailingZeros64(toFill) | ||
psize := uint64(1) << uint(next) | ||
toFill ^= psize | ||
|
||
padded := abi.PaddedPieceSize(psize) | ||
padPieces = append(padPieces, padded) | ||
sum += padded | ||
} | ||
|
||
return padPieces, sum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package proofsffi_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/ipfs/go-cid" | ||
"github.com/stretchr/testify/require" | ||
|
||
ffi "github.com/filecoin-project/filecoin-ffi" | ||
commpffi "github.com/filecoin-project/go-commp-utils/ffiwrapper" | ||
"github.com/filecoin-project/go-state-types/abi" | ||
|
||
proofsffi "github.com/filecoin-project/lotus/chain/proofs/ffi" | ||
) | ||
|
||
func TestGenerateUnsealedCID(t *testing.T) { | ||
pt := abi.RegisteredSealProof_StackedDrg2KiBV1 | ||
ups := int(abi.PaddedPieceSize(2048).Unpadded()) | ||
|
||
commP := func(b []byte) cid.Cid { | ||
pf, werr, err := commpffi.ToReadableFile(bytes.NewReader(b), int64(len(b))) | ||
require.NoError(t, err) | ||
|
||
c, err := ffi.GeneratePieceCIDFromFile(pt, pf, abi.UnpaddedPieceSize(len(b))) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, werr()) | ||
|
||
return c | ||
} | ||
|
||
testCommEq := func(name string, in [][]byte, expect [][]byte) { | ||
t.Run(name, func(t *testing.T) { | ||
upi := make([]abi.PieceInfo, len(in)) | ||
for i, b := range in { | ||
upi[i] = abi.PieceInfo{ | ||
Size: abi.UnpaddedPieceSize(len(b)).Padded(), | ||
PieceCID: commP(b), | ||
} | ||
} | ||
|
||
sectorPi := []abi.PieceInfo{ | ||
{ | ||
Size: 2048, | ||
PieceCID: commP(bytes.Join(expect, nil)), | ||
}, | ||
} | ||
|
||
expectCid, err := proofsffi.GenerateUnsealedCID(pt, sectorPi) | ||
require.NoError(t, err) | ||
|
||
actualCid, err := proofsffi.GenerateUnsealedCID(pt, upi) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, expectCid, actualCid) | ||
}) | ||
} | ||
|
||
barr := func(b byte, den int) []byte { | ||
return bytes.Repeat([]byte{b}, ups/den) | ||
} | ||
|
||
// 0000 | ||
testCommEq("zero", | ||
nil, | ||
[][]byte{barr(0, 1)}, | ||
) | ||
|
||
// 1111 | ||
testCommEq("one", | ||
[][]byte{barr(1, 1)}, | ||
[][]byte{barr(1, 1)}, | ||
) | ||
|
||
// 11 00 | ||
testCommEq("one|2", | ||
[][]byte{barr(1, 2)}, | ||
[][]byte{barr(1, 2), barr(0, 2)}, | ||
) | ||
|
||
// 1 0 00 | ||
testCommEq("one|4", | ||
[][]byte{barr(1, 4)}, | ||
[][]byte{barr(1, 4), barr(0, 4), barr(0, 2)}, | ||
) | ||
|
||
// 11 2 0 | ||
testCommEq("one|2-two|4", | ||
[][]byte{barr(1, 2), barr(2, 4)}, | ||
[][]byte{barr(1, 2), barr(2, 4), barr(0, 4)}, | ||
) | ||
|
||
// 1 0 22 | ||
testCommEq("one|4-two|2", | ||
[][]byte{barr(1, 4), barr(2, 2)}, | ||
[][]byte{barr(1, 4), barr(0, 4), barr(2, 2)}, | ||
) | ||
|
||
// 1 0 22 0000 | ||
testCommEq("one|8-two|4", | ||
[][]byte{barr(1, 8), barr(2, 4)}, | ||
[][]byte{barr(1, 8), barr(0, 8), barr(2, 4), barr(0, 2)}, | ||
) | ||
|
||
// 11 2 0 0000 | ||
testCommEq("one|4-two|8", | ||
[][]byte{barr(1, 4), barr(2, 8)}, | ||
[][]byte{barr(1, 4), barr(2, 8), barr(0, 8), barr(0, 2)}, | ||
) | ||
|
||
// 1 0 22 3 0 00 4444 5 0 00 | ||
testCommEq("one|16-two|8-three|16-four|4-five|16", | ||
[][]byte{barr(1, 16), barr(2, 8), barr(3, 16), barr(4, 4), barr(5, 16)}, | ||
[][]byte{barr(1, 16), barr(0, 16), barr(2, 8), barr(3, 16), barr(0, 16), barr(0, 8), barr(4, 4), barr(5, 16), barr(0, 16), barr(0, 8)}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package verifier | ||
package proofs | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.