forked from privacy-scaling-explorations/halo2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplonk.rs
354 lines (316 loc) · 11.2 KB
/
plonk.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#[macro_use]
extern crate criterion;
use group::ff::Field;
use halo2_proofs::circuit::{Cell, Layouter, SimpleFloorPlanner, Value};
use halo2_proofs::plonk::*;
use halo2_proofs::poly::Rotation;
use halo2curves::bn256;
use rand_core::OsRng;
use std::marker::PhantomData;
use criterion::{BenchmarkId, Criterion};
use halo2_proofs::poly::commitment::Guard;
use halo2_proofs::poly::kzg::params::ParamsVerifierKZG;
use halo2_proofs::poly::kzg::{params::ParamsKZG, KZGCommitmentScheme};
use halo2_proofs::transcript::{CircuitTranscript, Transcript};
use halo2_proofs::utils::rational::Rational;
use halo2curves::bn256::Bn256;
fn criterion_benchmark(c: &mut Criterion) {
#[derive(Clone)]
struct PlonkConfig {
a: Column<Advice>,
b: Column<Advice>,
c: Column<Advice>,
sa: Column<Fixed>,
sb: Column<Fixed>,
sc: Column<Fixed>,
sm: Column<Fixed>,
}
trait StandardCs<FF: Field> {
fn raw_multiply<F>(
&self,
layouter: &mut impl Layouter<FF>,
f: F,
) -> Result<(Cell, Cell, Cell), Error>
where
F: FnMut() -> Value<(Rational<FF>, Rational<FF>, Rational<FF>)>;
fn raw_add<F>(
&self,
layouter: &mut impl Layouter<FF>,
f: F,
) -> Result<(Cell, Cell, Cell), Error>
where
F: FnMut() -> Value<(Rational<FF>, Rational<FF>, Rational<FF>)>;
fn copy(&self, layouter: &mut impl Layouter<FF>, a: Cell, b: Cell) -> Result<(), Error>;
}
#[derive(Clone)]
struct MyCircuit<F: Field> {
a: Value<F>,
k: u32,
}
struct StandardPlonk<F: Field> {
config: PlonkConfig,
_marker: PhantomData<F>,
}
impl<FF: Field> StandardPlonk<FF> {
fn new(config: PlonkConfig) -> Self {
StandardPlonk {
config,
_marker: PhantomData,
}
}
}
impl<FF: Field> StandardCs<FF> for StandardPlonk<FF> {
fn raw_multiply<F>(
&self,
layouter: &mut impl Layouter<FF>,
mut f: F,
) -> Result<(Cell, Cell, Cell), Error>
where
F: FnMut() -> Value<(Rational<FF>, Rational<FF>, Rational<FF>)>,
{
layouter.assign_region(
|| "raw_multiply",
|mut region| {
let mut value = None;
let lhs = region.assign_advice(
|| "lhs",
self.config.a,
0,
|| {
value = Some(f());
value.unwrap().map(|v| v.0)
},
)?;
let rhs = region.assign_advice(
|| "rhs",
self.config.b,
0,
|| value.unwrap().map(|v| v.1),
)?;
let out = region.assign_advice(
|| "out",
self.config.c,
0,
|| value.unwrap().map(|v| v.2),
)?;
region.assign_fixed(|| "a", self.config.sa, 0, || Value::known(FF::ZERO))?;
region.assign_fixed(|| "b", self.config.sb, 0, || Value::known(FF::ZERO))?;
region.assign_fixed(|| "c", self.config.sc, 0, || Value::known(FF::ONE))?;
region.assign_fixed(|| "a * b", self.config.sm, 0, || Value::known(FF::ONE))?;
Ok((lhs.cell(), rhs.cell(), out.cell()))
},
)
}
fn raw_add<F>(
&self,
layouter: &mut impl Layouter<FF>,
mut f: F,
) -> Result<(Cell, Cell, Cell), Error>
where
F: FnMut() -> Value<(Rational<FF>, Rational<FF>, Rational<FF>)>,
{
layouter.assign_region(
|| "raw_add",
|mut region| {
let mut value = None;
let lhs = region.assign_advice(
|| "lhs",
self.config.a,
0,
|| {
value = Some(f());
value.unwrap().map(|v| v.0)
},
)?;
let rhs = region.assign_advice(
|| "rhs",
self.config.b,
0,
|| value.unwrap().map(|v| v.1),
)?;
let out = region.assign_advice(
|| "out",
self.config.c,
0,
|| value.unwrap().map(|v| v.2),
)?;
region.assign_fixed(|| "a", self.config.sa, 0, || Value::known(FF::ONE))?;
region.assign_fixed(|| "b", self.config.sb, 0, || Value::known(FF::ONE))?;
region.assign_fixed(|| "c", self.config.sc, 0, || Value::known(FF::ONE))?;
region.assign_fixed(
|| "a * b",
self.config.sm,
0,
|| Value::known(FF::ZERO),
)?;
Ok((lhs.cell(), rhs.cell(), out.cell()))
},
)
}
fn copy(
&self,
layouter: &mut impl Layouter<FF>,
left: Cell,
right: Cell,
) -> Result<(), Error> {
layouter.assign_region(|| "copy", |mut region| region.constrain_equal(left, right))
}
}
impl<F: Field> Circuit<F> for MyCircuit<F> {
type Config = PlonkConfig;
type FloorPlanner = SimpleFloorPlanner;
#[cfg(feature = "circuit-params")]
type Params = ();
fn without_witnesses(&self) -> Self {
Self {
a: Value::unknown(),
k: self.k,
}
}
fn configure(meta: &mut ConstraintSystem<F>) -> PlonkConfig {
meta.set_minimum_degree(5);
let a = meta.advice_column();
let b = meta.advice_column();
let c = meta.advice_column();
meta.enable_equality(a);
meta.enable_equality(b);
meta.enable_equality(c);
let sm = meta.fixed_column();
let sa = meta.fixed_column();
let sb = meta.fixed_column();
let sc = meta.fixed_column();
meta.create_gate("Combined add-mult", |meta| {
let a = meta.query_advice(a, Rotation::cur());
let b = meta.query_advice(b, Rotation::cur());
let c = meta.query_advice(c, Rotation::cur());
let sa = meta.query_fixed(sa, Rotation::cur());
let sb = meta.query_fixed(sb, Rotation::cur());
let sc = meta.query_fixed(sc, Rotation::cur());
let sm = meta.query_fixed(sm, Rotation::cur());
vec![a.clone() * sa + b.clone() * sb + a * b * sm - (c * sc)]
});
PlonkConfig {
a,
b,
c,
sa,
sb,
sc,
sm,
}
}
fn synthesize(
&self,
config: PlonkConfig,
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
let cs = StandardPlonk::new(config);
for _ in 0..((1 << (self.k - 1)) - 3) {
let a: Value<Rational<_>> = self.a.into();
let mut a_squared = Value::unknown();
let (a0, _, c0) = cs.raw_multiply(&mut layouter, || {
a_squared = a.square();
a.zip(a_squared).map(|(a, a_squared)| (a, a, a_squared))
})?;
let (a1, b1, _) = cs.raw_add(&mut layouter, || {
let fin = a_squared + a;
a.zip(a_squared)
.zip(fin)
.map(|((a, a_squared), fin)| (a, a_squared, fin))
})?;
cs.copy(&mut layouter, a0, a1)?;
cs.copy(&mut layouter, b1, c0)?;
}
Ok(())
}
}
fn keygen(
k: u32,
) -> (
ParamsKZG<bn256::Bn256>,
ProvingKey<bn256::Fr, KZGCommitmentScheme<bn256::Bn256>>,
) {
let params: ParamsKZG<Bn256> = ParamsKZG::unsafe_setup(k, OsRng);
let empty_circuit: MyCircuit<bn256::Fr> = MyCircuit {
a: Value::unknown(),
k,
};
let vk = keygen_vk_with_k(¶ms, &empty_circuit, k).expect("keygen_vk should not fail");
let pk = keygen_pk(vk, &empty_circuit).expect("keygen_pk should not fail");
(params, pk)
}
fn prover(
k: u32,
params: &ParamsKZG<bn256::Bn256>,
pk: &ProvingKey<bn256::Fr, KZGCommitmentScheme<bn256::Bn256>>,
) -> Vec<u8> {
let rng = OsRng;
let circuit: MyCircuit<bn256::Fr> = MyCircuit {
a: Value::known(bn256::Fr::random(rng)),
k,
};
let mut transcript = CircuitTranscript::init();
create_proof::<bn256::Fr, KZGCommitmentScheme<bn256::Bn256>, _, _>(
params,
pk,
&[circuit],
&[&[]],
rng,
&mut transcript,
)
.expect("proof generation should not fail");
transcript.finalize()
}
fn verifier(
params: &ParamsVerifierKZG<bn256::Bn256>,
vk: &VerifyingKey<bn256::Fr, KZGCommitmentScheme<bn256::Bn256>>,
proof: &[u8],
) {
let mut transcript = CircuitTranscript::init_from_bytes(proof);
assert!(prepare::<bn256::Fr, KZGCommitmentScheme<bn256::Bn256>, _>(
vk,
&[&[]],
&mut transcript
)
.unwrap()
.verify(params)
.is_ok());
}
let k_range = 8..=16;
let mut keygen_group = c.benchmark_group("plonk-keygen");
keygen_group.sample_size(10);
for k in k_range.clone() {
keygen_group.bench_with_input(BenchmarkId::from_parameter(k), &k, |b, &k| {
b.iter(|| keygen(k));
});
}
keygen_group.finish();
let mut prover_group = c.benchmark_group("plonk-prover");
prover_group.sample_size(10);
for k in k_range.clone() {
let (params, pk) = keygen(k);
prover_group.bench_with_input(
BenchmarkId::from_parameter(k),
&(k, ¶ms, &pk),
|b, &(k, params, pk)| {
b.iter(|| prover(k, params, pk));
},
);
}
prover_group.finish();
let mut verifier_group = c.benchmark_group("plonk-verifier");
for k in k_range {
let (params, pk) = keygen(k);
let proof = prover(k, ¶ms, &pk);
verifier_group.bench_with_input(
BenchmarkId::from_parameter(k),
&(¶ms, pk.get_vk(), &proof[..]),
|b, &(params, vk, proof)| {
b.iter(|| verifier(¶ms.verifier_params(), vk, proof));
},
);
}
verifier_group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);