mirror of
https://github.com/mii443/merkle.rs.git
synced 2025-08-22 16:05:30 +00:00
Move Proof::validate_lemma to Lemma::validate
This commit is contained in:
committed by
Romain Ruetschi
parent
2732321faf
commit
c1bb633a8d
45
src/proof.rs
45
src/proof.rs
@ -144,30 +144,9 @@ impl<T> Proof<T> {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.validate_lemma(&self.lemma)
|
self.lemma.validate(self.algorithm)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_lemma(&self, lemma: &Lemma) -> bool {
|
|
||||||
match lemma.sub_lemma {
|
|
||||||
None => lemma.sibling_hash.is_none(),
|
|
||||||
|
|
||||||
Some(ref sub) => match lemma.sibling_hash {
|
|
||||||
None => false,
|
|
||||||
|
|
||||||
Some(Positioned::Left(ref hash)) => {
|
|
||||||
let combined = self.algorithm.hash_nodes(hash, &sub.node_hash);
|
|
||||||
let hashes_match = combined.as_ref() == lemma.node_hash.as_slice();
|
|
||||||
hashes_match && self.validate_lemma(sub)
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(Positioned::Right(ref hash)) => {
|
|
||||||
let combined = self.algorithm.hash_nodes(&sub.node_hash, hash);
|
|
||||||
let hashes_match = combined.as_ref() == lemma.node_hash.as_slice();
|
|
||||||
hashes_match && self.validate_lemma(sub)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A `Lemma` holds the hash of a node, the hash of its sibling node,
|
/// A `Lemma` holds the hash of a node, the hash of its sibling node,
|
||||||
@ -235,6 +214,28 @@ impl Lemma {
|
|||||||
sub_lemma: Some(Box::new(sub_lemma)),
|
sub_lemma: Some(Box::new(sub_lemma)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn validate(&self, algorithm: &'static Algorithm) -> bool {
|
||||||
|
match self.sub_lemma {
|
||||||
|
None => self.sibling_hash.is_none(),
|
||||||
|
|
||||||
|
Some(ref sub) => match self.sibling_hash {
|
||||||
|
None => false,
|
||||||
|
|
||||||
|
Some(Positioned::Left(ref hash)) => {
|
||||||
|
let combined = algorithm.hash_nodes(hash, &sub.node_hash);
|
||||||
|
let hashes_match = combined.as_ref() == self.node_hash.as_slice();
|
||||||
|
hashes_match && sub.validate(algorithm)
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(Positioned::Right(ref hash)) => {
|
||||||
|
let combined = algorithm.hash_nodes(&sub.node_hash, hash);
|
||||||
|
let hashes_match = combined.as_ref() == self.node_hash.as_slice();
|
||||||
|
hashes_match && sub.validate(algorithm)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tags a value so that we know from which branch of a `Tree` (if any) it was found.
|
/// Tags a value so that we know from which branch of a `Tree` (if any) it was found.
|
||||||
|
Reference in New Issue
Block a user