diff --git a/zebra-chain/src/lib.rs b/zebra-chain/src/lib.rs index d97b71ed658..76c79a6fdb3 100644 --- a/zebra-chain/src/lib.rs +++ b/zebra-chain/src/lib.rs @@ -7,6 +7,7 @@ mod sha256d_writer; pub mod block; pub mod equihash_solution; pub mod note_commitment_tree; +pub mod proofs; pub mod serialization; pub mod transaction; pub mod types; diff --git a/zebra-chain/src/proofs.rs b/zebra-chain/src/proofs.rs new file mode 100644 index 00000000000..b93815fb680 --- /dev/null +++ b/zebra-chain/src/proofs.rs @@ -0,0 +1,22 @@ +//! ZK proofs used in Zcash. + +use std::fmt::Debug; + +mod bctv14; +mod groth16; + +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 {} +impl ZkSnarkProof for Bctv14Proof {} +impl ZkSnarkProof for Groth16Proof {} + +mod private { + use super::*; + + pub trait Sealed {} + impl Sealed for Bctv14Proof {} + impl Sealed for Groth16Proof {} +} diff --git a/zebra-chain/src/proofs/bctv14.rs b/zebra-chain/src/proofs/bctv14.rs new file mode 100644 index 00000000000..f10ebf4a86e --- /dev/null +++ b/zebra-chain/src/proofs/bctv14.rs @@ -0,0 +1,32 @@ +use std::fmt; + +/// An encoding of a BCTV14 proof, as used in Zcash. +pub struct Bctv14Proof(pub [u8; 296]); + +impl fmt::Debug for Bctv14Proof { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("Bctv14Proof") + .field(&hex::encode(&self.0[..])) + .finish() + } +} + +// These impls all only exist because of array length restrictions. + +impl Copy for Bctv14Proof {} + +impl Clone for Bctv14Proof { + fn clone(&self) -> Self { + let mut bytes = [0; 296]; + bytes[..].copy_from_slice(&self.0[..]); + Self(bytes) + } +} + +impl PartialEq for Bctv14Proof { + fn eq(&self, other: &Self) -> bool { + self.0[..] == other.0[..] + } +} + +impl Eq for Bctv14Proof {} diff --git a/zebra-chain/src/proofs/groth16.rs b/zebra-chain/src/proofs/groth16.rs new file mode 100644 index 00000000000..a2007e09640 --- /dev/null +++ b/zebra-chain/src/proofs/groth16.rs @@ -0,0 +1,32 @@ +use std::fmt; + +/// An encoding of a Groth16 proof, as used in Zcash. +pub struct Groth16Proof(pub [u8; 192]); + +impl fmt::Debug for Groth16Proof { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.debug_tuple("Groth16Proof") + .field(&hex::encode(&self.0[..])) + .finish() + } +} + +// These impls all only exist because of array length restrictions. + +impl Copy for Groth16Proof {} + +impl Clone for Groth16Proof { + fn clone(&self) -> Self { + let mut bytes = [0; 192]; + bytes[..].copy_from_slice(&self.0[..]); + Self(bytes) + } +} + +impl PartialEq for Groth16Proof { + fn eq(&self, other: &Self) -> bool { + self.0[..] == other.0[..] + } +} + +impl Eq for Groth16Proof {}