Skip to content

Commit

Permalink
feat: Introduce PoRepConfig::new_groth16() (#1635)
Browse files Browse the repository at this point in the history
Instead of constructing the `PoRepConfig` directly, use a constructor
function. This simplifies the code and makes things less error-prone.

Fixes #1632.

Co-authored-by: 卜翰 <[email protected]>
  • Loading branch information
hanbu97 and HammerBu authored Dec 12, 2022
1 parent a972a73 commit 62ed86f
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 216 deletions.
28 changes: 3 additions & 25 deletions fil-proofs-param/src/bin/paramcache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str::FromStr;
use dialoguer::{theme::ColorfulTheme, MultiSelect};
use filecoin_proofs::{
constants::{
DefaultPieceHasher, POREP_PARTITIONS, PUBLISHED_SECTOR_SIZES, WINDOW_POST_CHALLENGE_COUNT,
DefaultPieceHasher, PUBLISHED_SECTOR_SIZES, WINDOW_POST_CHALLENGE_COUNT,
WINDOW_POST_SECTOR_COUNT, WINNING_POST_CHALLENGE_COUNT, WINNING_POST_SECTOR_COUNT,
},
parameters::{public_params, window_post_public_params, winning_post_public_params},
Expand Down Expand Up @@ -213,37 +213,15 @@ fn generate_params_porep(sector_size: u64, api_version: ApiVersion) {
with_shape!(
sector_size,
cache_porep_params,
PoRepConfig {
sector_size: SectorSize(sector_size),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&sector_size)
.expect("unknown sector size"),
),
porep_id: [0; 32],
api_version,
}
PoRepConfig::new_groth16(sector_size, [0; 32], api_version)
);
}

fn generate_params_empty_sector_update(sector_size: u64, api_version: ApiVersion) {
with_shape!(
sector_size,
cache_empty_sector_update_params,
PoRepConfig {
sector_size: SectorSize(sector_size),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&sector_size)
.expect("unknown sector size"),
),
porep_id: [0; 32],
api_version,
}
PoRepConfig::new_groth16(sector_size, [0; 32], api_version)
);
}

Expand Down
16 changes: 7 additions & 9 deletions fil-proofs-tooling/src/bin/benchy/prodbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,8 @@ fn configure_global_config(inputs: &ProdbenchInputs) {
.expect("POREP_PARTITIONS poisoned")
.insert(inputs.sector_size_bytes(), inputs.porep_partitions);
POREP_MINIMUM_CHALLENGES
.write()
.expect("POREP_MINIMUM_CHALLENGES poisoned")
.insert(inputs.sector_size_bytes(), inputs.porep_challenges);
.get_mut()
.insert(inputs.sector_size_bytes(), inputs.porep_challenges as usize);
}

pub fn run(
Expand Down Expand Up @@ -314,12 +313,11 @@ fn generate_params(i: &ProdbenchInputs) {
);
let dummy_porep_id = [0; 32];

cache_porep_params(PoRepConfig {
sector_size,
partitions,
porep_id: dummy_porep_id,
api_version: i.api_version(),
});
cache_porep_params(PoRepConfig::new_groth16(
i.sector_size_bytes(),
dummy_porep_id,
i.api_version(),
));
}

fn cache_porep_params(porep_config: PoRepConfig) {
Expand Down
22 changes: 4 additions & 18 deletions fil-proofs-tooling/src/bin/benchy/window_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ use bincode::{deserialize, serialize};
use fil_proofs_tooling::measure::FuncMeasurement;
use fil_proofs_tooling::shared::{PROVER_ID, RANDOMNESS, TICKET_BYTES};
use fil_proofs_tooling::{measure, Metadata};
use filecoin_proofs::constants::{
POREP_PARTITIONS, WINDOW_POST_CHALLENGE_COUNT, WINDOW_POST_SECTOR_COUNT,
};
use filecoin_proofs::constants::{WINDOW_POST_CHALLENGE_COUNT, WINDOW_POST_SECTOR_COUNT};
use filecoin_proofs::types::{
PaddedBytesAmount, PieceInfo, PoRepConfig, PoRepProofPartitions, PoStConfig,
SealCommitPhase1Output, SealPreCommitOutput, SealPreCommitPhase1Output, SectorSize,
UnpaddedBytesAmount,
PaddedBytesAmount, PieceInfo, PoRepConfig, PoStConfig, SealCommitPhase1Output,
SealPreCommitOutput, SealPreCommitPhase1Output, SectorSize, UnpaddedBytesAmount,
};
use filecoin_proofs::{
add_piece, generate_piece_commitment, generate_window_post, seal_commit_phase1,
Expand Down Expand Up @@ -82,18 +79,7 @@ fn get_porep_config(sector_size: u64, api_version: ApiVersion) -> PoRepConfig {
let arbitrary_porep_id = [99; 32];

// Replicate the staged sector, write the replica file to `sealed_path`.
PoRepConfig {
sector_size: SectorSize(sector_size),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITONS poisoned")
.get(&(sector_size))
.expect("unknown sector size"),
),
porep_id: arbitrary_porep_id,
api_version,
}
PoRepConfig::new_groth16(sector_size, arbitrary_porep_id, api_version)
}

fn run_pre_commit_phases<Tree: 'static + MerkleTreeTrait>(
Expand Down
7 changes: 1 addition & 6 deletions fil-proofs-tooling/src/bin/circuitinfo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,7 @@ fn porep_info(sector_size: u64, api_version: ApiVersion) -> (CircuitInfo, usize)
let info = with_shape!(
sector_size,
get_porep_info,
PoRepConfig {
sector_size: SectorSize(sector_size),
partitions,
porep_id: [0; 32],
api_version,
}
PoRepConfig::new_groth16(sector_size, [0; 32], api_version)
);
(info, partitions.into())
}
Expand Down
17 changes: 3 additions & 14 deletions fil-proofs-tooling/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::io::{BufWriter, Seek, SeekFrom, Write};
use filecoin_proofs::{
add_piece, fauxrep_aux, seal_pre_commit_phase1, seal_pre_commit_phase2,
validate_cache_for_precommit_phase2, MerkleTreeTrait, PaddedBytesAmount, PieceInfo,
PoRepConfig, PoRepProofPartitions, PrivateReplicaInfo, PublicReplicaInfo, SealPreCommitOutput,
SealPreCommitPhase1Output, SectorSize, UnpaddedBytesAmount, POREP_PARTITIONS,
PoRepConfig, PrivateReplicaInfo, PublicReplicaInfo, SealPreCommitOutput,
SealPreCommitPhase1Output, SectorSize, UnpaddedBytesAmount,
};
use generic_array::typenum::Unsigned;
use log::info;
Expand Down Expand Up @@ -118,18 +118,7 @@ pub fn create_replicas<Tree: 'static + MerkleTreeTrait>(
let sector_size_unpadded_bytes_ammount =
UnpaddedBytesAmount::from(PaddedBytesAmount::from(sector_size));

let porep_config = PoRepConfig {
sector_size,
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("poisoned read access")
.get(&u64::from(sector_size))
.expect("unknown sector size"),
),
porep_id,
api_version,
};
let porep_config = PoRepConfig::new_groth16(u64::from(sector_size), porep_id, api_version);

let mut out: Vec<(SectorId, PreCommitReplicaOutput<Tree>)> = Default::default();
let mut sector_ids = Vec::new();
Expand Down
44 changes: 6 additions & 38 deletions filecoin-proofs/benches/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::time::Duration;
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use filecoin_proofs::{
caches::{get_stacked_srs_key, get_stacked_srs_verifier_key},
get_seal_inputs, PoRepConfig, PoRepProofPartitions, SectorShape2KiB, SectorShape32GiB,
SectorSize, POREP_PARTITIONS, SECTOR_SIZE_2_KIB, SECTOR_SIZE_32_GIB,
get_seal_inputs, PoRepConfig, SectorShape2KiB, SectorShape32GiB, SECTOR_SIZE_2_KIB,
SECTOR_SIZE_32_GIB,
};
use rand::{thread_rng, Rng};
use storage_proofs_core::{api_version::ApiVersion, is_legacy_porep_id};
Expand All @@ -28,18 +28,8 @@ fn bench_seal_inputs(c: &mut Criterion) {
porep_id[..8].copy_from_slice(&porep_id_v1_1.to_le_bytes());
assert!(!is_legacy_porep_id(porep_id));

let config = PoRepConfig {
sector_size: SectorSize(SECTOR_SIZE_2_KIB),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&SECTOR_SIZE_2_KIB)
.expect("unknown sector size"),
),
porep_id,
api_version: ApiVersion::V1_1_0,
};
let config = PoRepConfig::new_groth16(SECTOR_SIZE_2_KIB, porep_id, ApiVersion::V1_1_0);

let comm_r = [5u8; 32];
let comm_d = [6u8; 32];
let prover_id = [7u8; 32];
Expand Down Expand Up @@ -79,18 +69,7 @@ fn bench_stacked_srs_key(c: &mut Criterion) {
porep_id[..8].copy_from_slice(&porep_id_v1_1.to_le_bytes());
assert!(!is_legacy_porep_id(porep_id));

let config = PoRepConfig {
sector_size: SectorSize(SECTOR_SIZE_32_GIB),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&SECTOR_SIZE_32_GIB)
.expect("unknown sector size"),
),
porep_id,
api_version: ApiVersion::V1_1_0,
};
let config = PoRepConfig::new_groth16(SECTOR_SIZE_32_GIB, porep_id, ApiVersion::V1_1_0);

let mut group = c.benchmark_group("bench-stacked-srs-key");
for num_proofs_to_aggregate in params {
Expand Down Expand Up @@ -120,18 +99,7 @@ fn bench_stacked_srs_verifier_key(c: &mut Criterion) {
porep_id[..8].copy_from_slice(&porep_id_v1_1.to_le_bytes());
assert!(!is_legacy_porep_id(porep_id));

let config = PoRepConfig {
sector_size: SectorSize(SECTOR_SIZE_32_GIB),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&SECTOR_SIZE_32_GIB)
.expect("unknown sector size"),
),
porep_id,
api_version: ApiVersion::V1_1_0,
};
let config = PoRepConfig::new_groth16(SECTOR_SIZE_32_GIB, porep_id, ApiVersion::V1_1_0);

let mut group = c.benchmark_group("bench-stacked-srs-verifier-key");
for num_proofs_to_aggregate in params {
Expand Down
17 changes: 3 additions & 14 deletions filecoin-proofs/benches/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::time::Duration;

use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use filecoin_proofs::{
add_piece, get_seal_inputs, PaddedBytesAmount, PoRepConfig, PoRepProofPartitions,
SectorShape2KiB, SectorSize, UnpaddedBytesAmount, POREP_PARTITIONS, SECTOR_SIZE_2_KIB,
add_piece, get_seal_inputs, PaddedBytesAmount, PoRepConfig, SectorShape2KiB,
UnpaddedBytesAmount, SECTOR_SIZE_2_KIB,
};
use fr32::Fr32Reader;
use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -114,18 +114,7 @@ fn get_seal_inputs_benchmark(c: &mut Criterion) {
porep_id[..8].copy_from_slice(&porep_id_v1_1.to_le_bytes());
assert!(!is_legacy_porep_id(porep_id));

let config = PoRepConfig {
sector_size: SectorSize(SECTOR_SIZE_2_KIB),
partitions: PoRepProofPartitions(
*POREP_PARTITIONS
.read()
.expect("POREP_PARTITIONS poisoned")
.get(&SECTOR_SIZE_2_KIB)
.expect("unknown sector size"),
),
porep_id,
api_version: ApiVersion::V1_1_0,
};
let config = PoRepConfig::new_groth16(SECTOR_SIZE_2_KIB, porep_id, ApiVersion::V1_1_0);
let comm_r: [u8; 32] = [5u8; 32];
let comm_d: [u8; 32] = [6u8; 32];
let prover_id: [u8; 32] = [7u8; 32];
Expand Down
18 changes: 6 additions & 12 deletions filecoin-proofs/src/api/seal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ use storage_proofs_porep::stacked::{
TemporaryAux, TemporaryAuxCache,
};

use crate::POREP_MINIMUM_CHALLENGES;
use crate::{
api::{as_safe_commitment, commitment_from_fr, get_base_tree_leafs, get_base_tree_size},
caches::{
get_stacked_params, get_stacked_srs_key, get_stacked_srs_verifier_key,
get_stacked_verifying_key,
},
constants::{
DefaultBinaryTree, DefaultPieceDomain, DefaultPieceHasher, POREP_MINIMUM_CHALLENGES,
SINGLE_PARTITION_PROOF_LEN,
DefaultBinaryTree, DefaultPieceDomain, DefaultPieceHasher, SINGLE_PARTITION_PROOF_LEN,
},
parameters::setup_params,
pieces::{self, verify_pieces},
Expand Down Expand Up @@ -989,11 +989,8 @@ pub fn verify_seal<Tree: 'static + MerkleTreeTrait>(
&public_inputs,
&proof,
&ChallengeRequirements {
minimum_challenges: *POREP_MINIMUM_CHALLENGES
.read()
.expect("POREP_MINIMUM_CHALLENGES poisoned")
.get(&u64::from(SectorSize::from(porep_config)))
.expect("unknown sector size") as usize,
minimum_challenges: POREP_MINIMUM_CHALLENGES
.from_sector_size(u64::from(SectorSize::from(porep_config))),
},
)
};
Expand Down Expand Up @@ -1112,11 +1109,8 @@ pub fn verify_batch_seal<Tree: 'static + MerkleTreeTrait>(
&public_inputs,
&proofs,
&ChallengeRequirements {
minimum_challenges: *POREP_MINIMUM_CHALLENGES
.read()
.expect("POREP_MINIMUM_CHALLENGES poisoned")
.get(&u64::from(SectorSize::from(porep_config)))
.expect("unknown sector size") as usize,
minimum_challenges: POREP_MINIMUM_CHALLENGES
.from_sector_size(u64::from(SectorSize::from(porep_config))),
},
)
.map_err(Into::into);
Expand Down
59 changes: 41 additions & 18 deletions filecoin-proofs/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::sync::RwLock;
use std::{collections::HashMap, sync::RwLockWriteGuard};

pub use storage_proofs_core::drgraph::BASE_DEGREE as DRG_DEGREE;
pub use storage_proofs_porep::stacked::EXP_DEGREE;
Expand Down Expand Up @@ -47,24 +47,47 @@ pub const PUBLISHED_SECTOR_SIZES: [u64; 10] = [
SECTOR_SIZE_64_GIB,
];

pub struct PorepMinimumChallenges(RwLock<HashMap<u64, usize>>);
impl PorepMinimumChallenges {
fn new() -> Self {
Self(RwLock::new(
[
(SECTOR_SIZE_2_KIB, 2),
(SECTOR_SIZE_4_KIB, 2),
(SECTOR_SIZE_16_KIB, 2),
(SECTOR_SIZE_32_KIB, 2),
(SECTOR_SIZE_8_MIB, 2),
(SECTOR_SIZE_16_MIB, 2),
(SECTOR_SIZE_512_MIB, 2),
(SECTOR_SIZE_1_GIB, 2),
(SECTOR_SIZE_32_GIB, 176),
(SECTOR_SIZE_64_GIB, 176),
]
.iter()
.copied()
.collect(),
))
}

pub fn get_mut(&self) -> RwLockWriteGuard<'_, HashMap<u64, usize>> {
self.0.write().expect("POREP_MINIMUM_CHALLENGES poisoned")
}

pub fn from_sector_size(&self, sector_size: u64) -> usize {
match self
.0
.read()
.expect("POREP_MINIMUM_CHALLENGES poisoned")
.get(&sector_size)
{
Some(c) => *c,
None => panic!("invalid sector size"),
}
}
}

lazy_static! {
pub static ref POREP_MINIMUM_CHALLENGES: RwLock<HashMap<u64, u64>> = RwLock::new(
[
(SECTOR_SIZE_2_KIB, 2),
(SECTOR_SIZE_4_KIB, 2),
(SECTOR_SIZE_16_KIB, 2),
(SECTOR_SIZE_32_KIB, 2),
(SECTOR_SIZE_8_MIB, 2),
(SECTOR_SIZE_16_MIB, 2),
(SECTOR_SIZE_512_MIB, 2),
(SECTOR_SIZE_1_GIB, 2),
(SECTOR_SIZE_32_GIB, 176),
(SECTOR_SIZE_64_GIB, 176),
]
.iter()
.copied()
.collect()
);
pub static ref POREP_MINIMUM_CHALLENGES: PorepMinimumChallenges = PorepMinimumChallenges::new();
pub static ref POREP_PARTITIONS: RwLock<HashMap<u64, u8>> = RwLock::new(
[
(SECTOR_SIZE_2_KIB, 1),
Expand Down
Loading

0 comments on commit 62ed86f

Please sign in to comment.