From 5d138804617512c4e95f1948774cff7f8a3312b4 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Fri, 7 Aug 2020 05:43:34 -0400 Subject: [PATCH] Remove Zcash(De)Serialize impls for *Commitments --- zebra-chain/src/commitments/sapling.rs | 45 +++++----------------- zebra-chain/src/notes/sprout/nullifiers.rs | 2 +- zebra-chain/src/transaction/serialize.rs | 10 ++--- 3 files changed, 15 insertions(+), 42 deletions(-) diff --git a/zebra-chain/src/commitments/sapling.rs b/zebra-chain/src/commitments/sapling.rs index e4d42ccd..74f4a8aa 100644 --- a/zebra-chain/src/commitments/sapling.rs +++ b/zebra-chain/src/commitments/sapling.rs @@ -7,7 +7,7 @@ mod test_vectors; pub mod pedersen_hashes; -use std::{fmt, io}; +use std::fmt; use bitvec::prelude::*; use rand_core::{CryptoRng, RngCore}; @@ -15,7 +15,6 @@ use rand_core::{CryptoRng, RngCore}; use crate::{ keys::sapling::{find_group_hash, Diversifier, TransmissionKey}, serde_helpers, - serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}, types::amount::{Amount, NonNegative}, }; @@ -58,21 +57,6 @@ impl From for [u8; 32] { impl Eq for NoteCommitment {} -impl ZcashSerialize for NoteCommitment { - fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { - writer.write_all(&self.0.to_bytes())?; - Ok(()) - } -} - -impl ZcashDeserialize for NoteCommitment { - fn zcash_deserialize(mut reader: R) -> Result { - Ok(Self( - jubjub::AffinePoint::from_bytes(reader.read_32_bytes()?).unwrap(), - )) - } -} - impl NoteCommitment { /// Generate a new _NoteCommitment_ and the randomness used to create it. /// @@ -132,7 +116,7 @@ impl NoteCommitment { /// Output Descriptions. /// /// https://zips.z.cash/protocol/protocol.pdf#concretehomomorphiccommit -#[derive(Clone, Deserialize, PartialEq, Serialize)] +#[derive(Clone, Copy, Deserialize, PartialEq, Serialize)] pub struct ValueCommitment(#[serde(with = "serde_helpers::AffinePoint")] pub jubjub::AffinePoint); impl fmt::Debug for ValueCommitment { @@ -144,6 +128,10 @@ impl fmt::Debug for ValueCommitment { } } +/// LEBS2OSP256(repr_J(cv)) +/// +/// https://zips.z.cash/protocol/protocol.pdf#spendencoding +/// https://zips.z.cash/protocol/protocol.pdf#jubjub impl From<[u8; 32]> for ValueCommitment { fn from(bytes: [u8; 32]) -> Self { Self(jubjub::AffinePoint::from_bytes(bytes).unwrap()) @@ -158,28 +146,13 @@ impl From for ValueCommitment { impl Eq for ValueCommitment {} -impl From for [u8; 32] { - fn from(cm: ValueCommitment) -> [u8; 32] { - cm.0.to_bytes() - } -} - /// LEBS2OSP256(repr_J(cv)) /// /// https://zips.z.cash/protocol/protocol.pdf#spendencoding /// https://zips.z.cash/protocol/protocol.pdf#jubjub -impl ZcashSerialize for ValueCommitment { - fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { - writer.write_all(&self.0.to_bytes())?; - Ok(()) - } -} - -impl ZcashDeserialize for ValueCommitment { - fn zcash_deserialize(mut reader: R) -> Result { - Ok(Self( - jubjub::AffinePoint::from_bytes(reader.read_32_bytes()?).unwrap(), - )) +impl From for [u8; 32] { + fn from(cm: ValueCommitment) -> [u8; 32] { + cm.0.to_bytes() } } diff --git a/zebra-chain/src/notes/sprout/nullifiers.rs b/zebra-chain/src/notes/sprout/nullifiers.rs index 8609a863..70bfd483 100644 --- a/zebra-chain/src/notes/sprout/nullifiers.rs +++ b/zebra-chain/src/notes/sprout/nullifiers.rs @@ -1,4 +1,3 @@ -#![allow(clippy::unit_arg)] #![allow(dead_code)] use std::io; @@ -40,6 +39,7 @@ fn prf_nf(a_sk: [u8; 32], rho: [u8; 32]) -> [u8; 32] { /// Nullifier seed, named rho in the [spec][ps]. /// /// [ps]: https://zips.z.cash/protocol/protocol.pdf#sproutkeycomponents + #[derive(Clone, Copy, Debug)] #[cfg_attr(test, derive(proptest_derive::Arbitrary))] pub struct NullifierSeed(pub(crate) [u8; 32]); diff --git a/zebra-chain/src/transaction/serialize.rs b/zebra-chain/src/transaction/serialize.rs index 016ab131..d57b6387 100644 --- a/zebra-chain/src/transaction/serialize.rs +++ b/zebra-chain/src/transaction/serialize.rs @@ -9,7 +9,7 @@ use std::{ }; use crate::{ - commitments, notes, + notes, proofs::ZkSnarkProof, serialization::{ ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize, ZcashSerialize, @@ -321,7 +321,7 @@ impl ZcashDeserialize for Option> { impl ZcashSerialize for Spend { fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { - self.cv.zcash_serialize(&mut writer)?; + writer.write_all(&<[u8; 32]>::from(self.cv)[..])?; writer.write_all(&self.anchor.0[..])?; self.nullifier.zcash_serialize(&mut writer)?; writer.write_all(&<[u8; 32]>::from(self.rk)[..])?; @@ -335,7 +335,7 @@ impl ZcashDeserialize for Spend { fn zcash_deserialize(mut reader: R) -> Result { use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash; Ok(Spend { - cv: commitments::sapling::ValueCommitment::zcash_deserialize(&mut reader)?, + cv: reader.read_32_bytes()?.into(), anchor: SaplingNoteTreeRootHash(reader.read_32_bytes()?), nullifier: notes::sapling::Nullifier::zcash_deserialize(&mut reader)?, rk: reader.read_32_bytes()?.into(), @@ -347,7 +347,7 @@ impl ZcashDeserialize for Spend { impl ZcashSerialize for Output { fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { - self.cv.zcash_serialize(&mut writer)?; + writer.write_all(&<[u8; 32]>::from(self.cv)[..])?; writer.write_all(&self.cm_u.to_bytes())?; writer.write_all(&self.ephemeral_key.to_bytes())?; self.enc_ciphertext.zcash_serialize(&mut writer)?; @@ -360,7 +360,7 @@ impl ZcashSerialize for Output { impl ZcashDeserialize for Output { fn zcash_deserialize(mut reader: R) -> Result { Ok(Output { - cv: commitments::sapling::ValueCommitment::zcash_deserialize(&mut reader)?, + cv: reader.read_32_bytes()?.into(), cm_u: jubjub::Fq::from_bytes(&reader.read_32_bytes()?).unwrap(), ephemeral_key: jubjub::AffinePoint::from_bytes(reader.read_32_bytes()?).unwrap(), enc_ciphertext: notes::sapling::EncryptedCiphertext::zcash_deserialize(&mut reader)?,