zebra_chain/sprout/note/
mac.rs

1//! Sprout message authentication codes.
2
3use std::io::{self, Read};
4
5use crate::{
6    fmt::HexDebug,
7    serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize},
8};
9
10/// A sequence of message authentication tags ...
11///
12/// binding h_sig to each a_sk of the JoinSplit description, computed as
13/// described in ยง 4.10 'Non-malleability (Sprout)' on p. 37
14#[derive(PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
15#[cfg_attr(
16    any(test, feature = "proptest-impl"),
17    derive(proptest_derive::Arbitrary)
18)]
19pub struct Mac(HexDebug<[u8; 32]>);
20
21impl From<[u8; 32]> for Mac {
22    fn from(bytes: [u8; 32]) -> Self {
23        Self(bytes.into())
24    }
25}
26
27impl From<Mac> for [u8; 32] {
28    fn from(rt: Mac) -> [u8; 32] {
29        *rt.0
30    }
31}
32
33impl From<&Mac> for [u8; 32] {
34    fn from(mac: &Mac) -> Self {
35        mac.clone().into()
36    }
37}
38
39impl ZcashDeserialize for Mac {
40    fn zcash_deserialize<R: Read>(mut reader: R) -> Result<Self, SerializationError> {
41        let bytes = reader.read_32_bytes()?;
42
43        Ok(Self(bytes.into()))
44    }
45}
46
47impl ZcashSerialize for Mac {
48    fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
49        writer.write_all(&self.0[..])
50    }
51}