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
use super::{
create_random_proof,
generate_random_parameters,
prepare_verifying_key,
verify_proof,
Parameters,
PreparedVerifyingKey,
Proof,
VerifyingKey,
};
use crate::{errors::SNARKError, traits::SNARK};
use snarkvm_curves::traits::PairingEngine;
use snarkvm_fields::ToConstraintField;
use snarkvm_r1cs::ConstraintSynthesizer;
use rand::Rng;
use std::marker::PhantomData;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GM17<E: PairingEngine, C: ConstraintSynthesizer<E::Fr>, V: ToConstraintField<E::Fr> + ?Sized> {
_engine: PhantomData<E>,
_circuit: PhantomData<C>,
_verifier_input: PhantomData<V>,
}
impl<E: PairingEngine, C: ConstraintSynthesizer<E::Fr>, V: ToConstraintField<E::Fr> + ?Sized> SNARK for GM17<E, C, V> {
type AssignedCircuit = C;
type Circuit = C;
type PreparedVerificationParameters = PreparedVerifyingKey<E>;
type Proof = Proof<E>;
type ProvingParameters = Parameters<E>;
type VerificationParameters = VerifyingKey<E>;
type VerifierInput = V;
fn setup<R: Rng>(
circuit: &Self::Circuit,
rng: &mut R,
) -> Result<(Self::ProvingParameters, Self::PreparedVerificationParameters), SNARKError> {
let setup_time = start_timer!(|| "{Groth-Maller 2017}::Setup");
let pp = generate_random_parameters::<E, Self::Circuit, R>(circuit, rng)?;
let vk = prepare_verifying_key(pp.vk.clone());
end_timer!(setup_time);
Ok((pp, vk))
}
fn prove<R: Rng>(
pp: &Self::ProvingParameters,
input_and_witness: &Self::AssignedCircuit,
rng: &mut R,
) -> Result<Self::Proof, SNARKError> {
let proof_time = start_timer!(|| "{Groth-Maller 2017}::Prove");
let result = create_random_proof::<E, _, _>(input_and_witness, pp, rng)?;
end_timer!(proof_time);
Ok(result)
}
fn verify(
vk: &Self::PreparedVerificationParameters,
input: &Self::VerifierInput,
proof: &Self::Proof,
) -> Result<bool, SNARKError> {
let verify_time = start_timer!(|| "{Groth-Maller 2017}::Verify");
let conversion_time = start_timer!(|| "Convert input to E::Fr");
let input = input.to_field_elements()?;
end_timer!(conversion_time);
let verification = start_timer!(|| format!("Verify proof w/ input len: {}", input.len()));
let result = verify_proof(&vk, proof, &input)?;
end_timer!(verification);
end_timer!(verify_time);
Ok(result)
}
}