Skip to content

Commit

Permalink
Decompress proofs in parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
porcuquine committed Sep 1, 2020
1 parent 4a07a86 commit 4177aac
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions storage-proofs/core/src/multi_proof.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use bellperson::groth16;

use crate::error::Result;
use anyhow::Context;
use anyhow::{ensure, Context};
use paired::bls12_381::Bls12;
use rayon::prelude::*;
use std::io::{self, Read, Write};

pub struct MultiProof<'a> {
pub circuit_proofs: Vec<groth16::Proof<Bls12>>,
pub verifying_key: &'a groth16::VerifyingKey<Bls12>,
}

const GROTH_PROOF_SIZE: usize = 192;

impl<'a> MultiProof<'a> {
pub fn new(
groth_proofs: Vec<groth16::Proof<Bls12>>,
Expand All @@ -26,14 +29,35 @@ impl<'a> MultiProof<'a> {
mut reader: R,
verifying_key: &'a groth16::VerifyingKey<Bls12>,
) -> Result<Self> {
let num_proofs = match partitions {
Some(n) => n,
None => 1,
};
let proofs = (0..num_proofs)
.map(|_| groth16::Proof::read(&mut reader))
let num_proofs = partitions.unwrap_or(1);

let mut proof_vec: Vec<u8> = Vec::with_capacity(num_proofs * GROTH_PROOF_SIZE);
reader.read_to_end(&mut proof_vec)?;

Self::new_from_bytes(partitions, &proof_vec, verifying_key)
}

// Parallelizing reduces deserialization time for 10 proofs from 13ms to 2ms.
pub fn new_from_bytes(
partitions: Option<usize>,
proof_bytes: &[u8],
verifying_key: &'a groth16::VerifyingKey<Bls12>,
) -> Result<Self> {
let num_proofs = partitions.unwrap_or(1);

let proofs = proof_bytes
.par_chunks(GROTH_PROOF_SIZE)
.take(num_proofs)
.map(groth16::Proof::read)
.collect::<io::Result<Vec<_>>>()?;

ensure!(
num_proofs == proofs.len(),
"expected {} proofs but found only {}",
num_proofs,
proofs.len()
);

Ok(Self::new(proofs, verifying_key))
}

Expand Down

0 comments on commit 4177aac

Please sign in to comment.