Skip to content

Commit

Permalink
implement InclusionProof::try_from_bytes and PartialEq + Eq for `…
Browse files Browse the repository at this point in the history
…InclusionProof` (#2)

* implement InclusionProof::try_from_bytes

* derive PartialEq + Eq for InclusionProof
  • Loading branch information
noot authored Sep 15, 2023
1 parent 9f816d6 commit b031dd0
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/inclusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use serde::{Deserialize as SerdeDeserialize, Serialize as SerdeSerialize};
/// A proof that a value appears in a [`CtMerkleTree`]. The byte representation of a
/// [`InclusionProof`] is identical to that of `PATH(m, D[n])` described in RFC 6962 §2.1.1.
#[cfg_attr(feature = "serde", derive(SerdeSerialize, SerdeDeserialize))]
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InclusionProof<H: Digest> {
proof: Vec<u8>,
_marker: PhantomData<H>,
Expand All @@ -45,6 +45,23 @@ impl<H: Digest> InclusionProof<H> {
}
}
}

/// Constructs a `InclusionProof` from the given bytes.
///
/// # Errors
///
/// If when `bytes.len()` is not a multiple of `H::OutputSize::USIZE`, i.e., when `bytes`
/// is not a concatenated sequence of hash digests.
pub fn try_from_bytes(bytes: Vec<u8>) -> Result<Self, InclusionVerifError> {
if bytes.len() % H::OutputSize::USIZE != 0 {
return Err(InclusionVerifError::MalformedProof);
}

Ok(InclusionProof {
proof: bytes,
_marker: PhantomData,
})
}
}

impl<H, T> CtMerkleTree<H, T>
Expand Down

0 comments on commit b031dd0

Please sign in to comment.