Skip to content

Commit 49bfc2c

Browse files
committed
perf: pararellize proofs verification
1 parent b39a038 commit 49bfc2c

4 files changed

Lines changed: 27 additions & 13 deletions

File tree

aggregation_mode/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aggregation_mode/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ sha3 = "0.10.8"
1717
reqwest = { version = "0.12" }
1818
ciborium = "=0.2.2"
1919
lambdaworks-crypto = { git = "https://github.com/lambdaclass/lambdaworks.git", rev = "5f8f2cfcc8a1a22f77e8dff2d581f1166eefb80b", features = ["serde"]}
20+
rayon = "1.10.0"
2021
# Necessary for the VerificationData type
2122
aligned-sdk = { path = "../batcher/aligned-sdk/" }
2223
# zkvms

aggregation_mode/src/aggregators/sp1_aggregator.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::LazyLock;
33
use alloy::primitives::Keccak256;
44
use sp1_aggregation_program::SP1VkAndPubInputs;
55
use sp1_sdk::{
6-
EnvProver, HashableKey, Prover, ProverClient, SP1ProofWithPublicValues, SP1Stdin,
6+
CpuProver, EnvProver, HashableKey, Prover, ProverClient, SP1ProofWithPublicValues, SP1Stdin,
77
SP1VerifyingKey,
88
};
99

@@ -14,6 +14,11 @@ const USER_PROOFS_PROGRAM_ELF: &[u8] =
1414
include_bytes!("../../aggregation_programs/sp1/elf/sp1_user_proofs_aggregator_program");
1515

1616
static SP1_PROVER_CLIENT: LazyLock<EnvProver> = LazyLock::new(ProverClient::from_env);
17+
/// Separate prover instance configured to always use the CPU.
18+
/// This is used for verification, which is performed in parallel and
19+
/// cannot be done on the GPU.
20+
static SP1_PROVER_CLIENT_CPU: LazyLock<CpuProver> =
21+
LazyLock::new(|| ProverClient::builder().cpu().build());
1722

1823
pub struct SP1ProofWithPubValuesAndElf {
1924
pub proof_with_pub_values: SP1ProofWithPublicValues,
@@ -182,7 +187,7 @@ pub enum AlignedSP1VerificationError {
182187
pub(crate) fn verify(
183188
sp1_proof_with_pub_values_and_elf: &SP1ProofWithPubValuesAndElf,
184189
) -> Result<(), AlignedSP1VerificationError> {
185-
let client = &*SP1_PROVER_CLIENT;
190+
let client = &*SP1_PROVER_CLIENT_CPU;
186191

187192
let (_pk, vk) = client.setup(&sp1_proof_with_pub_values_and_elf.elf);
188193

@@ -202,7 +207,7 @@ pub(crate) fn verify(
202207
}
203208

204209
pub fn vk_from_elf(elf: &[u8]) -> SP1VerifyingKey {
205-
let prover = &*SP1_PROVER_CLIENT;
210+
let prover = &*SP1_PROVER_CLIENT_CPU;
206211
let (_, vk) = prover.setup(elf);
207212
vk
208213
}

aggregation_mode/src/backend/fetcher.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use alloy::{
1616
primitives::Address,
1717
providers::{Provider, ProviderBuilder},
1818
};
19+
use rayon::prelude::*;
1920
use risc0_zkvm::Receipt;
2021
use tracing::{error, info};
2122

@@ -109,7 +110,7 @@ impl ProofsFetcher {
109110
info!("Data downloaded from S3, number of proofs {}", data.len());
110111

111112
// Filter compatible proofs to be aggregated and push to queue
112-
let proofs_to_add: Vec<AlignedProof> = match engine {
113+
let mut proofs_to_add: Vec<AlignedProof> = match engine {
113114
ZKVMEngine::SP1 => data
114115
.into_iter()
115116
.filter_map(|p| match p.proving_system {
@@ -153,15 +154,21 @@ impl ProofsFetcher {
153154
proofs_to_add.len()
154155
);
155156

156-
// try to add them to the queue
157-
for proof in proofs_to_add {
158-
if let Err(err) = proof.verify() {
159-
error!("Could not add proof, verification failed: {:?}", err);
160-
continue;
161-
};
162-
163-
proofs.push(proof);
164-
}
157+
// Try to add them to the queue
158+
// We do this in parallel, as SP1 can take quite some time in verifying
159+
// because of the overhead of setting up the prover
160+
proofs_to_add = proofs_to_add
161+
.into_par_iter()
162+
.filter(|proof| match proof.verify() {
163+
Ok(_) => true,
164+
Err(err) => {
165+
error!("Could not add proof, verification failed: {:?}", err);
166+
return false;
167+
}
168+
})
169+
.collect();
170+
171+
proofs.extend(proofs_to_add);
165172
}
166173

167174
Ok(proofs)

0 commit comments

Comments
 (0)