Skip to content

Commit

Permalink
refactor: introduce Challenges enum
Browse files Browse the repository at this point in the history
The `Challenges` enum introduces a bit more type safety.
  • Loading branch information
vmx committed Dec 5, 2023
1 parent c0ad725 commit ded2058
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 189 deletions.
4 changes: 2 additions & 2 deletions fil-proofs-tooling/src/bin/gen_graph_cache/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use filecoin_proofs::{
};
use serde::{Deserialize, Serialize};
use storage_proofs_core::{api_version::ApiVersion, merkle::MerkleTreeTrait, proof::ProofScheme};
use storage_proofs_porep::stacked::{LayerChallenges, SetupParams, StackedDrg};
use storage_proofs_porep::stacked::{Challenges, SetupParams, StackedDrg};

const PARENT_CACHE_JSON_OUTPUT: &str = "./parent_cache.json";

Expand All @@ -36,7 +36,7 @@ fn gen_graph_cache<Tree: 'static + MerkleTreeTrait>(
// we just use dummy values of 1 for the setup params.
let num_layers = 1;
let challenge_count = 1;
let challenges = LayerChallenges::new(challenge_count);
let challenges = Challenges::new_interactive(challenge_count);

let sp = SetupParams {
nodes,
Expand Down
15 changes: 9 additions & 6 deletions filecoin-proofs/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{ensure, Result};
use storage_proofs_core::{api_version::ApiFeature, proof::ProofScheme};
use storage_proofs_porep::stacked::{self, LayerChallenges, StackedDrg};
use storage_proofs_porep::stacked::{self, Challenges, StackedDrg};
use storage_proofs_post::fallback::{self, FallbackPoSt};

use crate::{
Expand Down Expand Up @@ -114,11 +114,13 @@ fn select_challenges(
partitions: usize,
minimum_total_challenges: usize,
use_synthetic: bool,
) -> LayerChallenges {
) -> Challenges {
let challenges = div_ceil(minimum_total_challenges, partitions);
let mut result = LayerChallenges::new(challenges);
result.use_synthetic = use_synthetic;
result
if use_synthetic {
Challenges::new_synthetic(challenges)
} else {
Challenges::new_interactive(challenges)
}
}

#[cfg(test)]
Expand All @@ -129,7 +131,8 @@ mod tests {

#[test]
fn partition_layer_challenges_test() {
let f = |partitions| select_challenges(partitions, 12, false).challenges_count_all();
let f =
|partitions| select_challenges(partitions, 12, false).num_challenges_per_partition();
// Update to ensure all supported PoRepProofPartitions options are represented here.
assert_eq!(6, f(usize::from(PoRepProofPartitions(2))));

Expand Down
2 changes: 1 addition & 1 deletion storage-proofs-porep/src/stacked/circuit/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher>
comm_r: None,
comm_r_last: None,
comm_c: None,
proofs: (0..public_params.challenges.challenges_count_all())
proofs: (0..public_params.challenges.num_challenges_per_partition())
.map(|_challenge_index| Proof::empty(public_params))
.collect(),
}
Expand Down
123 changes: 62 additions & 61 deletions storage-proofs-porep/src/stacked/vanilla/challenges.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use blstrs::Scalar as Fr;
use log::trace;
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};

use filecoin_hashers::Domain;
use sha2::{Digest, Sha256};
Expand All @@ -14,67 +13,35 @@ fn bigint_to_challenge(bigint: BigUint, sector_nodes: usize) -> usize {
non_zero_node.to_u32_digits()[0] as usize
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LayerChallenges {
/// The maximum count of challenges
max_count: usize,
pub use_synthetic: bool,
#[derive(Clone, Debug)]
pub struct InteractiveChallenges {
challenges_per_partition: usize,
}

impl LayerChallenges {
pub const fn new(max_count: usize) -> Self {
LayerChallenges {
max_count,
use_synthetic: false,
impl InteractiveChallenges {
pub const fn new(challenges_per_partition: usize) -> Self {
Self {
challenges_per_partition,
}
}

pub const fn new_synthetic(max_count: usize) -> Self {
LayerChallenges {
max_count,
use_synthetic: true,
pub const fn new_synthetic(challenges_per_partition: usize) -> Self {
Self {
challenges_per_partition,
}
}

/// Porep challenge count per partition.
pub fn challenges_count_all(&self) -> usize {
self.max_count
}

/// Returns the porep challenges for partition `k`.
pub fn derive<D: Domain>(
&self,
sector_nodes: usize,
replica_id: &D,
comm_r: &D,
seed: &[u8; 32],
k: u8,
) -> Vec<usize> {
assert!(sector_nodes > 2, "Too few sector_nodes: {}", sector_nodes);
if self.use_synthetic {
trace!(
"deriving porep challenges from synthetic challenges (k = {})",
k,
);
self.derive_porep_synth(sector_nodes, replica_id, comm_r, seed, k)
} else {
trace!("deriving porep challenges (k = {})", k);
self.derive_porep(sector_nodes, replica_id, seed, k)
}
}

/// Returns the porep challenges for partition `k`.
pub(crate) fn derive_porep<D: Domain>(
&self,
sector_nodes: usize,
replica_id: &D,
seed: &[u8; 32],
k: u8,
) -> Vec<usize> {
let partition_challenge_count = self.challenges_count_all();
(0..partition_challenge_count)
(0..self.challenges_per_partition)
.map(|i| {
let j: u32 = ((partition_challenge_count * k as usize) + i) as u32;
let j: u32 = ((self.challenges_per_partition * k as usize) + i) as u32;

let hash = Sha256::new()
.chain_update(replica_id.into_bytes())
Expand All @@ -87,53 +54,60 @@ impl LayerChallenges {
})
.collect()
}
}

#[derive(Clone, Debug)]
pub struct SynthChallenges {
challenges_per_partition: usize,
}

impl SynthChallenges {
pub const fn new(challenges_per_partition: usize) -> Self {
Self {
challenges_per_partition,
}
}

/// Returns the porep challenges for partition `k` taken from the synthetic challenges.
fn derive_porep_synth<D: Domain>(
pub fn derive<D: Domain>(
&self,
sector_nodes: usize,
replica_id: &D,
comm_r: &D,
seed: &[u8; 32],
k: u8,
) -> Vec<usize> {
assert!(self.use_synthetic);
let partition_challenge_count = self.challenges_count_all();
let replica_id: Fr = (*replica_id).into();
let comm_r: Fr = (*comm_r).into();
SynthChallengeGenerator::default(sector_nodes, &replica_id, &comm_r)
.gen_porep_partition_challenges(partition_challenge_count, seed, k as usize)
.gen_porep_partition_challenges(self.challenges_per_partition, seed, k as usize)
}

/// Returns the synthetic challenge indexes of the porep challenges for partition `k`.
pub fn derive_synth_indexes<D: Domain>(
pub fn derive_indexes<D: Domain>(
&self,
sector_nodes: usize,
replica_id: &D,
comm_r: &D,
seed: &[u8; 32],
k: u8,
) -> Vec<usize> {
assert!(self.use_synthetic, "synth-porep is disabled");
trace!(
"generating porep partition synthetic challenge indexes (k = {})",
k,
);
let partition_challenge_count = self.challenges_count_all();
let replica_id: Fr = (*replica_id).into();
let comm_r: Fr = (*comm_r).into();
SynthChallengeGenerator::default(sector_nodes, &replica_id, &comm_r)
.gen_partition_synth_indexes(partition_challenge_count, seed, k as usize)
.gen_partition_synth_indexes(self.challenges_per_partition, seed, k as usize)
}

/// Returns the entire synthetic challenge set.
pub fn derive_synthetic<D: Domain>(
&self,
sector_nodes: usize,
replica_id: &D,
comm_r: &D,
) -> Vec<usize> {
assert!(self.use_synthetic);
let replica_id: Fr = (*replica_id).into();
let comm_r: Fr = (*comm_r).into();
let synth = SynthChallengeGenerator::default(sector_nodes, &replica_id, &comm_r);
Expand All @@ -145,6 +119,33 @@ impl LayerChallenges {
}
}

#[derive(Clone, Debug)]
pub enum Challenges {
Interactive(InteractiveChallenges),
Synth(SynthChallenges),
}

impl Challenges {
pub const fn new_interactive(challenges_per_partition: usize) -> Self {
Self::Interactive(InteractiveChallenges::new(challenges_per_partition))
}

pub const fn new_synthetic(challenges_per_partition: usize) -> Self {
Self::Synth(SynthChallenges::new(challenges_per_partition))
}

pub fn num_challenges_per_partition(&self) -> usize {
match self {
Self::Interactive(InteractiveChallenges {
challenges_per_partition,
})
| Self::Synth(SynthChallenges {
challenges_per_partition,
}) => *challenges_per_partition,
}
}
}

#[derive(Debug, Default)]
pub struct ChallengeRequirements {
pub minimum_challenges: usize,
Expand Down Expand Up @@ -358,10 +359,10 @@ mod test {

#[test]
fn test_calculate_fixed_challenges() {
let layer_challenges = LayerChallenges::new(333);
let layer_challenges = Challenges::new_interactive(333);
let expected = 333;

let calculated_count = layer_challenges.challenges_count_all();
let calculated_count = layer_challenges.num_challenges_per_partition();
assert_eq!(expected as usize, calculated_count);
}

Expand All @@ -370,7 +371,7 @@ mod test {
let n = 200;
let layers = 100;

let challenges = LayerChallenges::new(n);
let challenges = InteractiveChallenges::new(n);
let leaves = 1 << 30;
let rng = &mut thread_rng();
let replica_id: Sha256Domain = Sha256Domain::random(rng);
Expand All @@ -383,7 +384,7 @@ mod test {
for _layer in 1..=layers {
let mut histogram = HashMap::new();
for k in 0..partitions {
let challenges = challenges.derive_porep(leaves, &replica_id, &seed, k as u8);
let challenges = challenges.derive(leaves, &replica_id, &seed, k as u8);

for challenge in challenges {
let counter = histogram.entry(challenge).or_insert(0);
Expand Down Expand Up @@ -418,10 +419,10 @@ mod test {

for _layer in 1..=layers {
let one_partition_challenges =
LayerChallenges::new(total_challenges).derive_porep(leaves, &replica_id, &seed, 0);
InteractiveChallenges::new(total_challenges).derive(leaves, &replica_id, &seed, 0);
let many_partition_challenges = (0..partitions)
.flat_map(|k| {
LayerChallenges::new(n).derive_porep(leaves, &replica_id, &seed, k as u8)
InteractiveChallenges::new(n).derive(leaves, &replica_id, &seed, k as u8)
})
.collect::<Vec<_>>();

Expand Down
2 changes: 1 addition & 1 deletion storage-proofs-porep/src/stacked/vanilla/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod utils;

pub use challenges::{
synthetic::SYNTHETIC_POREP_VANILLA_PROOFS_EXT, synthetic::SYNTHETIC_POREP_VANILLA_PROOFS_KEY,
ChallengeRequirements, LayerChallenges,
ChallengeRequirements, Challenges,
};
pub use clear_files::{clear_cache_dir, clear_synthetic_proofs};
pub use column::Column;
Expand Down
Loading

0 comments on commit ded2058

Please sign in to comment.