Skip to content

Commit

Permalink
refactor: split challenges and layer information
Browse files Browse the repository at this point in the history
At the moment the challenges and the information about the number of layers
is put into a single `LayerChallenges` struct. This misleads into thinking
that the challenge generation is related to the number of layers, but it is
*not*. `LayerChallenges` now only contains the information about challenges.

This makes the code eaier to follow, as it's now clearer, whether a function
needs the layers or the challenges.
  • Loading branch information
vmx committed Nov 16, 2023
1 parent 995c0c3 commit 329eacb
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 159 deletions.
7 changes: 4 additions & 3 deletions fil-proofs-tooling/src/bin/gen_graph_cache/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ fn gen_graph_cache<Tree: 'static + MerkleTreeTrait>(

// Note that layers and challenge_count don't affect the graph, so
// we just use dummy values of 1 for the setup params.
let layers = 1;
let num_layers = 1;
let challenge_count = 1;
let layer_challenges = LayerChallenges::new(layers, challenge_count);
let challenges = LayerChallenges::new(challenge_count);

let sp = SetupParams {
nodes,
degree: DRG_DEGREE,
expansion_degree: EXP_DEGREE,
porep_id,
layer_challenges,
challenges,
num_layers,
api_version,
api_features: vec![],
};
Expand Down
2 changes: 1 addition & 1 deletion filecoin-proofs/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ where

StackedDrg::<Tree, DefaultPieceHasher>::extract_and_invert_transform_layers(
&pp.graph,
&pp.layer_challenges,
pp.num_layers,
&replica_id,
data,
config,
Expand Down
22 changes: 11 additions & 11 deletions filecoin-proofs/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ pub fn window_post_setup_params(post_config: &PoStConfig) -> WindowPostSetupPara
pub fn setup_params(porep_config: &PoRepConfig) -> Result<stacked::SetupParams> {
let use_synthetic = porep_config.feature_enabled(ApiFeature::SyntheticPoRep);
let sector_bytes = porep_config.padded_bytes_amount();
let layer_challenges = select_challenges(
let challenges = select_challenges(
usize::from(porep_config.partitions),
POREP_MINIMUM_CHALLENGES.from_sector_size(u64::from(sector_bytes)),
*LAYERS
.read()
.expect("LAYERS poisoned")
.get(&u64::from(sector_bytes))
.expect("unknown sector size"),
use_synthetic,
);
let num_layers = *LAYERS
.read()
.expect("LAYERS poisoned")
.get(&u64::from(sector_bytes))
.expect("unknown sector size");
let sector_bytes = u64::from(sector_bytes);

ensure!(
Expand All @@ -97,7 +97,8 @@ pub fn setup_params(porep_config: &PoRepConfig) -> Result<stacked::SetupParams>
degree,
expansion_degree,
porep_id: porep_config.porep_id,
layer_challenges,
challenges,
num_layers,
api_version: porep_config.api_version,
api_features: porep_config.api_features.clone(),
})
Expand All @@ -106,14 +107,13 @@ pub fn setup_params(porep_config: &PoRepConfig) -> Result<stacked::SetupParams>
fn select_challenges(
partitions: usize,
minimum_total_challenges: usize,
layers: usize,
use_synthetic: bool,
) -> LayerChallenges {
let mut count = 1;
let mut guess = LayerChallenges::new(layers, count);
let mut guess = LayerChallenges::new(count);
while partitions * guess.challenges_count_all() < minimum_total_challenges {
count += 1;
guess = LayerChallenges::new(layers, count);
guess = LayerChallenges::new(count);
}

guess.use_synthetic = use_synthetic;
Expand All @@ -128,7 +128,7 @@ mod tests {

#[test]
fn partition_layer_challenges_test() {
let f = |partitions| select_challenges(partitions, 12, 11, false).challenges_count_all();
let f = |partitions| select_challenges(partitions, 12, false).challenges_count_all();
// 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/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Column {
/// Create an empty `Column`, used in `blank_circuit`s.
pub fn empty<Tree: MerkleTreeTrait>(params: &PublicParams<Tree>) -> Self {
Column {
rows: vec![None; params.layer_challenges.layers()],
rows: vec![None; params.num_layers],
}
}

Expand Down
6 changes: 3 additions & 3 deletions storage-proofs-porep/src/stacked/circuit/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl<'a, Tree: MerkleTreeTrait, G: Hasher> Circuit<Fr> for StackedCircuit<'a, Tr
for (i, proof) in proofs.into_iter().enumerate() {
proof.synthesize(
&mut cs.namespace(|| format!("challenge_{}", i)),
public_params.layer_challenges.layers(),
public_params.num_layers,
&comm_d_num,
&comm_c_num,
&comm_r_last_num,
Expand Down Expand Up @@ -234,7 +234,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher>
let por_params = PoR::<Tree>::setup(&por_setup_params)?;
let por_params_d = PoR::<BinaryMerkleTree<G>>::setup(&por_setup_params)?;

let all_challenges = pub_in.challenges(&pub_params.layer_challenges, graph.size(), k);
let all_challenges = pub_in.challenges(&pub_params.challenges, graph.size(), k);

for challenge in all_challenges.into_iter() {
// comm_d inclusion proof for the data leaf
Expand Down Expand Up @@ -336,7 +336,7 @@ impl<'a, Tree: 'static + MerkleTreeTrait, G: 'static + Hasher>
comm_r: None,
comm_r_last: None,
comm_c: None,
proofs: (0..public_params.layer_challenges.challenges_count_all())
proofs: (0..public_params.challenges.challenges_count_all())
.map(|_challenge_index| Proof::empty(public_params))
.collect(),
}
Expand Down
46 changes: 9 additions & 37 deletions storage-proofs-porep/src/stacked/vanilla/challenges.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt;

use blstrs::Scalar as Fr;
use log::trace;
use num_bigint::BigUint;
Expand All @@ -16,49 +14,28 @@ fn bigint_to_challenge(bigint: BigUint, sector_nodes: usize) -> usize {
non_zero_node.to_u32_digits()[0] as usize
}

#[derive(Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct LayerChallenges {
/// How many layers we are generating challenges for.
layers: usize,
/// The maximum count of challenges
max_count: usize,
pub use_synthetic: bool,
}

/// Note that since this is used in the PublicParams 'identifier'
/// method (which affects the cacheable parameters), adding a single
/// field would normally change the default 'format!' of it, so we now
/// have to override it for backwards compatibility.
impl fmt::Debug for LayerChallenges {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("LayerChallenges")
.field("layers", &self.layers)
.field("max_count", &self.max_count)
.finish()
}
}

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

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

pub fn layers(&self) -> usize {
self.layers
}

/// Porep challenge count per partition.
pub fn challenges_count_all(&self) -> usize {
self.max_count
Expand Down Expand Up @@ -87,7 +64,7 @@ impl LayerChallenges {
}

/// Returns the porep challenges for partition `k`.
fn derive_porep<D: Domain>(
pub(crate) fn derive_porep<D: Domain>(
&self,
sector_nodes: usize,
replica_id: &D,
Expand Down Expand Up @@ -386,7 +363,7 @@ mod test {

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

let calculated_count = layer_challenges.challenges_count_all();
Expand All @@ -398,7 +375,7 @@ mod test {
let n = 200;
let layers = 100;

let challenges = LayerChallenges::new(layers, n);
let challenges = LayerChallenges::new(n);
let leaves = 1 << 30;
let rng = &mut thread_rng();
let replica_id: Sha256Domain = Sha256Domain::random(rng);
Expand Down Expand Up @@ -445,16 +422,11 @@ mod test {
let total_challenges = n * partitions;

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

Expand Down
53 changes: 35 additions & 18 deletions storage-proofs-porep/src/stacked/vanilla/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ pub struct SetupParams {
pub expansion_degree: usize,

pub porep_id: [u8; 32],
pub layer_challenges: LayerChallenges,
pub challenges: LayerChallenges,
pub num_layers: usize,
pub api_version: ApiVersion,
pub api_features: Vec<ApiFeature>,
}
Expand All @@ -60,7 +61,8 @@ where
Tree: 'static + MerkleTreeTrait,
{
pub graph: StackedBucketGraph<Tree::Hasher>,
pub layer_challenges: LayerChallenges,
pub challenges: LayerChallenges,
pub num_layers: usize,
_t: PhantomData<Tree>,
}

Expand All @@ -71,7 +73,8 @@ where
fn clone(&self) -> Self {
Self {
graph: self.graph.clone(),
layer_challenges: self.layer_challenges.clone(),
challenges: self.challenges.clone(),
num_layers: self.num_layers,
_t: Default::default(),
}
}
Expand All @@ -81,10 +84,15 @@ impl<Tree> PublicParams<Tree>
where
Tree: MerkleTreeTrait,
{
pub fn new(graph: StackedBucketGraph<Tree::Hasher>, layer_challenges: LayerChallenges) -> Self {
pub fn new(
graph: StackedBucketGraph<Tree::Hasher>,
challenges: LayerChallenges,
num_layers: usize,
) -> Self {
PublicParams {
graph,
layer_challenges,
challenges,
num_layers,
_t: PhantomData,
}
}
Expand All @@ -94,11 +102,14 @@ impl<Tree> ParameterSetMetadata for PublicParams<Tree>
where
Tree: MerkleTreeTrait,
{
// This identifier is used for the hash value of the parameters file name, the output string
// must stay the same at all times, else the filename parameters will be wrong.
fn identifier(&self) -> String {
format!(
"layered_drgporep::PublicParams{{ graph: {}, challenges: {:?}, tree: {} }}",
"layered_drgporep::PublicParams{{ graph: {}, challenges: LayerChallenges {{ layers: {}, max_count: {} }}, tree: {} }}",
self.graph.identifier(),
self.layer_challenges,
self.num_layers,
self.challenges.challenges_count_all(),
Tree::display()
)
}
Expand All @@ -113,7 +124,11 @@ where
Tree: MerkleTreeTrait,
{
fn from(other: &PublicParams<Tree>) -> PublicParams<Tree> {
PublicParams::new(other.graph.clone(), other.layer_challenges.clone())
PublicParams::new(
other.graph.clone(),
other.challenges.clone(),
other.num_layers,
)
}
}

Expand All @@ -137,18 +152,18 @@ impl<T: Domain, S: Domain> PublicInputs<T, S> {
/// in a single partition `k = 0`.
pub fn challenges(
&self,
layer_challenges: &LayerChallenges,
challenges: &LayerChallenges,
sector_nodes: usize,
k: Option<usize>,
) -> Vec<usize> {
let k = k.unwrap_or(0);

assert!(
layer_challenges.use_synthetic || self.seed.is_some(),
challenges.use_synthetic || self.seed.is_some(),
"challenge seed must be set when synth porep is disabled",
);
assert!(
!layer_challenges.use_synthetic || self.tau.is_some(),
!challenges.use_synthetic || self.tau.is_some(),
"comm_r must be set prior to generating synth porep challenges",
);
let comm_r = self
Expand All @@ -158,9 +173,9 @@ impl<T: Domain, S: Domain> PublicInputs<T, S> {
.unwrap_or(T::default());

if let Some(seed) = self.seed.as_ref() {
layer_challenges.derive(sector_nodes, &self.replica_id, &comm_r, seed, k as u8)
challenges.derive(sector_nodes, &self.replica_id, &comm_r, seed, k as u8)
} else if k == 0 {
layer_challenges.derive_synthetic(sector_nodes, &self.replica_id, &comm_r)
challenges.derive_synthetic(sector_nodes, &self.replica_id, &comm_r)
} else {
vec![]
}
Expand Down Expand Up @@ -261,7 +276,7 @@ impl<Tree: MerkleTreeTrait, G: Hasher> Proof<Tree, G> {

check!(self.verify_final_replica_layer(challenge));

check!(self.verify_labels(replica_id, &pub_params.layer_challenges));
check!(self.verify_labels(replica_id, pub_params.num_layers));

trace!("verify encoding");

Expand All @@ -278,10 +293,10 @@ impl<Tree: MerkleTreeTrait, G: Hasher> Proof<Tree, G> {
fn verify_labels(
&self,
replica_id: &<Tree::Hasher as Hasher>::Domain,
layer_challenges: &LayerChallenges,
num_layers: usize,
) -> bool {
// Verify Labels Layer 1..layers
for layer in 1..=layer_challenges.layers() {
for layer in 1..=num_layers {
trace!("verify labeling (layer: {})", layer,);

check!(self.labeling_proofs.get(layer - 1).is_some());
Expand Down Expand Up @@ -1270,7 +1285,8 @@ mod tests {
degree: BASE_DEGREE,
expansion_degree: EXP_DEGREE,
porep_id: [1u8; 32],
layer_challenges: LayerChallenges::new(11, 18),
challenges: LayerChallenges::new(18),
num_layers: 11,
api_version: ApiVersion::V1_1_0,
api_features: vec![],
};
Expand All @@ -1285,7 +1301,8 @@ mod tests {
degree: BASE_DEGREE,
expansion_degree: EXP_DEGREE,
porep_id: [1u8; 32],
layer_challenges: LayerChallenges::new(11, 18),
challenges: LayerChallenges::new(18),
num_layers: 11,
api_version: ApiVersion::V1_1_0,
api_features: vec![],
};
Expand Down
Loading

0 comments on commit 329eacb

Please sign in to comment.