ark-r1cs-std/crypto-primitives/src/nizk/gm17/mod.rs
crypto-primitives/src/nizk/gm17/mod.rsRaw
1use algebra::PairingEngine;
2use crate::Error;
3use rand::Rng;
4use gm17::{
5 create_random_proof, generate_random_parameters, prepare_verifying_key, verify_proof,
6 Parameters, PreparedVerifyingKey, Proof, VerifyingKey,
7};
8use r1cs_core::ConstraintSynthesizer;
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 Gm17<E: PairingEngine, C: ConstraintSynthesizer<E::Fr>, V: ToConstraintField<E::Fr> + ?Sized> {
21 #[doc(hidden)]
22 _engine: PhantomData<E>,
23 #[doc(hidden)]
24 _circuit: PhantomData<C>,
25 #[doc(hidden)]
26 _verifier_input: PhantomData<V>,
27}
28
29impl<E: PairingEngine, C: ConstraintSynthesizer<E::Fr>, V: ToConstraintField<E::Fr> + ?Sized> NIZK for Gm17<E, C, V> {
30 type Circuit = C;
31 type AssignedCircuit = C;
32 type ProvingParameters = Parameters<E>;
33 type VerificationParameters = VerifyingKey<E>;
34 type PreparedVerificationParameters = PreparedVerifyingKey<E>;
35 type VerifierInput = V;
36 type Proof = Proof<E>;
37
38 fn setup<R: Rng>(
39 circuit: Self::Circuit,
40 rng: &mut R,
41 ) -> Result<
42 (
43 Self::ProvingParameters,
44 Self::PreparedVerificationParameters,
45 ),
46 Error,
47 > {
48 let nizk_time = start_timer!(|| "{Groth-Maller 2017}::Setup");
49 let pp = generate_random_parameters::<E, Self::Circuit, R>(circuit, rng)?;
50 let vk = prepare_verifying_key(&pp.vk);
51 end_timer!(nizk_time);
52 Ok((pp, vk))
53 }
54
55 fn prove<R: Rng>(
56 pp: &Self::ProvingParameters,
57 input_and_witness: Self::AssignedCircuit,
58 rng: &mut R,
59 ) -> Result<Self::Proof, Error> {
60 let proof_time = start_timer!(|| "{Groth-Maller 2017}::Prove");
61 let result = create_random_proof::<E, _, _>(input_and_witness, pp, rng)?;
62 end_timer!(proof_time);
63 Ok(result)
64 }
65
66 fn verify(
67 vk: &Self::PreparedVerificationParameters,
68 input: &Self::VerifierInput,
69 proof: &Self::Proof,
70 ) -> Result<bool, Error> {
71 let verify_time = start_timer!(|| "{Groth-Maller 2017}::Verify");
72 let conversion_time = start_timer!(|| "Convert input to E::Fr");
73 let input = input.to_field_elements()?;
74 end_timer!(conversion_time);
75 let verification = start_timer!(|| format!("Verify proof w/ input len: {}", input.len()));
76 let result = verify_proof(&vk, proof, &input)?;
77 end_timer!(verification);
78 end_timer!(verify_time);
79 Ok(result)
80 }
81}