Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Don't re-seed the TestRng when calling check_merkle_tree #2399

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions console/collections/src/merkle_tree/tests/append.rs
Expand Up @@ -30,13 +30,12 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
path_hasher: &PH,
leaves: &[LH::Leaf],
additional_leaves: &[LH::Leaf],
rng: &mut TestRng,
) -> Result<()> {
// Construct the Merkle tree for the given leaves.
let mut merkle_tree = MerkleTree::<E, LH, PH, DEPTH>::new(leaf_hasher, path_hasher, leaves)?;
assert_eq!(leaves.len(), merkle_tree.number_of_leaves);

let mut rng = TestRng::default();

// Check each leaf in the Merkle tree.
if !leaves.is_empty() {
for (leaf_index, leaf) in leaves.iter().enumerate() {
Expand All @@ -47,7 +46,7 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
// Verify the Merkle proof **fails** on an invalid root.
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::zero(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::one(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(&mut rng), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(rng), leaf));
}
}
// If additional leaves are provided, check that the Merkle tree is consistent with them.
Expand All @@ -63,7 +62,7 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
// Verify the Merkle proof **fails** on an invalid root.
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::zero(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::one(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(&mut rng), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(rng), leaf));
}
}
Ok(())
Expand Down Expand Up @@ -388,6 +387,7 @@ fn test_merkle_tree_bhp() -> Result<()> {
&(0..num_additional_leaves)
.map(|_| Field::<CurrentEnvironment>::rand(rng).to_bits_le())
.collect::<Vec<Vec<bool>>>(),
rng,
)?;
}
}
Expand Down Expand Up @@ -439,6 +439,7 @@ fn test_merkle_tree_poseidon() -> Result<()> {
&path_hasher,
&(0..num_leaves).map(|_| vec![Uniform::rand(rng)]).collect::<Vec<_>>(),
&(0..num_additional_leaves).map(|_| vec![Uniform::rand(rng)]).collect::<Vec<_>>(),
rng,
)?;
}
}
Expand Down
9 changes: 5 additions & 4 deletions console/collections/src/merkle_tree/tests/update.rs
Expand Up @@ -32,13 +32,12 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
path_hasher: &PH,
leaves: &[LH::Leaf],
updates: &[(usize, LH::Leaf)],
rng: &mut TestRng,
) -> Result<()> {
// Construct the Merkle tree for the given leaves.
let mut merkle_tree = MerkleTree::<E, LH, PH, DEPTH>::new(leaf_hasher, path_hasher, leaves)?;
assert_eq!(leaves.len(), merkle_tree.number_of_leaves);

let mut rng = TestRng::default();

// Check each leaf in the Merkle tree.
for (leaf_index, leaf) in leaves.iter().enumerate() {
// Compute a Merkle proof for the leaf.
Expand All @@ -48,7 +47,7 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
// Verify the Merkle proof **fails** on an invalid root.
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::zero(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::one(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(&mut rng), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(rng), leaf));
}

// Update the leaves of the Merkle tree.
Expand All @@ -65,7 +64,7 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
// Verify the Merkle proof **fails** on an invalid root.
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::zero(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::one(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(&mut rng), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(rng), leaf));
}
Ok(())
}
Expand Down Expand Up @@ -236,6 +235,7 @@ fn test_merkle_tree_bhp() -> Result<()> {
&(0..num_updates)
.map(|i| ((i % num_leaves) as usize, Field::<CurrentEnvironment>::rand(rng).to_bits_le()))
.collect::<Vec<(usize, Vec<bool>)>>(),
rng,
)?;
}
}
Expand Down Expand Up @@ -275,6 +275,7 @@ fn test_merkle_tree_poseidon() -> Result<()> {
&(0..num_additional_leaves)
.map(|i| ((i % num_leaves) as usize, vec![Uniform::rand(rng)]))
.collect::<Vec<_>>(),
rng,
)?;
}
}
Expand Down
9 changes: 5 additions & 4 deletions console/collections/src/merkle_tree/tests/update_many.rs
Expand Up @@ -32,13 +32,12 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
path_hasher: &PH,
leaves: &[LH::Leaf],
updates: &BTreeMap<usize, LH::Leaf>,
rng: &mut TestRng,
) -> Result<()> {
// Construct the Merkle tree for the given leaves.
let mut merkle_tree = MerkleTree::<E, LH, PH, DEPTH>::new(leaf_hasher, path_hasher, leaves)?;
assert_eq!(leaves.len(), merkle_tree.number_of_leaves);

let mut rng = TestRng::default();

// Check each leaf in the Merkle tree.
for (leaf_index, leaf) in leaves.iter().enumerate() {
// Compute a Merkle proof for the leaf.
Expand All @@ -48,7 +47,7 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
// Verify the Merkle proof **fails** on an invalid root.
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::zero(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::one(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(&mut rng), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(rng), leaf));
}

// If additional leaves are provided, check that the Merkle tree is consistent with them.
Expand All @@ -64,7 +63,7 @@ fn check_merkle_tree<E: Environment, LH: LeafHash<Hash = PH::Hash>, PH: PathHash
// Verify the Merkle proof **fails** on an invalid root.
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::zero(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::one(), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(&mut rng), leaf));
assert!(!proof.verify(leaf_hasher, path_hasher, &PH::Hash::rand(rng), leaf));
}
}
Ok(())
Expand Down Expand Up @@ -235,6 +234,7 @@ fn test_merkle_tree_bhp() -> Result<()> {
.rev()
.map(|i| ((i % num_leaves) as usize, Field::<CurrentEnvironment>::rand(rng).to_bits_le()))
.collect::<BTreeMap<usize, Vec<bool>>>(),
rng,
)?;
}
}
Expand Down Expand Up @@ -275,6 +275,7 @@ fn test_merkle_tree_poseidon() -> Result<()> {
.rev()
.map(|i| ((i % num_leaves) as usize, vec![Uniform::rand(rng)]))
.collect::<BTreeMap<_, _>>(),
rng,
)?;
}
}
Expand Down