arnaucube
arnaucube/sonobePublic Self-hosted repository
sonobe/solidity-verifiers/src/verifiers/g16.rs
solidity-verifiers/src/verifiers/g16.rsRaw
 1use crate::utils::encoding::{g1_to_fq_repr, g2_to_fq_repr};
 2use crate::utils::encoding::{G1Repr, G2Repr};
 3use crate::utils::HeaderInclusion;
 4use crate::{ProtocolData, GPL3_SDPX_IDENTIFIER};
 5use ark_bn254::Bn254;
 6use ark_groth16::VerifyingKey;
 7use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
 8use askama::Template;
 9
10use super::PRAGMA_GROTH16_VERIFIER;
11
12#[derive(Template, Default)]
13#[template(path = "groth16_verifier.askama.sol", ext = "sol")]
14pub(crate) struct Groth16Verifier {
15    /// The `alpha * G`, where `G` is the generator of `G1`.
16    pub(crate) vkey_alpha_g1: G1Repr,
17    /// The `alpha * H`, where `H` is the generator of `G2`.
18    pub(crate) vkey_beta_g2: G2Repr,
19    /// The `gamma * H`, where `H` is the generator of `G2`.
20    pub(crate) vkey_gamma_g2: G2Repr,
21    /// The `delta * H`, where `H` is the generator of `G2`.
22    pub(crate) vkey_delta_g2: G2Repr,
23    /// Length of the `gamma_abc_g1` vector.
24    pub(crate) gamma_abc_len: usize,
25    /// The `gamma^{-1} * (beta * a_i + alpha * b_i + c_i) * H`, where `H` is the generator of `E::G1`.
26    pub(crate) gamma_abc_g1: Vec<G1Repr>,
27}
28
29impl From<Groth16Data> for Groth16Verifier {
30    fn from(g16_data: Groth16Data) -> Self {
31        Self {
32            vkey_alpha_g1: g1_to_fq_repr(g16_data.0.alpha_g1),
33            vkey_beta_g2: g2_to_fq_repr(g16_data.0.beta_g2),
34            vkey_gamma_g2: g2_to_fq_repr(g16_data.0.gamma_g2),
35            vkey_delta_g2: g2_to_fq_repr(g16_data.0.delta_g2),
36            gamma_abc_len: g16_data.0.gamma_abc_g1.len(),
37            gamma_abc_g1: g16_data
38                .0
39                .gamma_abc_g1
40                .iter()
41                .copied()
42                .map(g1_to_fq_repr)
43                .collect(),
44        }
45    }
46}
47
48// Ideally I would like to link this to the `Decider` trait in FoldingSchemes.
49// For now, this is the easiest as NovaCyclefold isn't clear target from where we can get all it's needed arguments.
50#[derive(CanonicalDeserialize, CanonicalSerialize, Clone, PartialEq, Debug)]
51pub struct Groth16Data(pub(crate) VerifyingKey<Bn254>);
52
53impl From<VerifyingKey<Bn254>> for Groth16Data {
54    fn from(value: VerifyingKey<Bn254>) -> Self {
55        Self(value)
56    }
57}
58
59impl ProtocolData for Groth16Data {
60    const PROTOCOL_NAME: &'static str = "Groth16";
61
62    fn render_as_template(self, pragma: Option<String>) -> Vec<u8> {
63        HeaderInclusion::<Groth16Verifier>::builder()
64            .sdpx(GPL3_SDPX_IDENTIFIER.to_string())
65            .pragma_version(pragma.unwrap_or(PRAGMA_GROTH16_VERIFIER.to_string()))
66            .template(self)
67            .build()
68            .render()
69            .unwrap()
70            .into_bytes()
71    }
72}