Skip to main content

miden_objects/decoded/primitives/
crypto.rs

1use crate::{Verify, proto};
2
3#[cfg(test)]
4mod tests;
5
6/// A canonically deserialized byte payload, with no verification of its application use.
7#[derive(Debug)]
8pub struct Canonical<T>(T);
9
10impl<T> Canonical<T> {
11    pub fn into_inner(self) -> T {
12        self.0
13    }
14}
15
16impl<T: miden_protocol::utils::serde::Deserializable + miden_protocol::utils::serde::Serializable>
17    TryFrom<alloc::vec::Vec<u8>> for Canonical<T>
18{
19    type Error = miden_protocol::utils::serde::DeserializationError;
20    fn try_from(bytes: alloc::vec::Vec<u8>) -> Result<Self, Self::Error> {
21        let value = T::read_from_bytes(&bytes)?;
22        if value.to_bytes() != bytes {
23            return Err(Self::Error::InvalidValue(alloc::string::String::from(
24                "non-canonical encoding or trailing bytes",
25            )));
26        }
27        Ok(Self(value))
28    }
29}
30
31pub use proto::primitives::DecodedPublicKey as PublicKey;
32
33/// Returns the canonical public key; ownership and authorization are not established here.
34impl Verify for PublicKey {
35    type Verified = miden_protocol::crypto::dsa::ecdsa_k256_keccak::PublicKey;
36    type Error = core::convert::Infallible;
37    fn verify(self) -> Result<Self::Verified, Self::Error> {
38        let proto::primitives::public_key::DecodedKey::EcdsaK256Keccak(key) = self.key;
39        Ok(key.into_inner())
40    }
41}
42
43pub use proto::primitives::DecodedSignature as Signature;
44
45/// Returns the canonical signature; authenticity requires a public key and signed message.
46impl Verify for Signature {
47    type Verified = miden_protocol::crypto::dsa::ecdsa_k256_keccak::Signature;
48    type Error = core::convert::Infallible;
49    fn verify(self) -> Result<Self::Verified, Self::Error> {
50        let proto::primitives::signature::DecodedSignature::EcdsaK256Keccak(signature) =
51            self.signature;
52        Ok(signature.into_inner())
53    }
54}