Commit 08bc3a52 authored by 董子豪's avatar 董子豪

rust-proofs-api

parent b5f12d28
version: 2.1
jobs:
ensure_groth_parameters_and_keys_linux:
docker:
- image: filecoin/rust:latest
working_directory: /mnt/crate
resource_class: 2xlarge+
steps:
- configure_environment_variables
- checkout
- ensure_filecoin_parameters
cargo_fetch:
docker:
- image: filecoin/rust:latest
working_directory: /mnt/crate
resource_class: 2xlarge+
steps:
- configure_environment_variables
- checkout
- run:
name: Install requirements
command: apt-get install sudo -yqq
- run:
name: Install hwloc 2.3.0
command: |
cd /tmp
curl https://download.open-mpi.org/release/hwloc/v2.3/hwloc-2.3.0.tar.gz --location --output /tmp/hwloc-2.3.0.tar.gz
tar xzvf hwloc-2.3.0.tar.gz
cd hwloc-2.3.0
./configure
make
sudo make install
- run:
name: Calculate dependencies
command: cargo generate-lockfile
- restore_cache:
keys:
- cargo-v13-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
- run: rustup install $(cat rust-toolchain)
- run: rustup default $(cat rust-toolchain)
- run: rustup component add rustfmt-preview
- run: rustup component add clippy-preview
- run: cargo update
- run: cargo fetch
- run: rustc +$(cat rust-toolchain) --version
- persist_to_workspace:
root: "."
paths:
- Cargo.lock
- save_cache:
key: cargo-v13-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
paths:
- /root/.cargo
- /root/.rustup
test_pairing:
docker:
- image: filecoin/rust:latest
working_directory: /mnt/crate
resource_class: 2xlarge+
steps:
- configure_environment_variables
- checkout
- attach_workspace:
at: "."
- restore_cache:
keys:
- cargo-v13-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
- run:
name: Test (pairing)
command: cargo +$(cat rust-toolchain) test --verbose
no_output_timeout: 15m
test_blst:
docker:
- image: filecoin/rust:latest
working_directory: /mnt/crate
resource_class: 2xlarge+
steps:
- configure_environment_variables
- checkout
- attach_workspace:
at: "."
- restore_cache:
keys:
- cargo-v13-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
- run:
name: Test (blst)
command: cargo +$(cat rust-toolchain) test --verbose --no-default-features --features blst
no_output_timeout: 15m
rustfmt:
docker:
- image: filecoin/rust:latest
working_directory: /mnt/crate
resource_class: 2xlarge
steps:
- configure_environment_variables
- checkout
- attach_workspace:
at: "."
- restore_cache:
keys:
- cargo-v13-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
- run:
name: Run cargo fmt
command: cargo fmt --all -- --check
clippy:
docker:
- image: filecoin/rust:latest
working_directory: /mnt/crate
resource_class: 2xlarge
steps:
- configure_environment_variables
- checkout
- attach_workspace:
at: "."
- restore_cache:
keys:
- cargo-v13-{{ checksum "rust-toolchain" }}-{{ checksum "Cargo.toml" }}-{{ checksum "Cargo.lock" }}-{{ arch }}
- run:
name: Run cargo clippy
command: cargo clippy --workspace
test_darwin_pairing:
macos:
xcode: "10.0.0"
working_directory: ~/crate
resource_class: large
steps:
- configure_environment_variables
- checkout
- run:
name: Install other dependencies with Homebrew
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install hwloc
- run:
name: Install Rust
command: |
curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: rustup install $(cat rust-toolchain)
- run: rustup default $(cat rust-toolchain)
- run: cargo update
- run: cargo fetch
- run:
name: Test (Darwin, pairing)
command: cargo +$(cat rust-toolchain) test --release --verbose
no_output_timeout: 15m
test_darwin_blst:
macos:
xcode: "10.0.0"
working_directory: ~/crate
resource_class: large
steps:
- configure_environment_variables
- checkout
- run:
name: Install other dependencies with Homebrew
command: HOMEBREW_NO_AUTO_UPDATE=1 brew install hwloc
- run:
name: Install Rust
command: |
curl https://sh.rustup.rs -sSf | sh -s -- -y
- run: rustup install $(cat rust-toolchain)
- run: rustup default $(cat rust-toolchain)
- run: cargo update
- run: cargo fetch
- run:
name: Test (Darwin, blst)
command: cargo +$(cat rust-toolchain) test --release --verbose --no-default-features --features blst
no_output_timeout: 15m
commands:
configure_environment_variables:
steps:
- run:
name: Configure environment variables
command: |
echo 'export FIL_PROOFS_PARAMETER_CACHE="${HOME}/filecoin-proof-parameters/"' >> $BASH_ENV
echo 'export PATH="${HOME}/.cargo/bin:${PATH}"' >> $BASH_ENV
echo 'export RUST_LOG=info' >> $BASH_ENV
workflows:
version: 2.1
test_all:
jobs:
- cargo_fetch
- rustfmt:
requires:
- cargo_fetch
- clippy:
requires:
- cargo_fetch
- test_pairing:
requires:
- cargo_fetch
- test_blst:
requires:
- cargo_fetch
- test_darwin_pairing
- test_darwin_blst
# Changelog
All notable changes to filecoin-proofs-api will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://book.async.rs/overview/stability-guarantees.html).
## [Unreleased]
## [8.0.1] - 2021-06-09
- Updates for finalizing snarkpack support [#57](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/57)
## [8.0.0] - 2021-06-01
- Integrate Proof Aggregation API [#51](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/51)
## [7.0.0] - 2021-04-28
- Improve RAM usage during unseal [#56](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/56)
- Update rust-toolchain to 1.51.0 [#55](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/55)
## [6.1.0] - 2021-03-11
- Update docs and changelog for release [#53](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/53)
- Improve docs and update to latest Proofs [#52](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/52)
## [6.0.0] - 2020-12-01
- Add support for updated proofs api_versioning [#49](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/49)
## [5.4.1] - 2020-11-02
- Upgrade bellperson to required version [#48](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/48)
## [5.4.0] - 2020-11-02
- Upgrade filecoin_proofs dependency to v5.4.0
- Add new v1_1 RegisteredSealProofs to increment porep_id [#47](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/47)
## [5.3.0] - 2020-10-29
- Upgrade filecoin_proofs dependency to v5.3.0
- Integrate blst backend [#46](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/46)
- Create SECURITY.md [#45](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/45)
## [5.2.0] - 2020-09-28
- Update rustc to 1.46.0 [#44](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/44)
- Expose distributed PoSt API from proofs [#41](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/41)
## [5.1.1] - 2020-09-08
- Export storage proofs errors [#40](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/40)
- Replace unwrap usage with expect [#39](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/39)
## [5.1.0] - 2020-08-13
- Upgrade filecoin_proofs dependency to v5.1.1 [#38](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/38)
## [5.0.0] - 2020-08-11
- Upgrade filecoin_proofs dependency to v5.0.0 (v28 parameters) [#37](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/37)
## [4.0.4] - 2020-07-28
- Add fauxrep2 API [#35](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/35)
- Don't start a new dev version after a release [#34](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/34)
## [4.0.3] - 2020-07-06
- Use correct fauxrep shape type parameters per sector size [#32](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/32)
## [4.0.2] - 2020-07-01
- Upgrade filecoin_proofs dependency to v4.0.3
- Support fauxrep API [#30](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/30)
## [4.0.1] - 2020-06-25
- Upgrade filecoin_proofs dependency to v4.0.2
- Add some tests to ensure param and vk methods work [#28](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/28)
## [4.0.0] - 2020-06-15
- Upgrade filecoin_proofs dependency to v4.0.0
## [3.0.0] - 2020-06-08
- Upgrade filecoin_proofs dependency to v3.0.0
- Construct porep_id per RegisteredSealProof [#24](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/24)
- Update toolchain to use rust stable [#20](https://github.com/filecoin-project/rust-filecoin-proofs-api/pull/20)
## [2.0.0] - 2020-05-27
- Add public access to unseal_range method
- Upgrade filecoin_proofs dependency to v2.0.0
## [1.0.0] - 2020-05-19
- Initial stable release
[Unreleased]: https://github.com/filecoin-project/rust-filecoin-proofs-api/compare/v8.0.1...HEAD
[8.0.1]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v8.0.1
[8.0.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v8.0.0
[7.0.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v7.0.0
[6.1.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v6.1.0
[6.0.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v6.0.0
[5.4.1]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v5.4.1
[5.4.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v5.4.0
[5.3.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v5.3.0
[5.2.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v5.2.0
[5.1.1]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v5.1.1
[5.1.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v5.1.0
[5.0.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v5.0.0
[4.0.4]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v4.0.4
[4.0.3]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v4.0.3
[4.0.2]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v4.0.2
[4.0.1]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v4.0.1
[3.0.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v3.0.0
[2.0.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v2.0.0
[1.0.0]: https://github.com/filecoin-project/rust-filecoin-proofs-api/tree/v1.0.0
[package]
name = "filecoin-proofs-api"
version = "8.0.1"
description = "API to interact with the proofs system in Filecoin"
authors = ["dignifiedquire <me@dignifiedquire.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
repository = "https://github.com/filecoin-project/rust-filecoin-proofs-api"
readme = "README.md"
[dependencies]
anyhow = "1.0.26"
bellperson = { version = "0.14.0", default-features = false }
bincode = "1.1.2"
serde = "1.0.104"
filecoin-proofs-v1 = { package = "filecoin-proofs", path = "../rust-fil-proofs/filecoin-proofs", version = "~8.0", default-features = false }
filecoin-hashers = { path = "../rust-fil-proofs/filecoin-hashers", version = "~3.0", default-features = false, features = ["poseidon", "sha256"] }
fr32 = { path = "../rust-fil-proofs/fr32", version = "~1.0", default-features = false }
storage-proofs-core = { path = "../rust-fil-proofs/storage-proofs-core", version = "~8.0", default-features = false }
[features]
default = ["pairing", "gpu"]
pairing = ["filecoin-proofs-v1/pairing", "bellperson/pairing", "storage-proofs-core/pairing", "fr32/pairing"]
blst = ["filecoin-proofs-v1/blst", "bellperson/blst", "storage-proofs-core/blst", "fr32/blst"]
gpu = ["filecoin-proofs-v1/gpu", "filecoin-hashers/gpu", "storage-proofs-core/gpu", "bellperson/gpu", "fr32/gpu"]
Copyright (c) 2018 Filecoin Project
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
\ No newline at end of file
# Filecoin Proofs API
This library is meant to be the official public API into the proofs library.
[![CircleCI](https://circleci.com/gh/filecoin-project/rust-filecoin-proofs-api/tree/master.svg?style=svg)](https://circleci.com/gh/filecoin-project/rust-filecoin-proofs-api/tree/master)
> The main API to interact with the proofs system in [Filecoin](https://filecoin.io).
## Default build options
The build options enabled by default are `pairing` and `gpu`. An alternative backend that can be used is `blst`. The `pairing` and `blst` options specify which bls12-381 pairing library to use..
## Running the tests
Running the tests with the default features can be done like this:
```
cargo test --release --all
```
Running with the `blst` and `gpu` features can be done like this:
```
cargo test --no-default-features --features blst,gpu --release --all
```
Running with `pairing` and without the `gpu` feature can be done like this:
```
cargo test --no-default-features --features pairing --release --all
```
## License
MIT or Apache 2.0
# Security Policy
## Reporting a Vulnerability
For reporting *critical* and *security* bugs, please consult our [Security Policy and Responsible Disclosure Program information](https://github.com/filecoin-project/community/blob/master/SECURITY.md)
## Reporting a non security bug
For non-critical bugs, please simply file a GitHub issue on this repo.
pre-release-commit-message = "chore({{crate_name}}): release {{version}}"
pro-release-commit-message = "chore({{crate_name}}): starting development cycle for {{next_version}}"
no-dev-version = true
#![deny(clippy::all, clippy::perf, clippy::correctness)]
#![allow(clippy::too_many_arguments)]
//requires nightly, or later stable version
//#![warn(clippy::unwrap_used)]
#![allow(clippy::upper_case_acronyms)]
pub mod post;
pub mod seal;
mod registry;
mod types;
pub use crate::registry::{RegisteredAggregationProof, RegisteredPoStProof, RegisteredSealProof};
pub use crate::types::{PrivateReplicaInfo, PublicReplicaInfo};
pub use filecoin_proofs_v1::types::{
AggregateSnarkProof, ChallengeSeed, Commitment, PaddedBytesAmount, PieceInfo, PoStType,
ProverId, Ticket, UnpaddedByteIndex, UnpaddedBytesAmount,
};
pub use filecoin_proofs_v1::{FallbackPoStSectorProof, SnarkProof, VanillaProof};
pub use fr32;
pub use storage_proofs_core::{
api_version::ApiVersion,
error::Error as StorageProofsError,
merkle::MerkleTreeTrait,
parameter_cache::{get_parameter_data, get_verifying_key_data},
sector::{OrderedSectorSet, SectorId},
util::NODE_SIZE,
};
use std::collections::BTreeMap;
use anyhow::{ensure, Result};
use filecoin_proofs_v1::with_shape;
use crate::types::VanillaProofBytes;
use crate::{
ChallengeSeed, FallbackPoStSectorProof, MerkleTreeTrait, PoStType, PrivateReplicaInfo,
ProverId, PublicReplicaInfo, RegisteredPoStProof, SectorId, SnarkProof,
};
pub fn generate_winning_post_sector_challenge(
proof_type: RegisteredPoStProof,
randomness: &ChallengeSeed,
sector_set_len: u64,
prover_id: ProverId,
) -> Result<Vec<u64>> {
ensure!(
proof_type.typ() == PoStType::Winning,
"invalid post type provided"
);
with_shape!(
u64::from(proof_type.sector_size()),
generate_winning_post_sector_challenge_inner,
proof_type,
randomness,
sector_set_len,
prover_id,
)
}
fn generate_winning_post_sector_challenge_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof_v1: RegisteredPoStProof,
randomness: &ChallengeSeed,
sector_set_len: u64,
prover_id: ProverId,
) -> Result<Vec<u64>> {
filecoin_proofs_v1::generate_winning_post_sector_challenge::<Tree>(
&registered_proof_v1.as_v1_config(),
randomness,
sector_set_len,
prover_id,
)
}
pub fn generate_fallback_sector_challenges(
registered_post_proof_type: RegisteredPoStProof,
randomness: &ChallengeSeed,
pub_sectors: &[SectorId],
prover_id: ProverId,
) -> Result<BTreeMap<SectorId, Vec<u64>>> {
ensure!(!pub_sectors.is_empty(), "no sectors supplied");
with_shape!(
u64::from(registered_post_proof_type.sector_size()),
generate_fallback_sector_challenges_inner,
registered_post_proof_type,
randomness,
pub_sectors,
prover_id,
)
}
fn generate_fallback_sector_challenges_inner<Tree: 'static + MerkleTreeTrait>(
registered_post_proof_type: RegisteredPoStProof,
randomness: &ChallengeSeed,
pub_sectors: &[SectorId],
prover_id: ProverId,
) -> Result<BTreeMap<SectorId, Vec<u64>>> {
filecoin_proofs_v1::generate_fallback_sector_challenges::<Tree>(
&registered_post_proof_type.as_v1_config(),
randomness,
pub_sectors,
prover_id,
)
}
pub fn generate_single_vanilla_proof(
registered_post_proof_type: RegisteredPoStProof,
sector_id: SectorId,
replica: &PrivateReplicaInfo,
challenges: &[u64],
) -> Result<VanillaProofBytes> {
ensure!(!challenges.is_empty(), "no challenges supplied");
with_shape!(
u64::from(registered_post_proof_type.sector_size()),
generate_single_vanilla_proof_inner,
registered_post_proof_type,
sector_id,
replica,
challenges,
)
}
fn generate_single_vanilla_proof_inner<Tree: 'static + MerkleTreeTrait>(
registered_post_proof_type: RegisteredPoStProof,
sector_id: SectorId,
replica: &PrivateReplicaInfo,
challenges: &[u64],
) -> Result<VanillaProofBytes> {
let PrivateReplicaInfo {
registered_proof,
comm_r,
cache_dir,
replica_path,
} = replica;
ensure!(
registered_proof == &registered_post_proof_type,
"can only generate the same kind of PoSt"
);
let info_v1 = filecoin_proofs_v1::PrivateReplicaInfo::<Tree>::new(
replica_path.clone(),
*comm_r,
cache_dir.into(),
)?;
let vanilla_proof: FallbackPoStSectorProof<Tree> =
filecoin_proofs_v1::generate_single_vanilla_proof::<Tree>(
&registered_post_proof_type.as_v1_config(),
sector_id,
&info_v1,
challenges,
)?;
let vanilla_proof_bytes_v1: VanillaProofBytes = bincode::serialize(&vanilla_proof)?;
Ok(vanilla_proof_bytes_v1)
}
pub fn generate_winning_post_with_vanilla(
registered_post_proof_type: RegisteredPoStProof,
randomness: &ChallengeSeed,
prover_id: ProverId,
vanilla_proofs: &[VanillaProofBytes],
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
with_shape!(
u64::from(registered_post_proof_type.sector_size()),
generate_winning_post_with_vanilla_inner,
registered_post_proof_type,
randomness,
prover_id,
vanilla_proofs,
)
}
fn generate_winning_post_with_vanilla_inner<Tree: 'static + MerkleTreeTrait>(
registered_post_proof_type: RegisteredPoStProof,
randomness: &ChallengeSeed,
prover_id: ProverId,
vanilla_proofs: &[VanillaProofBytes],
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
ensure!(
!vanilla_proofs.is_empty(),
"vanilla_proofs cannot be an empty list"
);
let fallback_post_sector_proofs: Vec<FallbackPoStSectorProof<Tree>> = vanilla_proofs
.iter()
.map(|proof_bytes| {
let proof: FallbackPoStSectorProof<Tree> = bincode::deserialize(proof_bytes)?;
Ok(proof)
})
.collect::<Result<_>>()?;
let posts_v1 = filecoin_proofs_v1::generate_winning_post_with_vanilla::<Tree>(
&registered_post_proof_type.as_v1_config(),
randomness,
prover_id,
fallback_post_sector_proofs,
)?;
// once there are multiple versions, merge them before returning
Ok(vec![(registered_post_proof_type, posts_v1)])
}
pub fn generate_winning_post(
randomness: &ChallengeSeed,
replicas: &BTreeMap<SectorId, PrivateReplicaInfo>,
prover_id: ProverId,
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
ensure!(!replicas.is_empty(), "no replicas supplied");
let registered_post_proof_type_v1 = replicas
.values()
.next()
.map(|v| v.registered_proof)
.expect("replica map failure");
ensure!(
registered_post_proof_type_v1.typ() == PoStType::Winning,
"invalid post type provided"
);
with_shape!(
u64::from(registered_post_proof_type_v1.sector_size()),
generate_winning_post_inner,
registered_post_proof_type_v1,
randomness,
replicas,
prover_id,
)
}
fn generate_winning_post_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof_v1: RegisteredPoStProof,
randomness: &ChallengeSeed,
replicas: &BTreeMap<SectorId, PrivateReplicaInfo>,
prover_id: ProverId,
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
let mut replicas_v1 = Vec::new();
for (id, info) in replicas.iter() {
let PrivateReplicaInfo {
registered_proof,
comm_r,
cache_dir,
replica_path,
} = info;
ensure!(
registered_proof == &registered_proof_v1,
"can only generate the same kind of PoSt"
);
let info_v1 = filecoin_proofs_v1::PrivateReplicaInfo::new(
replica_path.clone(),
*comm_r,
cache_dir.into(),
)?;
replicas_v1.push((*id, info_v1));
}
ensure!(!replicas_v1.is_empty(), "missing v1 replicas");
let posts_v1 = filecoin_proofs_v1::generate_winning_post::<Tree>(
&registered_proof_v1.as_v1_config(),
randomness,
&replicas_v1,
prover_id,
)?;
// once there are multiple versions, merge them before returning
Ok(vec![(registered_proof_v1, posts_v1)])
}
pub fn verify_winning_post(
randomness: &ChallengeSeed,
proof: &[u8],
replicas: &BTreeMap<SectorId, PublicReplicaInfo>,
prover_id: ProverId,
) -> Result<bool> {
ensure!(!replicas.is_empty(), "no replicas supplied");
let registered_post_proof_type_v1 = replicas
.values()
.next()
.map(|v| v.registered_proof)
.expect("replica map failure");
ensure!(
registered_post_proof_type_v1.typ() == PoStType::Winning,
"invalid post type provided"
);
with_shape!(
u64::from(registered_post_proof_type_v1.sector_size()),
verify_winning_post_inner,
registered_post_proof_type_v1,
randomness,
proof,
replicas,
prover_id,
)
}
fn verify_winning_post_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof_v1: RegisteredPoStProof,
randomness: &ChallengeSeed,
proof: &[u8],
replicas: &BTreeMap<SectorId, PublicReplicaInfo>,
prover_id: ProverId,
) -> Result<bool> {
let mut replicas_v1 = Vec::new();
for (id, info) in replicas.iter() {
let PublicReplicaInfo {
registered_proof,
comm_r,
} = info;
ensure!(
registered_proof == &registered_proof_v1,
"can only generate the same kind of PoSt"
);
let info_v1 = filecoin_proofs_v1::PublicReplicaInfo::new(*comm_r)?;
replicas_v1.push((*id, info_v1));
}
let valid_v1 = filecoin_proofs_v1::verify_winning_post::<Tree>(
&registered_proof_v1.as_v1_config(),
randomness,
&replicas_v1,
prover_id,
proof,
)?;
// once there are multiple versions, merge them before returning
Ok(valid_v1)
}
pub fn generate_window_post_with_vanilla(
registered_post_proof_type: RegisteredPoStProof,
randomness: &ChallengeSeed,
prover_id: ProverId,
vanilla_proofs: &[VanillaProofBytes],
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
with_shape!(
u64::from(registered_post_proof_type.sector_size()),
generate_window_post_with_vanilla_inner,
registered_post_proof_type,
randomness,
prover_id,
vanilla_proofs,
)
}
fn generate_window_post_with_vanilla_inner<Tree: 'static + MerkleTreeTrait>(
registered_post_proof_type: RegisteredPoStProof,
randomness: &ChallengeSeed,
prover_id: ProverId,
vanilla_proofs: &[VanillaProofBytes],
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
ensure!(
!vanilla_proofs.is_empty(),
"vanilla_proofs cannot be an empty list"
);
let fallback_post_sector_proofs: Vec<FallbackPoStSectorProof<Tree>> = vanilla_proofs
.iter()
.map(|proof_bytes| {
let proof: FallbackPoStSectorProof<Tree> = bincode::deserialize(proof_bytes)?;
Ok(proof)
})
.collect::<Result<_>>()?;
let posts_v1 = filecoin_proofs_v1::generate_window_post_with_vanilla::<Tree>(
&registered_post_proof_type.as_v1_config(),
randomness,
prover_id,
fallback_post_sector_proofs,
)?;
// once there are multiple versions, merge them before returning
Ok(vec![(registered_post_proof_type, posts_v1)])
}
pub fn generate_window_post(
randomness: &ChallengeSeed,
replicas: &BTreeMap<SectorId, PrivateReplicaInfo>,
prover_id: ProverId,
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
ensure!(!replicas.is_empty(), "no replicas supplied");
let registered_post_proof_type_v1 = replicas
.values()
.next()
.map(|v| v.registered_proof)
.expect("replica map failure");
ensure!(
registered_post_proof_type_v1.typ() == PoStType::Window,
"invalid post type provided"
);
with_shape!(
u64::from(registered_post_proof_type_v1.sector_size()),
generate_window_post_inner,
registered_post_proof_type_v1,
randomness,
replicas,
prover_id,
)
}
fn generate_window_post_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof_v1: RegisteredPoStProof,
randomness: &ChallengeSeed,
replicas: &BTreeMap<SectorId, PrivateReplicaInfo>,
prover_id: ProverId,
) -> Result<Vec<(RegisteredPoStProof, SnarkProof)>> {
let mut replicas_v1 = BTreeMap::new();
for (id, info) in replicas.iter() {
let PrivateReplicaInfo {
registered_proof,
comm_r,
cache_dir,
replica_path,
} = info;
ensure!(
registered_proof == &registered_proof_v1,
"can only generate the same kind of PoSt"
);
let info_v1 = filecoin_proofs_v1::PrivateReplicaInfo::new(
replica_path.clone(),
*comm_r,
cache_dir.into(),
)?;
replicas_v1.insert(*id, info_v1);
}
ensure!(!replicas_v1.is_empty(), "missing v1 replicas");
let posts_v1 = filecoin_proofs_v1::generate_window_post::<Tree>(
&registered_proof_v1.as_v1_config(),
randomness,
&replicas_v1,
prover_id,
)?;
// once there are multiple versions, merge them before returning
Ok(vec![(registered_proof_v1, posts_v1)])
}
pub fn verify_window_post(
randomness: &ChallengeSeed,
proofs: &[(RegisteredPoStProof, &[u8])],
replicas: &BTreeMap<SectorId, PublicReplicaInfo>,
prover_id: ProverId,
) -> Result<bool> {
ensure!(!replicas.is_empty(), "no replicas supplied");
ensure!(proofs.len() == 1, "only one version of PoSt supported");
let registered_post_proof_type_v1 = proofs[0].0;
ensure!(
registered_post_proof_type_v1.typ() == PoStType::Window,
"invalid post type provided"
);
ensure!(
registered_post_proof_type_v1.major_version() == 1,
"only V1 supported"
);
with_shape!(
u64::from(registered_post_proof_type_v1.sector_size()),
verify_window_post_inner,
registered_post_proof_type_v1,
randomness,
proofs,
replicas,
prover_id,
)
}
fn verify_window_post_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof_v1: RegisteredPoStProof,
randomness: &ChallengeSeed,
proofs: &[(RegisteredPoStProof, &[u8])],
replicas: &BTreeMap<SectorId, PublicReplicaInfo>,
prover_id: ProverId,
) -> Result<bool> {
let mut replicas_v1 = BTreeMap::new();
for (id, info) in replicas.iter() {
let PublicReplicaInfo {
registered_proof,
comm_r,
} = info;
ensure!(
registered_proof == &registered_proof_v1,
"can only generate the same kind of PoSt"
);
let info_v1 = filecoin_proofs_v1::PublicReplicaInfo::new(*comm_r)?;
replicas_v1.insert(*id, info_v1);
}
let valid_v1 = filecoin_proofs_v1::verify_window_post::<Tree>(
&registered_proof_v1.as_v1_config(),
randomness,
&replicas_v1,
prover_id,
proofs[0].1,
)?;
// once there are multiple versions, merge them before returning
Ok(valid_v1)
}
use std::path::PathBuf;
use anyhow::{ensure, Result};
use filecoin_proofs_v1::{constants, with_shape};
use filecoin_proofs_v1::{PoRepConfig, PoRepProofPartitions, PoStConfig, PoStType, SectorSize};
use serde::{Deserialize, Serialize};
use crate::{get_parameter_data, get_verifying_key_data, ApiVersion, MerkleTreeTrait};
/// Available seal proofs.
/// Enum is append-only: once published, a `RegisteredSealProof` value must never change.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegisteredSealProof {
StackedDrg2KiBV1,
StackedDrg8MiBV1,
StackedDrg16MiBV1,
StackedDrg32MiBV1,
StackedDrg64MiBV1,
StackedDrg128MiBV1,
StackedDrg256MiBV1,
StackedDrg512MiBV1,
StackedDrg32GiBV1,
StackedDrg64GiBV1,
StackedDrg2KiBV1_1,
StackedDrg8MiBV1_1,
StackedDrg16MiBV1_1,
StackedDrg32MiBV1_1,
StackedDrg64MiBV1_1,
StackedDrg128MiBV1_1,
StackedDrg256MiBV1_1,
StackedDrg512MiBV1_1,
StackedDrg32GiBV1_1,
StackedDrg64GiBV1_1,
}
/// Available aggregation proof types.
/// Enum is append-only: once published, a `RegisteredAggregationProof` value must never change.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegisteredAggregationProof {
SnarkPackV1,
}
// Hack to delegate to self config types.
macro_rules! self_shape {
($name:ident, $selfty:ty, $self:expr, $ret:ty) => {{
fn $name<Tree: 'static + MerkleTreeTrait>(s: $selfty) -> Result<$ret> {
s.as_v1_config().$name::<Tree>()
}
with_shape!(u64::from($self.sector_size()), $name, $self)
}};
}
impl RegisteredSealProof {
/// Return the version for this proof.
pub fn version(self) -> ApiVersion {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg16MiBV1 |
StackedDrg32MiBV1 | StackedDrg64MiBV1 | StackedDrg128MiBV1 |
StackedDrg256MiBV1 | StackedDrg512MiBV1 |
StackedDrg32GiBV1 | StackedDrg64GiBV1 => ApiVersion::V1_0_0,
StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1 | StackedDrg16MiBV1_1 |
StackedDrg32MiBV1_1 | StackedDrg64MiBV1_1 | StackedDrg128MiBV1_1 |
StackedDrg256MiBV1_1 | StackedDrg512MiBV1_1 |
StackedDrg32GiBV1_1 | StackedDrg64GiBV1_1 => ApiVersion::V1_1_0,
}
}
/// Return the major version for this proof.
pub fn major_version(self) -> u64 {
self.version().as_semver().major
}
/// Return the minor version for this proof.
pub fn minor_version(self) -> u64 {
self.version().as_semver().minor
}
/// Return the patch version for this proof.
pub fn patch_version(self) -> u64 {
self.version().as_semver().patch
}
/// Return the sector size for this proof.
pub fn sector_size(self) -> SectorSize {
use RegisteredSealProof::*;
let size = match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => constants::SECTOR_SIZE_2_KIB,
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => constants::SECTOR_SIZE_8_MIB,
StackedDrg16MiBV1 | StackedDrg16MiBV1_1 => constants::SECTOR_SIZE_16_MIB,
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => constants::SECTOR_SIZE_32_MIB,
StackedDrg64MiBV1 | StackedDrg64MiBV1_1 => constants::SECTOR_SIZE_64_MIB,
StackedDrg128MiBV1 | StackedDrg128MiBV1_1 => constants::SECTOR_SIZE_128_MIB,
StackedDrg256MiBV1 | StackedDrg256MiBV1_1 => constants::SECTOR_SIZE_256_MIB,
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => constants::SECTOR_SIZE_512_MIB,
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => constants::SECTOR_SIZE_32_GIB,
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => constants::SECTOR_SIZE_64_GIB,
};
SectorSize(size)
}
/// Return the number of partitions for this proof.
pub fn partitions(self) -> u8 {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_2_KIB)
.expect("invalid sector size"),
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_8_MIB)
.expect("invalid sector size"),
StackedDrg16MiBV1 | StackedDrg16MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_16_MIB)
.expect("invalid sector size"),
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_32_MIB)
.expect("invalid sector size"),
StackedDrg64MiBV1 | StackedDrg64MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_64_MIB)
.expect("invalid sector size"),
StackedDrg128MiBV1 | StackedDrg128MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_128_MIB)
.expect("invalid sector size"),
StackedDrg256MiBV1 | StackedDrg256MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_256_MIB)
.expect("invalid sector size"),
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_512_MIB)
.expect("invalid sector size"),
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_32_GIB)
.expect("invalid sector size"),
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => *constants::POREP_PARTITIONS
.read()
.expect("porep partitions read error")
.get(&constants::SECTOR_SIZE_64_GIB)
.expect("invalid sector size"),
}
}
pub fn single_partition_proof_len(self) -> usize {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg16MiBV1
| StackedDrg32MiBV1 | StackedDrg64MiBV1 | StackedDrg128MiBV1
| StackedDrg256MiBV1 | StackedDrg512MiBV1
| StackedDrg32GiBV1 | StackedDrg64GiBV1 | StackedDrg2KiBV1_1
| StackedDrg8MiBV1_1 | StackedDrg16MiBV1_1 | StackedDrg32MiBV1_1
| StackedDrg64MiBV1_1 | StackedDrg128MiBV1_1 | StackedDrg256MiBV1_1
| StackedDrg512MiBV1_1 | StackedDrg32GiBV1_1
| StackedDrg64GiBV1_1 => {
filecoin_proofs_v1::SINGLE_PARTITION_PROOF_LEN
}
}
}
fn nonce(self) -> u64 {
#[allow(clippy::match_single_binding)]
match self {
// If we ever need to change the nonce for any given RegisteredSealProof, match it here.
_ => 0,
}
}
fn porep_id(self) -> [u8; 32] {
let mut porep_id = [0; 32];
let registered_proof_id = self as u64;
let nonce = self.nonce();
porep_id[0..8].copy_from_slice(&registered_proof_id.to_le_bytes());
porep_id[8..16].copy_from_slice(&nonce.to_le_bytes());
porep_id
}
pub fn as_v1_config(self) -> PoRepConfig {
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg16MiBV1
| StackedDrg32MiBV1 | StackedDrg64MiBV1 | StackedDrg128MiBV1
| StackedDrg256MiBV1 | StackedDrg512MiBV1 | StackedDrg32GiBV1
| StackedDrg64GiBV1 => {
assert_eq!(self.version(), ApiVersion::V1_0_0);
PoRepConfig {
sector_size: self.sector_size(),
partitions: PoRepProofPartitions(self.partitions()),
porep_id: self.porep_id(),
api_version: self.version(),
}
}
StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1 | StackedDrg16MiBV1_1
| StackedDrg32MiBV1_1 | StackedDrg64MiBV1_1 | StackedDrg128MiBV1_1
| StackedDrg256MiBV1_1 | StackedDrg512MiBV1_1
| StackedDrg32GiBV1_1 | StackedDrg64GiBV1_1 => {
assert_eq!(self.version(), ApiVersion::V1_1_0);
PoRepConfig {
sector_size: self.sector_size(),
partitions: PoRepProofPartitions(self.partitions()),
porep_id: self.porep_id(),
api_version: self.version(),
}
} // _ => panic!("Can only be called on V1 configs"),
}
}
/// Returns the circuit identifier.
pub fn circuit_identifier(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
self_shape!(get_cache_identifier, RegisteredSealProof, self, String)
}
}
}
pub fn cache_verifying_key_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => self_shape!(
get_cache_verifying_key_path,
RegisteredSealProof,
self,
PathBuf
),
}
}
pub fn cache_params_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
self_shape!(get_cache_params_path, RegisteredSealProof, self, PathBuf)
}
}
}
pub fn verifying_key_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
let id = self.circuit_identifier()?;
let params = get_verifying_key_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params
.expect("verifying key cid params failure")
.cid
.clone())
}
}
}
pub fn params_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 | ApiVersion::V1_1_0 => {
let id = self.circuit_identifier()?;
let params = get_parameter_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params.expect("param cid failure").cid.clone())
}
}
}
pub fn into_winning_post(self) -> RegisteredPoStProof {
use RegisteredPoStProof::*;
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => StackedDrgWinning2KiBV1,
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => StackedDrgWinning8MiBV1,
StackedDrg16MiBV1 | StackedDrg16MiBV1_1 => StackedDrgWinning16MiBV1,
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => StackedDrgWinning32MiBV1,
StackedDrg64MiBV1 | StackedDrg64MiBV1_1 => StackedDrgWinning64MiBV1,
StackedDrg128MiBV1 | StackedDrg128MiBV1_1 => StackedDrgWinning128MiBV1,
StackedDrg256MiBV1 | StackedDrg256MiBV1_1 => StackedDrgWinning256MiBV1,
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => StackedDrgWinning512MiBV1,
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => StackedDrgWinning32GiBV1,
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => StackedDrgWinning64GiBV1,
}
}
pub fn into_window_post(self) -> RegisteredPoStProof {
use RegisteredPoStProof::*;
use RegisteredSealProof::*;
match self {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => StackedDrgWindow2KiBV1,
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => StackedDrgWindow8MiBV1,
StackedDrg16MiBV1 | StackedDrg16MiBV1_1 => StackedDrgWindow16MiBV1,
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => StackedDrgWindow32MiBV1,
StackedDrg64MiBV1 | StackedDrg64MiBV1_1 => StackedDrgWindow64MiBV1,
StackedDrg128MiBV1 | StackedDrg128MiBV1_1 => StackedDrgWindow128MiBV1,
StackedDrg256MiBV1 | StackedDrg256MiBV1_1 => StackedDrgWindow256MiBV1,
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => StackedDrgWindow512MiBV1,
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => StackedDrgWindow32GiBV1,
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => StackedDrgWindow64GiBV1,
}
}
}
/// Available PoSt proofs.
/// Enum is append-only: once published, a `RegisteredSealProof` value must never change.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RegisteredPoStProof {
StackedDrgWinning2KiBV1,
StackedDrgWinning8MiBV1,
StackedDrgWinning16MiBV1,
StackedDrgWinning32MiBV1,
StackedDrgWinning64MiBV1,
StackedDrgWinning128MiBV1,
StackedDrgWinning256MiBV1,
StackedDrgWinning512MiBV1,
StackedDrgWinning32GiBV1,
StackedDrgWinning64GiBV1,
StackedDrgWindow2KiBV1,
StackedDrgWindow8MiBV1,
StackedDrgWindow16MiBV1,
StackedDrgWindow32MiBV1,
StackedDrgWindow64MiBV1,
StackedDrgWindow128MiBV1,
StackedDrgWindow256MiBV1,
StackedDrgWindow512MiBV1,
StackedDrgWindow32GiBV1,
StackedDrgWindow64GiBV1,
}
impl RegisteredPoStProof {
/// Return the version for this proof.
pub fn version(self) -> ApiVersion {
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning16MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning64MiBV1
| StackedDrgWinning128MiBV1
| StackedDrgWinning256MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1
| StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow16MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow64MiBV1
| StackedDrgWindow128MiBV1
| StackedDrgWindow256MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => ApiVersion::V1_0_0,
}
}
/// Return the major version for this proof.
pub fn major_version(self) -> u64 {
self.version().as_semver().major
}
/// Return the minor version for this proof.
pub fn minor_version(self) -> u64 {
self.version().as_semver().minor
}
/// Return the patch version for this proof.
pub fn patch_version(self) -> u64 {
self.version().as_semver().patch
}
/// Return the sector size for this proof.
pub fn sector_size(self) -> SectorSize {
use RegisteredPoStProof::*;
let size = match self {
StackedDrgWinning2KiBV1 | StackedDrgWindow2KiBV1 => constants::SECTOR_SIZE_2_KIB,
StackedDrgWinning8MiBV1 | StackedDrgWindow8MiBV1 => constants::SECTOR_SIZE_8_MIB,
StackedDrgWinning16MiBV1 | StackedDrgWindow16MiBV1 => constants::SECTOR_SIZE_16_MIB,
StackedDrgWinning32MiBV1 | StackedDrgWindow32MiBV1 => constants::SECTOR_SIZE_32_MIB,
StackedDrgWinning64MiBV1 | StackedDrgWindow64MiBV1 => constants::SECTOR_SIZE_64_MIB,
StackedDrgWinning128MiBV1 | StackedDrgWindow128MiBV1 => constants::SECTOR_SIZE_128_MIB,
StackedDrgWinning256MiBV1 | StackedDrgWindow256MiBV1 => constants::SECTOR_SIZE_256_MIB,
StackedDrgWinning512MiBV1 | StackedDrgWindow512MiBV1 => constants::SECTOR_SIZE_512_MIB,
StackedDrgWinning32GiBV1 | StackedDrgWindow32GiBV1 => constants::SECTOR_SIZE_32_GIB,
StackedDrgWinning64GiBV1 | StackedDrgWindow64GiBV1 => constants::SECTOR_SIZE_64_GIB,
};
SectorSize(size)
}
/// Return the PoStType for this proof.
pub fn typ(self) -> PoStType {
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning16MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning64MiBV1
| StackedDrgWinning128MiBV1
| StackedDrgWinning256MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1 => PoStType::Winning,
StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow16MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow64MiBV1
| StackedDrgWindow128MiBV1
| StackedDrgWindow256MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => PoStType::Window,
}
}
pub fn single_partition_proof_len(self) -> usize {
match self.version() {
ApiVersion::V1_0_0 => filecoin_proofs_v1::SINGLE_PARTITION_PROOF_LEN,
_ => panic!("Invalid PoSt api version"),
}
}
/// Return the sector count for this proof.
pub fn sector_count(self) -> usize {
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning16MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning64MiBV1
| StackedDrgWinning128MiBV1
| StackedDrgWinning256MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1 => constants::WINNING_POST_SECTOR_COUNT,
StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow16MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow64MiBV1
| StackedDrgWindow128MiBV1
| StackedDrgWindow256MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => *constants::WINDOW_POST_SECTOR_COUNT
.read()
.expect("window post sector count failure")
.get(&u64::from(self.sector_size()))
.expect("invalid sector size"),
}
}
pub fn as_v1_config(self) -> PoStConfig {
assert_eq!(self.version(), ApiVersion::V1_0_0);
use RegisteredPoStProof::*;
match self {
StackedDrgWinning2KiBV1
| StackedDrgWinning8MiBV1
| StackedDrgWinning16MiBV1
| StackedDrgWinning32MiBV1
| StackedDrgWinning64MiBV1
| StackedDrgWinning128MiBV1
| StackedDrgWinning256MiBV1
| StackedDrgWinning512MiBV1
| StackedDrgWinning32GiBV1
| StackedDrgWinning64GiBV1 => PoStConfig {
typ: self.typ(),
sector_size: self.sector_size(),
sector_count: self.sector_count(),
challenge_count: constants::WINNING_POST_CHALLENGE_COUNT,
priority: true,
api_version: self.version(),
},
StackedDrgWindow2KiBV1
| StackedDrgWindow8MiBV1
| StackedDrgWindow16MiBV1
| StackedDrgWindow32MiBV1
| StackedDrgWindow64MiBV1
| StackedDrgWindow128MiBV1
| StackedDrgWindow256MiBV1
| StackedDrgWindow512MiBV1
| StackedDrgWindow32GiBV1
| StackedDrgWindow64GiBV1 => PoStConfig {
typ: self.typ(),
sector_size: self.sector_size(),
sector_count: self.sector_count(),
challenge_count: constants::WINDOW_POST_CHALLENGE_COUNT,
priority: true,
api_version: self.version(),
}, // _ => panic!("Can only be called on V1 configs"),
}
}
/// Returns the circuit identifier.
pub fn circuit_identifier(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 => {
self_shape!(get_cache_identifier, RegisteredPoStProof, self, String)
}
_ => panic!("Invalid PoSt api version"),
}
}
pub fn cache_verifying_key_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 => self_shape!(
get_cache_verifying_key_path,
RegisteredPoStProof,
self,
PathBuf
),
_ => panic!("Invalid PoSt api version"),
}
}
pub fn cache_params_path(self) -> Result<PathBuf> {
match self.version() {
ApiVersion::V1_0_0 => {
self_shape!(get_cache_params_path, RegisteredPoStProof, self, PathBuf)
}
_ => panic!("Invalid PoSt api version"),
}
}
pub fn verifying_key_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 => {
let id = self.circuit_identifier()?;
let params = get_verifying_key_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params
.expect("verifying key cid params failure")
.cid
.clone())
}
_ => panic!("Invalid PoSt api version"),
}
}
pub fn params_cid(self) -> Result<String> {
match self.version() {
ApiVersion::V1_0_0 => {
let id = self.circuit_identifier()?;
let params = get_parameter_data(&id);
ensure!(params.is_some(), "missing params for {}", &id);
Ok(params.expect("params cid failure").cid.clone())
}
_ => panic!("Invalid PoSt api version"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use filecoin_proofs_v1::MAX_LEGACY_REGISTERED_SEAL_PROOF_ID;
const REGISTERED_SEAL_PROOFS: [RegisteredSealProof; 10] = [
RegisteredSealProof::StackedDrg2KiBV1,
RegisteredSealProof::StackedDrg8MiBV1,
RegisteredSealProof::StackedDrg512MiBV1,
RegisteredSealProof::StackedDrg32GiBV1,
RegisteredSealProof::StackedDrg64GiBV1,
RegisteredSealProof::StackedDrg2KiBV1_1,
RegisteredSealProof::StackedDrg8MiBV1_1,
RegisteredSealProof::StackedDrg512MiBV1_1,
RegisteredSealProof::StackedDrg32GiBV1_1,
RegisteredSealProof::StackedDrg64GiBV1_1,
];
#[test]
fn test_porep_id() {
for rsp in &REGISTERED_SEAL_PROOFS {
test_porep_id_aux(rsp);
}
}
fn test_porep_id_aux(rsp: &RegisteredSealProof) {
let expected_porep_id = match rsp {
RegisteredSealProof::StackedDrg2KiBV1 => {
"0000000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg8MiBV1 => {
"0100000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg512MiBV1 => {
"0200000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg32GiBV1 => {
"0300000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg64GiBV1 => {
"0400000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg2KiBV1_1 => {
"0500000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg8MiBV1_1 => {
"0600000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg512MiBV1_1 => {
"0700000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg32GiBV1_1 => {
"0800000000000000000000000000000000000000000000000000000000000000"
}
RegisteredSealProof::StackedDrg64GiBV1_1 => {
"0900000000000000000000000000000000000000000000000000000000000000"
}
};
let hex: String = rsp
.porep_id()
.iter()
.map(|x| format!("{:01$x}", x, 2))
.collect();
assert_eq!(expected_porep_id, &hex);
}
#[test]
fn test_max_initial_porep_id() {
for rsp in &REGISTERED_SEAL_PROOFS {
let mut porep_id_type_bytes = [0u8; 8];
let porep_id = rsp.porep_id();
porep_id_type_bytes.copy_from_slice(&porep_id[..8]);
let porep_type = u64::from_le_bytes(porep_id_type_bytes);
let is_legacy = porep_type <= MAX_LEGACY_REGISTERED_SEAL_PROOF_ID;
match rsp {
RegisteredSealProof::StackedDrg2KiBV1
| RegisteredSealProof::StackedDrg8MiBV1
| RegisteredSealProof::StackedDrg512MiBV1
| RegisteredSealProof::StackedDrg32GiBV1
| RegisteredSealProof::StackedDrg64GiBV1 => assert!(is_legacy),
RegisteredSealProof::StackedDrg2KiBV1_1
| RegisteredSealProof::StackedDrg8MiBV1_1
| RegisteredSealProof::StackedDrg512MiBV1_1
| RegisteredSealProof::StackedDrg32GiBV1_1
| RegisteredSealProof::StackedDrg64GiBV1_1 => assert!(!is_legacy),
}
}
}
#[test]
fn test_verifying_key_path() {
for rsp in &REGISTERED_SEAL_PROOFS {
rsp.cache_verifying_key_path()
.expect("failed to get verifying key path");
}
}
#[test]
fn test_verifying_key_cid() {
for rsp in &REGISTERED_SEAL_PROOFS {
rsp.verifying_key_cid()
.expect("failed to get verifying key cid");
}
}
#[test]
fn test_params_path() {
for rsp in &REGISTERED_SEAL_PROOFS {
rsp.cache_params_path().expect("failed to get params path");
}
}
#[test]
fn test_params_cid() {
for rsp in &REGISTERED_SEAL_PROOFS {
rsp.params_cid().expect("failed to get params_cid");
}
}
}
use std::convert::TryInto;
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
use anyhow::{bail, ensure, Error, Result};
use bellperson::bls::Fr;
use filecoin_hashers::Hasher;
use filecoin_proofs_v1::constants::{
SectorShape16KiB, SectorShape16MiB, SectorShape1GiB, SectorShape2KiB, SectorShape32GiB,
SectorShape32KiB, SectorShape4KiB, SectorShape512MiB, SectorShape64GiB, SectorShape8MiB,
SectorShape32MiB, SectorShape64MiB, SectorShape128MiB, SectorShape256MiB,
SECTOR_SIZE_16_KIB, SECTOR_SIZE_16_MIB, SECTOR_SIZE_1_GIB, SECTOR_SIZE_2_KIB,
SECTOR_SIZE_32_GIB, SECTOR_SIZE_32_KIB, SECTOR_SIZE_4_KIB, SECTOR_SIZE_512_MIB,
SECTOR_SIZE_64_GIB, SECTOR_SIZE_8_MIB, SECTOR_SIZE_32_MIB, SECTOR_SIZE_64_MIB,
SECTOR_SIZE_128_MIB, SECTOR_SIZE_256_MIB
};
use filecoin_proofs_v1::types::MerkleTreeTrait;
use filecoin_proofs_v1::types::VanillaSealProof as RawVanillaSealProof;
use filecoin_proofs_v1::{with_shape, Labels as RawLabels};
use serde::{Deserialize, Serialize};
use crate::{
AggregateSnarkProof, Commitment, PieceInfo, ProverId, RegisteredAggregationProof,
RegisteredSealProof, SectorId, Ticket, UnpaddedByteIndex, UnpaddedBytesAmount,
};
/// The output of `seal_pre_commit_phase1`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SealPreCommitPhase1Output {
pub registered_proof: RegisteredSealProof,
pub labels: Labels,
pub config: filecoin_proofs_v1::StoreConfig,
pub comm_d: filecoin_proofs_v1::Commitment,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum Labels {
StackedDrg2KiBV1(RawLabels<SectorShape2KiB>),
StackedDrg8MiBV1(RawLabels<SectorShape8MiB>),
StackedDrg16MiBV1(RawLabels<SectorShape16MiB>),
StackedDrg32MiBV1(RawLabels<SectorShape32MiB>),
StackedDrg64MiBV1(RawLabels<SectorShape64MiB>),
StackedDrg128MiBV1(RawLabels<SectorShape128MiB>),
StackedDrg256MiBV1(RawLabels<SectorShape256MiB>),
StackedDrg512MiBV1(RawLabels<SectorShape512MiB>),
StackedDrg32GiBV1(RawLabels<SectorShape32GiB>),
StackedDrg64GiBV1(RawLabels<SectorShape64GiB>),
}
impl Labels {
fn from_raw<Tree: 'static + MerkleTreeTrait>(
proof: RegisteredSealProof,
labels: &RawLabels<Tree>,
) -> Result<Self> {
use std::any::Any;
use RegisteredSealProof::*;
match proof {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape2KiB>>(labels) {
Ok(Labels::StackedDrg2KiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape8MiB>>(labels) {
Ok(Labels::StackedDrg8MiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg16MiBV1 | StackedDrg16MiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape16MiB>>(labels) {
Ok(Labels::StackedDrg16MiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape32MiB>>(labels) {
Ok(Labels::StackedDrg32MiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg64MiBV1 | StackedDrg64MiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape64MiB>>(labels) {
Ok(Labels::StackedDrg64MiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg128MiBV1 | StackedDrg128MiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape128MiB>>(labels) {
Ok(Labels::StackedDrg128MiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg256MiBV1 | StackedDrg256MiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape256MiB>>(labels) {
Ok(Labels::StackedDrg256MiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape512MiB>>(labels) {
Ok(Labels::StackedDrg512MiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape32GiB>>(labels) {
Ok(Labels::StackedDrg32GiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => {
if let Some(labels) = Any::downcast_ref::<RawLabels<SectorShape64GiB>>(labels) {
Ok(Labels::StackedDrg64GiBV1(labels.clone()))
} else {
bail!("invalid labels provided")
}
}
}
}
}
impl<Tree: 'static + MerkleTreeTrait> TryInto<RawLabels<Tree>> for Labels {
type Error = Error;
fn try_into(self) -> Result<RawLabels<Tree>> {
use std::any::Any;
use Labels::*;
match self {
StackedDrg2KiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 2kib into different structure")
}
}
StackedDrg8MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 8Mib into different structure")
}
}
StackedDrg16MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 16Mib into different structure")
}
}
StackedDrg32MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 32Mib into different structure")
}
}
StackedDrg64MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 64Mib into different structure")
}
}
StackedDrg128MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 128Mib into different structure")
}
}
StackedDrg256MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 256Mib into different structure")
}
}
StackedDrg512MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 512Mib into different structure")
}
}
StackedDrg32GiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 32gib into different structure")
}
}
StackedDrg64GiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<RawLabels<Tree>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 64gib into different structure")
}
}
}
}
}
/// The output of `seal_pre_commit_phase2`.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SealPreCommitPhase2Output {
pub registered_proof: RegisteredSealProof,
pub comm_r: Commitment,
pub comm_d: Commitment,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SealCommitPhase1Output {
pub registered_proof: RegisteredSealProof,
pub vanilla_proofs: VanillaSealProof,
pub comm_r: Commitment,
pub comm_d: Commitment,
pub replica_id: <filecoin_proofs_v1::constants::DefaultTreeHasher as Hasher>::Domain,
pub seed: Ticket,
pub ticket: Ticket,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum VanillaSealProof {
StackedDrg2KiBV1(Vec<Vec<RawVanillaSealProof<SectorShape2KiB>>>),
StackedDrg8MiBV1(Vec<Vec<RawVanillaSealProof<SectorShape8MiB>>>),
StackedDrg16MiBV1(Vec<Vec<RawVanillaSealProof<SectorShape16MiB>>>),
StackedDrg32MiBV1(Vec<Vec<RawVanillaSealProof<SectorShape32MiB>>>),
StackedDrg64MiBV1(Vec<Vec<RawVanillaSealProof<SectorShape64MiB>>>),
StackedDrg128MiBV1(Vec<Vec<RawVanillaSealProof<SectorShape128MiB>>>),
StackedDrg256MiBV1(Vec<Vec<RawVanillaSealProof<SectorShape256MiB>>>),
StackedDrg512MiBV1(Vec<Vec<RawVanillaSealProof<SectorShape512MiB>>>),
StackedDrg32GiBV1(Vec<Vec<RawVanillaSealProof<SectorShape32GiB>>>),
StackedDrg64GiBV1(Vec<Vec<RawVanillaSealProof<SectorShape64GiB>>>),
}
impl VanillaSealProof {
#[allow(clippy::ptr_arg)]
fn from_raw<Tree: 'static + MerkleTreeTrait>(
proof: RegisteredSealProof,
proofs: &Vec<Vec<RawVanillaSealProof<Tree>>>,
) -> Result<Self> {
use std::any::Any;
use RegisteredSealProof::*;
match proof {
StackedDrg2KiBV1 | StackedDrg2KiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape2KiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg2KiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg8MiBV1 | StackedDrg8MiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape8MiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg8MiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg16MiBV1 | StackedDrg16MiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape16MiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg16MiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg32MiBV1 | StackedDrg32MiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape32MiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg32MiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg64MiBV1 | StackedDrg64MiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape64MiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg64MiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg128MiBV1 | StackedDrg128MiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape128MiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg128MiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg256MiBV1 | StackedDrg256MiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape256MiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg256MiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg512MiBV1 | StackedDrg512MiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape512MiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg512MiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg32GiBV1 | StackedDrg32GiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape32GiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg32GiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
StackedDrg64GiBV1 | StackedDrg64GiBV1_1 => {
if let Some(proofs) =
Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<SectorShape64GiB>>>>(proofs)
{
Ok(VanillaSealProof::StackedDrg64GiBV1(proofs.clone()))
} else {
bail!("invalid proofs provided")
}
}
}
}
}
impl<Tree: 'static + MerkleTreeTrait> TryInto<Vec<Vec<RawVanillaSealProof<Tree>>>>
for VanillaSealProof
{
type Error = Error;
fn try_into(self) -> Result<Vec<Vec<RawVanillaSealProof<Tree>>>> {
use std::any::Any;
use VanillaSealProof::*;
match self {
StackedDrg2KiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 2kib into different structure")
}
}
StackedDrg8MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 8Mib into different structure")
}
}
StackedDrg16MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 16Mib into different structure")
}
}
StackedDrg32MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 32Mib into different structure")
}
}
StackedDrg64MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 64Mib into different structure")
}
}
StackedDrg128MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 128Mib into different structure")
}
}
StackedDrg256MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 256Mib into different structure")
}
}
StackedDrg512MiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 512Mib into different structure")
}
}
StackedDrg32GiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 32gib into different structure")
}
}
StackedDrg64GiBV1(raw) => {
if let Some(raw) = Any::downcast_ref::<Vec<Vec<RawVanillaSealProof<Tree>>>>(&raw) {
Ok(raw.clone())
} else {
bail!("cannot convert 64gib into different structure")
}
}
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SealCommitPhase2Output {
pub proof: Vec<u8>,
}
pub fn clear_cache(sector_size: u64, cache_path: &Path) -> Result<()> {
use filecoin_proofs_v1::clear_cache;
with_shape!(sector_size, clear_cache, cache_path)
}
pub fn seal_pre_commit_phase1<R, S, T>(
registered_proof: RegisteredSealProof,
cache_path: R,
in_path: S,
out_path: T,
prover_id: ProverId,
sector_id: SectorId,
ticket: Ticket,
piece_infos: &[PieceInfo],
) -> Result<SealPreCommitPhase1Output>
where
R: AsRef<Path>,
S: AsRef<Path>,
T: AsRef<Path>,
{
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
with_shape!(
u64::from(registered_proof.sector_size()),
seal_pre_commit_phase1_inner,
registered_proof,
cache_path.as_ref(),
in_path.as_ref(),
out_path.as_ref(),
prover_id,
sector_id,
ticket,
piece_infos
)
}
fn seal_pre_commit_phase1_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof: RegisteredSealProof,
cache_path: &Path,
in_path: &Path,
out_path: &Path,
prover_id: ProverId,
sector_id: SectorId,
ticket: Ticket,
piece_infos: &[PieceInfo],
) -> Result<SealPreCommitPhase1Output> {
let config = registered_proof.as_v1_config();
let output = filecoin_proofs_v1::seal_pre_commit_phase1::<_, _, _, Tree>(
config,
cache_path,
in_path,
out_path,
prover_id,
sector_id,
ticket,
piece_infos,
)?;
let filecoin_proofs_v1::types::SealPreCommitPhase1Output::<Tree> {
labels,
config,
comm_d,
} = output;
Ok(SealPreCommitPhase1Output {
registered_proof,
labels: Labels::from_raw::<Tree>(registered_proof, &labels)?,
config,
comm_d,
})
}
pub fn seal_pre_commit_phase2<R, S>(
phase1_output: SealPreCommitPhase1Output,
cache_path: R,
out_path: S,
) -> Result<SealPreCommitPhase2Output>
where
R: AsRef<Path>,
S: AsRef<Path>,
{
ensure!(
phase1_output.registered_proof.major_version() == 1,
"unusupported version"
);
with_shape!(
u64::from(phase1_output.registered_proof.sector_size()),
seal_pre_commit_phase2_inner,
phase1_output,
cache_path.as_ref(),
out_path.as_ref(),
)
}
fn seal_pre_commit_phase2_inner<Tree: 'static + MerkleTreeTrait>(
phase1_output: SealPreCommitPhase1Output,
cache_path: &Path,
out_path: &Path,
) -> Result<SealPreCommitPhase2Output> {
let SealPreCommitPhase1Output {
registered_proof,
labels,
config,
comm_d,
} = phase1_output;
let seal_pre_commit_phase1_output =
filecoin_proofs_v1::types::SealPreCommitPhase1Output::<Tree> {
labels: labels.try_into()?,
config,
comm_d,
};
filecoin_proofs_v1::validate_cache_for_precommit_phase2::<_, _, Tree>(
&cache_path,
&out_path,
&seal_pre_commit_phase1_output,
)?;
let output = filecoin_proofs_v1::seal_pre_commit_phase2::<_, _, Tree>(
registered_proof.as_v1_config(),
seal_pre_commit_phase1_output,
cache_path,
out_path,
)?;
let filecoin_proofs_v1::types::SealPreCommitOutput { comm_d, comm_r } = output;
Ok(SealPreCommitPhase2Output {
registered_proof,
comm_d,
comm_r,
})
}
pub fn compute_comm_d(
registered_proof: RegisteredSealProof,
piece_infos: &[PieceInfo],
) -> Result<Commitment> {
filecoin_proofs_v1::compute_comm_d(registered_proof.sector_size(), piece_infos)
}
pub fn seal_commit_phase1<T: AsRef<Path>>(
cache_path: T,
replica_path: T,
prover_id: ProverId,
sector_id: SectorId,
ticket: Ticket,
seed: Ticket,
pre_commit: SealPreCommitPhase2Output,
piece_infos: &[PieceInfo],
) -> Result<SealCommitPhase1Output> {
ensure!(
pre_commit.registered_proof.major_version() == 1,
"unusupported version"
);
with_shape!(
u64::from(pre_commit.registered_proof.sector_size()),
seal_commit_phase1_inner,
cache_path.as_ref(),
replica_path.as_ref(),
prover_id,
sector_id,
ticket,
seed,
pre_commit,
piece_infos,
)
}
fn seal_commit_phase1_inner<Tree: 'static + MerkleTreeTrait>(
cache_path: &Path,
replica_path: &Path,
prover_id: ProverId,
sector_id: SectorId,
ticket: Ticket,
seed: Ticket,
pre_commit: SealPreCommitPhase2Output,
piece_infos: &[PieceInfo],
) -> Result<SealCommitPhase1Output> {
let SealPreCommitPhase2Output {
comm_r,
comm_d,
registered_proof,
} = pre_commit;
let config = registered_proof.as_v1_config();
let pc = filecoin_proofs_v1::types::SealPreCommitOutput { comm_r, comm_d };
filecoin_proofs_v1::validate_cache_for_commit::<_, _, Tree>(&cache_path, &replica_path)?;
let output = filecoin_proofs_v1::seal_commit_phase1::<_, Tree>(
config,
cache_path,
replica_path,
prover_id,
sector_id,
ticket,
seed,
pc,
piece_infos,
)?;
let filecoin_proofs_v1::types::SealCommitPhase1Output::<Tree> {
vanilla_proofs,
comm_r,
comm_d,
replica_id,
seed,
ticket,
} = output;
let replica_id: Fr = replica_id.into();
Ok(SealCommitPhase1Output {
registered_proof,
vanilla_proofs: VanillaSealProof::from_raw::<Tree>(registered_proof, &vanilla_proofs)?,
comm_r,
comm_d,
replica_id: replica_id.into(),
seed,
ticket,
})
}
pub fn seal_commit_phase2(
phase1_output: SealCommitPhase1Output,
prover_id: ProverId,
sector_id: SectorId,
) -> Result<SealCommitPhase2Output> {
ensure!(
phase1_output.registered_proof.major_version() == 1,
"unusupported version"
);
with_shape!(
u64::from(phase1_output.registered_proof.sector_size()),
seal_commit_phase2_inner,
phase1_output,
prover_id,
sector_id,
)
}
fn seal_commit_phase2_inner<Tree: 'static + MerkleTreeTrait>(
phase1_output: SealCommitPhase1Output,
prover_id: ProverId,
sector_id: SectorId,
) -> Result<SealCommitPhase2Output> {
let SealCommitPhase1Output {
vanilla_proofs,
comm_r,
comm_d,
replica_id,
seed,
ticket,
registered_proof,
} = phase1_output;
let config = registered_proof.as_v1_config();
let replica_id: Fr = replica_id.into();
let co = filecoin_proofs_v1::types::SealCommitPhase1Output {
vanilla_proofs: vanilla_proofs.try_into()?,
comm_r,
comm_d,
replica_id: replica_id.into(),
seed,
ticket,
};
let output = filecoin_proofs_v1::seal_commit_phase2::<Tree>(config, co, prover_id, sector_id)?;
Ok(SealCommitPhase2Output {
proof: output.proof,
})
}
pub fn get_seal_inputs(
registered_proof: RegisteredSealProof,
comm_r: Commitment,
comm_d: Commitment,
prover_id: ProverId,
sector_id: SectorId,
ticket: Ticket,
seed: Ticket,
) -> Result<Vec<Vec<Fr>>> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
with_shape!(
u64::from(registered_proof.sector_size()),
get_seal_inputs_inner,
registered_proof,
comm_r,
comm_d,
prover_id,
sector_id,
ticket,
seed,
)
}
pub fn get_seal_inputs_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof: RegisteredSealProof,
comm_r: Commitment,
comm_d: Commitment,
prover_id: ProverId,
sector_id: SectorId,
ticket: Ticket,
seed: Ticket,
) -> Result<Vec<Vec<Fr>>> {
let config = registered_proof.as_v1_config();
filecoin_proofs_v1::get_seal_inputs::<Tree>(
config, comm_r, comm_d, prover_id, sector_id, ticket, seed,
)
}
pub fn aggregate_seal_commit_proofs(
registered_proof: RegisteredSealProof,
registered_aggregation: RegisteredAggregationProof,
comm_rs: &[Commitment],
seeds: &[Ticket],
commit_outputs: &[SealCommitPhase2Output],
) -> Result<AggregateSnarkProof> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
ensure!(
registered_aggregation == RegisteredAggregationProof::SnarkPackV1,
"unusupported aggregation version"
);
with_shape!(
u64::from(registered_proof.sector_size()),
aggregate_seal_commit_proofs_inner,
registered_proof,
comm_rs,
seeds,
commit_outputs,
)
}
pub fn aggregate_seal_commit_proofs_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof: RegisteredSealProof,
comm_rs: &[Commitment],
seeds: &[Ticket],
commit_outputs: &[SealCommitPhase2Output],
) -> Result<AggregateSnarkProof> {
let config = registered_proof.as_v1_config();
let outputs: Vec<filecoin_proofs_v1::types::SealCommitOutput> = commit_outputs
.iter()
.map(|co| filecoin_proofs_v1::types::SealCommitOutput {
proof: co.proof.clone(),
})
.collect();
filecoin_proofs_v1::aggregate_seal_commit_proofs::<Tree>(config, comm_rs, seeds, &outputs)
}
pub fn verify_aggregate_seal_commit_proofs(
registered_proof: RegisteredSealProof,
registered_aggregation: RegisteredAggregationProof,
aggregate_proof_bytes: AggregateSnarkProof,
comm_rs: &[Commitment],
seeds: &[Ticket],
commit_inputs: Vec<Vec<Fr>>,
) -> Result<bool> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
ensure!(
registered_aggregation == RegisteredAggregationProof::SnarkPackV1,
"unusupported aggregation version"
);
with_shape!(
u64::from(registered_proof.sector_size()),
verify_aggregate_seal_commit_proofs_inner,
registered_proof,
aggregate_proof_bytes,
comm_rs,
seeds,
commit_inputs,
)
}
pub fn verify_aggregate_seal_commit_proofs_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof: RegisteredSealProof,
aggregate_proof_bytes: AggregateSnarkProof,
comm_rs: &[Commitment],
seeds: &[Ticket],
commit_inputs: Vec<Vec<Fr>>,
) -> Result<bool> {
let config = registered_proof.as_v1_config();
filecoin_proofs_v1::verify_aggregate_seal_commit_proofs::<Tree>(
config,
aggregate_proof_bytes,
comm_rs,
seeds,
commit_inputs,
)
}
pub fn fauxrep<R: AsRef<Path>, S: AsRef<Path>>(
registered_proof: RegisteredSealProof,
cache_path: R,
replica_path: S,
) -> Result<Commitment> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
let config = registered_proof.as_v1_config();
let sector_size: u64 = u64::from(registered_proof.sector_size());
// TODO: Clean-up this method, as it more or less unrolls the with_shape macro in order to pass along the R and S generics as well as the Tree.
//
// Note also that not all of these sector sizes are production, so some could be pruned.
match sector_size {
SECTOR_SIZE_2_KIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape2KiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_4_KIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape4KiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_16_KIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape16KiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_32_KIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape32KiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_8_MIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape8MiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_16_MIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape16MiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_32_MIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape32MiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_64_MIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape64MiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_128_MIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape128MiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_256_MIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape256MiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_512_MIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape512MiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_1_GIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape1GiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_32_GIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape32GiB>(config, cache_path, replica_path)
}
SECTOR_SIZE_64_GIB => {
filecoin_proofs_v1::fauxrep::<_, _, SectorShape64GiB>(config, cache_path, replica_path)
}
_ => panic!("unsupported sector size: {}", sector_size),
}
}
pub fn fauxrep2<R: AsRef<Path>, S: AsRef<Path>>(
registered_proof: RegisteredSealProof,
cache_path: R,
existing_p_aux_path: S,
) -> Result<Commitment> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
let sector_size: u64 = u64::from(registered_proof.sector_size());
// TODO: Clean-up this method, as it more or less unrolls the with_shape macro in order to pass along the R and S generics as well as the Tree.
//
// Note also that not all of these sector sizes are production, so some could be pruned.
match sector_size {
SECTOR_SIZE_2_KIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape2KiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_4_KIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape4KiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_16_KIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape16KiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_32_KIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape32KiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_8_MIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape8MiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_16_MIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape16MiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_32_MIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape32MiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_64_MIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape64MiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_128_MIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape128MiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_256_MIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape256MiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_512_MIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape512MiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_1_GIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape1GiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_32_GIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape32GiB>(cache_path, existing_p_aux_path)
}
SECTOR_SIZE_64_GIB => {
filecoin_proofs_v1::fauxrep2::<_, _, SectorShape64GiB>(cache_path, existing_p_aux_path)
}
_ => panic!("unsupported sector size: {}", sector_size),
}
}
pub fn verify_seal(
registered_proof: RegisteredSealProof,
comm_r_in: Commitment,
comm_d_in: Commitment,
prover_id: ProverId,
sector_id: SectorId,
ticket: Ticket,
seed: Ticket,
proof_vec: &[u8],
) -> Result<bool> {
let config = registered_proof.as_v1_config();
use filecoin_proofs_v1::verify_seal;
with_shape!(
u64::from(registered_proof.sector_size()),
verify_seal,
config,
comm_r_in,
comm_d_in,
prover_id,
sector_id,
ticket,
seed,
proof_vec,
)
}
pub fn verify_batch_seal(
registered_proof: RegisteredSealProof,
comm_r_ins: &[Commitment],
comm_d_ins: &[Commitment],
prover_ids: &[ProverId],
sector_ids: &[SectorId],
tickets: &[Ticket],
seeds: &[Ticket],
proof_vecs: &[&[u8]],
) -> Result<bool> {
let config = registered_proof.as_v1_config();
use filecoin_proofs_v1::verify_batch_seal;
with_shape!(
u64::from(registered_proof.sector_size()),
verify_batch_seal,
config,
comm_r_ins,
comm_d_ins,
prover_ids,
sector_ids,
tickets,
seeds,
proof_vecs,
)
}
pub fn get_unsealed_range<T: Into<PathBuf> + AsRef<Path>>(
registered_proof: RegisteredSealProof,
cache_path: T,
sealed_path: T,
output_path: T,
prover_id: ProverId,
sector_id: SectorId,
comm_d: Commitment,
ticket: Ticket,
offset: UnpaddedByteIndex,
num_bytes: UnpaddedBytesAmount,
) -> Result<UnpaddedBytesAmount> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
with_shape!(
u64::from(registered_proof.sector_size()),
get_unsealed_range_inner,
registered_proof,
cache_path.as_ref(),
sealed_path.as_ref(),
output_path.as_ref(),
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
)
}
fn get_unsealed_range_inner<Tree: 'static + MerkleTreeTrait>(
registered_proof: RegisteredSealProof,
cache_path: &Path,
sealed_path: &Path,
output_path: &Path,
prover_id: ProverId,
sector_id: SectorId,
comm_d: Commitment,
ticket: Ticket,
offset: UnpaddedByteIndex,
num_bytes: UnpaddedBytesAmount,
) -> Result<UnpaddedBytesAmount> {
let config = registered_proof.as_v1_config();
filecoin_proofs_v1::get_unsealed_range::<_, Tree>(
config,
cache_path,
sealed_path,
output_path,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
)
}
pub fn get_unsealed_range_mapped<T: Into<PathBuf> + AsRef<Path>, W: Write>(
registered_proof: RegisteredSealProof,
cache_path: T,
sealed_path: T,
unsealed_output: W,
prover_id: ProverId,
sector_id: SectorId,
comm_d: Commitment,
ticket: Ticket,
offset: UnpaddedByteIndex,
num_bytes: UnpaddedBytesAmount,
) -> Result<UnpaddedBytesAmount> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
let config = registered_proof.as_v1_config();
let sector_size: u64 = u64::from(registered_proof.sector_size());
// TODO: Clean-up this method, as it more or less unrolls the with_shape macro in order to pass along the R and W generics as well as the Tree.
//
// Note also that not all of these sector sizes are production, so some could be pruned.
match sector_size {
SECTOR_SIZE_2_KIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape2KiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_4_KIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape4KiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_16_KIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape16KiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_32_KIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape32KiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_8_MIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape8MiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_16_MIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape16MiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_32_MIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape32MiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_64_MIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape64MiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_128_MIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape128MiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_256_MIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape256MiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_512_MIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape512MiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_1_GIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape1GiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_32_GIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape32GiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_64_GIB => filecoin_proofs_v1::unseal_range_mapped::<_, _, SectorShape64GiB>(
config,
cache_path,
sealed_path.into(),
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
_ => panic!("unsupported sector size: {}", sector_size),
}
}
pub fn unseal_range<T: Into<PathBuf> + AsRef<Path>, R: Read, W: Write>(
registered_proof: RegisteredSealProof,
cache_path: T,
sealed_sector: R,
unsealed_output: W,
prover_id: ProverId,
sector_id: SectorId,
comm_d: Commitment,
ticket: Ticket,
offset: UnpaddedByteIndex,
num_bytes: UnpaddedBytesAmount,
) -> Result<UnpaddedBytesAmount> {
ensure!(
registered_proof.major_version() == 1,
"unusupported version"
);
let config = registered_proof.as_v1_config();
let sector_size: u64 = u64::from(registered_proof.sector_size());
// TODO: Clean-up this method, as it more or less unrolls the with_shape macro in order to pass along the R and W generics as well as the Tree.
//
// Note also that not all of these sector sizes are production, so some could be pruned.
match sector_size {
SECTOR_SIZE_2_KIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape2KiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_4_KIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape4KiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_16_KIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape16KiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_32_KIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape32KiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_8_MIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape8MiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_16_MIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape16MiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_32_MIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape32MiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_64_MIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape64MiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_128_MIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape128MiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_256_MIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape256MiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_512_MIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape512MiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_1_GIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape1GiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_32_GIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape32GiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
SECTOR_SIZE_64_GIB => filecoin_proofs_v1::unseal_range::<_, _, _, SectorShape64GiB>(
config,
cache_path,
sealed_sector,
unsealed_output,
prover_id,
sector_id,
comm_d,
ticket,
offset,
num_bytes,
),
_ => panic!("unsupported sector size: {}", sector_size),
}
}
pub fn generate_piece_commitment<T: Read>(
registered_proof: RegisteredSealProof,
source: T,
piece_size: UnpaddedBytesAmount,
) -> Result<PieceInfo> {
use RegisteredSealProof::*;
match registered_proof {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg16MiBV1| StackedDrg32MiBV1
| StackedDrg64MiBV1 | StackedDrg128MiBV1 | StackedDrg256MiBV1
| StackedDrg512MiBV1 | StackedDrg32GiBV1
| StackedDrg64GiBV1 | StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1 | StackedDrg16MiBV1_1
| StackedDrg32MiBV1_1 | StackedDrg64MiBV1_1 | StackedDrg128MiBV1_1
| StackedDrg256MiBV1_1 | StackedDrg512MiBV1_1
| StackedDrg32GiBV1_1 | StackedDrg64GiBV1_1 => {
filecoin_proofs_v1::generate_piece_commitment(source, piece_size)
}
}
}
pub fn add_piece<R, W>(
registered_proof: RegisteredSealProof,
source: R,
target: W,
piece_size: UnpaddedBytesAmount,
piece_lengths: &[UnpaddedBytesAmount],
) -> Result<(PieceInfo, UnpaddedBytesAmount)>
where
R: Read,
W: Read + Write + Seek,
{
use RegisteredSealProof::*;
match registered_proof {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg16MiBV1 | StackedDrg32MiBV1
| StackedDrg64MiBV1 | StackedDrg128MiBV1 | StackedDrg256MiBV1
| StackedDrg512MiBV1 | StackedDrg32GiBV1
| StackedDrg64GiBV1 | StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1 | StackedDrg16MiBV1_1
| StackedDrg32MiBV1_1 | StackedDrg64MiBV1_1 | StackedDrg128MiBV1_1
| StackedDrg256MiBV1_1 | StackedDrg512MiBV1_1
| StackedDrg32GiBV1_1 | StackedDrg64GiBV1_1 => {
filecoin_proofs_v1::add_piece(source, target, piece_size, piece_lengths)
}
}
}
pub fn write_and_preprocess<R, W>(
registered_proof: RegisteredSealProof,
source: R,
target: W,
piece_size: UnpaddedBytesAmount,
) -> Result<(PieceInfo, UnpaddedBytesAmount)>
where
R: Read,
W: Read + Write + Seek,
{
use RegisteredSealProof::*;
match registered_proof {
StackedDrg2KiBV1 | StackedDrg8MiBV1 | StackedDrg16MiBV1 | StackedDrg32MiBV1
| StackedDrg64MiBV1 | StackedDrg128MiBV1 | StackedDrg256MiBV1
| StackedDrg512MiBV1 | StackedDrg32GiBV1
| StackedDrg64GiBV1 | StackedDrg2KiBV1_1 | StackedDrg8MiBV1_1 | StackedDrg16MiBV1_1
| StackedDrg32MiBV1_1 | StackedDrg64MiBV1_1 | StackedDrg128MiBV1_1
| StackedDrg256MiBV1_1 | StackedDrg512MiBV1_1
| StackedDrg32GiBV1_1 | StackedDrg64GiBV1_1 => {
filecoin_proofs_v1::write_and_preprocess(source, target, piece_size)
}
}
}
use std::path::PathBuf;
use crate::{Commitment, RegisteredPoStProof};
// A byte serialized representation of a vanilla proof.
pub type VanillaProofBytes = Vec<u8>;
/// The minimal information required about a replica, in order to be able to generate
/// a PoSt over it.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PrivateReplicaInfo {
/// The version of this replica.
pub(crate) registered_proof: RegisteredPoStProof,
/// The replica commitment.
pub(crate) comm_r: Commitment,
/// Contains sector-specific (e.g. merkle trees) assets
pub(crate) cache_dir: PathBuf,
/// Contains the replica.
pub(crate) replica_path: PathBuf,
}
impl PrivateReplicaInfo {
pub fn new(
registered_proof: RegisteredPoStProof,
comm_r: Commitment,
cache_dir: PathBuf,
replica_path: PathBuf,
) -> Self {
PrivateReplicaInfo {
registered_proof,
comm_r,
cache_dir,
replica_path,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PublicReplicaInfo {
/// The version of this replica.
pub(crate) registered_proof: RegisteredPoStProof,
/// The replica commitment.
pub(crate) comm_r: Commitment,
}
impl PublicReplicaInfo {
pub fn new(registered_proof: RegisteredPoStProof, comm_r: Commitment) -> Self {
PublicReplicaInfo {
registered_proof,
comm_r,
}
}
}
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