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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(
unused,
future_incompatible,
nonstandard_style,
rust_2018_idioms,
missing_docs
)]
#![forbid(unsafe_code)]
use ark_ff::{PrimeField, ToBytes};
use ark_relations::r1cs::ConstraintSynthesizer;
use ark_std::rand::{CryptoRng, RngCore};
use core::fmt::Debug;
pub trait SNARK<F: PrimeField> {
type ProvingKey: Clone;
type VerifyingKey: Clone + ToBytes;
type Proof: Clone;
type ProcessedVerifyingKey: Clone;
type Error: 'static + ark_std::error::Error;
fn circuit_specific_setup<C: ConstraintSynthesizer<F>, R: RngCore + CryptoRng>(
circuit: C,
rng: &mut R,
) -> Result<(Self::ProvingKey, Self::VerifyingKey), Self::Error>;
fn prove<C: ConstraintSynthesizer<F>, R: RngCore + CryptoRng>(
circuit_pk: &Self::ProvingKey,
circuit: C,
rng: &mut R,
) -> Result<Self::Proof, Self::Error>;
fn verify(
circuit_vk: &Self::VerifyingKey,
public_input: &[F],
proof: &Self::Proof,
) -> Result<bool, Self::Error> {
let pvk = Self::process_vk(circuit_vk)?;
Self::verify_with_processed_vk(&pvk, public_input, proof)
}
fn process_vk(
circuit_vk: &Self::VerifyingKey,
) -> Result<Self::ProcessedVerifyingKey, Self::Error>;
fn verify_with_processed_vk(
circuit_pvk: &Self::ProcessedVerifyingKey,
public_input: &[F],
proof: &Self::Proof,
) -> Result<bool, Self::Error>;
}
pub trait CircuitSpecificSetupSNARK<F: PrimeField>: SNARK<F> {
fn setup<C: ConstraintSynthesizer<F>, R: RngCore + CryptoRng>(
circuit: C,
rng: &mut R,
) -> Result<(Self::ProvingKey, Self::VerifyingKey), Self::Error> {
<Self as SNARK<F>>::circuit_specific_setup(circuit, rng)
}
}
pub enum UniversalSetupIndexError<Bound, E> {
NeedLargerBound(Bound),
Other(E),
}
pub trait UniversalSetupSNARK<F: PrimeField>: SNARK<F> {
type ComputationBound: Clone + Default + Debug;
type PublicParameters: Clone + Debug;
fn universal_setup<R: RngCore + CryptoRng>(
compute_bound: &Self::ComputationBound,
rng: &mut R,
) -> Result<Self::PublicParameters, Self::Error>;
fn index<C: ConstraintSynthesizer<F>, R: RngCore + CryptoRng>(
pp: &Self::PublicParameters,
circuit: C,
rng: &mut R,
) -> Result<
(Self::ProvingKey, Self::VerifyingKey),
UniversalSetupIndexError<Self::ComputationBound, Self::Error>,
>;
}