Skip to content

Commit 56a9d87

Browse files
committed
feat: fetch proofs based on zkvm engine
1 parent 4bc1a52 commit 56a9d87

File tree

5 files changed

+82
-38
lines changed

5 files changed

+82
-38
lines changed

aggregation_mode/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ aligned-sdk = { path = "../batcher/aligned-sdk/" }
2222
# zkvms
2323
sp1-sdk = "4.1.3"
2424
sp1_aggregation_program = { path = "./aggregation_programs/sp1/" }
25-
risc0-zkvm = { version = "2.0.1"}
25+
risc0-zkvm = { version = "2.0.1" }
2626
risc0_aggregation_program = { path = "./aggregation_programs/risc0/" }
2727
risc0-ethereum-contracts = { git = "https://github.com/risc0/risc0-ethereum/", tag = "v2.0.0" }
2828

@@ -38,4 +38,7 @@ methods = ["./aggregation_programs/risc0"]
3838
opt-level = 3
3939

4040
[features]
41+
default = []
4142
prove = []
43+
risc0 = []
44+
sp1 = []

aggregation_mode/src/aggregators/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,42 @@ pub mod lib;
22
pub mod risc0_aggregator;
33
pub mod sp1_aggregator;
44

5+
use std::fmt::Display;
6+
57
use risc0_aggregator::{AlignedRisc0VerificationError, Risc0ProofReceiptAndImageId};
68
use sp1_aggregator::{AlignedSP1VerificationError, SP1ProofWithPubValuesAndElf};
9+
10+
#[derive(Clone, Debug)]
711
pub enum ZKVMEngine {
812
SP1,
913
RISC0,
1014
}
1115

16+
impl Display for ZKVMEngine {
17+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18+
match self {
19+
Self::SP1 => write!(f, "SP1"),
20+
Self::RISC0 => write!(f, "Risc0"),
21+
}
22+
}
23+
}
24+
25+
impl ZKVMEngine {
26+
pub fn from_rust_features() -> Option<Self> {
27+
#[cfg(feature = "sp1")]
28+
{
29+
return Some(ZKVMEngine::SP1);
30+
}
31+
32+
#[cfg(feature = "risc0")]
33+
{
34+
return Some(ZKVMEngine::RISC0);
35+
}
36+
37+
None
38+
}
39+
}
40+
1241
pub enum AlignedProof {
1342
SP1(SP1ProofWithPubValuesAndElf),
1443
Risc0(Risc0ProofReceiptAndImageId),

aggregation_mode/src/aggregators/risc0_aggregator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Risc0ProofReceiptAndImageId {
4040
}
4141

4242
pub struct Risc0AggregationInput {
43-
pub receipts: Vec<Risc0ProofReceiptAndImageId>,
43+
pub proofs: Vec<Risc0ProofReceiptAndImageId>,
4444
pub merkle_root: [u8; 32],
4545
}
4646

@@ -51,7 +51,7 @@ pub(crate) fn aggregate_proofs(
5151

5252
// write assumptions and proof image id + pub inputs
5353
let mut proofs_image_id_and_pub_inputs = vec![];
54-
for proof in input.receipts {
54+
for proof in input.proofs {
5555
proofs_image_id_and_pub_inputs.push(risc0_aggregation_program::Risc0ImageIdAndPubInputs {
5656
image_id: proof.image_id,
5757
public_inputs: proof.receipt.journal.bytes.clone(),

aggregation_mode/src/backend/fetcher.rs

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::{
77
use crate::{
88
aggregators::{
99
risc0_aggregator::Risc0ProofReceiptAndImageId, sp1_aggregator::SP1ProofWithPubValuesAndElf,
10-
AlignedProof,
10+
AlignedProof, ZKVMEngine,
1111
},
1212
backend::s3::get_aligned_batch_from_s3,
1313
};
@@ -50,7 +50,7 @@ impl ProofsFetcher {
5050
}
5151
}
5252

53-
pub async fn fetch(&self) -> Result<Vec<AlignedProof>, ProofsFetcherError> {
53+
pub async fn fetch(&self, engine: ZKVMEngine) -> Result<Vec<AlignedProof>, ProofsFetcherError> {
5454
let from_block = self.get_block_number_to_fetch_from().await?;
5555
info!(
5656
"Fetching proofs from batch logs starting from block number {}",
@@ -87,37 +87,47 @@ impl ProofsFetcher {
8787
info!("Data downloaded from S3, number of proofs {}", data.len());
8888

8989
// Filter compatible proofs to be aggregated and push to queue
90-
let proofs_to_add: Vec<AlignedProof> = data
91-
.into_iter()
92-
.filter_map(|p| match p.proving_system {
93-
ProvingSystemId::SP1 => {
94-
let elf = p.vm_program_code?;
95-
let proof_with_pub_values = bincode::deserialize(&p.proof).ok()?;
96-
let sp1_proof = SP1ProofWithPubValuesAndElf {
97-
proof_with_pub_values,
98-
elf,
99-
};
100-
101-
Some(AlignedProof::SP1(sp1_proof))
102-
}
103-
ProvingSystemId::Risc0 => {
104-
let mut image_id = [0u8; 32];
105-
image_id.copy_from_slice(p.vm_program_code?.as_slice());
106-
let public_inputs = p.pub_input?;
107-
let inner_receipt: risc0_zkvm::InnerReceipt =
108-
bincode::deserialize(&p.proof).ok()?;
109-
110-
let receipt = Receipt::new(inner_receipt, public_inputs);
111-
let risc0_proof = Risc0ProofReceiptAndImageId { image_id, receipt };
112-
113-
Some(AlignedProof::Risc0(risc0_proof))
114-
}
115-
_ => None,
116-
})
117-
.collect();
90+
let proofs_to_add: Vec<AlignedProof> = match engine {
91+
ZKVMEngine::SP1 => data
92+
.into_iter()
93+
.filter_map(|p| match p.proving_system {
94+
ProvingSystemId::SP1 => {
95+
let elf = p.vm_program_code?;
96+
let proof_with_pub_values = bincode::deserialize(&p.proof).ok()?;
97+
let sp1_proof = SP1ProofWithPubValuesAndElf {
98+
proof_with_pub_values,
99+
elf,
100+
};
101+
102+
Some(AlignedProof::SP1(sp1_proof))
103+
}
104+
105+
_ => None,
106+
})
107+
.collect(),
108+
ZKVMEngine::RISC0 => data
109+
.into_iter()
110+
.filter_map(|p| match p.proving_system {
111+
ProvingSystemId::Risc0 => {
112+
let mut image_id = [0u8; 32];
113+
image_id.copy_from_slice(p.vm_program_code?.as_slice());
114+
let public_inputs = p.pub_input?;
115+
let inner_receipt: risc0_zkvm::InnerReceipt =
116+
bincode::deserialize(&p.proof).ok()?;
117+
118+
let receipt = Receipt::new(inner_receipt, public_inputs);
119+
let risc0_proof = Risc0ProofReceiptAndImageId { image_id, receipt };
120+
121+
Some(AlignedProof::Risc0(risc0_proof))
122+
}
123+
_ => None,
124+
})
125+
.collect(),
126+
};
118127

119128
info!(
120-
"Proofs filtered, total proofs to add {}",
129+
"{} Proofs filtered, compatible proofs found {}",
130+
engine,
121131
proofs_to_add.len()
122132
);
123133

aggregation_mode/src/backend/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ impl ProofAggregator {
6363
.expect("AlignedProofAggregationService address should be valid"),
6464
rpc_provider,
6565
);
66+
67+
let engine = ZKVMEngine::from_rust_features().expect("A feature defining zkvm engine");
6668
let fetcher = ProofsFetcher::new(config);
6769

6870
Self {
69-
engine: ZKVMEngine::SP1,
71+
engine,
7072
proof_aggregation_service,
7173
fetcher,
7274
}
@@ -93,7 +95,7 @@ impl ProofAggregator {
9395
) -> Result<(), AggregatedProofSubmissionError> {
9496
let proofs = self
9597
.fetcher
96-
.fetch()
98+
.fetch(self.engine.clone())
9799
.await
98100
.map_err(AggregatedProofSubmissionError::FetchingProofs)?;
99101

@@ -127,7 +129,7 @@ impl ProofAggregator {
127129
.map_err(AggregatedProofSubmissionError::Aggregation)?
128130
}
129131
ZKVMEngine::RISC0 => {
130-
let receipts = proofs
132+
let proofs = proofs
131133
.into_iter()
132134
.filter_map(|proof| match proof {
133135
AlignedProof::Risc0(proof) => Some(proof),
@@ -136,7 +138,7 @@ impl ProofAggregator {
136138
.collect();
137139

138140
let input = Risc0AggregationInput {
139-
receipts,
141+
proofs,
140142
merkle_root,
141143
};
142144

0 commit comments

Comments
 (0)