Commit 5e835a05 authored by laser's avatar laser
Browse files

feat(updates to conform to new API)

- sector id is now a uint64
- generate_post accepts faults instead of producing them
- generate_post produces a single proof instead of a concatenation of proofs
- verify_post accepts a single proof
parent 8060bb80
...@@ -71,7 +71,7 @@ func VerifySeal( ...@@ -71,7 +71,7 @@ func VerifySeal(
commD [CommitmentBytesLen]byte, commD [CommitmentBytesLen]byte,
commRStar [CommitmentBytesLen]byte, commRStar [CommitmentBytesLen]byte,
proverID [31]byte, proverID [31]byte,
sectorID [31]byte, sectorID uint64,
proof []byte, proof []byte,
) (bool, error) { ) (bool, error) {
defer elapsed("VerifySeal")() defer elapsed("VerifySeal")()
...@@ -91,9 +91,6 @@ func VerifySeal( ...@@ -91,9 +91,6 @@ func VerifySeal(
proverIDCBytes := C.CBytes(proverID[:]) proverIDCBytes := C.CBytes(proverID[:])
defer C.free(proverIDCBytes) defer C.free(proverIDCBytes)
sectorIDCbytes := C.CBytes(sectorID[:])
defer C.free(sectorIDCbytes)
// a mutable pointer to a VerifySealResponse C-struct // a mutable pointer to a VerifySealResponse C-struct
resPtr := C.sector_builder_ffi_verify_seal( resPtr := C.sector_builder_ffi_verify_seal(
C.uint64_t(sectorSize), C.uint64_t(sectorSize),
...@@ -101,7 +98,7 @@ func VerifySeal( ...@@ -101,7 +98,7 @@ func VerifySeal(
(*[CommitmentBytesLen]C.uint8_t)(commDCBytes), (*[CommitmentBytesLen]C.uint8_t)(commDCBytes),
(*[CommitmentBytesLen]C.uint8_t)(commRStarCBytes), (*[CommitmentBytesLen]C.uint8_t)(commRStarCBytes),
(*[31]C.uint8_t)(proverIDCBytes), (*[31]C.uint8_t)(proverIDCBytes),
(*[31]C.uint8_t)(sectorIDCbytes), C.uint64_t(sectorID),
(*C.uint8_t)(proofCBytes), (*C.uint8_t)(proofCBytes),
C.size_t(len(proof)), C.size_t(len(proof)),
) )
...@@ -119,17 +116,13 @@ func VerifySeal( ...@@ -119,17 +116,13 @@ func VerifySeal(
func VerifyPoSt( func VerifyPoSt(
sectorSize uint64, sectorSize uint64,
sortedCommRs [][CommitmentBytesLen]byte, sortedCommRs [][CommitmentBytesLen]byte,
challengeSeed [CommitmentBytesLen]byte, sortedSectorIds []uint64, // TODO: Use a better type, combining with sortedCommRs
proofs [][]byte, challengeSeed [32]byte,
proof []byte,
faults []uint64, faults []uint64,
) (bool, error) { ) (bool, error) {
defer elapsed("VerifyPoSt")() defer elapsed("VerifyPoSt")()
// validate verification request
if len(proofs) == 0 {
return false, errors.New("must provide at least one proof to verify")
}
// CommRs must be provided to C.verify_post in the same order that they were // CommRs must be provided to C.verify_post in the same order that they were
// provided to the C.generate_post // provided to the C.generate_post
commRs := sortedCommRs commRs := sortedCommRs
...@@ -147,24 +140,28 @@ func VerifyPoSt( ...@@ -147,24 +140,28 @@ func VerifyPoSt(
challengeSeedCBytes := C.CBytes(challengeSeed[:]) challengeSeedCBytes := C.CBytes(challengeSeed[:])
defer C.free(challengeSeedCBytes) defer C.free(challengeSeedCBytes)
proofPartitions, proofsPtr, proofsLen := cPoStProofs(proofs) proofCBytes := C.CBytes(proof)
defer C.free(unsafe.Pointer(proofsPtr)) defer C.free(proofCBytes)
// allocate fixed-length array of uint64s in C heap // allocate fixed-length array of uint64s in C heap
sectorIdsPtr, sectorIdsSize := cUint64s(sortedSectorIds)
defer C.free(unsafe.Pointer(sectorIdsPtr))
faultsPtr, faultsSize := cUint64s(faults) faultsPtr, faultsSize := cUint64s(faults)
defer C.free(unsafe.Pointer(faultsPtr)) defer C.free(unsafe.Pointer(faultsPtr))
// a mutable pointer to a VerifyPoStResponse C-struct // a mutable pointer to a VerifyPoStResponse C-struct
resPtr := C.sector_builder_ffi_verify_post( resPtr := C.sector_builder_ffi_verify_post(
C.uint64_t(sectorSize), C.uint64_t(sectorSize),
proofPartitions,
(*C.uint8_t)(flattenedCommRsCBytes),
C.size_t(len(flattened)),
(*[CommitmentBytesLen]C.uint8_t)(challengeSeedCBytes), (*[CommitmentBytesLen]C.uint8_t)(challengeSeedCBytes),
proofsPtr, sectorIdsPtr,
proofsLen, sectorIdsSize,
faultsPtr, faultsPtr,
faultsSize, faultsSize,
(*C.uint8_t)(flattenedCommRsCBytes),
C.size_t(len(flattened)),
(*C.uint8_t)(proofCBytes),
C.size_t(len(proof)),
) )
defer C.sector_builder_ffi_destroy_verify_post_response(resPtr) defer C.sector_builder_ffi_destroy_verify_post_response(resPtr)
...@@ -210,7 +207,7 @@ func InitSectorBuilder( ...@@ -210,7 +207,7 @@ func InitSectorBuilder(
cSealedSectorDir := C.CString(sealedSectorDir) cSealedSectorDir := C.CString(sealedSectorDir)
defer C.free(unsafe.Pointer(cSealedSectorDir)) defer C.free(unsafe.Pointer(cSealedSectorDir))
class, err := cSectorClass(sectorSize, poRepProofPartitions, poStProofPartitions) class, err := cSectorClass(sectorSize, poRepProofPartitions)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "failed to get sector class") return nil, errors.Wrap(err, "failed to get sector class")
} }
...@@ -420,7 +417,8 @@ func GeneratePoSt( ...@@ -420,7 +417,8 @@ func GeneratePoSt(
sectorBuilderPtr unsafe.Pointer, sectorBuilderPtr unsafe.Pointer,
sortedCommRs [][CommitmentBytesLen]byte, sortedCommRs [][CommitmentBytesLen]byte,
challengeSeed [CommitmentBytesLen]byte, challengeSeed [CommitmentBytesLen]byte,
) ([][]byte, []uint64, error) { faults []uint64,
) ([]byte, error) {
defer elapsed("GeneratePoSt")() defer elapsed("GeneratePoSt")()
// flattening the byte slice makes it easier to copy into the C heap // flattening the byte slice makes it easier to copy into the C heap
...@@ -436,25 +434,25 @@ func GeneratePoSt( ...@@ -436,25 +434,25 @@ func GeneratePoSt(
challengeSeedPtr := unsafe.Pointer(&(challengeSeed)[0]) challengeSeedPtr := unsafe.Pointer(&(challengeSeed)[0])
faultsPtr, faultsSize := cUint64s(faults)
defer C.free(unsafe.Pointer(faultsPtr))
// a mutable pointer to a GeneratePoStResponse C-struct // a mutable pointer to a GeneratePoStResponse C-struct
resPtr := C.sector_builder_ffi_generate_post( resPtr := C.sector_builder_ffi_generate_post(
(*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr), (*C.sector_builder_ffi_SectorBuilder)(sectorBuilderPtr),
(*C.uint8_t)(cflattened), (*C.uint8_t)(cflattened),
C.size_t(len(flattened)), C.size_t(len(flattened)),
(*[CommitmentBytesLen]C.uint8_t)(challengeSeedPtr), (*[CommitmentBytesLen]C.uint8_t)(challengeSeedPtr),
faultsPtr,
faultsSize,
) )
defer C.sector_builder_ffi_destroy_generate_post_response(resPtr) defer C.sector_builder_ffi_destroy_generate_post_response(resPtr)
if resPtr.status_code != 0 { if resPtr.status_code != 0 {
return nil, nil, errors.New(C.GoString(resPtr.error_msg)) return nil, errors.New(C.GoString(resPtr.error_msg))
}
proofs, err := goPoStProofs(resPtr.proof_partitions, resPtr.flattened_proofs_ptr, resPtr.flattened_proofs_len)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to convert to []PoStProof")
} }
return proofs, goUint64s(resPtr.faults_ptr, resPtr.faults_len), nil return goBytes(resPtr.proof_ptr, resPtr.proof_len), nil
} }
// VerifyPieceInclusionProof returns true if the piece inclusion proof is valid // VerifyPieceInclusionProof returns true if the piece inclusion proof is valid
......
...@@ -64,7 +64,7 @@ func TestSectorBuilderLifecycle(t *testing.T) { ...@@ -64,7 +64,7 @@ func TestSectorBuilderLifecycle(t *testing.T) {
require.Equal(t, 1, len(status.Pieces), "expected to see the one piece we added") require.Equal(t, 1, len(status.Pieces), "expected to see the one piece we added")
// verify the seal proof // verify the seal proof
isValid, err := sb.VerifySeal(1024, status.CommR, status.CommD, status.CommRStar, [31]byte{}, sectorIdAsBytes(sectorID), status.Proof) isValid, err := sb.VerifySeal(1024, status.CommR, status.CommD, status.CommRStar, [31]byte{}, sectorID, status.Proof)
require.NoError(t, err) require.NoError(t, err)
require.True(t, isValid) require.True(t, isValid)
...@@ -74,11 +74,11 @@ func TestSectorBuilderLifecycle(t *testing.T) { ...@@ -74,11 +74,11 @@ func TestSectorBuilderLifecycle(t *testing.T) {
require.True(t, isValid) require.True(t, isValid)
// generate a PoSt // generate a PoSt
proofs, faults, err := sb.GeneratePoSt(ptr, [][32]byte{status.CommR}, [32]byte{}) proofs, err := sb.GeneratePoSt(ptr, [][32]byte{status.CommR}, [32]byte{}, []uint64{})
require.NoError(t, err) require.NoError(t, err)
// verify the PoSt // verify the PoSt
isValid, err = sb.VerifyPoSt(1024, [][32]byte{status.CommR}, [32]byte{}, proofs, faults) isValid, err = sb.VerifyPoSt(1024, [][32]byte{status.CommR}, []uint64{status.SectorID}, [32]byte{}, proofs, []uint64{})
require.NoError(t, err) require.NoError(t, err)
require.True(t, isValid) require.True(t, isValid)
......
...@@ -16,22 +16,6 @@ import "C" ...@@ -16,22 +16,6 @@ import "C"
// with the number of partitions used when creating that proof. // with the number of partitions used when creating that proof.
const SingleProofPartitionProofLen = 192 const SingleProofPartitionProofLen = 192
func cPoStProofs(src [][]byte) (C.uint8_t, *C.uint8_t, C.size_t) {
proofSize := len(src[0])
flattenedLen := C.size_t(proofSize * len(src))
// flattening the byte slice makes it easier to copy into the C heap
flattened := make([]byte, flattenedLen)
for idx, proof := range src {
copy(flattened[(proofSize*idx):(proofSize*(1+idx))], proof[:])
}
proofPartitions := proofSize / SingleProofPartitionProofLen
return C.uint8_t(proofPartitions), (*C.uint8_t)(C.CBytes(flattened)), flattenedLen
}
func cUint64s(src []uint64) (*C.uint64_t, C.size_t) { func cUint64s(src []uint64) (*C.uint64_t, C.size_t) {
srcCSizeT := C.size_t(len(src)) srcCSizeT := C.size_t(len(src))
...@@ -47,11 +31,10 @@ func cUint64s(src []uint64) (*C.uint64_t, C.size_t) { ...@@ -47,11 +31,10 @@ func cUint64s(src []uint64) (*C.uint64_t, C.size_t) {
return (*C.uint64_t)(cUint64s), srcCSizeT return (*C.uint64_t)(cUint64s), srcCSizeT
} }
func cSectorClass(sectorSize uint64, poRepProofPartitions uint8, poStProofPartitions uint8) (C.sector_builder_ffi_FFISectorClass, error) { func cSectorClass(sectorSize uint64, poRepProofPartitions uint8) (C.sector_builder_ffi_FFISectorClass, error) {
return C.sector_builder_ffi_FFISectorClass{ return C.sector_builder_ffi_FFISectorClass{
sector_size: C.uint64_t(sectorSize), sector_size: C.uint64_t(sectorSize),
porep_proof_partitions: C.uint8_t(poRepProofPartitions), porep_proof_partitions: C.uint8_t(poRepProofPartitions),
post_proof_partitions: C.uint8_t(poStProofPartitions),
}, nil }, nil
} }
...@@ -138,20 +121,6 @@ func goPieceMetadata(src *C.sector_builder_ffi_FFIPieceMetadata, size C.size_t) ...@@ -138,20 +121,6 @@ func goPieceMetadata(src *C.sector_builder_ffi_FFIPieceMetadata, size C.size_t)
return ps, nil return ps, nil
} }
func goPoStProofs(partitions C.uint8_t, src *C.uint8_t, size C.size_t) ([][]byte, error) {
tmp := goBytes(src, size)
arraySize := len(tmp)
chunkSize := int(partitions) * SingleProofPartitionProofLen
out := make([][]byte, arraySize/chunkSize)
for i := 0; i < len(out); i++ {
out[i] = append([]byte{}, tmp[i*chunkSize:(i+1)*chunkSize]...)
}
return out, nil
}
func goUint64s(src *C.uint64_t, size C.size_t) []uint64 { func goUint64s(src *C.uint64_t, size C.size_t) []uint64 {
out := make([]uint64, size) out := make([]uint64, size)
if src != nil { if src != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment