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
91
92
93
94
95
96
97
98
99
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(
unused,
future_incompatible,
nonstandard_style,
rust_2018_idioms,
missing_docs
)]
#![allow(clippy::many_single_char_names, clippy::op_ref)]
#![forbid(unsafe_code)]
#[macro_use]
extern crate ark_std;
#[cfg(feature = "r1cs")]
#[macro_use]
extern crate derivative;
pub mod r1cs_to_qap;
pub mod data_structures;
pub mod generator;
pub mod prover;
pub mod verifier;
#[cfg(feature = "r1cs")]
pub mod constraints;
#[cfg(test)]
mod test;
pub use self::data_structures::*;
pub use self::{generator::*, prover::*, verifier::*};
use ark_crypto_primitives::snark::*;
use ark_ec::pairing::Pairing;
use ark_relations::r1cs::{ConstraintSynthesizer, SynthesisError};
use ark_std::rand::RngCore;
use ark_std::{marker::PhantomData, vec::Vec};
use r1cs_to_qap::{LibsnarkReduction, R1CSToQAP};
pub struct Groth16<E: Pairing, QAP: R1CSToQAP = LibsnarkReduction> {
_p: PhantomData<(E, QAP)>,
}
impl<E: Pairing, QAP: R1CSToQAP> SNARK<E::ScalarField> for Groth16<E, QAP> {
type ProvingKey = ProvingKey<E>;
type VerifyingKey = VerifyingKey<E>;
type Proof = Proof<E>;
type ProcessedVerifyingKey = PreparedVerifyingKey<E>;
type Error = SynthesisError;
fn circuit_specific_setup<C: ConstraintSynthesizer<E::ScalarField>, R: RngCore>(
circuit: C,
rng: &mut R,
) -> Result<(Self::ProvingKey, Self::VerifyingKey), Self::Error> {
let pk = Self::generate_random_parameters_with_reduction(circuit, rng)?;
let vk = pk.vk.clone();
Ok((pk, vk))
}
fn prove<C: ConstraintSynthesizer<E::ScalarField>, R: RngCore>(
pk: &Self::ProvingKey,
circuit: C,
rng: &mut R,
) -> Result<Self::Proof, Self::Error> {
Self::create_random_proof_with_reduction(circuit, pk, rng)
}
fn process_vk(
circuit_vk: &Self::VerifyingKey,
) -> Result<Self::ProcessedVerifyingKey, Self::Error> {
Ok(prepare_verifying_key(circuit_vk))
}
fn verify_with_processed_vk(
circuit_pvk: &Self::ProcessedVerifyingKey,
x: &[E::ScalarField],
proof: &Self::Proof,
) -> Result<bool, Self::Error> {
Ok(Self::verify_proof(&circuit_pvk, proof, &x)?)
}
}
impl<E: Pairing, QAP: R1CSToQAP> CircuitSpecificSetupSNARK<E::ScalarField> for Groth16<E, QAP> {}