Skip to content

Commit aa41575

Browse files
committed
fix: use ensure!() instead of assert!() for storage-proofs
1 parent a33e7ac commit aa41575

35 files changed

+509
-373
lines changed

fil-proofs-tooling/src/bin/benchy/stacked.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ pub struct RunOpts {
497497
}
498498

499499
pub fn run(opts: RunOpts) -> anyhow::Result<()> {
500-
let config = StackedConfig::new(opts.layers, opts.window_challenges, opts.wrapper_challenges);
500+
let config = StackedConfig::new(opts.layers, opts.window_challenges, opts.wrapper_challenges)?;
501501

502502
let params = Params {
503503
config,

filecoin-proofs/examples/stacked.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ fn main() {
507507
let circuit = matches.is_present("circuit");
508508
let extract = matches.is_present("extract");
509509

510-
let config = StackedConfig::new(layers, window_challenge_count, wrapper_challenge_count);
510+
let config =
511+
StackedConfig::new(layers, window_challenge_count, wrapper_challenge_count).unwrap();
511512

512513
info!("hasher: {}", hasher);
513514
match hasher.as_ref() {

filecoin-proofs/src/parameters.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ pub fn setup_params(
5757
sector_bytes: PaddedBytesAmount,
5858
partitions: usize,
5959
) -> Result<stacked::SetupParams> {
60-
let window_challenges = select_challenges(partitions, POREP_WINDOW_MINIMUM_CHALLENGES, LAYERS);
60+
let window_challenges = select_challenges(partitions, POREP_WINDOW_MINIMUM_CHALLENGES, LAYERS)?;
6161
let wrapper_challenges =
62-
select_challenges(partitions, POREP_WRAPPER_MINIMUM_CHALLENGES, LAYERS);
63-
let window_size_nodes = window_size_nodes_for_sector_bytes(sector_bytes).unwrap();
62+
select_challenges(partitions, POREP_WRAPPER_MINIMUM_CHALLENGES, LAYERS)?;
63+
let window_size_nodes = window_size_nodes_for_sector_bytes(sector_bytes)?;
6464
let sector_bytes = usize::from(sector_bytes);
6565

6666
let config = StackedConfig {
@@ -96,14 +96,14 @@ fn select_challenges(
9696
partitions: usize,
9797
minimum_total_challenges: usize,
9898
layers: usize,
99-
) -> LayerChallenges {
99+
) -> Result<LayerChallenges> {
100100
let mut count = 1;
101-
let mut guess = LayerChallenges::new(layers, count);
101+
let mut guess = LayerChallenges::new(layers, count)?;
102102
while partitions * guess.challenges_count_all() < minimum_total_challenges {
103103
count += 1;
104-
guess = LayerChallenges::new(layers, count);
104+
guess = LayerChallenges::new(layers, count)?;
105105
}
106-
guess
106+
Ok(guess)
107107
}
108108

109109
#[cfg(test)]
@@ -114,7 +114,11 @@ mod tests {
114114

115115
#[test]
116116
fn partition_layer_challenges_test() {
117-
let f = |partitions| select_challenges(partitions, 12, LAYERS).challenges_count_all();
117+
let f = |partitions| {
118+
select_challenges(partitions, 12, LAYERS)
119+
.unwrap()
120+
.challenges_count_all()
121+
};
118122
// Update to ensure all supported PoRepProofPartitions options are represented here.
119123
assert_eq!(6, f(usize::from(PoRepProofPartitions(2))));
120124

filecoin-proofs/src/pieces.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ mod tests {
646646
BASE_DEGREE,
647647
EXP_DEGREE,
648648
new_seed(),
649-
);
649+
)?;
650650

651651
let mut staged_sector = Vec::with_capacity(u64::from(sector_size) as usize);
652652
let mut staged_sector_io = std::io::Cursor::new(&mut staged_sector);

storage-proofs/benches/drgraph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn drgraph(c: &mut Criterion) {
1010
.iter()
1111
.map(|n| {
1212
(
13-
BucketGraph::<PedersenHasher>::new(*n, BASE_DEGREE, 0, new_seed()),
13+
BucketGraph::<PedersenHasher>::new(*n, BASE_DEGREE, 0, new_seed()).unwrap(),
1414
2,
1515
)
1616
})
@@ -22,7 +22,7 @@ fn drgraph(c: &mut Criterion) {
2222
|b, (graph, i)| {
2323
b.iter(|| {
2424
let mut parents = vec![0; 6];
25-
black_box(graph.parents(*i, &mut parents));
25+
black_box(graph.parents(*i, &mut parents).unwrap());
2626
})
2727
},
2828
params,

storage-proofs/benches/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn pregenerate_data<H: Hasher>(degree: usize) -> Pregenerated<H> {
2929
let parents: Vec<u32> = (0..degree as u32).map(|pos| pos).collect();
3030
let replica_id: H::Domain = H::Domain::random(&mut rng);
3131

32-
let graph = StackedBucketGraph::<H>::new_stacked(degree + 1, degree, 0, new_seed());
32+
let graph = StackedBucketGraph::<H>::new_stacked(degree + 1, degree, 0, new_seed()).unwrap();
3333

3434
Pregenerated {
3535
data,

storage-proofs/benches/merkle.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ fn merkle_benchmark(c: &mut Criterion) {
2626
BASE_DEGREE,
2727
EXP_DEGREE,
2828
new_seed(),
29-
);
29+
)
30+
.unwrap();
3031

3132
b.iter(|| black_box(graph.merkle_tree(&data).unwrap()))
3233
},
@@ -40,7 +41,8 @@ fn merkle_benchmark(c: &mut Criterion) {
4041
BASE_DEGREE,
4142
EXP_DEGREE,
4243
new_seed(),
43-
);
44+
)
45+
.unwrap();
4446

4547
b.iter(|| black_box(graph.merkle_tree(&data).unwrap()))
4648
})

storage-proofs/benches/parents.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ fn stop_profile() {}
4141

4242
fn pregenerate_graph<H: Hasher>(size: usize) -> StackedBucketGraph<H> {
4343
let seed = [1u8; 28];
44-
StackedBucketGraph::<H>::new_stacked(size, BASE_DEGREE, EXP_DEGREE, seed)
44+
StackedBucketGraph::<H>::new_stacked(size, BASE_DEGREE, EXP_DEGREE, seed).unwrap()
4545
}
4646

4747
fn parents_loop<H: Hasher, G: Graph<H>>(graph: &G, parents: &mut [u32]) {
4848
(0..graph.size())
49-
.map(|node| graph.parents(node, parents))
49+
.map(|node| graph.parents(node, parents).unwrap())
5050
.collect()
5151
}
5252

storage-proofs/src/circuit/drgporep.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::compound_proof::{CircuitComponent, CompoundProof};
1515
use crate::crypto::pedersen::JJ_PARAMS;
1616
use crate::drgporep::DrgPoRep;
1717
use crate::drgraph::Graph;
18+
use crate::error::Result;
1819
use crate::fr32::fr_into_bytes;
1920
use crate::hasher::Hasher;
2021
use crate::merklepor;
@@ -148,11 +149,14 @@ where
148149
// We can ignore k because challenges are generated by caller and included
149150
// in PublicInputs.
150151
_k: Option<usize>,
151-
) -> Vec<Fr> {
152+
) -> Result<Vec<Fr>> {
152153
let replica_id = pub_in.replica_id.expect("missing replica id");
153154
let challenges = &pub_in.challenges;
154155

155-
assert_eq!(pub_in.tau.is_none(), pub_params.private);
156+
ensure!(
157+
pub_in.tau.is_none() == pub_params.private,
158+
"Public input parameter tau must be unset"
159+
);
156160

157161
let (comm_r, comm_d) = match pub_in.tau {
158162
None => (None, None),
@@ -172,7 +176,7 @@ where
172176
let mut parents = vec![0; pub_params.graph.degree()];
173177
for challenge in challenges {
174178
let mut por_nodes = vec![*challenge as u32];
175-
pub_params.graph.parents(*challenge, &mut parents);
179+
pub_params.graph.parents(*challenge, &mut parents)?;
176180
por_nodes.extend_from_slice(&parents);
177181

178182
for node in por_nodes {
@@ -184,7 +188,7 @@ where
184188
&por_pub_inputs,
185189
&por_pub_params,
186190
None,
187-
);
191+
)?;
188192

189193
input.extend(por_inputs);
190194
}
@@ -195,24 +199,30 @@ where
195199
};
196200

197201
let por_inputs =
198-
PoRCompound::<H>::generate_public_inputs(&por_pub_inputs, &por_pub_params, None);
202+
PoRCompound::<H>::generate_public_inputs(&por_pub_inputs, &por_pub_params, None)?;
199203
input.extend(por_inputs);
200204
}
201-
input
205+
Ok(input)
202206
}
203207

204208
fn circuit(
205209
public_inputs: &<DrgPoRep<'a, H, G> as ProofScheme<'a>>::PublicInputs,
206210
component_private_inputs: <DrgPoRepCircuit<'a, H> as CircuitComponent>::ComponentPrivateInputs,
207211
proof: &<DrgPoRep<'a, H, G> as ProofScheme<'a>>::Proof,
208212
public_params: &<DrgPoRep<'a, H, G> as ProofScheme<'a>>::PublicParams,
209-
) -> DrgPoRepCircuit<'a, H> {
213+
) -> Result<DrgPoRepCircuit<'a, H>> {
210214
let challenges = public_params.challenges_count;
211215
let len = proof.nodes.len();
212216

213-
assert!(len <= challenges, "too many challenges");
214-
assert_eq!(proof.replica_parents.len(), len);
215-
assert_eq!(proof.replica_nodes.len(), len);
217+
ensure!(len <= challenges, "too many challenges");
218+
ensure!(
219+
proof.replica_parents.len() == len,
220+
"Number of replica parents must match"
221+
);
222+
ensure!(
223+
proof.replica_nodes.len() == len,
224+
"Number of replica nodes must match"
225+
);
216226

217227
let replica_nodes: Vec<_> = proof
218228
.replica_nodes
@@ -277,13 +287,12 @@ where
277287
.map(|node| node.proof.as_options())
278288
.collect();
279289

280-
assert_eq!(
281-
public_inputs.tau.is_none(),
282-
public_params.private,
290+
ensure!(
291+
public_inputs.tau.is_none() == public_params.private,
283292
"inconsistent private state"
284293
);
285294

286-
DrgPoRepCircuit {
295+
Ok(DrgPoRepCircuit {
287296
params: &*JJ_PARAMS,
288297
replica_nodes,
289298
replica_nodes_paths,
@@ -296,7 +305,7 @@ where
296305
replica_id: replica_id.map(Into::into),
297306
private: public_params.private,
298307
_h: Default::default(),
299-
}
308+
})
300309
}
301310

302311
fn blank_circuit(
@@ -666,7 +675,8 @@ mod tests {
666675
&pub_inputs,
667676
&pp,
668677
None,
669-
);
678+
)
679+
.unwrap();
670680
let expected_inputs = cs.get_inputs();
671681

672682
for ((input, label), generated_input) in
@@ -812,7 +822,8 @@ mod tests {
812822
&public_params,
813823
&public_inputs,
814824
&private_inputs,
815-
);
825+
)
826+
.unwrap();
816827

817828
let mut cs = TestConstraintSystem::new();
818829

storage-proofs/src/circuit/election_post.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::compound_proof::{CircuitComponent, CompoundProof};
1818
use crate::crypto::pedersen::JJ_PARAMS;
1919
use crate::drgraph;
2020
use crate::election_post::{self, ElectionPoSt};
21+
use crate::error::Result;
2122
use crate::fr32::fr_into_bytes;
2223
use crate::hasher::Hasher;
2324
use crate::merklepor;
@@ -75,7 +76,7 @@ where
7576
pub_inputs: &<ElectionPoSt<'a, H> as ProofScheme<'a>>::PublicInputs,
7677
pub_params: &<ElectionPoSt<'a, H> as ProofScheme<'a>>::PublicParams,
7778
_partition_k: Option<usize>,
78-
) -> Vec<Fr> {
79+
) -> Result<Vec<Fr>> {
7980
let mut inputs = Vec::new();
8081

8182
let por_pub_params = merklepor::PublicParams {
@@ -95,7 +96,7 @@ where
9596
pub_inputs.sector_challenge_index,
9697
n as u64,
9798
pub_params.sector_size,
98-
);
99+
)?;
99100
for i in 0..election_post::POST_CHALLENGED_NODES {
100101
let por_pub_inputs = merklepor::PublicInputs {
101102
commitment: None,
@@ -105,7 +106,7 @@ where
105106
&por_pub_inputs,
106107
&por_pub_params,
107108
None,
108-
);
109+
)?;
109110

110111
inputs.extend(por_inputs);
111112
}
@@ -114,15 +115,15 @@ where
114115
// 3. Inputs for verifying partial_ticket generation
115116
inputs.push(pub_inputs.partial_ticket);
116117

117-
inputs
118+
Ok(inputs)
118119
}
119120

120121
fn circuit(
121122
pub_in: &<ElectionPoSt<'a, H> as ProofScheme<'a>>::PublicInputs,
122123
_priv_in: <ElectionPoStCircuit<'a, Bls12, H> as CircuitComponent>::ComponentPrivateInputs,
123124
vanilla_proof: &<ElectionPoSt<'a, H> as ProofScheme<'a>>::Proof,
124125
_pub_params: &<ElectionPoSt<'a, H> as ProofScheme<'a>>::PublicParams,
125-
) -> ElectionPoStCircuit<'a, Bls12, H> {
126+
) -> Result<ElectionPoStCircuit<'a, Bls12, H>> {
126127
let comm_r = pub_in.comm_r.into();
127128
let comm_c = vanilla_proof.comm_c.into();
128129
let comm_q = vanilla_proof.comm_q.into();
@@ -140,7 +141,7 @@ where
140141
.map(|v| v.iter().map(|p| Some(((*p).0.into(), p.1))).collect())
141142
.collect();
142143

143-
ElectionPoStCircuit {
144+
Ok(ElectionPoStCircuit {
144145
params: &*JJ_PARAMS,
145146
leafs,
146147
comm_r: Some(comm_r),
@@ -153,7 +154,7 @@ where
153154
prover_id: bytes_into_bits_opt(&pub_in.prover_id[..]),
154155
sector_id: Some(u64::from(pub_in.sector_id)),
155156
_h: PhantomData,
156-
}
157+
})
157158
}
158159

159160
fn blank_circuit(
@@ -388,7 +389,7 @@ mod tests {
388389
.flat_map(|_| fr_into_bytes::<Bls12>(&Fr::random(rng)))
389390
.collect();
390391

391-
let graph = BucketGraph::<PedersenHasher>::new(32, BASE_DEGREE, 0, new_seed());
392+
let graph = BucketGraph::<PedersenHasher>::new(32, BASE_DEGREE, 0, new_seed()).unwrap();
392393
let tree = graph.merkle_tree(data.as_slice()).unwrap();
393394
trees.insert(i.into(), tree);
394395
}
@@ -476,7 +477,8 @@ mod tests {
476477
&pub_inputs,
477478
&pub_params,
478479
None,
479-
);
480+
)
481+
.unwrap();
480482
let expected_inputs = cs.get_inputs();
481483

482484
for ((input, label), generated_input) in
@@ -515,7 +517,7 @@ mod tests {
515517
.flat_map(|_| fr_into_bytes::<Bls12>(&Fr::random(rng)))
516518
.collect();
517519

518-
let graph = BucketGraph::<PedersenHasher>::new(32, BASE_DEGREE, 0, new_seed());
520+
let graph = BucketGraph::<PedersenHasher>::new(32, BASE_DEGREE, 0, new_seed()).unwrap();
519521
let tree = graph.merkle_tree(data.as_slice()).unwrap();
520522
trees.insert(i.into(), tree);
521523
}
@@ -557,7 +559,8 @@ mod tests {
557559

558560
{
559561
let (circuit, inputs) =
560-
ElectionPoStCompound::circuit_for_test(&pub_params, &pub_inputs, &priv_inputs);
562+
ElectionPoStCompound::circuit_for_test(&pub_params, &pub_inputs, &priv_inputs)
563+
.unwrap();
561564

562565
let mut cs = TestConstraintSystem::new();
563566

@@ -578,7 +581,8 @@ mod tests {
578581
// Use this to debug differences between blank and regular circuit generation.
579582
{
580583
let (circuit1, _inputs) =
581-
ElectionPoStCompound::circuit_for_test(&pub_params, &pub_inputs, &priv_inputs);
584+
ElectionPoStCompound::circuit_for_test(&pub_params, &pub_inputs, &priv_inputs)
585+
.unwrap();
582586
let blank_circuit =
583587
ElectionPoStCompound::<PedersenHasher>::blank_circuit(&pub_params.vanilla_params);
584588

0 commit comments

Comments
 (0)