zebra_chain/sprout/note/
ciphertexts.rs

1//! Encrypted parts of Sprout notes.
2
3use std::{fmt, io};
4
5use serde::{Deserialize, Serialize};
6use serde_big_array::BigArray;
7
8use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
9
10/// A ciphertext component for encrypted output notes.
11///
12/// Corresponds to the Sprout 'encCiphertext's
13#[derive(Serialize, Deserialize)]
14pub struct EncryptedNote(#[serde(with = "BigArray")] pub [u8; 601]);
15
16impl fmt::Debug for EncryptedNote {
17    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
18        f.debug_tuple("EncryptedNote")
19            .field(&hex::encode(&self.0[..]))
20            .finish()
21    }
22}
23
24// These impls all only exist because of array length restrictions.
25
26impl Copy for EncryptedNote {}
27
28impl Clone for EncryptedNote {
29    fn clone(&self) -> Self {
30        *self
31    }
32}
33
34impl PartialEq for EncryptedNote {
35    fn eq(&self, other: &Self) -> bool {
36        self.0[..] == other.0[..]
37    }
38}
39
40impl Eq for EncryptedNote {}
41
42impl ZcashSerialize for EncryptedNote {
43    fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
44        writer.write_all(&self.0[..])?;
45        Ok(())
46    }
47}
48
49impl ZcashDeserialize for EncryptedNote {
50    fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
51        let mut bytes = [0; 601];
52        reader.read_exact(&mut bytes[..])?;
53        Ok(Self(bytes))
54    }
55}
56
57#[cfg(test)]
58use proptest::prelude::*;
59#[cfg(test)]
60proptest! {
61
62    #[test]
63    fn encrypted_ciphertext_roundtrip(ec in any::<EncryptedNote>()) {
64        let _init_guard = zebra_test::init();
65
66        let mut data = Vec::new();
67
68        ec.zcash_serialize(&mut data).expect("EncryptedNote should serialize");
69
70        let ec2 = EncryptedNote::zcash_deserialize(&data[..]).expect("randomized EncryptedNote should deserialize");
71
72        prop_assert_eq![ec, ec2];
73    }
74}