legogroth16/
data_structures.rs

1use crate::link::{EK, PP, VK};
2use ark_ec::pairing::{Pairing, PairingOutput};
3use ark_serialize::*;
4use ark_std::vec::Vec;
5
6/// A proof in the Groth16 SNARK
7#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
8pub struct Proof<E: Pairing> {
9    /// The `A` element in `G1`.
10    pub a: E::G1Affine,
11    /// The `B` element in `G2`.
12    pub b: E::G2Affine,
13    /// The `C` element in `G1`.
14    pub c: E::G1Affine,
15    /// The `D` element in `G1`. Commits to a subset of private inputs of the circuit
16    pub d: E::G1Affine,
17}
18
19/// A proof in the Groth16 SNARK with CP_link proof
20#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
21pub struct ProofWithLink<E: Pairing> {
22    pub groth16_proof: Proof<E>,
23    /// cp_{link}
24    pub link_d: E::G1Affine,
25    /// proof of commitment opening equality between `cp_{link}` and `d`
26    pub link_pi: E::G1Affine,
27}
28
29impl<E: Pairing> Default for Proof<E> {
30    fn default() -> Self {
31        Self {
32            a: E::G1Affine::default(),
33            b: E::G2Affine::default(),
34            c: E::G1Affine::default(),
35            d: E::G1Affine::default(),
36        }
37    }
38}
39
40impl<E: Pairing> Default for ProofWithLink<E> {
41    fn default() -> Self {
42        Self {
43            groth16_proof: Proof::default(),
44            link_pi: E::G1Affine::default(),
45            link_d: E::G1Affine::default(),
46        }
47    }
48}
49
50////////////////////////////////////////////////////////////////////////////////
51////////////////////////////////////////////////////////////////////////////////
52
53/// A verification key in the Groth16 SNARK.
54#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
55pub struct VerifyingKey<E: Pairing> {
56    /// The `alpha * G`, where `G` is the generator of `E::G1`.
57    pub alpha_g1: E::G1Affine,
58    /// The `alpha * H`, where `H` is the generator of `E::G2`.
59    pub beta_g2: E::G2Affine,
60    /// The `gamma * H`, where `H` is the generator of `E::G2`.
61    pub gamma_g2: E::G2Affine,
62    /// The `delta * H`, where `H` is the generator of `E::G2`.
63    pub delta_g2: E::G2Affine,
64    /// The `gamma^{-1} * (beta * a_i + alpha * b_i + c_i) * H`, where `H` is the generator of `E::G1`.
65    pub gamma_abc_g1: Vec<E::G1Affine>,
66    /// The element `eta*gamma^-1 * G` in `E::G1`.
67    pub eta_gamma_inv_g1: E::G1Affine,
68    /// No of witness to commit
69    pub commit_witness_count: u32,
70}
71
72/// A verification key in the Groth16 SNARK with CP_link verification parameters
73#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
74pub struct VerifyingKeyWithLink<E: Pairing> {
75    pub groth16_vk: VerifyingKey<E>,
76    /// Public parameters of the Subspace Snark
77    pub link_pp: PP<E::G1Affine, E::G2Affine>,
78    /// Commitment key of the link commitment cp_link
79    pub link_bases: Vec<E::G1Affine>,
80    /// Verification key of the Subspace Snark
81    pub link_vk: VK<E::G2Affine>,
82}
83
84impl<E: Pairing> Default for VerifyingKey<E> {
85    fn default() -> Self {
86        Self {
87            alpha_g1: E::G1Affine::default(),
88            beta_g2: E::G2Affine::default(),
89            gamma_g2: E::G2Affine::default(),
90            delta_g2: E::G2Affine::default(),
91            gamma_abc_g1: Vec::new(),
92            eta_gamma_inv_g1: E::G1Affine::default(),
93            commit_witness_count: 0,
94        }
95    }
96}
97
98impl<E: Pairing> Default for VerifyingKeyWithLink<E> {
99    fn default() -> Self {
100        Self {
101            groth16_vk: VerifyingKey::default(),
102            link_pp: PP::<E::G1Affine, E::G2Affine>::default(),
103            link_bases: Vec::new(),
104            link_vk: VK::<E::G2Affine>::default(),
105        }
106    }
107}
108
109/// Preprocessed verification key parameters that enable faster verification
110/// at the expense of larger size in memory.
111#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
112pub struct PreparedVerifyingKey<E: Pairing> {
113    /// The unprepared verification key.
114    pub vk: VerifyingKey<E>,
115    /// The element `e(alpha * G, beta * H)` in `E::GT`.
116    pub alpha_g1_beta_g2: PairingOutput<E>,
117    /// The element `- gamma * H` in `E::G2`, prepared for use in pairings.
118    pub gamma_g2_neg_pc: E::G2Prepared,
119    /// The element `- delta * H` in `E::G2`, prepared for use in pairings.
120    pub delta_g2_neg_pc: E::G2Prepared,
121}
122
123impl<E: Pairing> From<PreparedVerifyingKey<E>> for VerifyingKey<E> {
124    fn from(other: PreparedVerifyingKey<E>) -> Self {
125        other.vk
126    }
127}
128
129impl<E: Pairing> From<&VerifyingKey<E>> for PreparedVerifyingKey<E> {
130    fn from(other: &VerifyingKey<E>) -> Self {
131        crate::prepare_verifying_key(other)
132    }
133}
134
135impl<E: Pairing> Default for PreparedVerifyingKey<E> {
136    fn default() -> Self {
137        Self {
138            vk: VerifyingKey::default(),
139            alpha_g1_beta_g2: PairingOutput::<E>::default(),
140            gamma_g2_neg_pc: E::G2Prepared::default(),
141            delta_g2_neg_pc: E::G2Prepared::default(),
142        }
143    }
144}
145
146////////////////////////////////////////////////////////////////////////////////
147////////////////////////////////////////////////////////////////////////////////
148
149/// The common elements for Proving Key for with and without CP_link
150#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
151pub struct ProvingKeyCommon<E: Pairing> {
152    /// The element `beta * G` in `E::G1`.
153    pub beta_g1: E::G1Affine,
154    /// The element `delta * G` in `E::G1`.
155    pub delta_g1: E::G1Affine,
156    /// The element `eta*delta^-1 * G` in `E::G1`.
157    pub eta_delta_inv_g1: E::G1Affine,
158    /// The elements `a_i * G` in `E::G1`.
159    pub a_query: Vec<E::G1Affine>,
160    /// The elements `b_i * G` in `E::G1`.
161    pub b_g1_query: Vec<E::G1Affine>,
162    /// The elements `b_i * H` in `E::G2`.
163    pub b_g2_query: Vec<E::G2Affine>,
164    /// The elements `h_i * G` in `E::G1`.
165    pub h_query: Vec<E::G1Affine>,
166    /// The elements `l_i * G` in `E::G1`.
167    pub l_query: Vec<E::G1Affine>,
168}
169
170/// The prover key for for the Groth16 zkSNARK.
171#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
172pub struct ProvingKey<E: Pairing> {
173    /// The underlying verification key.
174    pub vk: VerifyingKey<E>,
175    pub common: ProvingKeyCommon<E>,
176}
177
178/// The prover key for for the Groth16 zkSNARK with CP_link parameters
179#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
180pub struct ProvingKeyWithLink<E: Pairing> {
181    /// The underlying verification key.
182    pub vk: VerifyingKeyWithLink<E>,
183    pub common: ProvingKeyCommon<E>,
184    /// Evaluation key of cp_{link}
185    pub link_ek: EK<E::G1Affine>,
186}
187
188/// Public parameters for CP link
189#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
190pub struct LinkPublicGenerators<E: Pairing> {
191    pub pedersen_gens: Vec<E::G1Affine>,
192    pub g1: E::G1Affine,
193    pub g2: E::G2Affine,
194}
195
196impl<E: Pairing> VerifyingKey<E> {
197    pub fn num_public_inputs(&self) -> u32 {
198        self.gamma_abc_g1.len() as u32 - self.commit_witness_count
199    }
200
201    pub fn num_committed_witnesses(&self) -> u32 {
202        self.commit_witness_count
203    }
204
205    /// Get the commitment key used for the Pedersen commitment to witnesses in the proof
206    pub fn get_commitment_key_for_witnesses(&self) -> Vec<E::G1Affine> {
207        let start = self.num_public_inputs();
208        let end = start + self.commit_witness_count;
209        let mut key = Vec::with_capacity(self.commit_witness_count as usize + 1);
210        key.extend_from_slice(&self.gamma_abc_g1[start as usize..end as usize]);
211        key.push(self.eta_gamma_inv_g1);
212        key
213    }
214}