2020-04-19 11:45:25 -07:00
|
|
|
//!
|
2020-05-26 18:00:58 -07:00
|
|
|
#![allow(dead_code)]
|
2020-04-19 11:45:25 -07:00
|
|
|
|
2020-02-10 15:17:41 -08:00
|
|
|
use std::{fmt, io};
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
use proptest::{arbitrary::Arbitrary, collection::vec, prelude::*};
|
|
|
|
|
2020-06-15 15:08:14 -07:00
|
|
|
use crate::serde_helpers;
|
2020-02-10 15:17:41 -08:00
|
|
|
use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize};
|
|
|
|
|
2020-02-22 15:22:28 -08:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/// A _Diversifier_, an 11 byte value used to randomize the
|
|
|
|
/// recipient's final public shielded payment address to create a
|
|
|
|
/// _diversified payment address_.
|
|
|
|
///
|
|
|
|
/// When used, this value is mapped to an affine JubJub group element.
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
|
|
|
pub struct Diversifier(pub [u8; 11]);
|
2020-02-21 16:35:24 -08:00
|
|
|
|
2020-04-19 11:45:25 -07:00
|
|
|
///
|
2020-02-21 16:35:24 -08:00
|
|
|
pub struct Note {
|
|
|
|
diversifier: Diversifier,
|
2020-02-22 18:40:48 -08:00
|
|
|
// TODO: refine as a type, derived from a scalar mult of the
|
|
|
|
// diversifier as a jubjub group element and the incoming view key
|
|
|
|
// scalar.
|
|
|
|
transmission_key: [u8; 32],
|
|
|
|
value: u64,
|
|
|
|
note_commitment_randomness: NoteCommitmentRandomness,
|
2020-02-21 16:35:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-22 15:22:28 -08:00
|
|
|
/// The decrypted form of encrypted Sapling notes on the blockchain.
|
2020-02-21 16:35:24 -08:00
|
|
|
pub struct NotePlaintext {
|
|
|
|
diversifier: Diversifier,
|
|
|
|
value: u64,
|
|
|
|
// TODO: refine as jub-jub appropriate in the base field.
|
2020-02-22 18:40:48 -08:00
|
|
|
note_commitment_randomness: NoteCommitmentRandomness,
|
2020-02-22 15:22:28 -08:00
|
|
|
memo: memo::Memo,
|
2020-02-21 16:35:24 -08:00
|
|
|
}
|
|
|
|
|
2020-02-10 15:17:41 -08:00
|
|
|
/// A ciphertext component for encrypted output notes.
|
2020-06-15 15:08:14 -07:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct EncryptedCiphertext(#[serde(with = "serde_helpers::BigArray")] pub [u8; 580]);
|
2020-02-10 15:17:41 -08:00
|
|
|
|
|
|
|
impl fmt::Debug for EncryptedCiphertext {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_tuple("EncryptedCiphertext")
|
|
|
|
.field(&hex::encode(&self.0[..]))
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These impls all only exist because of array length restrictions.
|
|
|
|
|
|
|
|
impl Copy for EncryptedCiphertext {}
|
|
|
|
|
|
|
|
impl Clone for EncryptedCiphertext {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let mut bytes = [0; 580];
|
|
|
|
bytes[..].copy_from_slice(&self.0[..]);
|
|
|
|
Self(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for EncryptedCiphertext {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0[..] == other.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for EncryptedCiphertext {}
|
|
|
|
|
|
|
|
impl ZcashSerialize for EncryptedCiphertext {
|
|
|
|
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
|
|
|
|
writer.write_all(&self.0[..])?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ZcashDeserialize for EncryptedCiphertext {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
impl Arbitrary for EncryptedCiphertext {
|
|
|
|
type Parameters = ();
|
|
|
|
|
|
|
|
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
|
|
|
|
(vec(any::<u8>(), 580))
|
|
|
|
.prop_map(|v| {
|
|
|
|
let mut bytes = [0; 580];
|
|
|
|
bytes.copy_from_slice(v.as_slice());
|
2020-05-26 18:00:58 -07:00
|
|
|
Self(bytes)
|
2020-02-10 15:17:41 -08:00
|
|
|
})
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy = BoxedStrategy<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A ciphertext component for encrypted output notes.
|
2020-06-15 15:08:14 -07:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
pub struct OutCiphertext(#[serde(with = "serde_helpers::BigArray")] pub [u8; 80]);
|
2020-02-10 15:17:41 -08:00
|
|
|
|
|
|
|
impl fmt::Debug for OutCiphertext {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_tuple("OutCiphertext")
|
|
|
|
.field(&hex::encode(&self.0[..]))
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// These impls all only exist because of array length restrictions.
|
|
|
|
|
|
|
|
impl Copy for OutCiphertext {}
|
|
|
|
|
|
|
|
impl Clone for OutCiphertext {
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
let mut bytes = [0; 80];
|
|
|
|
bytes[..].copy_from_slice(&self.0[..]);
|
|
|
|
Self(bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for OutCiphertext {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.0[..] == other.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for OutCiphertext {}
|
|
|
|
|
|
|
|
impl ZcashSerialize for OutCiphertext {
|
|
|
|
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
|
|
|
|
writer.write_all(&self.0[..])?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ZcashDeserialize for OutCiphertext {
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
impl Arbitrary for OutCiphertext {
|
|
|
|
type Parameters = ();
|
|
|
|
|
|
|
|
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
|
|
|
|
(vec(any::<u8>(), 80))
|
|
|
|
.prop_map(|v| {
|
|
|
|
let mut bytes = [0; 80];
|
|
|
|
bytes.copy_from_slice(v.as_slice());
|
2020-05-26 18:00:58 -07:00
|
|
|
Self(bytes)
|
2020-02-10 15:17:41 -08:00
|
|
|
})
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Strategy = BoxedStrategy<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
proptest! {
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn encrypted_ciphertext_roundtrip(ec in any::<EncryptedCiphertext>()) {
|
|
|
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
|
|
|
|
ec.zcash_serialize(&mut data).expect("EncryptedCiphertext should serialize");
|
|
|
|
|
|
|
|
let ec2 = EncryptedCiphertext::zcash_deserialize(&data[..]).expect("randomized EncryptedCiphertext should deserialize");
|
|
|
|
|
|
|
|
prop_assert_eq![ec, ec2];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn out_ciphertext_roundtrip(oc in any::<OutCiphertext>()) {
|
|
|
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
|
|
|
|
oc.zcash_serialize(&mut data).expect("OutCiphertext should serialize");
|
|
|
|
|
|
|
|
let oc2 = OutCiphertext::zcash_deserialize(&data[..]).expect("randomized OutCiphertext should deserialize");
|
|
|
|
|
|
|
|
prop_assert_eq![oc, oc2];
|
|
|
|
}
|
|
|
|
}
|