From 619afffa161f1cff320387972709348ec21b5478 Mon Sep 17 00:00:00 2001 From: Deirdre Connolly Date: Sun, 9 Aug 2020 18:42:19 -0400 Subject: [PATCH] Use convenience Reader/Writer methods to only use From/Into for nullifer serialization And thus remove duplicative Zcash(De)Serialization impls --- zebra-chain/src/notes/sapling/nullifiers.rs | 19 ++++------------- zebra-chain/src/notes/sprout/nullifiers.rs | 23 ++------------------- zebra-chain/src/transaction/serialize.rs | 12 +++++------ 3 files changed, 12 insertions(+), 42 deletions(-) diff --git a/zebra-chain/src/notes/sapling/nullifiers.rs b/zebra-chain/src/notes/sapling/nullifiers.rs index 8f9bdc872..626e4acf2 100644 --- a/zebra-chain/src/notes/sapling/nullifiers.rs +++ b/zebra-chain/src/notes/sapling/nullifiers.rs @@ -1,12 +1,9 @@ #![allow(clippy::unit_arg)] #![allow(dead_code)] -use std::io; - use crate::{ commitments::sapling::{pedersen_hashes::mixing_pedersen_hash, NoteCommitment}, keys::sapling::NullifierDerivingKey, - serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}, treestate::note_commitment_tree::Position, }; @@ -29,7 +26,7 @@ fn prf_nf(nk: [u8; 32], rho: [u8; 32]) -> [u8; 32] { } /// A Nullifier for Sapling transactions -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)] #[cfg_attr(test, derive(proptest_derive::Arbitrary))] pub struct Nullifier([u8; 32]); @@ -47,16 +44,8 @@ impl<'a> From<(NoteCommitment, Position, &'a NullifierDerivingKey)> for Nullifie } } -impl ZcashDeserialize for Nullifier { - fn zcash_deserialize(mut reader: R) -> Result { - let bytes = reader.read_32_bytes()?; - - Ok(Self(bytes)) - } -} - -impl ZcashSerialize for Nullifier { - fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { - writer.write_all(&self.0[..]) +impl From for [u8; 32] { + fn from(n: Nullifier) -> Self { + n.0 } } diff --git a/zebra-chain/src/notes/sprout/nullifiers.rs b/zebra-chain/src/notes/sprout/nullifiers.rs index 70bfd4830..6e25c8590 100644 --- a/zebra-chain/src/notes/sprout/nullifiers.rs +++ b/zebra-chain/src/notes/sprout/nullifiers.rs @@ -1,14 +1,9 @@ #![allow(dead_code)] -use std::io; - use byteorder::{ByteOrder, LittleEndian}; use serde::{Deserialize, Serialize}; -use crate::{ - keys::sprout::SpendingKey, - serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize}, -}; +use crate::keys::sprout::SpendingKey; /// PRF^nf is used to derive a Sprout nullifer from the receiver's /// spending key a_sk and a nullifier seed ρ, instantiated using the @@ -63,7 +58,7 @@ impl From for [u8; 32] { } /// A Nullifier for Sprout transactions -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] #[cfg_attr(test, derive(proptest_derive::Arbitrary))] pub struct Nullifier(pub(crate) [u8; 32]); @@ -84,17 +79,3 @@ impl From for [u8; 32] { n.0 } } - -impl ZcashDeserialize for Nullifier { - fn zcash_deserialize(mut reader: R) -> Result { - let bytes = reader.read_32_bytes()?; - - Ok(Self(bytes)) - } -} - -impl ZcashSerialize for Nullifier { - fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { - writer.write_all(&self.0[..]) - } -} diff --git a/zebra-chain/src/transaction/serialize.rs b/zebra-chain/src/transaction/serialize.rs index 016ab1319..0c4883d5b 100644 --- a/zebra-chain/src/transaction/serialize.rs +++ b/zebra-chain/src/transaction/serialize.rs @@ -242,8 +242,8 @@ impl ZcashSerialize for JoinSplit

{ writer.write_u64::(self.vpub_old.into())?; writer.write_u64::(self.vpub_new.into())?; writer.write_all(&self.anchor[..])?; - self.nullifiers[0].zcash_serialize(&mut writer)?; - self.nullifiers[1].zcash_serialize(&mut writer)?; + writer.write_32_bytes(&self.nullifiers[0].into())?; + writer.write_32_bytes(&self.nullifiers[1].into())?; writer.write_all(&self.commitments[0][..])?; writer.write_all(&self.commitments[1][..])?; writer.write_all(&self.ephemeral_key.as_bytes()[..])?; @@ -264,8 +264,8 @@ impl ZcashDeserialize for JoinSplit

{ vpub_new: reader.read_u64::()?.try_into()?, anchor: reader.read_32_bytes()?, nullifiers: [ - notes::sprout::Nullifier::zcash_deserialize(&mut reader)?, - notes::sprout::Nullifier::zcash_deserialize(&mut reader)?, + notes::sprout::Nullifier::from(reader.read_32_bytes()?), + notes::sprout::Nullifier::from(reader.read_32_bytes()?), ], commitments: [reader.read_32_bytes()?, reader.read_32_bytes()?], ephemeral_key: x25519_dalek::PublicKey::from(reader.read_32_bytes()?), @@ -323,7 +323,7 @@ impl ZcashSerialize for Spend { fn zcash_serialize(&self, mut writer: W) -> Result<(), io::Error> { self.cv.zcash_serialize(&mut writer)?; writer.write_all(&self.anchor.0[..])?; - self.nullifier.zcash_serialize(&mut writer)?; + writer.write_32_bytes(&self.nullifier.into())?; writer.write_all(&<[u8; 32]>::from(self.rk)[..])?; self.zkproof.zcash_serialize(&mut writer)?; writer.write_all(&<[u8; 64]>::from(self.spend_auth_sig)[..])?; @@ -337,7 +337,7 @@ impl ZcashDeserialize for Spend { Ok(Spend { cv: commitments::sapling::ValueCommitment::zcash_deserialize(&mut reader)?, anchor: SaplingNoteTreeRootHash(reader.read_32_bytes()?), - nullifier: notes::sapling::Nullifier::zcash_deserialize(&mut reader)?, + nullifier: notes::sapling::Nullifier::from(reader.read_32_bytes()?), rk: reader.read_32_bytes()?.into(), zkproof: Groth16Proof::zcash_deserialize(&mut reader)?, spend_auth_sig: reader.read_64_bytes()?.into(),