arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/crypto-primitives/src/nizk/groth16/mod.rs
crypto-primitives/src/nizk/groth16/mod.rsRaw
 1use crate::Error;
 2use algebra::PairingEngine;
 3use groth16::{
 4    create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
 5    Parameters, PreparedVerifyingKey, Proof, VerifyingKey,
 6};
 7use r1cs_core::ConstraintSynthesizer;
 8use rand::Rng;
 9
10use algebra::ToConstraintField;
11use std::marker::PhantomData;
12
13use super::NIZK;
14
15#[cfg(feature = "r1cs")]
16pub mod constraints;
17
18/// Note: V should serialize its contents to `Vec<E::Fr>` in the same order as
19/// during the constraint generation.
20pub struct Groth16<
21    E: PairingEngine,
22    C: ConstraintSynthesizer<E::Fr>,
23    V: ToConstraintField<E::Fr> + ?Sized,
24> {
25    #[doc(hidden)]
26    _engine: PhantomData<E>,
27    #[doc(hidden)]
28    _circuit: PhantomData<C>,
29    #[doc(hidden)]
30    _verifier_input: PhantomData<V>,
31}
32
33impl<E: PairingEngine, C: ConstraintSynthesizer<E::Fr>, V: ToConstraintField<E::Fr> + ?Sized> NIZK
34    for Groth16<E, C, V>
35{
36    type Circuit = C;
37    type AssignedCircuit = C;
38    type ProvingParameters = Parameters<E>;
39    type VerificationParameters = VerifyingKey<E>;
40    type PreparedVerificationParameters = PreparedVerifyingKey<E>;
41    type VerifierInput = V;
42    type Proof = Proof<E>;
43
44    fn setup<R: Rng>(
45        circuit: Self::Circuit,
46        rng: &mut R,
47    ) -> Result<
48        (
49            Self::ProvingParameters,
50            Self::PreparedVerificationParameters,
51        ),
52        Error,
53    > {
54        let nizk_time = start_timer!(|| "{Groth 2016}::Setup");
55        let pp = generate_random_parameters::<E, Self::Circuit, R>(circuit, rng)?;
56        let vk = prepare_verifying_key(&pp.vk);
57        end_timer!(nizk_time);
58        Ok((pp, vk))
59    }
60
61    fn prove<R: Rng>(
62        pp: &Self::ProvingParameters,
63        input_and_witness: Self::AssignedCircuit,
64        rng: &mut R,
65    ) -> Result<Self::Proof, Error> {
66        let proof_time = start_timer!(|| "{Groth 2016}::Prove");
67        let result = create_random_proof::<E, _, _>(input_and_witness, pp, rng)?;
68        end_timer!(proof_time);
69        Ok(result)
70    }
71
72    fn verify(
73        vk: &Self::PreparedVerificationParameters,
74        input: &Self::VerifierInput,
75        proof: &Self::Proof,
76    ) -> Result<bool, Error> {
77        let verify_time = start_timer!(|| "{Groth 2016}::Verify");
78        let conversion_time = start_timer!(|| "Convert input to E::Fr");
79        let input = input.to_field_elements()?;
80        end_timer!(conversion_time);
81        let verification = start_timer!(|| format!("Verify proof w/ input len: {}", input.len()));
82        let result = verify_proof(&vk, proof, &input)?;
83        end_timer!(verification);
84        end_timer!(verify_time);
85        Ok(result)
86    }
87}