Skip to content

Commit

Permalink
Merge pull request #1364 from filecoin-project/split-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire authored Dec 11, 2020
2 parents c59743e + 494eac5 commit 4131dc5
Show file tree
Hide file tree
Showing 41 changed files with 4,362 additions and 4,121 deletions.
106 changes: 11 additions & 95 deletions storage-proofs/core/src/gadgets/por.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ impl<'a, Tree: MerkleTreeTrait> Circuit<Bls12> for PoRCircuit<Tree> {
}

impl<'a, Tree: MerkleTreeTrait> PoRCircuit<Tree> {
pub fn new(proof: Tree::Proof, private: bool) -> Self {
PoRCircuit::<Tree> {
value: Root::Val(Some(proof.leaf().into())),
auth_path: proof.as_options().into(),
root: Root::Val(Some(proof.root().into())),
private,
_tree: PhantomData,
}
}

#[allow(clippy::type_complexity)]
pub fn synthesize<CS>(
mut cs: CS,
Expand Down Expand Up @@ -502,12 +512,11 @@ mod tests {
use super::*;

use bellperson::gadgets::multipack;
use bellperson::gadgets::num::AllocatedNum;
use ff::Field;
use generic_array::typenum;
use merkletree::store::VecStore;
use pretty_assertions::assert_eq;
use rand::{Rng, SeedableRng};
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

use crate::compound_proof;
Expand Down Expand Up @@ -1018,97 +1027,4 @@ mod tests {
assert!(cs.verify(&expected_inputs), "failed to verify inputs");
}
}

#[test]
fn test_por_no_challenge_input() {
type Arity = typenum::U8;
type Tree = TestTree<PoseidonHasher, Arity>;

// == Setup
let rng = &mut XorShiftRng::from_seed(crate::TEST_SEED);

let height = 3;
let n_leaves = Arity::to_usize() << height;

let data: Vec<u8> = (0..n_leaves)
.flat_map(|_| fr_into_bytes(&Fr::random(rng)))
.collect();

let tree = create_base_merkle_tree::<Tree>(None, n_leaves, &data)
.expect("create_base_merkle_tree failure");
let root = tree.root();

let challenge = rng.gen::<usize>() % n_leaves;
let leaf_bytes = data_at_node(&data, challenge).expect("data_at_node failure");
let leaf = bytes_into_fr(leaf_bytes).expect("bytes_into_fr failure");

// == Vanilla PoR proof
let proof = {
use por::{PoR, PrivateInputs, PublicInputs, PublicParams};
let pub_params = PublicParams {
leaves: n_leaves,
private: false,
};
let pub_inputs = PublicInputs {
challenge,
commitment: None,
};
let priv_inputs = PrivateInputs {
leaf: leaf.into(),
tree: &tree,
};
let proof =
PoR::<Tree>::prove(&pub_params, &pub_inputs, &priv_inputs).expect("proving failed");
let is_valid =
PoR::<Tree>::verify(&pub_params, &pub_inputs, &proof).expect("verification failed");
assert!(is_valid, "failed to verify por proof");
proof.proof
};

// == Test PoR gadget
let mut cs = TestConstraintSystem::<Bls12>::new();

let challenge_bit_len = n_leaves.trailing_zeros() as usize;
let challenge: Vec<AllocatedBit> = (0..challenge_bit_len)
.map(|i| {
AllocatedBit::alloc(
cs.namespace(|| format!("challenge bit {}", i)),
Some((challenge >> i) & 1 == 1),
)
.expect("failed to allocate challenge bit")
})
.collect();

let leaf = AllocatedNum::alloc(cs.namespace(|| "leaf".to_string()), || Ok(leaf))
.expect("failed to allocate leaf");

let path_values: Vec<Vec<num::AllocatedNum<Bls12>>> = proof
.path()
.iter()
.enumerate()
.map(|(height, (siblings, _insert_index))| {
siblings
.iter()
.enumerate()
.map(|(sib_index, &sib)| {
AllocatedNum::alloc(
cs.namespace(|| format!("sib {}, height {}", sib_index, height)),
|| Ok(sib.into()),
)
.expect("failed to allocate sibling")
})
.collect()
})
.collect();

let root = AllocatedNum::alloc(cs.namespace(|| "root".to_string()), || Ok(root.into()))
.expect("failed to allocate root");

por_no_challenge_input::<Tree, _>(&mut cs, challenge, leaf, path_values, root)
.expect("por gadget failed");

assert!(cs.is_satisfied());
let public_inputs = vec![];
assert!(cs.verify(&public_inputs));
}
}
3 changes: 1 addition & 2 deletions storage-proofs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ pub mod util;

pub use self::data::Data;

#[cfg(test)]
pub(crate) const TEST_SEED: [u8; 16] = [
pub const TEST_SEED: [u8; 16] = [
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5,
];

Expand Down
20 changes: 0 additions & 20 deletions storage-proofs/core/src/merkle/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,6 @@ pub trait MerkleProofTrait:
fn expected_len(&self, leaves: usize) -> usize {
compound_path_length::<Self::Arity, Self::SubTreeArity, Self::TopTreeArity>(leaves)
}

/// Test only method to break a valid proof.
#[cfg(test)]
fn break_me(&mut self, leaf: <Self::Hasher as Hasher>::Domain);
}

pub fn base_path_length<A: Unsigned, B: Unsigned, C: Unsigned>(leaves: usize) -> usize {
Expand Down Expand Up @@ -298,22 +294,6 @@ impl<
fn path_index(&self) -> usize {
forward_method!(self.data, path_index)
}

/// Test only method to break a valid proof.
#[cfg(test)]
fn break_me(&mut self, leaf: H::Domain) {
match self.data {
ProofData::Single(ref mut proof) => {
proof.leaf = leaf;
}
ProofData::Sub(ref mut proof) => {
proof.leaf = leaf;
}
ProofData::Top(ref mut proof) => {
proof.leaf = leaf;
}
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
Loading

0 comments on commit 4131dc5

Please sign in to comment.