1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use ark_ec::PairingEngine;
use ark_ff::bytes::ToBytes;
use ark_serialize::*;
use ark_std::{
    io::{self, Result as IoResult},
    vec::Vec,
};

/// A proof in the GM17 SNARK.
#[derive(PartialEq, Eq, Clone, Default, CanonicalSerialize, CanonicalDeserialize)]
pub struct Proof<E: PairingEngine> {
    #[doc(hidden)]
    pub a: E::G1Affine,
    #[doc(hidden)]
    pub b: E::G2Affine,
    #[doc(hidden)]
    pub c: E::G1Affine,
}

impl<E: PairingEngine> ToBytes for Proof<E> {
    #[inline]
    fn write<W: Write>(&self, mut writer: W) -> io::Result<()> {
        self.a.write(&mut writer)?;
        self.b.write(&mut writer)?;
        self.c.write(&mut writer)
    }
}

/// A verification key in the GM17 SNARK.
#[derive(Eq, PartialEq, Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct VerifyingKey<E: PairingEngine> {
    #[doc(hidden)]
    pub h_g2: E::G2Affine,
    #[doc(hidden)]
    pub g_alpha_g1: E::G1Affine,
    #[doc(hidden)]
    pub h_beta_g2: E::G2Affine,
    #[doc(hidden)]
    pub g_gamma_g1: E::G1Affine,
    #[doc(hidden)]
    pub h_gamma_g2: E::G2Affine,
    #[doc(hidden)]
    pub query: Vec<E::G1Affine>,
}

impl<E: PairingEngine> ToBytes for VerifyingKey<E> {
    fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
        self.h_g2.write(&mut writer)?;
        self.g_alpha_g1.write(&mut writer)?;
        self.h_beta_g2.write(&mut writer)?;
        self.g_gamma_g1.write(&mut writer)?;
        self.h_gamma_g2.write(&mut writer)?;
        for q in &self.query {
            q.write(&mut writer)?;
        }
        Ok(())
    }
}

impl<E: PairingEngine> Default for VerifyingKey<E> {
    fn default() -> Self {
        Self {
            h_g2: E::G2Affine::default(),
            g_alpha_g1: E::G1Affine::default(),
            h_beta_g2: E::G2Affine::default(),
            g_gamma_g1: E::G1Affine::default(),
            h_gamma_g2: E::G2Affine::default(),
            query: Vec::new(),
        }
    }
}

/// Preprocessed verification key parameters that enable faster verification
/// at the expense of larger size in memory.
#[derive(PartialEq, Eq, Clone)]
pub struct PreparedVerifyingKey<E: PairingEngine> {
    #[doc(hidden)]
    pub vk: VerifyingKey<E>,
    #[doc(hidden)]
    pub g_alpha: E::G1Affine,
    #[doc(hidden)]
    pub h_beta: E::G2Affine,
    #[doc(hidden)]
    pub g_alpha_h_beta_ml: E::Fqk,
    #[doc(hidden)]
    pub g_gamma_pc: E::G1Prepared,
    #[doc(hidden)]
    pub h_gamma_pc: E::G2Prepared,
    #[doc(hidden)]
    pub h_pc: E::G2Prepared,
    #[doc(hidden)]
    pub query: Vec<E::G1Affine>,
}

impl<E: PairingEngine> Default for PreparedVerifyingKey<E> {
    fn default() -> Self {
        Self {
            vk: VerifyingKey::default(),
            g_alpha: E::G1Affine::default(),
            h_beta: E::G2Affine::default(),
            g_alpha_h_beta_ml: E::Fqk::default(),
            g_gamma_pc: E::G1Prepared::default(),
            h_gamma_pc: E::G2Prepared::default(),
            h_pc: E::G2Prepared::default(),
            query: Vec::new(),
        }
    }
}

impl<E: PairingEngine> From<PreparedVerifyingKey<E>> for VerifyingKey<E> {
    fn from(other: PreparedVerifyingKey<E>) -> Self {
        other.vk
    }
}

impl<E: PairingEngine> From<VerifyingKey<E>> for PreparedVerifyingKey<E> {
    fn from(other: VerifyingKey<E>) -> Self {
        crate::prepare_verifying_key(&other)
    }
}

impl<E: PairingEngine> ToBytes for PreparedVerifyingKey<E> {
    fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
        self.vk.write(&mut writer)?;
        self.g_alpha.write(&mut writer)?;
        self.h_beta.write(&mut writer)?;
        self.g_alpha_h_beta_ml.write(&mut writer)?;
        self.g_gamma_pc.write(&mut writer)?;
        self.h_gamma_pc.write(&mut writer)?;
        self.h_pc.write(&mut writer)?;
        for q in &self.query {
            q.write(&mut writer)?;
        }
        Ok(())
    }
}

/// Full public (prover and verifier) parameters for the GM17 zkSNARK.
#[derive(PartialEq, Eq, Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct ProvingKey<E: PairingEngine> {
    #[doc(hidden)]
    pub vk: VerifyingKey<E>,
    #[doc(hidden)]
    pub a_query: Vec<E::G1Affine>,
    #[doc(hidden)]
    pub b_query: Vec<E::G2Affine>,
    #[doc(hidden)]
    pub c_query_1: Vec<E::G1Affine>,
    #[doc(hidden)]
    pub c_query_2: Vec<E::G1Affine>,
    #[doc(hidden)]
    pub g_gamma_z: E::G1Affine,
    #[doc(hidden)]
    pub h_gamma_z: E::G2Affine,
    #[doc(hidden)]
    pub g_ab_gamma_z: E::G1Affine,
    #[doc(hidden)]
    pub g_gamma2_z2: E::G1Affine,
    #[doc(hidden)]
    pub g_gamma2_z_t: Vec<E::G1Affine>,
}