From b031dd071d5cbd2a60b388f9a92cf95743299a30 Mon Sep 17 00:00:00 2001 From: noot <36753753+noot@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:29:27 -0400 Subject: [PATCH] implement `InclusionProof::try_from_bytes` and `PartialEq + Eq` for `InclusionProof` (#2) * implement InclusionProof::try_from_bytes * derive PartialEq + Eq for InclusionProof --- src/inclusion.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/inclusion.rs b/src/inclusion.rs index a6c07c7..6cf6019 100644 --- a/src/inclusion.rs +++ b/src/inclusion.rs @@ -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 { proof: Vec, _marker: PhantomData, @@ -45,6 +45,23 @@ impl InclusionProof { } } } + + /// 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) -> Result { + if bytes.len() % H::OutputSize::USIZE != 0 { + return Err(InclusionVerifError::MalformedProof); + } + + Ok(InclusionProof { + proof: bytes, + _marker: PhantomData, + }) + } } impl CtMerkleTree