in src/proof.rs [620:704]
fn verify_random_sampling_proof(&self, root: &Self::ProofNodeType) -> bool {
// Verify the Merkle proof first.
if !self.merkle_proof.verify_inclusion_proof(&self.leaves, root) {
return false;
}
let list = self.merkle_proof.get_indexes();
let siblings = self.merkle_proof.get_path_siblings();
match list.len() {
0 => {
// When the tree is empty, only a padding root exists.
if self.padding_proofs.len() != 1 {
return false;
}
// Verify that the root is a padding node.
<V as PaddingProvable>::verify_padding_node(
root,
&self.padding_proofs[0],
&TreeIndex::zero(0),
)
}
1 => {
if list[0] == self.index {
// When the sampled index exists as a real leaf node in the tree,
// there isn't a padding node to be proved.
self.padding_proofs.is_empty()
} else {
// When the sampled index doesn't exist as a real leaf node in the tree,
// and the neighbour on one side doesn't exist,
// there is only one neighbour proved in the Merkle proof.
let padding_refs;
if list[0] < self.index {
// Only the left neighbour exists.
// Get references to padding nodes that prove the left neighbour is the right-most node in the tree.
padding_refs =
SparseMerkleTree::<V>::get_padding_proof_by_dir_index_ref_pairs(
&list[0],
ChildDir::Left,
);
} else {
// Only the right neighbour exists.
// Get references to padding nodes that prove the right neighbour is the left-most node in the tree.
padding_refs =
SparseMerkleTree::<V>::get_padding_proof_by_dir_index_ref_pairs(
&list[0],
ChildDir::Right,
);
}
// If the number of necessary padding nodes doesn't match, the proof is invalid.
if padding_refs.len() != self.padding_proofs.len() {
return false;
}
// Verify each necessary padding node is indeed a padding node
// according to the Merkle proof data and the padding node proof.
self.verify_padding_nodes(&siblings, &padding_refs)
}
}
2 => {
// When the sampled index doesn't exist as a real leaf node in the tree,
// but neighbours on both sides exist,
// the two closest neighbours are proved nodes in the Merkle proof.
// Get references to padding nodes that prove the indexes between the two neighbours
// don't exist as real leaf nodes in the tree.
let padding_refs = SparseMerkleTree::<V>::get_padding_proof_batch_index_ref_pairs(
&list[0], &list[1],
);
// If the number of necessary padding nodes doesn't match, the proof is invalid.
if padding_refs.len() != self.padding_proofs.len() {
return false;
}
// Verify each necessary padding node is indeed a padding node
// according to the Merkle proof data and the padding node proof.
self.verify_padding_nodes(&siblings, &padding_refs)
}
_ => {
// The Merkle proof shouldn't prove more than 2 nodes.
false
}
}
}