forked from zcash/halo2
-
Notifications
You must be signed in to change notification settings - Fork 129
/
shuffle.rs
352 lines (306 loc) · 11 KB
/
shuffle.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
use ff::{BatchInvert, FromUniformBytes};
use halo2_proofs::{
arithmetic::{CurveAffine, Field},
circuit::{floor_planner::V1, Layouter, Value},
dev::{metadata, FailureLocation, MockProver, VerifyFailure},
halo2curves::pasta::EqAffine,
plonk::*,
poly::{
commitment::ParamsProver,
ipa::{
commitment::{IPACommitmentScheme, ParamsIPA},
multiopen::{ProverIPA, VerifierIPA},
strategy::AccumulatorStrategy,
},
VerificationStrategy,
},
transcript::{
Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer,
},
};
use rand_core::{OsRng, RngCore};
use std::iter;
fn rand_2d_array<F: Field, R: RngCore, const W: usize, const H: usize>(rng: &mut R) -> [[F; H]; W] {
[(); W].map(|_| [(); H].map(|_| F::random(&mut *rng)))
}
fn shuffled<F: Field, R: RngCore, const W: usize, const H: usize>(
original: [[F; H]; W],
rng: &mut R,
) -> [[F; H]; W] {
let mut shuffled = original;
for row in (1..H).rev() {
let rand_row = (rng.next_u32() as usize) % row;
for column in shuffled.iter_mut() {
column.swap(row, rand_row);
}
}
shuffled
}
#[derive(Clone)]
struct MyConfig<const W: usize> {
q_shuffle: Selector,
q_first: Selector,
q_last: Selector,
original: [Column<Advice>; W],
shuffled: [Column<Advice>; W],
theta: Challenge,
gamma: Challenge,
z: Column<Advice>,
}
impl<const W: usize> MyConfig<W> {
fn configure<F: Field>(meta: &mut ConstraintSystem<F>) -> Self {
let [q_shuffle, q_first, q_last] = [(); 3].map(|_| meta.selector());
// First phase
let original = [(); W].map(|_| meta.advice_column_in(FirstPhase));
let shuffled = [(); W].map(|_| meta.advice_column_in(FirstPhase));
let [theta, gamma] = [(); 2].map(|_| meta.challenge_usable_after(FirstPhase));
// Second phase
let z = meta.advice_column_in(SecondPhase);
meta.create_gate("z should start with 1", |_| {
let one = Expression::Constant(F::ONE);
vec![q_first.expr() * (one - z.cur())]
});
meta.create_gate("z should end with 1", |_| {
let one = Expression::Constant(F::ONE);
vec![q_last.expr() * (one - z.cur())]
});
meta.create_gate("z should have valid transition", |_| {
let q_shuffle = q_shuffle.expr();
let original = original.map(|advice| advice.cur());
let shuffled = shuffled.map(|advice| advice.cur());
let [theta, gamma] = [theta, gamma].map(|challenge| challenge.expr());
// Compress
let original = original
.iter()
.cloned()
.reduce(|acc, a| acc * theta.clone() + a)
.unwrap();
let shuffled = shuffled
.iter()
.cloned()
.reduce(|acc, a| acc * theta.clone() + a)
.unwrap();
vec![q_shuffle * (z.cur() * (original + gamma.clone()) - z.next() * (shuffled + gamma))]
});
Self {
q_shuffle,
q_first,
q_last,
original,
shuffled,
theta,
gamma,
z,
}
}
}
#[derive(Clone, Default)]
struct MyCircuit<F: Field, const W: usize, const H: usize> {
original: Value<[[F; H]; W]>,
shuffled: Value<[[F; H]; W]>,
}
impl<F: Field, const W: usize, const H: usize> MyCircuit<F, W, H> {
fn rand<R: RngCore>(rng: &mut R) -> Self {
let original = rand_2d_array::<F, _, W, H>(rng);
let shuffled = shuffled(original, rng);
Self {
original: Value::known(original),
shuffled: Value::known(shuffled),
}
}
}
impl<F: Field, const W: usize, const H: usize> Circuit<F> for MyCircuit<F, W, H> {
type Config = MyConfig<W>;
type FloorPlanner = V1;
#[cfg(feature = "circuit-params")]
type Params = ();
fn without_witnesses(&self) -> Self {
Self::default()
}
fn configure(meta: &mut ConstraintSystem<F>) -> Self::Config {
MyConfig::configure(meta)
}
fn synthesize(
&self,
config: Self::Config,
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
let theta = layouter.get_challenge(config.theta);
let gamma = layouter.get_challenge(config.gamma);
layouter.assign_region(
|| "Shuffle original into shuffled",
|mut region| {
// Keygen
config.q_first.enable(&mut region, 0)?;
config.q_last.enable(&mut region, H)?;
for offset in 0..H {
config.q_shuffle.enable(&mut region, offset)?;
}
// First phase
for (idx, (&column, values)) in config
.original
.iter()
.zip(self.original.transpose_array().iter())
.enumerate()
{
for (offset, &value) in values.transpose_array().iter().enumerate() {
region.assign_advice(
|| format!("original[{idx}][{offset}]"),
column,
offset,
|| value,
)?;
}
}
for (idx, (&column, values)) in config
.shuffled
.iter()
.zip(self.shuffled.transpose_array().iter())
.enumerate()
{
for (offset, &value) in values.transpose_array().iter().enumerate() {
region.assign_advice(
|| format!("shuffled[{idx}][{offset}]"),
column,
offset,
|| value,
)?;
}
}
// Second phase
let z = self.original.zip(self.shuffled).zip(theta).zip(gamma).map(
|(((original, shuffled), theta), gamma)| {
let mut product = vec![F::ZERO; H];
for (idx, product) in product.iter_mut().enumerate() {
let mut compressed = F::ZERO;
for value in shuffled.iter() {
compressed *= theta;
compressed += value[idx];
}
*product = compressed + gamma
}
product.iter_mut().batch_invert();
for (idx, product) in product.iter_mut().enumerate() {
let mut compressed = F::ZERO;
for value in original.iter() {
compressed *= theta;
compressed += value[idx];
}
*product *= compressed + gamma
}
#[allow(clippy::let_and_return)]
let z = iter::once(F::ONE)
.chain(product)
.scan(F::ONE, |state, cur| {
*state *= &cur;
Some(*state)
})
.collect::<Vec<_>>();
#[cfg(feature = "sanity-checks")]
assert_eq!(F::ONE, *z.last().unwrap());
z
},
);
for (offset, value) in z.transpose_vec(H + 1).into_iter().enumerate() {
region.assign_advice(|| format!("z[{offset}]"), config.z, offset, || value)?;
}
Ok(())
},
)
}
}
fn test_mock_prover<F: Ord + FromUniformBytes<64>, const W: usize, const H: usize>(
k: u32,
circuit: MyCircuit<F, W, H>,
expected: Result<(), Vec<(metadata::Constraint, FailureLocation)>>,
) {
let prover = MockProver::run(k, &circuit, vec![]).unwrap();
match (prover.verify(), expected) {
(Ok(_), Ok(_)) => {}
(Err(err), Err(expected)) => {
assert_eq!(
err.into_iter()
.map(|failure| match failure {
VerifyFailure::ConstraintNotSatisfied {
constraint,
location,
..
} => (constraint, location),
_ => panic!("MockProver::verify has result unmatching expected"),
})
.collect::<Vec<_>>(),
expected
)
}
(_, _) => panic!("MockProver::verify has result unmatching expected"),
};
}
fn test_prover<C: CurveAffine, const W: usize, const H: usize>(
k: u32,
circuit: MyCircuit<C::Scalar, W, H>,
expected: bool,
) where
C::Scalar: FromUniformBytes<64>,
{
let params = ParamsIPA::<C>::new(k);
let vk = keygen_vk(¶ms, &circuit).unwrap();
let pk = keygen_pk(¶ms, vk, &circuit).unwrap();
let proof = {
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
create_proof::<IPACommitmentScheme<C>, ProverIPA<C>, _, _, _, _>(
¶ms,
&pk,
&[circuit],
&[&[]],
OsRng,
&mut transcript,
)
.expect("proof generation should not fail");
transcript.finalize()
};
let accepted = {
let strategy = AccumulatorStrategy::new(¶ms);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof[..]);
verify_proof::<IPACommitmentScheme<C>, VerifierIPA<C>, _, _, _>(
¶ms,
pk.get_vk(),
strategy,
&[&[]],
&mut transcript,
)
.map(|strategy| strategy.finalize())
.unwrap_or_default()
};
assert_eq!(accepted, expected);
}
fn main() {
const W: usize = 4;
const H: usize = 32;
const K: u32 = 8;
let circuit = &MyCircuit::<_, W, H>::rand(&mut OsRng);
{
test_mock_prover(K, circuit.clone(), Ok(()));
test_prover::<EqAffine, W, H>(K, circuit.clone(), true);
}
#[cfg(not(feature = "sanity-checks"))]
{
use std::ops::IndexMut;
let mut circuit = circuit.clone();
circuit.shuffled = circuit.shuffled.map(|mut shuffled| {
shuffled.index_mut(0).swap(0, 1);
shuffled
});
test_mock_prover(
K,
circuit.clone(),
Err(vec![(
((1, "z should end with 1").into(), 0, "").into(),
FailureLocation::InRegion {
region: (0, "Shuffle original into shuffled").into(),
offset: 32,
},
)]),
);
test_prover::<EqAffine, W, H>(K, circuit, false);
}
}