forked from privacy-scaling-explorations/zkevm-circuits
-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathprover.rs
388 lines (347 loc) · 13.4 KB
/
prover.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
use std::{collections::BTreeMap, env, iter::repeat};
use aggregator::{
eip4844::decode_blob, BatchData, BatchHash, BatchHeader, ChunkInfo, MAX_AGG_SNARKS,
};
use anyhow::{bail, Result};
use eth_types::H256;
use halo2_proofs::{halo2curves::bn256::Bn256, poly::kzg::commitment::ParamsKZG};
use sha2::{Digest, Sha256};
use snark_verifier_sdk::Snark;
use crate::{
common,
config::LayerId,
consts::{BATCH_KECCAK_ROW, BATCH_VK_FILENAME, BUNDLE_VK_FILENAME, CHUNK_PROTOCOL_FILENAME},
io::{force_to_read, try_to_read},
proof::BundleProof,
types::BundleProvingTask,
BatchProof, BatchProvingTask, ChunkProof,
};
#[derive(Debug)]
pub struct Prover<'params> {
// Make it public for testing with inner functions (unnecessary for FFI).
pub prover_impl: common::Prover<'params>,
pub chunk_protocol: Vec<u8>,
raw_vk_batch: Option<Vec<u8>>,
raw_vk_bundle: Option<Vec<u8>>,
}
impl<'params> Prover<'params> {
pub fn from_params_and_assets(
params_map: &'params BTreeMap<u32, ParamsKZG<Bn256>>,
assets_dir: &str,
) -> Self {
log::debug!("set env KECCAK_ROWS={}", BATCH_KECCAK_ROW.to_string());
env::set_var("KECCAK_ROWS", BATCH_KECCAK_ROW.to_string());
let prover_impl = common::Prover::from_params_map(params_map);
let chunk_protocol = force_to_read(assets_dir, &CHUNK_PROTOCOL_FILENAME);
let raw_vk_batch = try_to_read(assets_dir, &BATCH_VK_FILENAME);
let raw_vk_bundle = try_to_read(assets_dir, &BUNDLE_VK_FILENAME);
if raw_vk_batch.is_none() {
log::warn!(
"batch-prover: {} doesn't exist in {}",
*BATCH_VK_FILENAME,
assets_dir
);
}
if raw_vk_bundle.is_none() {
log::warn!(
"batch-prover: {} doesn't exist in {}",
*BUNDLE_VK_FILENAME,
assets_dir
);
}
Self {
prover_impl,
chunk_protocol,
raw_vk_batch,
raw_vk_bundle,
}
}
// Return true if chunk proofs are valid (same protocol), false otherwise.
pub fn check_protocol_of_chunks(&self, chunk_proofs: &[ChunkProof]) -> bool {
chunk_proofs.iter().enumerate().all(|(i, proof)| {
let result = proof.protocol == self.chunk_protocol;
if !result {
log::error!(
"Non-match protocol of chunk-proof index-{}: expected = {:x}, actual = {:x}",
i,
Sha256::digest(&self.chunk_protocol),
Sha256::digest(&proof.protocol),
);
}
result
})
}
pub fn get_batch_vk(&self) -> Option<Vec<u8>> {
self.prover_impl
.raw_vk(LayerId::Layer4.id())
.or_else(|| self.raw_vk_batch.clone())
}
pub fn get_bundle_vk(&self) -> Option<Vec<u8>> {
self.prover_impl
.raw_vk(LayerId::Layer6.id())
.or_else(|| self.raw_vk_bundle.clone())
}
// Return the batch proof for a BatchProvingTask.
// TODO: should we rename the method name to `load_or_gen_batch_proof`?
pub fn gen_batch_proof(
&mut self,
batch: BatchProvingTask,
name: Option<&str>,
output_dir: Option<&str>,
) -> Result<BatchProof> {
let name = name.map_or_else(|| batch.identifier(), |name| name.to_string());
log::info!("gen_batch_proof with identifier {name}");
if let Some(output_dir) = output_dir {
if let Ok(batch_proof) = BatchProof::from_json_file(output_dir, &name) {
log::info!("batch proof loaded from {output_dir}");
return Ok(batch_proof);
}
}
let (layer3_snark, batch_hash) =
self.load_or_gen_last_agg_snark::<MAX_AGG_SNARKS>(&name, batch, output_dir)?;
// Load or generate batch compression thin proof (layer-4).
let layer4_snark = self.prover_impl.load_or_gen_comp_snark(
&name,
LayerId::Layer4.id(),
true,
LayerId::Layer4.degree(),
layer3_snark,
output_dir,
)?;
log::info!("Got batch compression thin proof (layer-4): {name}");
self.check_batch_vk();
let pk = self.prover_impl.pk(LayerId::Layer4.id());
let batch_proof = BatchProof::new(layer4_snark, pk, batch_hash)?;
if let Some(output_dir) = output_dir {
batch_proof.dump_vk(output_dir, "agg")?;
batch_proof.dump(output_dir, &name)?;
log::debug!("batch proof dumped to {output_dir}");
}
Ok(batch_proof)
}
// Generate layer3 snark.
// Then it could be used to generate a layer4 proof.
pub fn load_or_gen_last_agg_snark<const N_SNARKS: usize>(
&mut self,
name: &str,
batch: BatchProvingTask,
output_dir: Option<&str>,
) -> Result<(Snark, H256)> {
let real_chunk_count = batch.chunk_proofs.len();
assert!((1..=MAX_AGG_SNARKS).contains(&real_chunk_count));
if !self.check_protocol_of_chunks(&batch.chunk_proofs) {
bail!("non-match-chunk-protocol: {name}");
}
let mut chunk_hashes: Vec<_> = batch
.chunk_proofs
.iter()
.map(|p| p.chunk_info.clone())
.collect();
let mut layer2_snarks: Vec<_> = batch
.chunk_proofs
.into_iter()
.map(|p| p.to_snark())
.collect();
if real_chunk_count < MAX_AGG_SNARKS {
let padding_snark = layer2_snarks.last().unwrap().clone();
let mut padding_chunk_hash = chunk_hashes.last().unwrap().clone();
padding_chunk_hash.is_padding = true;
// Extend to MAX_AGG_SNARKS for both chunk hashes and layer-2 snarks.
chunk_hashes.extend(repeat(padding_chunk_hash).take(MAX_AGG_SNARKS - real_chunk_count));
layer2_snarks.extend(repeat(padding_snark).take(MAX_AGG_SNARKS - real_chunk_count));
}
// Load or generate aggregation snark (layer-3).
let batch_header = BatchHeader::construct_from_chunks(
batch.batch_header.version,
batch.batch_header.batch_index,
batch.batch_header.l1_message_popped,
batch.batch_header.total_l1_message_popped,
batch.batch_header.parent_batch_hash,
batch.batch_header.last_block_timestamp,
&chunk_hashes,
&batch.blob_bytes,
);
// sanity check between:
// - BatchHeader supplied from infra
// - BatchHeader re-constructed by circuits
//
// for the fields data_hash, z, y, blob_versioned_hash.
assert_eq!(
batch_header.data_hash, batch.batch_header.data_hash,
"BatchHeader(sanity) mismatch data_hash expected={}, got={}",
batch.batch_header.data_hash, batch_header.data_hash
);
assert_eq!(
batch_header.blob_data_proof[0], batch.batch_header.blob_data_proof[0],
"BatchHeader(sanity) mismatch blob data proof (z) expected={}, got={}",
batch.batch_header.blob_data_proof[0], batch_header.blob_data_proof[0],
);
assert_eq!(
batch_header.blob_data_proof[1], batch.batch_header.blob_data_proof[1],
"BatchHeader(sanity) mismatch blob data proof (y) expected={}, got={}",
batch.batch_header.blob_data_proof[1], batch_header.blob_data_proof[1],
);
assert_eq!(
batch_header.blob_versioned_hash, batch.batch_header.blob_versioned_hash,
"BatchHeader(sanity) mismatch blob versioned hash expected={}, got={}",
batch.batch_header.blob_versioned_hash, batch_header.blob_versioned_hash,
);
let batch_hash = batch_header.batch_hash();
let batch_info: BatchHash<N_SNARKS> =
BatchHash::construct(&chunk_hashes, batch_header, &batch.blob_bytes);
let batch_data: BatchData<N_SNARKS> = BatchData::from(&batch_info);
// sanity check:
// - conditionally decoded blob should match batch data.
let batch_bytes = batch_data.get_batch_data_bytes();
let decoded_blob_bytes = decode_blob(&batch.blob_bytes)?;
assert_eq!(
batch_bytes, decoded_blob_bytes,
"BatchProvingTask(sanity) mismatch batch bytes and decoded blob bytes",
);
let layer3_snark = self.prover_impl.load_or_gen_agg_snark(
name,
LayerId::Layer3.id(),
LayerId::Layer3.degree(),
batch_info,
&layer2_snarks,
output_dir,
)?;
log::info!("Got aggregation snark (layer-3): {name}");
Ok((layer3_snark, batch_hash))
}
// Given a bundle proving task that consists of a list of batch proofs for all intermediate
// batches, bundles them into a single bundle proof using the RecursionCircuit, effectively
// proving the validity of all those batches.
pub fn gen_bundle_proof(
&mut self,
bundle: BundleProvingTask,
name: Option<&str>,
output_dir: Option<&str>,
) -> Result<BundleProof> {
let name = name.map_or_else(|| bundle.identifier(), |name| name.to_string());
let bundle_snarks = bundle
.batch_proofs
.iter()
.map(|proof| proof.into())
.collect::<Vec<_>>();
let layer5_snark = self.prover_impl.load_or_gen_recursion_snark(
&name,
LayerId::Layer5.id(),
LayerId::Layer5.degree(),
&bundle_snarks,
output_dir,
)?;
let layer6_evm_proof = self.prover_impl.load_or_gen_comp_evm_proof(
&name,
LayerId::Layer6.id(),
true,
LayerId::Layer6.degree(),
layer5_snark,
output_dir,
)?;
self.check_bundle_vk();
let bundle_proof: BundleProof = layer6_evm_proof.proof.into();
if let Some(output_dir) = output_dir {
bundle_proof.dump(output_dir, "recursion")?;
}
Ok(bundle_proof)
}
/// Check vk generated is same with vk loaded from assets
fn check_batch_vk(&self) {
if self.raw_vk_batch.is_some() {
let gen_vk = self
.prover_impl
.raw_vk(LayerId::Layer4.id())
.unwrap_or_default();
if gen_vk.is_empty() {
log::warn!("no gen_vk found, skip check_vk");
return;
}
let init_vk = self.raw_vk_batch.clone().unwrap_or_default();
if gen_vk != init_vk {
log::error!(
"batch-prover: generated VK is different with init one - gen_vk = {}, init_vk = {}",
base64::encode(gen_vk),
base64::encode(init_vk),
);
}
}
}
/// Check vk generated is same with vk loaded from assets
fn check_bundle_vk(&self) {
if self.raw_vk_bundle.is_some() {
let gen_vk = self
.prover_impl
.raw_vk(LayerId::Layer6.id())
.unwrap_or_default();
if gen_vk.is_empty() {
log::warn!("no gen_vk found, skip check_vk");
return;
}
let init_vk = self.raw_vk_bundle.clone().unwrap_or_default();
if gen_vk != init_vk {
log::error!(
"bundle-prover: generated VK is different with init one - gen_vk = {}, init_vk = {}",
base64::encode(gen_vk),
base64::encode(init_vk),
);
}
}
}
}
pub fn check_chunk_hashes(
name: &str,
chunk_hashes_proofs: &[(ChunkInfo, ChunkProof)],
) -> Result<()> {
for (idx, (in_arg, chunk_proof)) in chunk_hashes_proofs.iter().enumerate() {
let in_proof = &chunk_proof.chunk_info;
crate::proof::compare_chunk_info(&format!("{name} chunk num {idx}"), in_arg, in_proof)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use eth_types::H256;
#[test]
fn test_check_chunk_hashes() {
let chunk_hashes_proofs = vec![
(ChunkInfo::default(), ChunkProof::default()),
(
ChunkInfo {
chain_id: 1,
prev_state_root: H256::zero(),
data_hash: [100; 32].into(),
..Default::default()
},
ChunkProof {
chunk_info: ChunkInfo {
chain_id: 1,
prev_state_root: [0; 32].into(),
data_hash: [100; 32].into(),
..Default::default()
},
..Default::default()
},
),
(
ChunkInfo {
post_state_root: H256::zero(),
..Default::default()
},
ChunkProof {
chunk_info: ChunkInfo {
post_state_root: [1; 32].into(),
..Default::default()
},
..Default::default()
},
),
];
let result = check_chunk_hashes("test-batch", &chunk_hashes_proofs);
assert_eq!(
result.unwrap_err().downcast_ref::<String>().unwrap(),
"test-batch chunk num 2 chunk different post_state_root: 0x0000…0000 != 0x0101…0101"
);
}
}