Skip to content

Commit ef2da49

Browse files
authored
Merge pull request #450 from filecoin-project/tapered-challenges
feat(storage-proofs): implement challenge tapering
2 parents 40a93ef + c08a909 commit ef2da49

File tree

7 files changed

+207
-91
lines changed

7 files changed

+207
-91
lines changed

filecoin-proofs/examples/encoding.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use storage_proofs::drgraph::*;
3030
use storage_proofs::example_helper::prettyb;
3131
use storage_proofs::fr32::fr_into_bytes;
3232
use storage_proofs::hasher::{Hasher, PedersenHasher};
33-
use storage_proofs::layered_drgporep;
33+
use storage_proofs::layered_drgporep::{self, LayerChallenges};
3434
use storage_proofs::proof::ProofScheme;
3535
use storage_proofs::vde;
3636
use storage_proofs::zigzag_drgporep::*;
@@ -109,8 +109,7 @@ where
109109
},
110110
sloth_iter,
111111
},
112-
layers: 1,
113-
challenge_count: 1,
112+
layer_challenges: LayerChallenges::new_fixed(1, 1),
114113
};
115114

116115
info!(FCP_LOG, "running setup");

filecoin-proofs/examples/zigzag.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use storage_proofs::drgraph::*;
3636
use storage_proofs::example_helper::prettyb;
3737
use storage_proofs::fr32::fr_into_bytes;
3838
use storage_proofs::hasher::{Blake2sHasher, Hasher, PedersenHasher, Sha256Hasher};
39-
use storage_proofs::layered_drgporep;
39+
use storage_proofs::layered_drgporep::{self, LayerChallenges};
4040
use storage_proofs::porep::PoRep;
4141
use storage_proofs::proof::ProofScheme;
4242
use storage_proofs::zigzag_drgporep::*;
@@ -92,8 +92,7 @@ fn do_the_work<H: 'static>(
9292
m: usize,
9393
expansion_degree: usize,
9494
sloth_iter: usize,
95-
challenge_count: usize,
96-
layers: usize,
95+
layer_challenges: LayerChallenges,
9796
partitions: usize,
9897
circuit: bool,
9998
groth: bool,
@@ -108,8 +107,8 @@ fn do_the_work<H: 'static>(
108107
info!(FCP_LOG, "m: {}", m; "target" => "config");
109108
info!(FCP_LOG, "expansion_degree: {}", expansion_degree; "target" => "config");
110109
info!(FCP_LOG, "sloth: {}", sloth_iter; "target" => "config");
111-
info!(FCP_LOG, "challenge_count: {}", challenge_count; "target" => "config");
112-
info!(FCP_LOG, "layers: {}", layers; "target" => "config");
110+
info!(FCP_LOG, "layer_challenges: {:?}", layer_challenges; "target" => "config");
111+
info!(FCP_LOG, "layers: {}", layer_challenges.layers(); "target" => "config");
113112
info!(FCP_LOG, "partitions: {}", partitions; "target" => "config");
114113
info!(FCP_LOG, "circuit: {:?}", circuit; "target" => "config");
115114
info!(FCP_LOG, "groth: {:?}", groth; "target" => "config");
@@ -133,8 +132,7 @@ fn do_the_work<H: 'static>(
133132
},
134133
sloth_iter,
135134
},
136-
layers,
137-
challenge_count,
135+
layer_challenges: layer_challenges.clone(),
138136
};
139137

140138
info!(FCP_LOG, "running setup");
@@ -153,7 +151,6 @@ fn do_the_work<H: 'static>(
153151
stop_profile();
154152
let pub_inputs = layered_drgporep::PublicInputs::<H::Domain> {
155153
replica_id,
156-
challenge_count,
157154
tau: Some(tau.simplify().into()),
158155
comm_r_star: tau.comm_r_star,
159156
k: Some(0),
@@ -406,6 +403,8 @@ fn main() {
406403
let circuit = matches.is_present("circuit");
407404
let extract = matches.is_present("extract");
408405

406+
let challenges = LayerChallenges::new_fixed(layers, challenge_count);
407+
409408
info!(FCP_LOG, "hasher: {}", hasher; "target" => "config");
410409
match hasher.as_ref() {
411410
"pedersen" => {
@@ -414,8 +413,7 @@ fn main() {
414413
m,
415414
expansion_degree,
416415
sloth_iter,
417-
challenge_count,
418-
layers,
416+
challenges,
419417
partitions,
420418
circuit,
421419
groth,
@@ -429,8 +427,7 @@ fn main() {
429427
m,
430428
expansion_degree,
431429
sloth_iter,
432-
challenge_count,
433-
layers,
430+
challenges,
434431
partitions,
435432
circuit,
436433
groth,
@@ -444,8 +441,7 @@ fn main() {
444441
m,
445442
expansion_degree,
446443
sloth_iter,
447-
challenge_count,
448-
layers,
444+
challenges,
449445
partitions,
450446
circuit,
451447
groth,

filecoin-proofs/src/api/internal.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use storage_proofs::drgraph::{new_seed, DefaultTreeHasher, Graph};
2121
use storage_proofs::fr32::{bytes_into_fr, fr_into_bytes, Fr32Ary};
2222
use storage_proofs::hasher::pedersen::{PedersenDomain, PedersenHasher};
2323
use storage_proofs::hasher::{Domain, Hasher};
24-
use storage_proofs::layered_drgporep;
24+
use storage_proofs::layered_drgporep::{self, LayerChallenges};
2525
use storage_proofs::merkle::MerkleTree;
2626
use storage_proofs::parameter_cache::CacheableParameters;
2727
use storage_proofs::parameter_cache::{
@@ -113,7 +113,7 @@ const DEGREE: usize = 1; // TODO: 5; FIXME: increasing degree introduces a test
113113
const EXPANSION_DEGREE: usize = 6;
114114
const SLOTH_ITER: usize = 0;
115115
const LAYERS: usize = 2; // TODO: 10;
116-
const CHALLENGE_COUNT: usize = 1;
116+
const CHALLENGES: LayerChallenges = LayerChallenges::new_fixed(LAYERS, 1);
117117

118118
fn setup_params(sector_bytes: usize) -> layered_drgporep::SetupParams {
119119
assert!(
@@ -132,8 +132,7 @@ fn setup_params(sector_bytes: usize) -> layered_drgporep::SetupParams {
132132
},
133133
sloth_iter: SLOTH_ITER,
134134
},
135-
layers: LAYERS,
136-
challenge_count: CHALLENGE_COUNT,
135+
layer_challenges: CHALLENGES,
137136
}
138137
}
139138

@@ -387,7 +386,7 @@ pub fn seal<T: Into<PathBuf> + AsRef<Path>>(
387386
get_config(sector_config);
388387

389388
let public_params = public_params(proof_sector_bytes);
390-
let challenge_count = public_params.challenge_count;
389+
let challenges = public_params.layer_challenges;
391390
if let Some(delay) = delay_seconds {
392391
delay_seal(delay);
393392
};
@@ -434,7 +433,6 @@ pub fn seal<T: Into<PathBuf> + AsRef<Path>>(
434433

435434
let public_inputs = layered_drgporep::PublicInputs {
436435
replica_id,
437-
challenge_count,
438436
tau: Some(public_tau),
439437
comm_r_star: tau.comm_r_star,
440438
k: None,
@@ -614,7 +612,7 @@ pub fn verify_seal(
614612
let (_fake, _delay_seconds, _sector_bytes, proof_sector_bytes, uses_official_circuit) =
615613
get_config(sector_config);
616614

617-
let challenge_count = CHALLENGE_COUNT;
615+
let challenges = CHALLENGES;
618616
let prover_id = pad_safe_fr(prover_id_in);
619617
let sector_id = pad_safe_fr(sector_id_in);
620618
let replica_id = replica_id::<DefaultTreeHasher>(prover_id, sector_id);
@@ -638,7 +636,6 @@ pub fn verify_seal(
638636

639637
let public_inputs = layered_drgporep::PublicInputs::<<DefaultTreeHasher as Hasher>::Domain> {
640638
replica_id,
641-
challenge_count,
642639
tau: Some(Tau {
643640
comm_r: comm_r.into(),
644641
comm_d: comm_d.into(),

storage-proofs/src/challenge_derivation.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
use crate::crypto::blake2s::blake2s;
2-
use crate::hasher::Domain;
31
use byteorder::{LittleEndian, WriteBytesExt};
42
use num_bigint::BigUint;
53
use num_traits::cast::ToPrimitive;
64

5+
use crate::crypto::blake2s::blake2s;
6+
use crate::hasher::Domain;
7+
use crate::layered_drgporep::LayerChallenges;
8+
79
pub fn derive_challenges<D: Domain>(
8-
n: usize,
10+
challenges: &LayerChallenges,
911
layer: u8,
1012
leaves: usize,
1113
replica_id: &D,
1214
commitment: &D,
1315
k: u8,
1416
) -> Vec<usize> {
17+
let n = challenges.challenges_for_layer(layer as usize);
1518
(0..n)
1619
.map(|i| {
1720
let mut bytes = replica_id.into_bytes();
@@ -40,21 +43,29 @@ mod test {
4043
#[test]
4144
fn challenge_derivation() {
4245
let n = 200;
46+
let layers = 100;
47+
48+
let challenges = LayerChallenges::new_fixed(layers, n);
4349
let leaves = 1 << 30;
4450
let mut rng = thread_rng();
4551
let replica_id: PedersenDomain = rng.gen();
4652
let commitment: PedersenDomain = rng.gen();
4753
let partitions = 5;
4854
let total_challenges = partitions * n;
49-
let layers = 100;
5055

5156
let mut layers_with_duplicates = 0;
5257

5358
for layer in 0..layers {
5459
let mut histogram = HashMap::new();
5560
for k in 0..partitions {
56-
let challenges =
57-
derive_challenges(n, layer, leaves, &replica_id, &commitment, k as u8);
61+
let challenges = derive_challenges(
62+
&challenges,
63+
layer as u8,
64+
leaves,
65+
&replica_id,
66+
&commitment,
67+
k as u8,
68+
);
5869

5970
for challenge in challenges {
6071
let counter = histogram.entry(challenge).or_insert(0);
@@ -84,12 +95,26 @@ mod test {
8495
let partitions = 5;
8596
let layers = 100;
8697
let total_challenges = n * partitions;
98+
8799
for layer in 0..layers {
88-
let one_partition_challenges =
89-
derive_challenges(total_challenges, layer, leaves, &replica_id, &commitment, 0);
100+
let one_partition_challenges = derive_challenges(
101+
&LayerChallenges::new_fixed(layers, total_challenges),
102+
layer as u8,
103+
leaves,
104+
&replica_id,
105+
&commitment,
106+
0,
107+
);
90108
let many_partition_challenges = (0..partitions)
91109
.flat_map(|k| {
92-
derive_challenges(n, layer, leaves, &replica_id, &commitment, k as u8)
110+
derive_challenges(
111+
&LayerChallenges::new_fixed(layers, n),
112+
layer as u8,
113+
leaves,
114+
&replica_id,
115+
&commitment,
116+
k as u8,
117+
)
93118
})
94119
.collect::<Vec<_>>();
95120

storage-proofs/src/circuit/zigzag.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,11 @@ impl<'a, H: 'static + Hasher>
223223
let comm_r = pub_in.tau.unwrap().comm_r.into();
224224
inputs.push(comm_r);
225225

226-
for i in 0..pub_params.layers {
226+
for i in 0..pub_params.layer_challenges.layers() {
227227
let drgporep_pub_inputs = drgporep::PublicInputs {
228228
replica_id: pub_in.replica_id,
229229
challenges: pub_in.challenges(
230+
&pub_params.layer_challenges,
230231
pub_params.drg_porep_public_params.graph.size(),
231232
i as u8,
232233
k,
@@ -243,7 +244,7 @@ impl<'a, H: 'static + Hasher>
243244
drgporep_pub_params = <ZigZagDrgPoRep<H> as layered_drgporep::Layers>::transform(
244245
&drgporep_pub_params,
245246
i,
246-
pub_params.layers,
247+
pub_params.layer_challenges.layers(),
247248
);
248249
}
249250
inputs.push(pub_in.comm_r_star.into());
@@ -290,7 +291,7 @@ impl<'a, H: 'static + Hasher>
290291
let rng = &mut XorShiftRng::from_seed([0x3dbe6259, 0x8d313d76, 0x3237db17, 0xe5bc0654]);
291292
let replica_id = rng.gen();
292293

293-
let layers = (0..public_params.layers)
294+
let layers = (0..public_params.layer_challenges.layers())
294295
.map(|_| {
295296
let layer_public_inputs = drgporep::PublicInputs {
296297
replica_id,
@@ -327,10 +328,11 @@ mod tests {
327328
use crate::drgraph::new_seed;
328329
use crate::fr32::fr_into_bytes;
329330
use crate::hasher::pedersen::*;
330-
use crate::layered_drgporep;
331+
use crate::layered_drgporep::{self, LayerChallenges};
331332
use crate::porep::PoRep;
332333
use crate::proof::ProofScheme;
333334
use crate::zigzag_graph::{ZigZag, ZigZagGraph};
335+
334336
use pairing::Field;
335337
use rand::{Rng, SeedableRng, XorShiftRng};
336338
use sapling_crypto::jubjub::JubjubBls12;
@@ -341,8 +343,8 @@ mod tests {
341343
let nodes = 5;
342344
let degree = 1;
343345
let expansion_degree = 2;
344-
let challenge_count = 1;
345346
let num_layers = 2;
347+
let layer_challenges = LayerChallenges::new_fixed(num_layers, 1);
346348
let sloth_iter = 1;
347349

348350
let n = nodes; // FIXME: Consolidate variable names.
@@ -368,8 +370,7 @@ mod tests {
368370
},
369371
sloth_iter,
370372
},
371-
layers: num_layers,
372-
challenge_count,
373+
layer_challenges: layer_challenges.clone(),
373374
};
374375

375376
let pp = ZigZagDrgPoRep::setup(&sp).unwrap();
@@ -382,7 +383,6 @@ mod tests {
382383

383384
let pub_inputs = layered_drgporep::PublicInputs::<PedersenDomain> {
384385
replica_id: replica_id.into(),
385-
challenge_count,
386386
tau: Some(tau.simplify().into()),
387387
comm_r_star: tau.comm_r_star.into(),
388388
k: None,
@@ -455,7 +455,7 @@ mod tests {
455455
let base_degree = 2;
456456
let expansion_degree = 2;
457457
let replica_id: Fr = rng.gen();
458-
let challenge_count = 1;
458+
let layer_challenges = LayerChallenges::new_fixed(num_layers, 1);
459459
let challenge = 1;
460460
let sloth_iter = 2;
461461

@@ -478,8 +478,7 @@ mod tests {
478478
ZigZagGraph::new_zigzag(n, base_degree, expansion_degree, new_seed()),
479479
sloth_iter,
480480
),
481-
layers: num_layers,
482-
challenge_count,
481+
layer_challenges,
483482
};
484483

485484
ZigZagCircuit::<Bls12, PedersenHasher>::synthesize(
@@ -506,8 +505,8 @@ mod tests {
506505
let nodes = 5;
507506
let degree = 2;
508507
let expansion_degree = 1;
509-
let challenge_count = 2;
510508
let num_layers = 2;
509+
let layer_challenges = LayerChallenges::new_fixed(num_layers, 2);
511510
let sloth_iter = 1;
512511
let partition_count = 1;
513512

@@ -537,8 +536,7 @@ mod tests {
537536
},
538537
sloth_iter,
539538
},
540-
layers: num_layers,
541-
challenge_count,
539+
layer_challenges: layer_challenges.clone(),
542540
},
543541
partitions: Some(partition_count),
544542
};
@@ -555,7 +553,6 @@ mod tests {
555553

556554
let public_inputs = layered_drgporep::PublicInputs::<PedersenDomain> {
557555
replica_id: replica_id.into(),
558-
challenge_count,
559556
tau: Some(tau.simplify()),
560557
comm_r_star: tau.comm_r_star,
561558
k: None,

0 commit comments

Comments
 (0)