forked from axiom-crypto/snark-verifier
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathhalo2_api.rs
320 lines (291 loc) · 10 KB
/
halo2_api.rs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::{
fs::{self, File},
io::BufWriter,
path::Path,
};
use crate::{
circuit_ext::CircuitExt,
file_io::{read_pk, read_snark},
read_instances,
types::{PoseidonTranscript, POSEIDON_SPEC},
write_instances, Snark,
};
#[cfg(feature = "display")]
use ark_std::end_timer;
#[cfg(feature = "display")]
use ark_std::start_timer;
use halo2_base::halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::{create_proof, keygen_pk, keygen_vk, verify_proof, Circuit, ProvingKey, VerifyingKey},
poly::{
commitment::{ParamsProver, Prover, Verifier},
kzg::{
commitment::{KZGCommitmentScheme, ParamsKZG},
msm::DualMSM,
multiopen::{ProverGWC, ProverSHPLONK, VerifierGWC, VerifierSHPLONK},
strategy::{AccumulatorStrategy, GuardKZG, SingleStrategy},
},
VerificationStrategy,
},
transcript::TranscriptReadBuffer,
SerdeFormat, {self},
};
use itertools::Itertools;
use rand::Rng;
use snark_verifier::{
loader::native::NativeLoader,
system::halo2::{compile, Config},
};
#[allow(clippy::let_and_return)]
pub fn gen_pk<C: Circuit<Fr>>(
params: &ParamsKZG<Bn256>, // TODO: read pk without params
circuit: &C,
path: Option<&Path>,
) -> ProvingKey<G1Affine> {
if let Some(path) = path {
if let Ok(pk) = read_pk::<C>(path) {
return pk;
}
}
#[cfg(feature = "display")]
let pk_time = start_timer!(|| "Generating vkey & pkey");
let vk = keygen_vk(params, circuit).unwrap();
let pk = keygen_pk(params, vk, circuit).unwrap();
#[cfg(feature = "display")]
end_timer!(pk_time);
if let Some(path) = path {
#[cfg(feature = "display")]
let write_time = start_timer!(|| format!("Writing pkey to {path:?}"));
path.parent().and_then(|dir| fs::create_dir_all(dir).ok()).unwrap();
let mut f = BufWriter::new(File::create(path).unwrap());
pk.write(&mut f, SerdeFormat::RawBytesUnchecked).unwrap();
#[cfg(feature = "display")]
end_timer!(write_time);
}
pk
}
/// Generates a native proof using either SHPLONK or GWC proving method. Uses Poseidon for Fiat-Shamir.
///
/// Caches the instances and proof if `path = Some(instance_path, proof_path)` is specified.
pub fn gen_proof<'params, C, P, V>(
// TODO: pass Option<&'params ParamsKZG<Bn256>> but hard to get lifetimes to work with `Cow`
params: &'params ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,
rng: &mut (impl Rng + Send),
path: Option<(&Path, &Path)>,
) -> Vec<u8>
where
C: Circuit<Fr>,
P: Prover<'params, KZGCommitmentScheme<Bn256>>,
V: Verifier<
'params,
KZGCommitmentScheme<Bn256>,
Guard = GuardKZG<'params, Bn256>,
MSMAccumulator = DualMSM<'params, Bn256>,
>,
{
#[cfg(debug_assertions)]
{
use halo2_proofs::poly::commitment::Params;
halo2_proofs::dev::MockProver::run(params.k(), &circuit, instances.clone())
.unwrap()
.assert_satisfied();
}
if let Some((instance_path, proof_path)) = path {
let cached_instances = read_instances(instance_path);
if matches!(cached_instances, Ok(tmp) if tmp == instances) && proof_path.exists() {
#[cfg(feature = "display")]
let read_time = start_timer!(|| format!("Reading proof from {proof_path:?}"));
let proof = fs::read(proof_path).unwrap();
#[cfg(feature = "display")]
end_timer!(read_time);
return proof;
}
}
let instances = instances.iter().map(Vec::as_slice).collect_vec();
#[cfg(feature = "display")]
let proof_time = start_timer!(|| "Create proof");
let mut transcript =
PoseidonTranscript::<NativeLoader, Vec<u8>>::from_spec(vec![], POSEIDON_SPEC.clone());
create_proof::<_, P, _, _, _, _>(params, pk, &[circuit], &[&instances], rng, &mut transcript)
.unwrap();
let proof = transcript.finalize();
#[cfg(feature = "display")]
end_timer!(proof_time);
if let Some((instance_path, proof_path)) = path {
write_instances(&instances, instance_path);
fs::write(proof_path, &proof).unwrap();
}
debug_assert!({
let mut transcript_read = PoseidonTranscript::<NativeLoader, &[u8]>::new(proof.as_slice());
VerificationStrategy::<_, V>::finalize(
verify_proof::<_, V, _, _, _>(
params.verifier_params(),
pk.get_vk(),
AccumulatorStrategy::new(params.verifier_params()),
&[instances.as_slice()],
&mut transcript_read,
)
.unwrap(),
)
});
proof
}
/// Generates a native proof using original Plonk (GWC '19) multi-open scheme. Uses Poseidon for Fiat-Shamir.
///
/// Caches the instances and proof if `path = Some(instance_path, proof_path)` is specified.
pub fn gen_proof_gwc<C: Circuit<Fr>>(
params: &ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,
rng: &mut (impl Rng + Send),
path: Option<(&Path, &Path)>,
) -> Vec<u8> {
gen_proof::<C, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, instances, rng, path)
}
/// Generates a native proof using SHPLONK multi-open scheme. Uses Poseidon for Fiat-Shamir.
///
/// Caches the instances and proof if `path` is specified.
pub fn gen_proof_shplonk<C: Circuit<Fr>>(
params: &ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,
rng: &mut (impl Rng + Send),
path: Option<(&Path, &Path)>,
) -> Vec<u8> {
gen_proof::<C, ProverSHPLONK<_>, VerifierSHPLONK<_>>(params, pk, circuit, instances, rng, path)
}
/// Generates a SNARK using either SHPLONK or GWC multi-open scheme. Uses Poseidon for Fiat-Shamir.
///
/// Tries to first deserialize from / later serialize the entire SNARK into `path` if specified.
/// Serialization is done using `bincode`.
pub fn gen_snark<'params, ConcreteCircuit, P, V>(
params: &'params ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: ConcreteCircuit,
rng: &mut (impl Rng + Send),
path: Option<impl AsRef<Path>>,
) -> Snark
where
ConcreteCircuit: CircuitExt<Fr>,
P: Prover<'params, KZGCommitmentScheme<Bn256>>,
V: Verifier<
'params,
KZGCommitmentScheme<Bn256>,
Guard = GuardKZG<'params, Bn256>,
MSMAccumulator = DualMSM<'params, Bn256>,
>,
{
if let Some(path) = &path {
if let Ok(snark) = read_snark(path) {
return snark;
}
}
let protocol = compile(
params,
pk.get_vk(),
Config::kzg()
.with_num_instance(circuit.num_instance())
.with_accumulator_indices(ConcreteCircuit::accumulator_indices()),
);
let instances = circuit.instances();
let proof =
gen_proof::<ConcreteCircuit, P, V>(params, pk, circuit, instances.clone(), rng, None);
let snark = Snark::new(protocol, instances, proof);
if let Some(path) = &path {
let f = File::create(path).unwrap();
#[cfg(feature = "display")]
let write_time = start_timer!(|| "Write SNARK");
bincode::serialize_into(f, &snark).unwrap();
#[cfg(feature = "display")]
end_timer!(write_time);
}
snark
}
/// Generates a SNARK using GWC multi-open scheme. Uses Poseidon for Fiat-Shamir.
///
/// Tries to first deserialize from / later serialize the entire SNARK into `path` if specified.
/// Serialization is done using `bincode`.
pub fn gen_snark_gwc<ConcreteCircuit: CircuitExt<Fr>>(
params: &ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: ConcreteCircuit,
rng: &mut (impl Rng + Send),
path: Option<impl AsRef<Path>>,
) -> Snark {
gen_snark::<ConcreteCircuit, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, rng, path)
}
/// Generates a SNARK using SHPLONK multi-open scheme. Uses Poseidon for Fiat-Shamir.
///
/// Tries to first deserialize from / later serialize the entire SNARK into `path` if specified.
/// Serialization is done using `bincode`.
pub fn gen_snark_shplonk<ConcreteCircuit: CircuitExt<Fr>>(
params: &ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: ConcreteCircuit,
rng: &mut (impl Rng + Send),
path: Option<impl AsRef<Path>>,
) -> Snark {
gen_snark::<ConcreteCircuit, ProverSHPLONK<_>, VerifierSHPLONK<_>>(
params, pk, circuit, rng, path,
)
}
/// Verifies a native proof using either SHPLONK or GWC proving method. Uses Poseidon for Fiat-Shamir.
///
pub fn verify_snark<'params, ConcreteCircuit, V>(
verifier_params: &'params ParamsKZG<Bn256>,
snark: Snark,
vk: &VerifyingKey<G1Affine>,
) -> bool
where
ConcreteCircuit: CircuitExt<Fr>,
V: Verifier<
'params,
KZGCommitmentScheme<Bn256>,
Guard = GuardKZG<'params, Bn256>,
MSMAccumulator = DualMSM<'params, Bn256>,
>,
{
let mut transcript: PoseidonTranscript<_, _> =
TranscriptReadBuffer::<_, G1Affine, _>::init(snark.proof.as_slice());
let strategy = SingleStrategy::new(verifier_params);
let instance_slice = snark.instances.iter().map(|x| &x[..]).collect::<Vec<_>>();
match verify_proof::<_, V, _, _, _>(
verifier_params,
vk,
strategy,
&[instance_slice.as_slice()],
&mut transcript,
) {
Ok(_p) => true,
Err(_e) => false,
}
}
/// Verifies a native proof using SHPLONK proving method. Uses Poseidon for Fiat-Shamir.
///
pub fn verify_snark_shplonk<ConcreteCircuit>(
verifier_params: &ParamsKZG<Bn256>,
snark: Snark,
vk: &VerifyingKey<G1Affine>,
) -> bool
where
ConcreteCircuit: CircuitExt<Fr>,
{
verify_snark::<ConcreteCircuit, VerifierSHPLONK<_>>(verifier_params, snark, vk)
}
/// Verifies a native proof using GWC proving method. Uses Poseidon for Fiat-Shamir.
///
pub fn verify_snark_gwc<ConcreteCircuit>(
verifier_params: &ParamsKZG<Bn256>,
snark: Snark,
vk: &VerifyingKey<G1Affine>,
) -> bool
where
ConcreteCircuit: CircuitExt<Fr>,
{
verify_snark::<ConcreteCircuit, VerifierGWC<_>>(verifier_params, snark, vk)
}