Skip to content

Commit 7a95c59

Browse files
committed
feat: interfaces + basic service to run sp1
1 parent 72d0478 commit 7a95c59

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

aggregation-mode/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod verifiers;
2+
3+
use verifiers::interface::{
4+
verify_proofs, AggregatedVerificationError, InputProofs, ProgramInput, ProgramOutput,
5+
};
6+
7+
pub fn verify_aggregated_proofs() -> Result<ProgramOutput, AggregatedVerificationError> {
8+
let sp1_compressed_proofs = vec![];
9+
let input = ProgramInput::new(InputProofs::SP1Compressed(sp1_compressed_proofs));
10+
verify_proofs(input)
11+
}

aggregation-mode/src/main.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use std::time::Duration;
2+
3+
use proof_aggregator::verify_aggregated_proofs;
4+
use tracing::{error, info};
5+
use tracing_subscriber::FmtSubscriber;
6+
7+
fn main() {
8+
let subscriber = FmtSubscriber::builder().finish();
9+
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
10+
11+
// simulate a service that sends proofs every n seconds after some processing
12+
loop {
13+
info!("Waiting 2 seconds before aggregating proofs...");
14+
std::thread::sleep(Duration::from_secs(2));
15+
16+
let Ok(output) = verify_aggregated_proofs() else {
17+
error!("Error while aggregating and verifying proofs");
18+
return;
19+
};
20+
info!("Proof aggregated, sending to aligned verification contract...");
21+
22+
// TODO: send a blob transaction to with the merkle leaves
23+
let _leaves = &output.leaves;
24+
25+
// TODO: call contract to verify proof + attach blob transaction
26+
// the contract should emit a log with the verification and the path to the blob
27+
let _calldata = output.calldata();
28+
}
29+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use super::sp1::{self, SP1AggregatedProof};
2+
use serde::{Deserialize, Serialize};
3+
use zkvm_sp1_interface::SP1CompressedProof;
4+
5+
#[derive(Serialize, Deserialize)]
6+
pub enum InputProofs {
7+
SP1Compressed(Vec<SP1CompressedProof>),
8+
}
9+
10+
#[derive(Serialize, Deserialize)]
11+
pub struct ProgramInput {
12+
proofs: InputProofs,
13+
}
14+
15+
impl ProgramInput {
16+
pub fn new(input_proofs: InputProofs) -> Self {
17+
ProgramInput {
18+
proofs: input_proofs,
19+
}
20+
}
21+
}
22+
23+
pub enum AggregatedProof {
24+
SP1(SP1AggregatedProof),
25+
}
26+
27+
pub struct ProgramOutput {
28+
pub proof: AggregatedProof,
29+
pub leaves: Vec<Vec<u8>>,
30+
}
31+
32+
impl ProgramOutput {
33+
pub fn new(proof: AggregatedProof, leaves: Vec<Vec<u8>>) -> Self {
34+
Self { proof, leaves }
35+
}
36+
37+
/// TODO: return the contract calldata to verify proof
38+
pub fn calldata(&self) -> Vec<u8> {
39+
vec![]
40+
}
41+
}
42+
43+
#[derive(Debug)]
44+
pub enum AggregatedVerificationError {
45+
SP1Verification(sp1_sdk::SP1VerificationError),
46+
SP1Proving,
47+
}
48+
49+
pub fn verify_proofs(input: ProgramInput) -> Result<ProgramOutput, AggregatedVerificationError> {
50+
match input.proofs {
51+
InputProofs::SP1Compressed(proofs) => sp1::verify_proof_aggregation(proofs),
52+
}
53+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod interface;
2+
pub mod sp1;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use sp1_sdk::{ProverClient, SP1ProofWithPublicValues, SP1Stdin, SP1VerifyingKey};
2+
use zkvm_sp1_interface::SP1CompressedProof;
3+
4+
use super::interface::{AggregatedProof, AggregatedVerificationError, ProgramOutput};
5+
6+
const PROGRAM_ELF: &[u8] = include_bytes!("../../zkvm/sp1/elf/sp1_verifier_program");
7+
8+
pub struct SP1AggregatedProof {
9+
proof: SP1ProofWithPublicValues,
10+
vk: SP1VerifyingKey,
11+
}
12+
13+
pub(crate) fn verify_proof_aggregation(
14+
proofs: Vec<SP1CompressedProof>,
15+
) -> Result<ProgramOutput, AggregatedVerificationError> {
16+
let mut stdin = SP1Stdin::new();
17+
stdin.write(&proofs);
18+
19+
let client = ProverClient::from_env();
20+
let (pk, vk) = client.setup(PROGRAM_ELF);
21+
let proof = client
22+
.prove(&pk, &stdin)
23+
.groth16()
24+
.run()
25+
.map_err(|_| AggregatedVerificationError::SP1Proving)?;
26+
27+
// a sanity check, vm already performs it
28+
client
29+
.verify(&proof, &vk)
30+
.map_err(AggregatedVerificationError::SP1Verification)?;
31+
32+
let output = ProgramOutput::new(
33+
AggregatedProof::SP1(SP1AggregatedProof { proof, vk }),
34+
vec![],
35+
);
36+
37+
Ok(output)
38+
}

0 commit comments

Comments
 (0)