Skip to content

Commit

Permalink
Add data types for Bctv14 and Groth16 proofs.
Browse files Browse the repository at this point in the history
This also adds a trait to abstract over them.
  • Loading branch information
hdevalence committed Nov 28, 2019
1 parent a712806 commit 2f3a7a0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions zebra-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
22 changes: 22 additions & 0 deletions zebra-chain/src/proofs.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
32 changes: 32 additions & 0 deletions zebra-chain/src/proofs/bctv14.rs
Original file line number Diff line number Diff line change
@@ -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 {}
32 changes: 32 additions & 0 deletions zebra-chain/src/proofs/groth16.rs
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit 2f3a7a0

Please sign in to comment.