From 353f00ad58308a79b6a2b4a781fcad4403860a3d Mon Sep 17 00:00:00 2001 From: Andreas Fackler Date: Fri, 15 Jun 2018 09:25:36 +0200 Subject: [PATCH] Make index panic on malformed lemma. --- src/proof.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/proof.rs b/src/proof.rs index 936afab..db4136e 100644 --- a/src/proof.rs +++ b/src/proof.rs @@ -156,6 +156,10 @@ impl Proof { } /// 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"), } }