Skip to content

Commit caec40d

Browse files
ortyomkaturuslan
andauthored
Proofs v24 (#162)
* Updated filecoin-ffi version Signed-off-by: artyom-yurin <[email protected]> * Updated bls-provider Signed-off-by: artyom-yurin <[email protected]> * Added CURL for downloading proofs Returned proof param provider Renamed dir of header Signed-off-by: artyom-yurin <[email protected]> * Added error indicator in proof param provider Added getSectorSize function for getting actual size from ProofType Signed-off-by: artyom-yurin <[email protected]> * Added util functions Signed-off-by: artyom-yurin <[email protected]> * Proofs v24: changes (#163) Signed-off-by: turuslan <[email protected]> Co-authored-by: Ruslan Tushov <[email protected]>
1 parent 1d65126 commit caec40d

32 files changed

+1255
-463
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
uses: actions/[email protected]
4040
env:
4141
cache-name: cache-filecoin-ffi-proofs
42-
version: v23
42+
version: v24
4343
with:
4444
path: /var/tmp/filecoin-proof-parameters
4545
key: build-${{ env.cache-name }}-${{ env.version }}

.github/workflows/lsan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/[email protected]
1919
env:
2020
cache-name: cache-filecoin-ffi-proofs
21-
version: v23
21+
version: v24
2222
with:
2323
path: /var/tmp/filecoin-proof-parameters
2424
key: build-${{ env.cache-name }}-${{ env.version }}

.github/workflows/tsan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/[email protected]
1919
env:
2020
cache-name: cache-filecoin-ffi-proofs
21-
version: v23
21+
version: v24
2222
with:
2323
path: /var/tmp/filecoin-proof-parameters
2424
key: build-${{ env.cache-name }}-${{ env.version }}

.github/workflows/ubsan.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/[email protected]
1919
env:
2020
cache-name: cache-filecoin-ffi-proofs
21-
version: v23
21+
version: v24
2222
with:
2323
path: /var/tmp/filecoin-proof-parameters
2424
key: build-${{ env.cache-name }}-${{ env.version }}

cmake/Hunter/config.cmake

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ hunter_config(
1010
CMAKE_ARGS "CMAKE_CXX_FLAGS=-Wno-deprecated-copy"
1111
)
1212

13+
hunter_config(
14+
CURL
15+
VERSION 7.60.0-p2
16+
CMAKE_ARGS "HTTP_ONLY=ON"
17+
)
18+
1319
hunter_config(
1420
spdlog
1521
URL https://github.com/gabime/spdlog/archive/v1.4.2.zip

cmake/dependencies.cmake

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ find_package(Microsoft.GSL CONFIG REQUIRED)
1818
hunter_add_package(OpenSSL)
1919
find_package(OpenSSL REQUIRED)
2020

21+
# https://hunter.readthedocs.io/en/latest/packages/pkg/CURL.html#pkg-curl
22+
hunter_add_package(CURL)
23+
find_package(CURL CONFIG REQUIRED)
24+
2125
# https://developers.google.com/protocol-buffers/
2226
hunter_add_package(Protobuf)
2327
find_package(Protobuf CONFIG REQUIRED)

core/common/blob.hpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
#include <boost/format.hpp>
1212
#include <boost/functional/hash.hpp>
13+
1314
#include "common/hexutil.hpp"
15+
#include "common/span.hpp"
1416

1517
namespace fc::common {
1618

@@ -74,14 +76,7 @@ namespace fc::common {
7476
* @return result containing Blob object if string has proper size
7577
*/
7678
static outcome::result<Blob<size_>> fromString(std::string_view data) {
77-
if (data.size() != size_) {
78-
return BlobError::INCORRECT_LENGTH;
79-
}
80-
81-
Blob<size_> b;
82-
std::copy(data.begin(), data.end(), b.begin());
83-
84-
return b;
79+
return fromSpan(span::cbytes(data));
8580
}
8681

8782
/**

core/common/ffi.hpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef CPP_FILECOIN_CORE_COMMON_FFI_HPP
7+
#define CPP_FILECOIN_CORE_COMMON_FFI_HPP
8+
9+
#include <memory>
10+
11+
#include "common/blob.hpp"
12+
13+
namespace fc::common::ffi {
14+
template <typename T, typename D>
15+
auto wrap(T *ptr, D deleter) {
16+
return std::unique_ptr<T, D>(ptr, deleter);
17+
}
18+
19+
template <size_t size>
20+
auto array(const uint8_t (&rhs)[size]) {
21+
Blob<size> lhs;
22+
std::copy(std::begin(rhs), std::end(rhs), std::begin(lhs));
23+
return lhs;
24+
}
25+
26+
template <size_t size>
27+
void array(uint8_t (&lhs)[size], const std::array<uint8_t, size> rhs) {
28+
std::copy(std::begin(rhs), std::end(rhs), std::begin(lhs));
29+
}
30+
} // namespace fc::common::ffi
31+
32+
#endif // CPP_FILECOIN_CORE_COMMON_FFI_HPP

core/common/span.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace fc::common::span {
1212
template <typename To, typename From>
1313
constexpr auto cast(gsl::span<From> span) {
14+
static_assert(sizeof(To) == 1);
1415
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1516
return gsl::make_span(reinterpret_cast<To *>(span.data()), span.size());
1617
}

core/crypto/bls/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ add_library(bls_provider
88
)
99

1010
target_link_libraries(bls_provider
11+
blob
1112
filecoin_ffi
1213
outcome
1314
)

core/crypto/bls/bls_provider.hpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
#ifndef CRYPTO_BLS_PROVIDER_HPP
77
#define CRYPTO_BLS_PROVIDER_HPP
88

9-
#include <vector>
10-
119
#include <gsl/span>
10+
1211
#include "crypto/bls/bls_types.hpp"
1312

1413
namespace fc::crypto::bls {
@@ -59,7 +58,7 @@ namespace fc::crypto::bls {
5958
* @return aggregated single signature
6059
*/
6160
virtual outcome::result<Signature> aggregateSignatures(
62-
const std::vector<Signature> &signatures) const = 0;
61+
gsl::span<const Signature> signatures) const = 0;
6362
};
6463
} // namespace fc::crypto::bls
6564

core/crypto/bls/impl/bls_provider_impl.cpp

+34-61
Original file line numberDiff line numberDiff line change
@@ -5,106 +5,79 @@
55

66
#include "crypto/bls/impl/bls_provider_impl.hpp"
77

8-
#include <filecoin-ffi/filecoin.h>
9-
#include <gsl/gsl_util>
8+
#include <filecoin-ffi/filcrypto.h>
9+
10+
#include "common/ffi.hpp"
11+
#include "common/span.hpp"
1012

1113
namespace fc::crypto::bls {
14+
namespace ffi = common::ffi;
15+
1216
outcome::result<KeyPair> BlsProviderImpl::generateKeyPair() const {
13-
KeyPair key_pair{};
14-
PrivateKeyGenerateResponse *private_key_response = private_key_generate();
15-
if (private_key_response == nullptr) {
17+
auto response{ffi::wrap(fil_private_key_generate(),
18+
fil_destroy_private_key_generate_response)};
19+
if (response == nullptr) {
1620
return Errors::KeyPairGenerationFailed;
1721
}
18-
auto free_private_response = gsl::finally([private_key_response]() {
19-
destroy_private_key_generate_response(private_key_response);
20-
});
21-
std::move(std::begin(private_key_response->private_key),
22-
std::end(private_key_response->private_key),
23-
key_pair.private_key.begin());
24-
OUTCOME_TRY(public_key, derivePublicKey(key_pair.private_key));
25-
key_pair.public_key = public_key;
26-
return key_pair;
22+
auto private_key = ffi::array(response->private_key.inner);
23+
OUTCOME_TRY(public_key, derivePublicKey(private_key));
24+
return KeyPair{private_key, public_key};
2725
}
2826

2927
outcome::result<PublicKey> BlsProviderImpl::derivePublicKey(
3028
const PrivateKey &key) const {
31-
PublicKey public_key;
32-
PrivateKeyPublicKeyResponse *response = private_key_public_key(key.data());
29+
auto response{ffi::wrap(fil_private_key_public_key(key.data()),
30+
fil_destroy_private_key_public_key_response)};
3331
if (response == nullptr) {
3432
return Errors::InvalidPrivateKey;
3533
}
36-
auto free_response = gsl::finally(
37-
[response]() { destroy_private_key_public_key_response(response); });
38-
std::move(std::begin(response->public_key),
39-
std::end(response->public_key),
40-
public_key.begin());
41-
return public_key;
34+
return ffi::array(response->public_key.inner);
4235
}
4336

4437
outcome::result<Signature> BlsProviderImpl::sign(
4538
gsl::span<const uint8_t> message, const PrivateKey &key) const {
46-
Signature signature;
47-
PrivateKeySignResponse *response =
48-
private_key_sign(key.data(), message.data(), message.size());
39+
auto response{ffi::wrap(
40+
fil_private_key_sign(key.data(), message.data(), message.size()),
41+
fil_destroy_private_key_sign_response)};
4942
if (response == nullptr) {
5043
return Errors::SignatureGenerationFailed;
5144
}
52-
auto free_response = gsl::finally(
53-
[response]() { destroy_private_key_sign_response(response); });
54-
std::move(std::begin(response->signature),
55-
std::end(response->signature),
56-
signature.begin());
57-
return signature;
45+
return ffi::array(response->signature.inner);
5846
}
5947

6048
outcome::result<bool> BlsProviderImpl::verifySignature(
6149
gsl::span<const uint8_t> message,
6250
const Signature &signature,
6351
const PublicKey &key) const {
6452
OUTCOME_TRY(digest, generateHash(message));
65-
if (verify(signature.data(),
66-
digest.data(),
67-
digest.size(),
68-
key.data(),
69-
key.size())
70-
!= 0) {
71-
return true;
72-
}
73-
return false;
53+
return fil_verify(signature.data(),
54+
digest.data(),
55+
digest.size(),
56+
key.data(),
57+
key.size())
58+
> 0;
7459
}
7560

7661
outcome::result<Digest> BlsProviderImpl::generateHash(
7762
gsl::span<const uint8_t> message) {
78-
Digest digest;
79-
HashResponse *response = hash(message.data(), message.size());
63+
auto response{ffi::wrap(fil_hash(message.data(), message.size()),
64+
fil_destroy_hash_response)};
8065
if (response == nullptr) {
8166
return Errors::InternalError;
8267
}
83-
auto free_response =
84-
gsl::finally([response]() { destroy_hash_response(response); });
85-
std::move(std::begin(response->digest),
86-
std::end(response->digest),
87-
digest.begin());
88-
return digest;
68+
return ffi::array(response->digest.inner);
8969
}
9070

9171
outcome::result<Signature> BlsProviderImpl::aggregateSignatures(
92-
const std::vector<Signature> &signatures) const {
93-
Signature signature;
94-
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
95-
const uint8_t *flat_bytes =
96-
reinterpret_cast<const uint8_t *>(signatures.data());
97-
size_t flat_size = sizeof(Signature) * signatures.size();
98-
AggregateResponse *response = ::aggregate(flat_bytes, flat_size);
72+
gsl::span<const Signature> signatures) const {
73+
auto response{ffi::wrap(
74+
fil_aggregate(common::span::cast<const uint8_t>(signatures).data(),
75+
signatures.size_bytes()),
76+
fil_destroy_aggregate_response)};
9977
if (response == nullptr) {
10078
return Errors::InternalError;
10179
}
102-
auto free_response =
103-
gsl::finally([response]() { destroy_aggregate_response(response); });
104-
std::move(std::begin(response->signature),
105-
std::end(response->signature),
106-
signature.begin());
107-
return signature;
80+
return ffi::array(response->signature.inner);
10881
}
10982
}; // namespace fc::crypto::bls
11083

core/crypto/bls/impl/bls_provider_impl.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace fc::crypto::bls {
2424
const PublicKey &key) const override;
2525

2626
outcome::result<Signature> aggregateSignatures(
27-
const std::vector<Signature> &signatures) const override;
27+
gsl::span<const Signature> signatures) const override;
2828

2929
private:
3030
/**

core/primitives/cid/comm_cid.cpp

+9-21
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,21 @@
44
*/
55

66
#include "primitives/cid/comm_cid.hpp"
7+
#include "common/outcome_throw.hpp"
78
#include "primitives/cid/comm_cid_errors.hpp"
89

910
namespace fc::common {
1011

1112
using libp2p::multi::HashType;
1213

13-
Comm cppCommitment(gsl::span<const uint8_t> bytes) {
14-
if (bytes.size() != 32) return {};
15-
Comm result;
16-
std::copy(bytes.begin(), bytes.end(), result.begin());
17-
return result;
18-
}
19-
2014
CID replicaCommitmentV1ToCID(gsl::span<const uint8_t> comm_r) {
21-
auto cid = commitmentToCID(comm_r, FilecoinHashType::FC_SEALED_V1);
22-
if (cid.has_error()) return CID();
23-
return cid.value();
15+
OUTCOME_EXCEPT(cid, commitmentToCID(comm_r, FC_SEALED_V1));
16+
return cid;
2417
}
2518

2619
CID dataCommitmentV1ToCID(gsl::span<const uint8_t> comm_d) {
27-
auto cid = commitmentToCID(comm_d, FC_UNSEALED_V1);
28-
if (cid.has_error()) return CID();
29-
return cid.value();
20+
OUTCOME_EXCEPT(cid, commitmentToCID(comm_d, FC_UNSEALED_V1));
21+
return cid;
3022
}
3123

3224
CID pieceCommitmentV1ToCID(gsl::span<const uint8_t> comm_p) {
@@ -58,25 +50,21 @@ namespace fc::common {
5850
if (static_cast<FilecoinHashType>(result.getType()) != FC_UNSEALED_V1) {
5951
return CommCidError::INVALID_HASH;
6052
}
61-
return cppCommitment(result.getHash());
53+
return Comm::fromSpan(result.getHash());
6254
}
6355

6456
outcome::result<Multihash> CIDToCommitment(const CID &cid) {
65-
OUTCOME_TRY(result,
66-
Multihash::createFromBytes(cid.content_address.toBuffer()));
67-
68-
if (!validFilecoinMultihash(result.getType())) {
57+
if (!validFilecoinMultihash(cid.content_address.getType())) {
6958
return CommCidError::INVALID_HASH;
7059
}
71-
72-
return std::move(result);
60+
return cid.content_address;
7361
}
7462

7563
outcome::result<Comm> CIDToReplicaCommitmentV1(const CID &cid) {
7664
OUTCOME_TRY(result, CIDToCommitment(cid));
7765
if (static_cast<FilecoinHashType>(result.getType()) != FC_SEALED_V1) {
7866
return CommCidError::INVALID_HASH;
7967
}
80-
return cppCommitment(result.getHash());
68+
return Comm::fromSpan(result.getHash());
8169
}
8270
} // namespace fc::common

core/primitives/cid/comm_cid.hpp

-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ namespace fc::common {
7676
outcome::result<CID> commitmentToCID(gsl::span<const uint8_t> commitment,
7777
FilecoinMultihashCode code);
7878

79-
Comm cppCommitment(gsl::span<const uint8_t> bytes);
80-
8179
outcome::result<Comm> CIDToPieceCommitmentV1(const CID &cid);
8280
outcome::result<Comm> CIDToReplicaCommitmentV1(const CID &cid);
8381

0 commit comments

Comments
 (0)