Remove Zcash(De)Serialize impls for *Commitments

This commit is contained in:
Deirdre Connolly 2020-08-07 05:43:34 -04:00 committed by Deirdre Connolly
parent 014afd8e4a
commit 5d13880461
3 changed files with 15 additions and 42 deletions

View File

@ -7,7 +7,7 @@ mod test_vectors;
pub mod pedersen_hashes; pub mod pedersen_hashes;
use std::{fmt, io}; use std::fmt;
use bitvec::prelude::*; use bitvec::prelude::*;
use rand_core::{CryptoRng, RngCore}; use rand_core::{CryptoRng, RngCore};
@ -15,7 +15,6 @@ use rand_core::{CryptoRng, RngCore};
use crate::{ use crate::{
keys::sapling::{find_group_hash, Diversifier, TransmissionKey}, keys::sapling::{find_group_hash, Diversifier, TransmissionKey},
serde_helpers, serde_helpers,
serialization::{ReadZcashExt, SerializationError, ZcashDeserialize, ZcashSerialize},
types::amount::{Amount, NonNegative}, types::amount::{Amount, NonNegative},
}; };
@ -58,21 +57,6 @@ impl From<NoteCommitment> for [u8; 32] {
impl Eq for NoteCommitment {} impl Eq for NoteCommitment {}
impl ZcashSerialize for NoteCommitment {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> {
writer.write_all(&self.0.to_bytes())?;
Ok(())
}
}
impl ZcashDeserialize for NoteCommitment {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Self(
jubjub::AffinePoint::from_bytes(reader.read_32_bytes()?).unwrap(),
))
}
}
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.
/// ///
@ -132,7 +116,7 @@ impl NoteCommitment {
/// Output Descriptions. /// Output Descriptions.
/// ///
/// https://zips.z.cash/protocol/protocol.pdf#concretehomomorphiccommit /// 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); pub struct ValueCommitment(#[serde(with = "serde_helpers::AffinePoint")] pub jubjub::AffinePoint);
impl fmt::Debug for ValueCommitment { 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 { impl From<[u8; 32]> for ValueCommitment {
fn from(bytes: [u8; 32]) -> Self { fn from(bytes: [u8; 32]) -> Self {
Self(jubjub::AffinePoint::from_bytes(bytes).unwrap()) Self(jubjub::AffinePoint::from_bytes(bytes).unwrap())
@ -158,28 +146,13 @@ impl From<jubjub::ExtendedPoint> for ValueCommitment {
impl Eq for ValueCommitment {} impl Eq for ValueCommitment {}
impl From<ValueCommitment> for [u8; 32] {
fn from(cm: ValueCommitment) -> [u8; 32] {
cm.0.to_bytes()
}
}
/// LEBS2OSP256(repr_J(cv)) /// LEBS2OSP256(repr_J(cv))
/// ///
/// https://zips.z.cash/protocol/protocol.pdf#spendencoding /// https://zips.z.cash/protocol/protocol.pdf#spendencoding
/// https://zips.z.cash/protocol/protocol.pdf#jubjub /// https://zips.z.cash/protocol/protocol.pdf#jubjub
impl ZcashSerialize for ValueCommitment { impl From<ValueCommitment> for [u8; 32] {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> { fn from(cm: ValueCommitment) -> [u8; 32] {
writer.write_all(&self.0.to_bytes())?; cm.0.to_bytes()
Ok(())
}
}
impl ZcashDeserialize for ValueCommitment {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Self(
jubjub::AffinePoint::from_bytes(reader.read_32_bytes()?).unwrap(),
))
} }
} }

View File

@ -1,4 +1,3 @@
#![allow(clippy::unit_arg)]
#![allow(dead_code)] #![allow(dead_code)]
use std::io; 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]. /// Nullifier seed, named rho in the [spec][ps].
/// ///
/// [ps]: https://zips.z.cash/protocol/protocol.pdf#sproutkeycomponents /// [ps]: https://zips.z.cash/protocol/protocol.pdf#sproutkeycomponents
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
#[cfg_attr(test, derive(proptest_derive::Arbitrary))] #[cfg_attr(test, derive(proptest_derive::Arbitrary))]
pub struct NullifierSeed(pub(crate) [u8; 32]); pub struct NullifierSeed(pub(crate) [u8; 32]);

View File

@ -9,7 +9,7 @@ use std::{
}; };
use crate::{ use crate::{
commitments, notes, notes,
proofs::ZkSnarkProof, proofs::ZkSnarkProof,
serialization::{ serialization::{
ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize, ZcashSerialize, ReadZcashExt, SerializationError, WriteZcashExt, ZcashDeserialize, ZcashSerialize,
@ -321,7 +321,7 @@ impl<P: ZkSnarkProof> ZcashDeserialize for Option<JoinSplitData<P>> {
impl ZcashSerialize for Spend { impl ZcashSerialize for Spend {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> { fn zcash_serialize<W: io::Write>(&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[..])?; writer.write_all(&self.anchor.0[..])?;
self.nullifier.zcash_serialize(&mut writer)?; self.nullifier.zcash_serialize(&mut writer)?;
writer.write_all(&<[u8; 32]>::from(self.rk)[..])?; writer.write_all(&<[u8; 32]>::from(self.rk)[..])?;
@ -335,7 +335,7 @@ impl ZcashDeserialize for Spend {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> { fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash; use crate::treestate::note_commitment_tree::SaplingNoteTreeRootHash;
Ok(Spend { Ok(Spend {
cv: commitments::sapling::ValueCommitment::zcash_deserialize(&mut reader)?, cv: reader.read_32_bytes()?.into(),
anchor: SaplingNoteTreeRootHash(reader.read_32_bytes()?), anchor: SaplingNoteTreeRootHash(reader.read_32_bytes()?),
nullifier: notes::sapling::Nullifier::zcash_deserialize(&mut reader)?, nullifier: notes::sapling::Nullifier::zcash_deserialize(&mut reader)?,
rk: reader.read_32_bytes()?.into(), rk: reader.read_32_bytes()?.into(),
@ -347,7 +347,7 @@ impl ZcashDeserialize for Spend {
impl ZcashSerialize for Output { impl ZcashSerialize for Output {
fn zcash_serialize<W: io::Write>(&self, mut writer: W) -> Result<(), io::Error> { fn zcash_serialize<W: io::Write>(&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.cm_u.to_bytes())?;
writer.write_all(&self.ephemeral_key.to_bytes())?; writer.write_all(&self.ephemeral_key.to_bytes())?;
self.enc_ciphertext.zcash_serialize(&mut writer)?; self.enc_ciphertext.zcash_serialize(&mut writer)?;
@ -360,7 +360,7 @@ impl ZcashSerialize for Output {
impl ZcashDeserialize for Output { impl ZcashDeserialize for Output {
fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> { fn zcash_deserialize<R: io::Read>(mut reader: R) -> Result<Self, SerializationError> {
Ok(Output { 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(), cm_u: jubjub::Fq::from_bytes(&reader.read_32_bytes()?).unwrap(),
ephemeral_key: jubjub::AffinePoint::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)?, enc_ciphertext: notes::sapling::EncryptedCiphertext::zcash_deserialize(&mut reader)?,