Defer to From/Into [u8; 32] vs Zcash(De)Serialize for sprout::NoteCommitments

This commit is contained in:
Deirdre Connolly 2020-08-09 19:35:51 -04:00 committed by Deirdre Connolly
parent 619afffa16
commit c8771ef620
5 changed files with 30 additions and 41 deletions

View File

@ -38,6 +38,8 @@ impl fmt::Debug for NoteCommitment {
} }
} }
impl Eq for NoteCommitment {}
impl From<jubjub::ExtendedPoint> for NoteCommitment { impl From<jubjub::ExtendedPoint> for NoteCommitment {
fn from(extended_point: jubjub::ExtendedPoint) -> Self { fn from(extended_point: jubjub::ExtendedPoint) -> Self {
Self(jubjub::AffinePoint::from(extended_point)) Self(jubjub::AffinePoint::from(extended_point))
@ -50,8 +52,6 @@ impl From<NoteCommitment> for [u8; 32] {
} }
} }
impl Eq for NoteCommitment {}
impl TryFrom<[u8; 32]> for NoteCommitment { impl TryFrom<[u8; 32]> for NoteCommitment {
type Error = &'static str; type Error = &'static str;
@ -66,19 +66,6 @@ impl TryFrom<[u8; 32]> for NoteCommitment {
} }
} }
impl ZcashSerialize for NoteCommitment {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
writer.write_all(&<[u8; 32]>::from(*self)[..])?;
Ok(())
}
}
impl ZcashDeserialize for NoteCommitment {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Self::try_from(reader.read_32_bytes()?).map_err(|e| SerializationError::Parse(e))
}
}
impl NoteCommitment { impl NoteCommitment {
/// Generate a new _NoteCommitment_ and the randomness used to create it. /// Generate a new _NoteCommitment_ and the randomness used to create it.
/// ///

View File

@ -2,14 +2,9 @@
#![allow(clippy::unit_arg)] #![allow(clippy::unit_arg)]
use std::io;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use crate::{ use crate::notes::sprout::Note;
notes::sprout::Note,
serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize},
};
/// The randomness used in the Pedersen Hash for note commitment. /// The randomness used in the Pedersen Hash for note commitment.
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq)]
@ -23,10 +18,18 @@ impl AsRef<[u8]> for CommitmentRandomness {
} }
/// Note commitments for the output notes. /// Note commitments for the output notes.
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))] #[cfg_attr(test, derive(proptest_derive::Arbitrary))]
pub struct NoteCommitment(pub(crate) [u8; 32]); pub struct NoteCommitment(pub(crate) [u8; 32]);
impl Eq for NoteCommitment {}
impl From<[u8; 32]> for NoteCommitment {
fn from(bytes: [u8; 32]) -> Self {
Self(bytes)
}
}
impl From<Note> for NoteCommitment { impl From<Note> for NoteCommitment {
/// NoteCommit_rcm^Sprout(a_pk, v, rho) /// NoteCommit_rcm^Sprout(a_pk, v, rho)
/// ///
@ -43,15 +46,8 @@ impl From<Note> for NoteCommitment {
} }
} }
impl ZcashSerialize for NoteCommitment { impl From<NoteCommitment> for [u8; 32] {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> { fn from(cm: NoteCommitment) -> [u8; 32] {
writer.write_all(&self.0[..])?; cm.0
Ok(())
}
}
impl ZcashDeserialize for NoteCommitment {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Self(reader.read_32_bytes()?))
} }
} }

View File

@ -1,7 +1,12 @@
use crate::types::amount::{Amount, NonNegative};
use crate::{ed25519_zebra, notes::sprout, proofs::ZkSnarkProof};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{
ed25519_zebra,
notes::sprout,
proofs::ZkSnarkProof,
types::amount::{Amount, NonNegative},
};
/// A _JoinSplit Description_, as described in [protocol specification §7.2][ps]. /// A _JoinSplit Description_, as described in [protocol specification §7.2][ps].
/// ///
/// [ps]: https://zips.z.cash/protocol/protocol.pdf#joinsplitencoding /// [ps]: https://zips.z.cash/protocol/protocol.pdf#joinsplitencoding
@ -23,9 +28,7 @@ pub struct JoinSplit<P: ZkSnarkProof> {
/// A nullifier for the input notes. /// A nullifier for the input notes.
pub nullifiers: [crate::notes::sprout::Nullifier; 2], pub nullifiers: [crate::notes::sprout::Nullifier; 2],
/// A note commitment for this output note. /// A note commitment for this output note.
/// pub commitments: [crate::commitments::sprout::NoteCommitment; 2],
/// XXX refine type to [T; 2] -- there are two commitments
pub commitments: [[u8; 32]; 2],
/// An X25519 public key. /// An X25519 public key.
pub ephemeral_key: x25519_dalek::PublicKey, pub ephemeral_key: x25519_dalek::PublicKey,
/// A 256-bit seed that must be chosen independently at random for each /// A 256-bit seed that must be chosen independently at random for each

View File

@ -244,8 +244,8 @@ impl<P: ZkSnarkProof> ZcashSerialize for JoinSplit<P> {
writer.write_all(&self.anchor[..])?; writer.write_all(&self.anchor[..])?;
writer.write_32_bytes(&self.nullifiers[0].into())?; writer.write_32_bytes(&self.nullifiers[0].into())?;
writer.write_32_bytes(&self.nullifiers[1].into())?; writer.write_32_bytes(&self.nullifiers[1].into())?;
writer.write_all(&self.commitments[0][..])?; writer.write_32_bytes(&self.commitments[0].into())?;
writer.write_all(&self.commitments[1][..])?; writer.write_32_bytes(&self.commitments[1].into())?;
writer.write_all(&self.ephemeral_key.as_bytes()[..])?; writer.write_all(&self.ephemeral_key.as_bytes()[..])?;
writer.write_all(&self.random_seed[..])?; writer.write_all(&self.random_seed[..])?;
self.vmacs[0].zcash_serialize(&mut writer)?; self.vmacs[0].zcash_serialize(&mut writer)?;
@ -267,7 +267,10 @@ impl<P: ZkSnarkProof> ZcashDeserialize for JoinSplit<P> {
notes::sprout::Nullifier::from(reader.read_32_bytes()?), notes::sprout::Nullifier::from(reader.read_32_bytes()?),
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()?], commitments: [
commitments::sprout::NoteCommitment::from(reader.read_32_bytes()?),
commitments::sprout::NoteCommitment::from(reader.read_32_bytes()?),
],
ephemeral_key: x25519_dalek::PublicKey::from(reader.read_32_bytes()?), ephemeral_key: x25519_dalek::PublicKey::from(reader.read_32_bytes()?),
random_seed: reader.read_32_bytes()?, random_seed: reader.read_32_bytes()?,
vmacs: [ vmacs: [

View File

@ -24,7 +24,7 @@ impl<P: ZkSnarkProof + Arbitrary + 'static> Arbitrary for JoinSplit<P> {
any::<Amount<NonNegative>>(), any::<Amount<NonNegative>>(),
array::uniform32(any::<u8>()), array::uniform32(any::<u8>()),
array::uniform2(any::<sprout::Nullifier>()), array::uniform2(any::<sprout::Nullifier>()),
array::uniform2(array::uniform32(any::<u8>())), array::uniform2(any::<commitments::sprout::NoteCommitment>()),
array::uniform32(any::<u8>()), array::uniform32(any::<u8>()),
array::uniform32(any::<u8>()), array::uniform32(any::<u8>()),
array::uniform2(any::<crate::types::MAC>()), array::uniform2(any::<crate::types::MAC>()),