forked from availproject/avail-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproof.rs
63 lines (54 loc) · 1.52 KB
/
proof.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
//! Parallelized proof verification
use color_eyre::eyre;
use dusk_plonk::commitment_scheme::kzg10::PublicParameters;
use itertools::{Either, Itertools};
use kate_recovery::{
data::Cell,
matrix::{Dimensions, Position},
proof,
};
use std::sync::Arc;
use tokio::{task::JoinSet, time::Instant};
use tracing::debug;
async fn verify_proof(
public_parameters: Arc<PublicParameters>,
dimensions: Dimensions,
commitment: [u8; 48],
cell: Cell,
) -> Result<(Position, bool), proof::Error> {
proof::verify(&public_parameters, dimensions, &commitment, &cell)
.map(|verified| (cell.position, verified))
}
/// Verifies proofs for given block, cells and commitments
pub async fn verify(
block_num: u32,
dimensions: Dimensions,
cells: &[Cell],
commitments: &[[u8; 48]],
public_parameters: Arc<PublicParameters>,
) -> eyre::Result<(Vec<Position>, Vec<Position>)> {
if cells.is_empty() {
return Ok((Vec::new(), Vec::new()));
};
let start_time = Instant::now();
let mut tasks = JoinSet::new();
for cell in cells {
tasks.spawn(verify_proof(
public_parameters.clone(),
dimensions,
commitments[cell.position.row as usize],
cell.clone(),
));
}
let mut results = Vec::with_capacity(cells.len());
while let Some(result) = tasks.join_next().await {
results.push(result??)
}
debug!(block_num, duration = ?start_time.elapsed(), "Proof verification completed");
Ok(results
.into_iter()
.partition_map(|(position, is_verified)| match is_verified {
true => Either::Left(position),
false => Either::Right(position),
}))
}