Skip to content

Commit

Permalink
refactor(storage-proofs): remove panic-able unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPeterVanNostrand authored and dignifiedquire committed Jun 4, 2019
1 parent 49a4318 commit efc6b04
Show file tree
Hide file tree
Showing 27 changed files with 264 additions and 178 deletions.
20 changes: 11 additions & 9 deletions storage-proofs/src/batchpost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,21 +246,23 @@ mod tests {

let priv_inputs = PrivateInputs::<H>::new(data.as_slice(), &tree);

let proof = BatchPoST::<H>::prove(&pub_params, &pub_inputs, &priv_inputs).unwrap();
let proof = BatchPoST::<H>::prove(&pub_params, &pub_inputs, &priv_inputs)
.expect("failed to create proof");

assert!(
BatchPoST::<H>::verify(&pub_params, &pub_inputs, &proof).unwrap(),
"failed to verify"
);
let proof_is_valid = BatchPoST::<H>::verify(&pub_params, &pub_inputs, &proof)
.expect("failed to verify proof");

assert!(proof_is_valid, "failed to verify");

// mess with a single part of the proof
{
let mut proof = proof;
proof.challenges[0] = proof.challenges[0] + 1;
assert!(
!BatchPoST::<H>::verify(&pub_params, &pub_inputs, &proof).unwrap(),
"verified invalid proof"
);

let proof_is_valid = BatchPoST::<H>::verify(&pub_params, &pub_inputs, &proof)
.expect("failed to verify proof");

assert!(!proof_is_valid, "verified invalid proof");
}
}

Expand Down
11 changes: 8 additions & 3 deletions storage-proofs/src/beacon_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ mod tests {
post_periods_count: 3,
};

let pub_params = BeaconPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp).unwrap();
let pub_params =
BeaconPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp).expect("setup failed");

let data0: Vec<u8> = (0..1024)
.flat_map(|_| fr_into_bytes::<Bls12>(&rng.gen()))
Expand All @@ -260,8 +261,12 @@ mod tests {
_h: PhantomData,
};

let proof = BeaconPoSt::prove(&pub_params, &pub_inputs, &priv_inputs).unwrap();
let proof = BeaconPoSt::prove(&pub_params, &pub_inputs, &priv_inputs)
.expect("failed to create proof");

assert!(BeaconPoSt::verify(&pub_params, &pub_inputs, &proof).unwrap());
let proof_is_valid =
BeaconPoSt::verify(&pub_params, &pub_inputs, &proof).expect("failed to verify proof");

assert!(proof_is_valid);
}
}
6 changes: 5 additions & 1 deletion storage-proofs/src/challenge_derivation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ pub fn derive_challenges<D: Domain>(
let j = ((n * k as usize) + i) as u32;
bytes.extend(commitment.into_bytes());
bytes.push(layer);
// Unwraping here is safe, all hash domains are larger than 4 bytes (the size of a `u32`).
bytes.write_u32::<LittleEndian>(j).unwrap();

let hash = blake2s(bytes.as_slice());
let big_challenge = BigUint::from_bytes_le(hash.as_ref());

// For now, we cannot try to prove the first or last node, so make sure the challenge can never be 0 or leaves - 1.
let big_mod_challenge = big_challenge % (leaves - 2);
big_mod_challenge.to_usize().unwrap() + 1
let big_mod_challenge = big_mod_challenge
.to_usize()
.expect("`big_mod_challenge` exceeds size of `usize`");
big_mod_challenge + 1
})
.collect()
}
Expand Down
38 changes: 15 additions & 23 deletions storage-proofs/src/circuit/apex_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,12 @@ impl<E: JubjubEngine> BinaryApexCommitment<E> {
BinaryApexCommitment::Branch(left_boxed, right_boxed) => {
assert!(length > 0, "Path too short for BinaryCommitment size.");
let curr_is_right = &path[0];
let cs = &mut cs.namespace(|| {
format!(
"path-{}",
if curr_is_right.get_value().unwrap() {
"1"
} else {
"0"
}
)
});
let bool_as_char = match curr_is_right.get_value() {
Some(true) => '1',
Some(false) => '0',
None => panic!("Boolean variable was never set"),
};
let cs = &mut cs.namespace(|| format!("path-{}", bool_as_char));

let (left, right) = match ((**left_boxed).clone(), (**right_boxed).clone()) {
(BinaryApexCommitment::Leaf(left), BinaryApexCommitment::Leaf(right)) => {
Expand Down Expand Up @@ -214,16 +210,12 @@ impl<E: JubjubEngine> ApexCommitment<E> for FlatApexCommitment<E> {
let reduced_size = size / 2; // Must divide evenly because size must be power of 2.
let mut new_allocated = Vec::with_capacity(reduced_size);
let curr_is_right = &path[0];
let mut cs = &mut cs.namespace(|| {
format!(
"path-{}",
if curr_is_right.get_value().unwrap() {
"1"
} else {
"0"
}
)
});
let bool_as_char = match curr_is_right.get_value() {
Some(true) => '1',
Some(false) => '0',
None => panic!("Boolean variable was never set"),
};
let mut cs = &mut cs.namespace(|| format!("path-{}", bool_as_char));

for i in 0..reduced_size {
let left = &self.allocated_nums[i];
Expand Down Expand Up @@ -313,8 +305,8 @@ mod tests {
})
.unwrap();

let (bc, root) =
T::commit(&mut outer_cs.namespace(|| "apex_commit"), &nums, &params).unwrap();
let (bc, root) = T::commit(&mut outer_cs.namespace(|| "apex_commit"), &nums, &params)
.expect("apex commitment failed");

constraint::equal(
&mut outer_cs,
Expand All @@ -331,7 +323,7 @@ mod tests {
let path = path_from_index(cs, i, n);

bc.includes(cs, || format!("apex inclusion check {}", i), num, &path)
.unwrap();
.expect("binary commitment `includes` failed");
}
let num_constraints = outer_cs.num_constraints() - starting_constraints;
// length, size: constraints
Expand Down
11 changes: 7 additions & 4 deletions storage-proofs/src/circuit/beacon_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ mod tests {
post_periods_count: 3,
};

let pub_params =
beacon_post::BeaconPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp).unwrap();
let pub_params = beacon_post::BeaconPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp)
.expect("setup failed");

let data0: Vec<u8> = (0..256)
.flat_map(|_| fr_into_bytes::<Bls12>(&rng.gen()))
Expand All @@ -180,9 +180,12 @@ mod tests {
let trees = [&tree0, &tree1];
let priv_inputs = beacon_post::PrivateInputs::new(&replicas[..], &trees[..]);

let proof = BeaconPoSt::prove(&pub_params, &pub_inputs, &priv_inputs).unwrap();
let proof =
BeaconPoSt::prove(&pub_params, &pub_inputs, &priv_inputs).expect("proving failed");

assert!(BeaconPoSt::verify(&pub_params, &pub_inputs, &proof).unwrap());
let is_valid =
BeaconPoSt::verify(&pub_params, &pub_inputs, &proof).expect("verification failed");
assert!(is_valid);

// actual circuit test

Expand Down
2 changes: 1 addition & 1 deletion storage-proofs/src/circuit/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ mod tests {
parents_bits.clone(),
m,
)
.unwrap();
.expect("key derivation function failed");

assert!(cs.is_satisfied(), "constraints not satisfied");
assert_eq!(cs.num_constraints(), 240282);
Expand Down
4 changes: 2 additions & 2 deletions storage-proofs/src/circuit/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ mod tests {
let mut cs = cs.namespace(|| "data");
bytes_into_boolean_vec(&mut cs, Some(data.as_slice()), data.len()).unwrap()
};
let out =
pedersen_md_no_padding(cs.namespace(|| "pedersen"), params, &data_bits).unwrap();
let out = pedersen_md_no_padding(cs.namespace(|| "pedersen"), params, &data_bits)
.expect("pedersen hashing failed");

assert!(cs.is_satisfied(), "constraints not satisfied");

Expand Down
41 changes: 22 additions & 19 deletions storage-proofs/src/circuit/por.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ mod tests {
&public_params.vanilla_params,
setup_params.engine_params,
)
.unwrap();
.expect("failed to generate groth params");

let proof = PoRCompound::<PedersenHasher>::prove(
&public_params,
&public_inputs,
Expand Down Expand Up @@ -379,14 +380,13 @@ mod tests {
);

// create a non circuit proof
let proof =
merklepor::MerklePoR::<H>::prove(&pub_params, &pub_inputs, &priv_inputs).unwrap();
let proof = merklepor::MerklePoR::<H>::prove(&pub_params, &pub_inputs, &priv_inputs)
.expect("proving failed");

// make sure it verifies
assert!(
merklepor::MerklePoR::<H>::verify(&pub_params, &pub_inputs, &proof).unwrap(),
"failed to verify merklepor proof"
);
let is_valid = merklepor::MerklePoR::<H>::verify(&pub_params, &pub_inputs, &proof)
.expect("verification failed");
assert!(is_valid, "failed to verify merklepor proof");

// -- Circuit

Expand All @@ -400,7 +400,7 @@ mod tests {
_h: Default::default(),
};

por.synthesize(&mut cs).unwrap();
por.synthesize(&mut cs).expect("circuit synthesis failed");
assert!(cs.is_satisfied(), "constraints not satisfied");

assert_eq!(cs.num_inputs(), 3, "wrong number of inputs");
Expand Down Expand Up @@ -487,15 +487,19 @@ mod tests {
&tree,
);

let gparams = PoRCompound::<H>::groth_params(
let groth_params = PoRCompound::<H>::groth_params(
&public_params.vanilla_params,
setup_params.engine_params,
)
.unwrap();
.expect("failed to generate groth params");

let proof =
PoRCompound::<H>::prove(&public_params, &public_inputs, &private_inputs, &gparams)
.expect("failed while proving");
let proof = PoRCompound::<H>::prove(
&public_params,
&public_inputs,
&private_inputs,
&groth_params,
)
.expect("proving failed");

{
let (circuit, inputs) = PoRCompound::<H>::circuit_for_test(
Expand Down Expand Up @@ -562,14 +566,13 @@ mod tests {
&pub_inputs,
&priv_inputs,
)
.unwrap();
.expect("proving failed");

// make sure it verifies
assert!(
let is_valid =
merklepor::MerklePoR::<PedersenHasher>::verify(&pub_params, &pub_inputs, &proof)
.unwrap(),
"failed to verify merklepor proof"
);
.expect("verification failed");
assert!(is_valid, "failed to verify merklepor proof");

// -- Circuit

Expand All @@ -584,7 +587,7 @@ mod tests {
_h: Default::default(),
};

por.synthesize(&mut cs).unwrap();
por.synthesize(&mut cs).expect("circuit synthesis failed");
assert!(cs.is_satisfied(), "constraints not satisfied");

assert_eq!(cs.num_inputs(), 2, "wrong number of inputs");
Expand Down
11 changes: 7 additions & 4 deletions storage-proofs/src/circuit/porc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,12 @@ mod tests {
trees: &[&tree1, &tree2],
};

let proof = PoRC::<PedersenHasher>::prove(&pub_params, &pub_inputs, &priv_inputs).unwrap();
let proof = PoRC::<PedersenHasher>::prove(&pub_params, &pub_inputs, &priv_inputs)
.expect("proving failed");

assert!(PoRC::<PedersenHasher>::verify(&pub_params, &pub_inputs, &proof).unwrap());
let is_valid = PoRC::<PedersenHasher>::verify(&pub_params, &pub_inputs, &proof)
.expect("verification failed");
assert!(is_valid);

// actual circuit test

Expand Down Expand Up @@ -427,11 +430,11 @@ mod tests {

let gparams =
PoRCCompound::<PedersenHasher>::groth_params(&pub_params.vanilla_params, &params)
.unwrap();
.expect("failed to create groth params");

let proof =
PoRCCompound::<PedersenHasher>::prove(&pub_params, &pub_inputs, &priv_inputs, &gparams)
.expect("failed while proving");
.expect("proving failed");

let (circuit, inputs) = PoRCCompound::<PedersenHasher>::circuit_for_test(
&pub_params,
Expand Down
16 changes: 7 additions & 9 deletions storage-proofs/src/circuit/ppor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,13 @@ mod tests {

for i in 0..leaves {
// make sure it verifies
assert!(
merklepor::MerklePoR::<PedersenHasher>::verify(
&pub_params,
&pub_inputs[i],
&proofs[i]
)
.unwrap(),
"failed to verify merklepor proof"
);
let is_valid = merklepor::MerklePoR::<PedersenHasher>::verify(
&pub_params,
&pub_inputs[i],
&proofs[i],
)
.expect("verification failed");
assert!(is_valid, "failed to verify merklepor proof");
}

let auth_paths: Vec<_> = proofs.iter().map(|p| p.proof.as_options()).collect();
Expand Down
2 changes: 1 addition & 1 deletion storage-proofs/src/circuit/sloth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ mod tests {
let a = num::AllocatedNum::alloc(cs.namespace(|| "a"), || Ok(rng.gen())).unwrap();
let b = num::AllocatedNum::alloc(cs.namespace(|| "b"), || Ok(rng.gen())).unwrap();

let res = sub(cs.namespace(|| "a-b"), &a, &b).unwrap();
let res = sub(cs.namespace(|| "a-b"), &a, &b).expect("subtraction failed");

let mut tmp = a.get_value().unwrap().clone();
tmp.sub_assign(&b.get_value().unwrap());
Expand Down
5 changes: 4 additions & 1 deletion storage-proofs/src/circuit/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ fn hash_lc<E: Engine>(terms: &[(Variable, E::Fr)], h: &mut Blake2s) {
}
}

coeff.into_repr().write_be(&mut buf[9..]).unwrap();
coeff
.into_repr()
.write_be(&mut buf[9..])
.expect("failed to write coeff");

h.update(&buf[..]);
}
Expand Down
23 changes: 12 additions & 11 deletions storage-proofs/src/circuit/vdf_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ mod tests {
sectors_count: 2,
};

let pub_params = vdf_post::VDFPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp).unwrap();
let pub_params = vdf_post::VDFPoSt::<PedersenHasher, vdf_sloth::Sloth>::setup(&sp)
.expect("setup failed");

let data0: Vec<u8> = (0..1024)
.flat_map(|_| fr_into_bytes::<Bls12>(&rng.gen()))
Expand Down Expand Up @@ -433,16 +434,16 @@ mod tests {
&pub_inputs,
&priv_inputs,
)
.unwrap();
.expect("proving failed");

assert!(
vdf_post::VDFPoSt::<PedersenHasher, vdf_sloth::Sloth>::verify(
&pub_params,
&pub_inputs,
&proof
)
.unwrap()
);
let is_valid = vdf_post::VDFPoSt::<PedersenHasher, vdf_sloth::Sloth>::verify(
&pub_params,
&pub_inputs,
&proof,
)
.expect("verification failed");

assert!(is_valid);

// actual circuit test

Expand Down Expand Up @@ -588,7 +589,7 @@ mod tests {
VDFPoSt<PedersenHasher, _>,
VDFPoStCircuit<_>,
>>::groth_params(&pub_params.vanilla_params, &params)
.unwrap();
.expect("failed to create groth params");

let proof = VDFPostCompound::prove(&pub_params, &pub_inputs, &priv_inputs, &gparams)
.expect("failed while proving");
Expand Down
Loading

0 comments on commit efc6b04

Please sign in to comment.