Skip to content

Commit 2f54502

Browse files
committed
feat: [wip] risc0 aggregator backend
1 parent 3200e0c commit 2f54502

File tree

4 files changed

+112
-6
lines changed

4 files changed

+112
-6
lines changed

aggregation_mode/src/aggregators/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
use super::sp1_aggregator::{self, SP1AggregationInput, SP1ProofWithPubValuesAndElf};
1+
use risc0_zkvm::Receipt;
2+
3+
use super::{
4+
risc0_aggregator::{self, Risc0AggregationInput},
5+
sp1_aggregator::{self, SP1AggregationInput, SP1ProofWithPubValuesAndElf},
6+
};
27

38
pub enum ProgramInput {
49
SP1(SP1AggregationInput),
10+
Risc0(Risc0AggregationInput),
511
}
612

713
pub enum AggregatedProof {
814
SP1(SP1ProofWithPubValuesAndElf),
15+
Risc0(Receipt),
916
}
1017

1118
pub struct ProgramOutput {
@@ -28,5 +35,6 @@ pub enum ProofAggregationError {
2835
pub fn aggregate_proofs(input: ProgramInput) -> Result<ProgramOutput, ProofAggregationError> {
2936
match input {
3037
ProgramInput::SP1(input) => sp1_aggregator::aggregate_proofs(input),
38+
ProgramInput::Risc0(input) => risc0_aggregator::aggregate_proofs(input),
3139
}
3240
}

aggregation_mode/src/aggregators/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
pub mod lib;
2+
pub mod risc0_aggregator;
23
pub mod sp1_aggregator;
34

5+
use risc0_aggregator::{AlignedRisc0VerificationError, Risc0ProofWithPubValuesAndImageId};
46
use sp1_aggregator::{AlignedSP1VerificationError, SP1ProofWithPubValuesAndElf};
57
pub enum ZKVMEngine {
68
SP1,
9+
RISC0,
710
}
811

912
pub enum AlignedProof {
1013
SP1(SP1ProofWithPubValuesAndElf),
14+
Risc0(Risc0ProofWithPubValuesAndImageId),
1115
}
1216

1317
impl AlignedProof {
1418
pub fn hash(&self) -> [u8; 32] {
1519
match self {
1620
AlignedProof::SP1(proof) => proof.hash_vk_and_pub_inputs(),
21+
AlignedProof::Risc0(proof) => proof.hash_image_id_and_public_inputs(),
1722
}
1823
}
1924
}
2025

2126
#[derive(Debug)]
2227
pub enum AlignedVerificationError {
2328
Sp1(AlignedSP1VerificationError),
29+
Risc0(AlignedRisc0VerificationError),
2430
}
2531

2632
impl AlignedProof {
@@ -31,6 +37,9 @@ impl AlignedProof {
3137
AlignedVerificationError::Sp1(arg0)
3238
},
3339
),
40+
AlignedProof::Risc0(proof) => {
41+
risc0_aggregator::verify(&proof.receipt).map_err(AlignedVerificationError::Risc0)
42+
}
3443
}
3544
}
3645
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
include!(concat!(env!("OUT_DIR"), "/methods.rs"));
2+
3+
use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, Receipt};
4+
5+
use super::lib::{AggregatedProof, ProgramOutput, ProofAggregationError};
6+
7+
pub struct Risc0ProofWithPubValuesAndImageId {
8+
pub image_id: [u32; 8],
9+
pub receipt: Receipt,
10+
pub public_values: Vec<u8>,
11+
}
12+
13+
impl Risc0ProofWithPubValuesAndImageId {
14+
pub fn hash_image_id_and_public_inputs(&self) -> [u8; 32] {
15+
[0u8; 32]
16+
}
17+
}
18+
19+
pub struct Risc0AggregationInput {
20+
pub receipts: Vec<Risc0ProofWithPubValuesAndImageId>,
21+
pub merkle_root: [u8; 32],
22+
}
23+
24+
pub(crate) fn aggregate_proofs(
25+
input: Risc0AggregationInput,
26+
) -> Result<ProgramOutput, ProofAggregationError> {
27+
let mut env_builder = ExecutorEnv::builder();
28+
29+
// write assumptions and proof image id + pub inputs
30+
let mut proofs_image_id_and_pub_inputs = vec![];
31+
for r in input.receipts {
32+
proofs_image_id_and_pub_inputs.push(risc0_aggregation_program::Risc0ImageIdAndPubInputs {
33+
image_id: r.image_id,
34+
public_inputs: r.public_values,
35+
});
36+
env_builder.add_assumption(r.receipt);
37+
}
38+
39+
// write input data
40+
let input = risc0_aggregation_program::Input {
41+
merkle_root: input.merkle_root,
42+
proofs_image_id_and_pub_inputs,
43+
};
44+
env_builder.write(&input).unwrap();
45+
46+
let env = env_builder.build().unwrap();
47+
48+
let prover = default_prover();
49+
let receipt = prover
50+
.prove_with_opts(env, RISC0_AGGREGATOR_PROGRAM_ELF, &ProverOpts::groth16())
51+
.unwrap()
52+
.receipt;
53+
54+
Ok(ProgramOutput::new(AggregatedProof::Risc0(receipt)))
55+
}
56+
57+
#[derive(Debug)]
58+
pub enum AlignedRisc0VerificationError {
59+
Verification,
60+
UnsupportedProof,
61+
}
62+
63+
pub(crate) fn verify(receipt: &Receipt) -> Result<(), AlignedRisc0VerificationError> {
64+
// TODO validate and verify receipt is of type Compressed, as only they can be aggregated recursively
65+
Ok(())
66+
}

aggregation_mode/src/backend/mod.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ mod types;
66

77
use crate::aggregators::{
88
lib::{AggregatedProof, ProofAggregationError},
9-
sp1_aggregator::{aggregate_proofs, SP1AggregationInput},
9+
risc0_aggregator::{self, Risc0AggregationInput},
10+
sp1_aggregator::{self, SP1AggregationInput},
1011
AlignedProof, ZKVMEngine,
1112
};
1213

@@ -101,16 +102,17 @@ impl ProofAggregator {
101102

102103
info!("Proofs fetched, constructing merkle root...");
103104
let (merkle_root, leaves) = compute_proofs_merkle_root(&proofs);
104-
info!("Merkle root constructed: {}", hex::encode(merkle_root));
105+
info!("Merkle root constructed: 0x{}", hex::encode(merkle_root));
105106

106107
info!("Starting proof aggregation program...");
107108
let output = match self.engine {
108109
ZKVMEngine::SP1 => {
109110
// only SP1 compressed proofs are supported
110111
let proofs = proofs
111112
.into_iter()
112-
.map(|proof| match proof {
113-
AlignedProof::SP1(proof) => proof,
113+
.filter_map(|proof| match proof {
114+
AlignedProof::SP1(proof) => Some(proof),
115+
_ => None,
114116
})
115117
.collect();
116118

@@ -119,7 +121,25 @@ impl ProofAggregator {
119121
merkle_root,
120122
};
121123

122-
aggregate_proofs(input).map_err(AggregatedProofSubmissionError::Aggregation)?
124+
sp1_aggregator::aggregate_proofs(input)
125+
.map_err(AggregatedProofSubmissionError::Aggregation)?
126+
}
127+
ZKVMEngine::RISC0 => {
128+
let receipts = proofs
129+
.into_iter()
130+
.filter_map(|proof| match proof {
131+
AlignedProof::Risc0(proof) => Some(proof),
132+
_ => None,
133+
})
134+
.collect();
135+
136+
let input = Risc0AggregationInput {
137+
receipts,
138+
merkle_root,
139+
};
140+
141+
risc0_aggregator::aggregate_proofs(input)
142+
.map_err(AggregatedProofSubmissionError::Aggregation)?
123143
}
124144
};
125145
info!("Proof aggregation program finished");
@@ -170,6 +190,9 @@ impl ProofAggregator {
170190
.await
171191
.map_err(AggregatedProofSubmissionError::ReceiptError)
172192
}
193+
AggregatedProof::Risc0(proof) => {
194+
todo!()
195+
}
173196
}
174197
}
175198

0 commit comments

Comments
 (0)