2020-07-15 01:50:05 -07:00
|
|
|
use std::{fmt, io};
|
|
|
|
|
2020-08-14 23:06:03 -07:00
|
|
|
use crate::serialization::{serde_helpers, SerializationError, ZcashDeserialize, ZcashSerialize};
|
2020-07-15 01:50:05 -07:00
|
|
|
|
|
|
|
/// A ciphertext component for encrypted output notes.
|
2020-08-27 21:30:02 -07:00
|
|
|
///
|
|
|
|
/// Corresponds to the Sapling 'encCiphertext's
|
2020-07-15 01:50:05 -07:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2020-08-25 10:16:52 -07:00
|
|
|
pub struct EncryptedNote(#[serde(with = "serde_helpers::BigArray")] pub [u8; 580]);
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
impl fmt::Debug for EncryptedNote {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-08-25 10:16:52 -07:00
|
|
|
f.debug_tuple("EncryptedNote")
|
2020-07-15 01:50:05 -07:00
|
|
|
.field(&hex::encode(&self.0[..]))
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These impls all only exist because of array length restrictions.
|
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
impl Copy for EncryptedNote {}
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
impl Clone for EncryptedNote {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let mut bytes = [0; 580];
|
|
|
|
bytes[..].copy_from_slice(&self.0[..]);
|
|
|
|
Self(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
impl PartialEq for EncryptedNote {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0[..] == other.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
impl Eq for EncryptedNote {}
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
impl ZcashSerialize for EncryptedNote {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
|
|
|
|
writer.write_all(&self.0[..])?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
impl ZcashDeserialize for EncryptedNote {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
|
|
|
|
let mut bytes = [0; 580];
|
|
|
|
reader.read_exact(&mut bytes[..])?;
|
|
|
|
Ok(Self(bytes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A ciphertext component for encrypted output notes.
|
2020-08-27 21:32:01 -07:00
|
|
|
///
|
|
|
|
/// Corresponds to Sapling's 'outCiphertext'
|
2020-07-15 01:50:05 -07:00
|
|
|
#[derive(Deserialize, Serialize)]
|
2020-08-27 21:32:01 -07:00
|
|
|
pub struct WrappedNoteKey(#[serde(with = "serde_helpers::BigArray")] pub [u8; 80]);
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
impl fmt::Debug for WrappedNoteKey {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-08-27 21:32:01 -07:00
|
|
|
f.debug_tuple("WrappedNoteKey")
|
2020-07-15 01:50:05 -07:00
|
|
|
.field(&hex::encode(&self.0[..]))
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These impls all only exist because of array length restrictions.
|
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
impl Copy for WrappedNoteKey {}
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
impl Clone for WrappedNoteKey {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let mut bytes = [0; 80];
|
|
|
|
bytes[..].copy_from_slice(&self.0[..]);
|
|
|
|
Self(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
impl PartialEq for WrappedNoteKey {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0[..] == other.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
impl Eq for WrappedNoteKey {}
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
impl ZcashSerialize for WrappedNoteKey {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
|
|
|
|
writer.write_all(&self.0[..])?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
impl ZcashDeserialize for WrappedNoteKey {
|
2020-07-15 01:50:05 -07:00
|
|
|
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
|
|
|
|
let mut bytes = [0; 80];
|
|
|
|
reader.read_exact(&mut bytes[..])?;
|
|
|
|
Ok(Self(bytes))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 18:52:52 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
use proptest::prelude::*;
|
2020-07-15 01:50:05 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
proptest! {
|
|
|
|
|
|
|
|
#[test]
|
2020-08-25 10:16:52 -07:00
|
|
|
fn encrypted_ciphertext_roundtrip(ec in any::<EncryptedNote>()) {
|
2020-07-15 01:50:05 -07:00
|
|
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
ec.zcash_serialize(&mut data).expect("EncryptedNote should serialize");
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-25 10:16:52 -07:00
|
|
|
let ec2 = EncryptedNote::zcash_deserialize(&data[..]).expect("randomized EncryptedNote should deserialize");
|
2020-07-15 01:50:05 -07:00
|
|
|
|
|
|
|
prop_assert_eq![ec, ec2];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-27 21:32:01 -07:00
|
|
|
fn out_ciphertext_roundtrip(oc in any::<WrappedNoteKey>()) {
|
2020-07-15 01:50:05 -07:00
|
|
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
oc.zcash_serialize(&mut data).expect("WrappedNoteKey should serialize");
|
2020-07-15 01:50:05 -07:00
|
|
|
|
2020-08-27 21:32:01 -07:00
|
|
|
let oc2 = WrappedNoteKey::zcash_deserialize(&data[..]).expect("randomized WrappedNoteKey should deserialize");
|
2020-07-15 01:50:05 -07:00
|
|
|
|
|
|
|
prop_assert_eq![oc, oc2];
|
|
|
|
}
|
|
|
|
}
|