diff --git a/zebra-chain/src/proofs.rs b/zebra-chain/src/proofs.rs index b93815fb6..beebcf8f0 100644 --- a/zebra-chain/src/proofs.rs +++ b/zebra-chain/src/proofs.rs @@ -2,6 +2,8 @@ use std::fmt::Debug; +use crate::serialization::{ZcashDeserialize, ZcashSerialize}; + mod bctv14; mod groth16; @@ -9,7 +11,10 @@ pub use bctv14::Bctv14Proof; pub use groth16::Groth16Proof; /// A marker trait used to abstract over BCTV14 or Groth16 proofs. -pub trait ZkSnarkProof: Copy + Clone + Debug + PartialEq + Eq + private::Sealed {} +pub trait ZkSnarkProof: + Copy + Clone + Debug + PartialEq + Eq + ZcashSerialize + ZcashDeserialize + private::Sealed +{ +} impl ZkSnarkProof for Bctv14Proof {} impl ZkSnarkProof for Groth16Proof {} diff --git a/zebra-chain/src/proofs/bctv14.rs b/zebra-chain/src/proofs/bctv14.rs index f10ebf4a8..e296f3f7c 100644 --- a/zebra-chain/src/proofs/bctv14.rs +++ b/zebra-chain/src/proofs/bctv14.rs @@ -1,4 +1,6 @@ -use std::fmt; +use std::{fmt, io}; + +use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; /// An encoding of a BCTV14 proof, as used in Zcash. pub struct Bctv14Proof(pub [u8; 296]); @@ -30,3 +32,18 @@ impl PartialEq for Bctv14Proof { } impl Eq for Bctv14Proof {} + +impl ZcashSerialize for Bctv14Proof { + fn zcash_serialize(&self, mut writer: W) -> Result<(), SerializationError> { + writer.write_all(&self.0[..])?; + Ok(()) + } +} + +impl ZcashDeserialize for Bctv14Proof { + fn zcash_deserialize(mut reader: R) -> Result { + let mut bytes = [0; 296]; + reader.read_exact(&mut bytes[..])?; + Ok(Self(bytes)) + } +} diff --git a/zebra-chain/src/proofs/groth16.rs b/zebra-chain/src/proofs/groth16.rs index a2007e096..15c68ed95 100644 --- a/zebra-chain/src/proofs/groth16.rs +++ b/zebra-chain/src/proofs/groth16.rs @@ -1,4 +1,6 @@ -use std::fmt; +use std::{fmt, io}; + +use crate::serialization::{SerializationError, ZcashDeserialize, ZcashSerialize}; /// An encoding of a Groth16 proof, as used in Zcash. pub struct Groth16Proof(pub [u8; 192]); @@ -30,3 +32,18 @@ impl PartialEq for Groth16Proof { } impl Eq for Groth16Proof {} + +impl ZcashSerialize for Groth16Proof { + fn zcash_serialize(&self, mut writer: W) -> Result<(), SerializationError> { + writer.write_all(&self.0[..])?; + Ok(()) + } +} + +impl ZcashDeserialize for Groth16Proof { + fn zcash_deserialize(mut reader: R) -> Result { + let mut bytes = [0; 192]; + reader.read_exact(&mut bytes[..])?; + Ok(Self(bytes)) + } +}