mirror of
https://github.com/mii443/merkle.rs.git
synced 2025-08-22 16:05:30 +00:00
Make index panic on malformed lemma.
This commit is contained in:
committed by
Romain Ruetschi
parent
4f60edafcd
commit
353f00ad58
13
src/proof.rs
13
src/proof.rs
@ -156,6 +156,10 @@ impl<T> Proof<T> {
|
||||
}
|
||||
|
||||
/// Returns the index of this proof's value, given the total number of items in the tree.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the proof is malformed. Call `validate` first.
|
||||
pub fn index(&self, count: usize) -> usize {
|
||||
self.lemma.index(count)
|
||||
}
|
||||
@ -246,14 +250,17 @@ impl Lemma {
|
||||
}
|
||||
|
||||
/// Returns the index of this lemma's value, given the total number of items in the tree.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the lemma is malformed. Call `validate_lemma` first.
|
||||
pub fn index(&self, count: usize) -> usize {
|
||||
let left_count = count.next_power_of_two() / 2;
|
||||
match (self.sub_lemma.as_ref(), self.sibling_hash.as_ref()) {
|
||||
(None, Some(&Positioned::Right(_))) | (None, None) => 0,
|
||||
(None, Some(&Positioned::Left(_))) => 1,
|
||||
(Some(l), None) => l.index(count),
|
||||
(None, None) => 0,
|
||||
(Some(l), Some(&Positioned::Left(_))) => left_count + l.index(count - left_count),
|
||||
(Some(l), Some(&Positioned::Right(_))) => l.index(left_count),
|
||||
(None, Some(_)) | (Some(_), None) => panic!("malformed lemma"),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user